Pixel-Composer/scripts/draw_text_function/draw_text_function.gml

232 lines
5.2 KiB
Text
Raw Normal View History

2023-11-24 10:41:53 +01:00
function draw_text_line(_x, _y, _text, _sep, _w) { #region
INLINE
2023-06-05 19:41:01 +02:00
__draw_text_ext_transformed(_x, _y, _text, _sep, _w, 1, 1, 0);
2023-11-24 10:41:53 +01:00
} #endregion
2023-06-05 19:41:01 +02:00
2023-11-24 10:41:53 +01:00
function draw_text_add(_x, _y, _text, scale = 1) { #region
INLINE
2023-03-26 07:13:36 +02:00
BLEND_ALPHA_MULP;
2023-12-11 05:45:12 +01:00
if(scale == 1) draw_text(round(_x), round(_y), _text);
else draw_text_transformed(round(_x), round(_y), _text, scale, scale, 0);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2023-11-24 10:41:53 +01:00
} #endregion
2022-12-19 13:35:30 +01:00
2023-11-24 10:41:53 +01:00
function draw_text_over(_x, _y, _text, scale = 1) { #region
INLINE
2023-11-13 15:42:14 +01:00
BLEND_OVERRIDE;
2023-12-11 05:45:12 +01:00
draw_text_transformed(round(_x), round(_y), _text, scale, scale, 0);
2023-11-13 15:42:14 +01:00
BLEND_NORMAL;
2023-11-24 10:41:53 +01:00
} #endregion
2023-11-13 15:42:14 +01:00
2023-11-24 10:41:53 +01:00
function draw_text_lang_add(_x, _y, _text, scale = 1) { #region
INLINE
2023-03-26 07:13:36 +02:00
BLEND_ALPHA_MULP;
2023-11-24 10:41:53 +01:00
draw_text_lang(_x, _y, _text, scale);
BLEND_NORMAL;
} #endregion
function draw_text_lang_over(_x, _y, _text, scale = 1) { #region
INLINE
BLEND_OVERRIDE;
draw_text_lang(_x, _y, _text, scale);
BLEND_NORMAL;
} #endregion
function draw_text_lang(_x, _y, _text, scale = 1) { #region
var _w = string_width(_text);
var _h = string_height(_text);
var _ha = draw_get_halign();
switch(_ha) {
case fa_left : break;
case fa_center : _x -= _w / 2; break;
case fa_right : _x -= _w; break;
}
draw_set_halign(fa_left);
var amo = string_length(_text);
var _f = draw_get_font();
var _font = _f;
var _gMap = GLYPH_MAP[$ _f];
var gly, _g , _ff;
for( var i = 1; i <= amo; i++ ) {
gly = string_char_at(_text, i);
if(struct_has(_gMap, gly)) {
_ff = _gMap[$ gly];
_g = gly;
} else {
_ff = _f;
_g = "?";
}
if(_font != _ff) draw_set_font(_ff);
_font = _ff;
2023-12-11 05:45:12 +01:00
draw_text_transformed(round(_x), round(_y), _g, scale, scale, 0);
2023-11-24 10:41:53 +01:00
_x += string_width(_g) * scale;
}
draw_set_font(_f);
draw_set_halign(_ha);
} #endregion
function draw_text_ext_add(_x, _y, _text, _sep, _w, scale = 1, forceCut = false) { #region
INLINE
BLEND_ALPHA_MULP;
var h = __draw_text_ext_transformed(_x, _y, _text, _sep, _w, scale, scale, 0, forceCut);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2023-06-17 14:30:49 +02:00
return h;
2023-11-24 10:41:53 +01:00
} #endregion
2023-11-24 10:41:53 +01:00
function draw_text_bbox(bbox, text) { #region
INLINE
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-11-24 10:41:53 +01:00
} #endregion
2023-06-05 19:41:01 +02:00
2023-11-24 10:41:53 +01:00
function draw_text_cut(x, y, str, w, scale = 1) { #region
INLINE
BLEND_ALPHA_MULP;
2023-12-11 05:45:12 +01:00
draw_text_transformed(round(x), round(y), string_cut(str, w,, scale), scale, scale, 0);
BLEND_NORMAL;
2023-11-24 10:41:53 +01:00
} #endregion
2023-06-05 19:41:01 +02:00
2023-12-11 05:45:12 +01:00
function draw_text_int(x, y, str) { #region
INLINE
draw_text(round(x), round(y), str);
} #endregion
2023-11-24 10:41:53 +01:00
function __draw_text_ext_transformed(_x, _y, _text, _sep, _w, sx, sy, rotation, forceCut = false) { #region
INLINE
2023-12-11 05:45:12 +01:00
_x = round(_x);
_y = round(_y);
2023-11-24 10:41:53 +01:00
if(!LOCALE.config.per_character_line_break && !forceCut) {
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-11-24 10:41:53 +01:00
} #endregion
2023-06-05 19:41:01 +02:00
#macro _string_width_ext string_width_ext
#macro string_width_ext __string_width_ext
2023-11-24 10:41:53 +01:00
function __string_width_ext(text, sep, w) { #region
INLINE
2023-06-05 19:41:01 +02:00
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;
2023-11-24 10:41:53 +01:00
} #endregion
2023-06-05 19:41:01 +02:00
#macro _string_height_ext string_height_ext
#macro string_height_ext __string_height_ext
2023-11-24 10:41:53 +01:00
function __string_height_ext(text, sep, w, _break = LOCALE.config.per_character_line_break) { #region
INLINE
2023-08-13 13:10:20 +02:00
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;
2023-11-24 10:41:53 +01:00
} #endregion