Pixel-Composer/scripts/string_cut/string_cut.gml

49 lines
799 B
Plaintext
Raw Normal View History

2022-12-12 09:08:03 +01:00
function draw_text_cut(x, y, str, w, scale = 1) {
draw_text_transformed(x, y, string_cut(str, w,, scale), scale, scale, 0);
2022-01-13 05:24:03 +01:00
}
2022-12-12 09:08:03 +01:00
function string_cut(str, w, tail = "...", scale = 1) {
2022-01-13 05:24:03 +01:00
var ww = 0;
var ind = 1;
var ss = "";
2022-11-01 03:06:03 +01:00
var tw = string_width(tail);
if(string_width(str) <= w) return str;
2022-01-13 05:24:03 +01:00
while(ind <= string_length(str)) {
var ch = string_char_at(str, ind);
2022-12-12 09:08:03 +01:00
var _w = string_width(ch) * scale;
2022-01-13 05:24:03 +01:00
2022-11-01 03:06:03 +01:00
if(ww + _w + tw >= w) {
2022-01-13 05:24:03 +01:00
ss += tail;
2022-11-01 03:06:03 +01:00
break;
} else
2022-01-13 05:24:03 +01:00
ss += ch;
2022-11-01 03:06:03 +01:00
2022-01-13 05:24:03 +01:00
ww += _w;
ind++;
}
return ss;
}
function string_cut_line(str, w) {
var i = 1;
var ss = "";
var ww = 0;
while(i <= string_length(str) ) {
var _chr = string_char_at(str, i);
ss += _chr;
ww += string_width(_chr);
if(ww > w) {
ww = 0;
ss += "\n";
}
i++;
}
return ss;
}