Skip to content

Commit bec8645

Browse files
committed
Truncate start of custom sound filename to fit the textbox
1 parent 2ccde8d commit bec8645

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

scripts/draw_window_instruments/draw_window_instruments.gml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function draw_window_instruments() {
104104
if (ins.user) {draw_theme_color()}
105105
if (!ins.loaded) draw_set_color(c_red)
106106
if (insselect = b) draw_set_color(c_white)
107-
draw_text(x1 + 18 + 194, y1 + 90 + 20 * a, condstr(ins.filename = "", "None") + ins.filename)
107+
draw_text(x1 + 18 + 194, y1 + 90 + 20 * a, condstr(ins.filename = "", "None") + string_truncate(ins.filename, 144, true))
108108
if (ins.filename = "") popup_set_window(x1 + 14 + 194, y1 + 88 + 20 * a, 160, 20, "No sound file has been selected\nfor this instrument.")
109109
else if (!ins.loaded) popup_set_window(x1 + 14 + 194, y1 + 88 + 20 * a, 160, 20, "This sound file could not be found.")
110110
if (mouse_rectangle(x1 + 14 + 194, y1 + 88 + 20 * a, 160, 20) && insselect = b && wmenu = 0) {

scripts/string_maxwidth/string_maxwidth.gml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
function string_maxwidth(argument0, argument1) {
22
// string_maxwidth(str, w)
3-
var str, nstr, maxw, w, c;
3+
var str, nstr, maxw, w, c, truncate_start;
44
str = argument0
55
maxw = argument1
6+
truncate_start = false
7+
if (argument_count > 2) {
8+
truncate_start = argument[2]
9+
}
610
w = 0
711
nstr = ""
8-
for (c = 1; c <= string_length(str); c += 1) {
9-
w += string_width(string_char_at(str, c))
10-
if (string_char_at(str, c) = "\n") w = 0
11-
if (w > maxw) break
12-
nstr += string_char_at(str, c)
12+
if (truncate_start) {
13+
for (c = string_length(str); c >= 1; c -= 1) {
14+
w += string_width(string_char_at(str, c))
15+
if (string_char_at(str, c) = "\n") w = 0
16+
if (w > maxw) break
17+
nstr = string_char_at(str, c) + nstr
18+
}
19+
} else {
20+
for (c = 1; c <= string_length(str); c += 1) {
21+
w += string_width(string_char_at(str, c))
22+
if (string_char_at(str, c) = "\n") w = 0
23+
if (w > maxw) break
24+
nstr += string_char_at(str, c)
25+
}
1326
}
1427
return nstr
1528

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
function string_truncate(argument0, argument1) {
22
// string_truncate(str, maxwidth)
3-
// Truncates a string to maxwidth and adds ellipses (...) at the end
3+
// Truncates a string to maxwidth and adds ellipses (...) at the end or start
44

5-
var str, maxwidth
5+
var str, maxwidth, truncate_start
66
str = argument0
77
maxwidth = argument1
8-
8+
truncate_start = false
9+
if (argument_count > 2) {
10+
truncate_start = argument[2]
11+
}
12+
913
if (string_width(str) > maxwidth) {
10-
return string_maxwidth(str, maxwidth) + "..."
14+
if (truncate_start) {
15+
return "..." + string_maxwidth(str, maxwidth, true)
16+
} else {
17+
return string_maxwidth(str, maxwidth) + "..."
18+
}
1119
}
1220
return str
13-
14-
1521
}

0 commit comments

Comments
 (0)