Pixel-Composer/scripts/draw_text_function/draw_text_function.gml

161 lines
3.5 KiB
Text
Raw Normal View History

2023-06-05 19:41:01 +02:00
function draw_text_line(_x, _y, _text, _sep, _w) {
__draw_text_ext_transformed(_x, _y, _text, _sep, _w, 1, 1, 0);
}
2023-06-04 12:38:40 +02:00
function draw_text_add(_x, _y, _text, scale = 1) {
2023-03-26 07:13:36 +02:00
BLEND_ALPHA_MULP;
2023-06-04 12:38:40 +02:00
draw_text_transformed(_x, _y, _text, scale, scale, 0);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2022-12-19 13:35:30 +01:00
}
2023-11-13 15:42:14 +01:00
function draw_text_over(_x, _y, _text, scale = 1) {
BLEND_OVERRIDE;
draw_text_transformed(_x, _y, _text, scale, scale, 0);
BLEND_NORMAL;
}
2023-06-04 12:38:40 +02:00
function draw_text_ext_add(_x, _y, _text, _sep, _w, scale = 1) {
2023-03-26 07:13:36 +02:00
BLEND_ALPHA_MULP;
2023-06-17 14:30:49 +02:00
var h = __draw_text_ext_transformed(_x, _y, _text, _sep, _w, scale, scale, 0);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2023-06-17 14:30:49 +02:00
return h;
2023-02-14 05:32:32 +01:00
}
function draw_text_bbox(bbox, text) {
var ss = min(bbox.w / string_width(text), bbox.h / string_height(text));
ss = max(0.5, ss);
2023-09-11 16:08:58 +02:00
draw_set_halign(fa_center);
draw_set_valign(fa_center);
2023-10-06 11:51:11 +02:00
draw_text_cut(bbox.xc, bbox.yc, text, bbox.w, ss);
2023-06-05 19:41:01 +02:00
}
function draw_text_cut(x, y, str, w, scale = 1) {
BLEND_ALPHA_MULP;
2023-06-05 19:41:01 +02:00
draw_text_transformed(x, y, string_cut(str, w,, scale), scale, scale, 0);
BLEND_NORMAL;
2023-06-05 19:41:01 +02:00
}
function __draw_text_ext_transformed(_x, _y, _text, _sep, _w, sx, sy, rotation) {
if(!LOCALE.config.per_character_line_break) {
2023-08-31 18:49:57 +02:00
BLEND_ALPHA_MULP;
2023-06-05 19:41:01 +02:00
draw_text_ext_transformed(_x, _y, _text, _sep, _w, sx, sy, rotation);
2023-08-31 18:49:57 +02:00
BLEND_NORMAL;
2023-06-17 14:30:49 +02:00
return string_height_ext(_text, _sep, _w) * sy;
2023-06-05 19:41:01 +02:00
}
var lines = [];
var line = "";
var line_w = 0;
var amo = string_length(_text);
for( var i = 1; i <= amo; i++ ) {
var ch = string_char_at(_text, i);
var ww = string_width(ch) * sx;
2023-06-17 14:30:49 +02:00
if(ch == "\n" || line_w + ww > _w) {
2023-06-05 19:41:01 +02:00
array_push(lines, line);
2023-06-17 14:30:49 +02:00
if(ch != "\n") {
line = ch;
line_w = ww;
} else {
line = "";
line_w = 0;
}
} else if(ch != "\n") {
2023-06-05 19:41:01 +02:00
line += ch;
line_w += ww;
}
}
if(line != "") array_push(lines, line);
var ha = draw_get_halign();
var va = draw_get_valign();
var xx = _x, yy = _y;
var hh = string_height("M") * array_length(lines) * sy;
draw_set_halign(fa_left);
draw_set_valign(fa_top);
switch(va) {
case fa_top : yy = _y; break;
case fa_middle : yy = _y - hh / 2; break;
case fa_bottom : yy = _y - hh; break;
}
2023-08-31 18:49:57 +02:00
BLEND_ALPHA_MULP;
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(lines); i < n; i++ ) {
2023-06-05 19:41:01 +02:00
var lw = string_width(lines[i]) * sx;
switch(ha) {
case fa_left : xx = _x; break;
case fa_center : xx = _x - lw / 2; break;
case fa_right : xx = _x - lw; break;
}
draw_text_transformed(xx, yy, lines[i], sx, sy, rotation);
yy += string_height("M") * sy;
}
2023-08-31 18:49:57 +02:00
BLEND_NORMAL;
2023-06-05 19:41:01 +02:00
draw_set_halign(ha);
draw_set_valign(va);
2023-06-17 14:30:49 +02:00
return hh;
2023-06-05 19:41:01 +02:00
}
#macro _string_width_ext string_width_ext
#macro string_width_ext __string_width_ext
function __string_width_ext(text, sep, w) {
if(!LOCALE.config.per_character_line_break)
return _string_width_ext(text, sep, w);
var mxw = 0;
var lw = 0;
var amo = string_length(text);
for( var i = 1; i <= amo; i++ ) {
var ch = string_char_at(text, i);
var ww = string_width(ch);
if(lw + ww > w) {
mxw = max(mxw, lw);
lw = ww;
} else
lw += ww;
}
mxw = max(mxw, lw);
return mxw;
}
#macro _string_height_ext string_height_ext
#macro string_height_ext __string_height_ext
2023-08-13 13:10:20 +02:00
function __string_height_ext(text, sep, w, _break = LOCALE.config.per_character_line_break) {
if(!_break)
2023-06-05 19:41:01 +02:00
return _string_height_ext(text, sep, w);
var lw = 0;
var amo = string_length(text);
if(amo == 0) return 0;
var hh = string_height("M");
for( var i = 1; i <= amo; i++ ) {
var ch = string_char_at(text, i);
var ww = string_width(ch);
if(lw + ww > w) {
hh += string_height("M");
lw = ww;
} else
lw += ww;
}
return hh;
}