Pixel-Composer/scripts/node_display_text/node_display_text.gml

545 lines
14 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Display_Text(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-01-25 06:49:00 +01:00
name = "Display Text";
2024-04-30 05:57:34 +02:00
setDimension(16, 16);
2023-01-17 08:11:55 +01:00
previewable = false;
size_dragging = false;
size_dragging_w = w;
size_dragging_h = h;
size_dragging_mx = w;
size_dragging_my = h;
auto_height = false;
2023-07-30 13:56:22 +02:00
name_hover = false;
draw_scale = 1;
2024-08-03 12:43:09 +02:00
init_size = true;
2023-07-30 13:56:22 +02:00
2024-08-08 06:57:51 +02:00
ta_editor = new textArea(TEXTBOX_INPUT.text, function(val) { inputs[1].setValue(val); })
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Color("Color", self, c_white ))
2023-02-14 05:32:32 +01:00
.rejectArray();
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Text("Text", self, "Text"));
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Enum_Scroll("Style", self, 2, [ "Header", "Sub header", "Normal" ]))
2023-02-14 05:32:32 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Float("Alpha", self, 0.75))
.setDisplay(VALUE_DISPLAY.slider)
2023-02-14 05:32:32 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(4, nodeValue_Float("Line width", self, -1))
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-01-29 14:25:18 +01:00
2024-08-18 09:13:41 +02:00
newInput(5, nodeValue_Vec2("Position", self, [ x, y ]))
2023-11-25 08:54:35 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(6, nodeValue_Float("Line height", self, 0))
2023-11-25 08:54:35 +01:00
.rejectArray();
2024-08-03 12:43:09 +02:00
input_display_list = [1,
2024-08-03 12:43:09 +02:00
["Styling", false], 2, 0, 4, 6,
["Display", false], 5,
2023-11-25 08:54:35 +01:00
];
2022-01-29 14:25:18 +01:00
_prev_text = "";
2023-11-25 08:54:35 +01:00
font = f_sdf_medium;
fsize = 1;
2024-08-03 12:43:09 +02:00
line_h = 0;
2022-01-29 14:25:18 +01:00
_lines = [];
2024-08-03 12:43:09 +02:00
draw_simple = false;
2022-01-29 14:25:18 +01:00
2023-11-25 08:54:35 +01:00
pos_x = x;
pos_y = y;
ml_press = 0;
ml_release = 0;
ml_double = 0;
mr_press = 0;
mr_release = 0;
mm_press = 0;
mm_release = 0;
static move = function(_x, _y, _s) {
2023-11-25 08:54:35 +01:00
if(x == _x && y == _y) return;
if(!LOADING) PROJECT.modified = true;
x = _x;
y = _y;
2024-08-08 06:57:51 +02:00
if(inputs[5].setValue([ _x, _y ]))
2023-11-25 08:54:35 +01:00
UNDO_HOLDING = true;
}
2023-11-25 08:54:35 +01:00
static button_reactive_update = function() {
2024-04-02 05:24:55 +02:00
ml_press = lerp_float(ml_press , 0, 5);
ml_release = lerp_float(ml_release, 0, 5);
ml_double = lerp_float(ml_double, 0, 5);
mr_press = lerp_float(mr_press , 0, 5);
mr_release = lerp_float(mr_release, 0, 5);
mm_press = lerp_float(mm_press , 0, 5);
mm_release = lerp_float(mm_release, 0, 5);
2023-11-25 08:54:35 +01:00
if(mouse_press(mb_left)) ml_press = 2;
if(mouse_release(mb_left)) ml_release = 2;
if(DOUBLE_CLICK) ml_double = 2;
if(mouse_press(mb_right)) mr_press = 2;
if(mouse_release(mb_right)) mr_release = 2;
if(mouse_press(mb_middle)) mm_press = 2;
if(mouse_release(mb_middle)) mm_release = 2;
}
2023-11-25 08:54:35 +01:00
static button_reactive = function(key) {
2023-11-25 08:54:35 +01:00
switch(key) {
case "left_mouse_click" : return clamp(ml_press, 0, 1);
case "left_mouse_double_click" : return clamp(ml_double, 0, 1);
case "left_mouse_release" : return clamp(ml_release, 0, 1);
case "left_mouse_drag" : return mouse_click(mb_left);
case "right_mouse_click" : return clamp(mr_press, 0, 1);
case "right_mouse_release" : return clamp(mr_release, 0, 1);
case "right_mouse_drag" : return mouse_click(mb_right);
case "middle_mouse_click" : return clamp(mm_press, 0, 1);
case "middle_mouse_release" : return clamp(mm_release, 0, 1);
case "middle_mouse_drag" : return mouse_click(mb_middle);
case "ctrl" : return key_mod_press(CTRL);
case "alt" : return key_mod_press(ALT);
case "shift" : return key_mod_press(SHIFT);
case "space" : return keyboard_check(vk_space);
case "f1" : return keyboard_check(vk_f1);
case "f2" : return keyboard_check(vk_f2);
case "f3" : return keyboard_check(vk_f3);
case "f4" : return keyboard_check(vk_f4);
case "f5" : return keyboard_check(vk_f5);
case "f6" : return keyboard_check(vk_f6);
case "f7" : return keyboard_check(vk_f7);
case "f8" : return keyboard_check(vk_f8);
case "f9" : return keyboard_check(vk_f9);
case "f10" : return keyboard_check(vk_f10);
case "f11" : return keyboard_check(vk_f11);
case "f12" : return keyboard_check(vk_f12);
}
if(string_length(key) == 1) return keyboard_check(ord(string_upper(key)));
return 0;
}
2023-11-25 08:54:35 +01:00
static draw_text_style = function(_x, _y, txt, _s, _mx, _my) {
2024-08-03 12:43:09 +02:00
var _ss = _s * fsize;
if(draw_simple) {
draw_text_add_float(_x, _y, txt, _ss);
return string_width(txt) * fsize;
}
2024-04-25 15:49:55 +02:00
var _tx = _x;
2022-01-29 14:25:18 +01:00
var index = 1;
2024-04-25 15:49:55 +02:00
var _len = string_length(txt);
var _ch = "";
2024-08-03 12:43:09 +02:00
var _ch_h = string_height("l") * _ss;
2022-01-29 14:25:18 +01:00
var _mode = 0;
2024-08-03 12:43:09 +02:00
2024-04-25 15:49:55 +02:00
var _cmd = "";
2024-08-03 12:43:09 +02:00
var _str = "";
2022-01-29 14:25:18 +01:00
var width = 0;
2024-04-25 15:49:55 +02:00
var _tw, _th;
2023-11-25 08:54:35 +01:00
var _ff = draw_get_font();
2022-01-29 14:25:18 +01:00
var _cc = draw_get_color();
2023-11-25 08:54:35 +01:00
var _aa = draw_get_alpha();
2022-01-29 14:25:18 +01:00
2024-08-03 12:43:09 +02:00
repeat(_len) {
2022-01-29 14:25:18 +01:00
_ch = string_char_at(txt, index);
index++;
switch(_ch) {
case "<" : _mode = 1; continue;
2022-01-29 14:25:18 +01:00
case ">" :
var _c = string_splice(_cmd, " ");
if(array_length(_c) > 1) {
switch(_c[0]) {
case "bt" :
var _bch = "";
for( var i = 1; i < array_length(_c); i++ )
_bch += i > 1? " " + _c[i] : _c[i];
var _bw = string_width(_bch);
_tx += 4 * _s;
2024-08-03 12:43:09 +02:00
_tw = _bw * _ss;
_th = string_height(_bch) * _ss;
2023-11-25 08:54:35 +01:00
draw_sprite_stretched_points(THEME.ui_panel_bg, 0, _tx - 4, _y - 4, _tx + _tw + 4, _y + _th + 4, COLORS._main_icon_light);
draw_sprite_stretched_points(THEME.ui_panel, 1, _tx - 4, _y - 4, _tx + _tw + 4, _y + _th + 4, CDEF.main_dkgrey);
2022-01-29 14:25:18 +01:00
draw_set_color(_cc);
2024-08-03 12:43:09 +02:00
draw_text_add_float(_tx, _y, _bch, _ss);
2023-11-25 08:54:35 +01:00
var _reac = button_reactive(string_to_var(_bch));
if(_reac > 0) {
draw_sprite_stretched_points(THEME.ui_panel_bg, 4, _tx - 4, _y - 4, _tx + _tw + 4, _y + _th + 4, COLORS._main_accent, _reac);
draw_set_color(merge_color(0, COLORS.panel_bg_clear_inner, 0.5));
draw_set_alpha(_reac);
2024-08-03 12:43:09 +02:00
draw_text_transformed(_tx, _y, _bch, _ss, _ss, 0);
2023-11-25 08:54:35 +01:00
draw_set_alpha(_aa);
draw_set_color(_cc);
}
_tx += _tw + 4 * _s;
width += _bw * fsize + 8;
2023-11-25 08:54:35 +01:00
break;
case "node" :
var _bch = "";
for( var i = 1; i < array_length(_c); i++ )
_bch += i > 1? " " + _c[i] : _c[i];
var _bw = string_width(_bch);
_tx += 4 * _s;
2024-08-03 12:43:09 +02:00
_tw = _bw * _ss;
_th = string_height(_bch) * _ss;
draw_sprite_stretched_ext(THEME.node_bg, 0, _tx - 4, _y - 4, _tw + 8, _th + 8, c_white, .75);
draw_sprite_stretched_add(THEME.node_bg, 0, _tx - 4, _y - 4, _tw + 8, _th + 8, c_white, .10);
draw_set_color(_cc);
2024-08-03 12:43:09 +02:00
draw_text_add_float(_tx, _y, _bch, _ss);
_tx += _tw + 4 * _s;
width += _bw * fsize + 8;
break;
2023-11-25 08:54:35 +01:00
case "panel" :
var _key = _c[1] + " panel";
var _tss = 11 / 32;
draw_set_color(_cc);
draw_set_font(f_sdf);
_tw = string_width(_key) * _s * _tss;
_th = string_height(_key) * _s * _tss;
2024-04-02 05:24:55 +02:00
draw_set_color(COLORS._main_accent);
2024-04-03 09:40:37 +02:00
if(PANEL_GRAPH.node_hovering == self && point_in_rectangle(_mx, _my, _tx - 4, _y - 4, _tx + _tw + 4, _y + _th + 4)) {
draw_sprite_stretched_points(THEME.ui_panel, 1, _tx - 4, _y - 4, _tx + _tw + 4, _y + _th + 4, COLORS._main_accent, 1);
2023-11-25 08:54:35 +01:00
switch(string_lower(_c[1])) {
case "graph" : FOCUSING_PANEL = PANEL_GRAPH; break;
case "preview" : FOCUSING_PANEL = PANEL_PREVIEW; break;
case "inspector" : FOCUSING_PANEL = PANEL_INSPECTOR; break;
case "animation" : FOCUSING_PANEL = PANEL_ANIMATION; break;
case "collection" : FOCUSING_PANEL = PANEL_COLLECTION; break;
2023-11-25 08:54:35 +01:00
}
}
2024-04-02 05:24:55 +02:00
2024-04-25 15:49:55 +02:00
draw_text_add_float(_tx, _y, _key, _s * _tss);
2023-11-25 08:54:35 +01:00
_tx += _tw;
2023-11-25 08:54:35 +01:00
width += string_width(_key) * _tss;
draw_set_font(_ff);
draw_set_color(_cc);
draw_set_alpha(_aa);
2022-01-29 14:25:18 +01:00
break;
2022-01-29 14:25:18 +01:00
case "spr" :
var _spr_t = _c[1];
2022-12-10 05:06:01 +01:00
if(!variable_struct_exists(THEME, _spr_t)) break;
var _spr = variable_struct_get(THEME, _spr_t);
2022-01-29 14:25:18 +01:00
var _spr_i = array_length(_c) > 2? real(_c[2]) : 0;
var _spr_s = array_length(_c) > 3? _s * real(_c[3]) : _s;
_tw = sprite_get_width(_spr);
_th = sprite_get_height(_spr) * _spr_s;
var _ow = sprite_get_xoffset(_spr) * _spr_s;
var _oh = sprite_get_yoffset(_spr) * _spr_s;
draw_sprite_ext(_spr, _spr_i, _tx + _ow, _y + _ch_h / 2 - _th / 2 + _oh, _spr_s, _spr_s, 0, c_white, 1);
_tx += _tw * _spr_s;
2022-01-29 14:25:18 +01:00
width += _tw;
break;
}
}
_mode = 0;
_cmd = "";
continue;
}
switch(_mode) {
2024-08-03 12:43:09 +02:00
case 0 :
_str += _ch;
2022-01-29 14:25:18 +01:00
break;
2024-08-03 12:43:09 +02:00
case 1 :
if(_str != "") {
draw_text_add_float(_tx, _y, _str, _ss);
_tw = string_width(_str);
_tx += _tw * _ss;
width += _tw * fsize;
_str = "";
}
_cmd += _ch;
break;
2022-01-29 14:25:18 +01:00
}
}
2024-08-03 12:43:09 +02:00
if(_str != "") {
draw_text_add_float(_tx, _y, _str, _ss);
_tw = string_width(_str);
_tx += _tw * _ss;
width += _tw * fsize;
_str = "";
}
2022-01-29 14:25:18 +01:00
return width;
}
2022-01-29 14:25:18 +01:00
static string_raw = function(txt) {
2024-04-30 05:57:34 +02:00
var index = 1;
var _len = string_length(txt);
var _ch = "";
var _mode = 0;
2022-01-29 14:25:18 +01:00
var ch_str = "";
var b = buffer_create(1, buffer_grow, 1);
2022-01-29 14:25:18 +01:00
while(index <= _len) {
_ch = string_char_at(txt, index);
index++;
switch(_ch) {
case "<" : _mode = 1; continue;
2022-01-29 14:25:18 +01:00
case ">" :
var _c = string_splice(ch_str, " ");
if(array_length(_c) > 1) {
switch(_c[0]) {
case "bt" :
case "node" :
2022-01-29 14:25:18 +01:00
var _bch = "";
for( var i = 1; i < array_length(_c); i++ )
_bch += i > 1? " " + _c[i] : _c[i];
2022-01-29 14:25:18 +01:00
buffer_write(b, buffer_text, _bch);
2022-01-29 14:25:18 +01:00
break;
}
}
ch_str = "";
_mode = 0;
continue;
}
switch(_mode) {
case 0 : buffer_write(b, buffer_text, _ch); break;
2022-01-29 14:25:18 +01:00
case 1 : ch_str += _ch; break;
}
}
buffer_to_start(b);
var ss = buffer_read(b, buffer_text);
buffer_delete(b);
2022-01-29 14:25:18 +01:00
return ss;
}
2022-01-29 14:25:18 +01:00
static line_update = function(txt, line_width = -1) {
2022-01-29 14:25:18 +01:00
_prev_text = txt;
_lines = [];
var ch, i = 1, ss = "", _txt = _prev_text;
var len = string_length(_prev_text);
2023-07-30 13:56:22 +02:00
var _line_man = string_splice(_txt, "\n");
2022-01-29 14:25:18 +01:00
draw_set_font(font);
2023-07-30 13:56:22 +02:00
for( var i = 0, n = array_length(_line_man); i < n; i++ ) {
var _tx = _line_man[i];
2022-01-29 14:25:18 +01:00
2023-07-30 13:56:22 +02:00
while(string_length(_tx) > 0) {
var sp = min(string_pos(" ", _tx));
2023-07-30 13:56:22 +02:00
if(sp == 0) sp = string_length(_tx);
2022-01-29 14:25:18 +01:00
2023-07-30 13:56:22 +02:00
var _ps = string_copy(_tx, 1, sp);
_tx = string_copy(_tx, sp + 1, string_length(_tx) - sp);
var fullStr = ss + _ps;
if(line_width > 0 && string_width(string_raw(fullStr)) * fsize >= line_width) {
2023-07-30 13:56:22 +02:00
array_push(_lines, ss);
ss = _ps;
2024-04-30 05:57:34 +02:00
2023-07-30 13:56:22 +02:00
} else if(string_length(_tx) <= 0) {
array_push(_lines, fullStr);
2023-07-30 13:56:22 +02:00
ss = "";
2024-04-30 05:57:34 +02:00
2023-07-30 13:56:22 +02:00
} else
ss += _ps;
2022-01-29 14:25:18 +01:00
}
2024-08-03 12:43:09 +02:00
array_push(_lines, "/");
2022-01-29 14:25:18 +01:00
}
2023-07-30 13:56:22 +02:00
if(ss != "") array_push(_lines, ss);
}
2022-01-29 14:25:18 +01:00
static onValueUpdate = function(index = 0) {
2024-04-30 05:57:34 +02:00
if(index == 1 || index == 4) line_update(getInputData(1), getInputData(4));
}
2024-04-30 05:57:34 +02:00
static preDraw = function(_x, _y, _s) {
2024-04-30 05:57:34 +02:00
var xx = (x - 3) * _s + _x;
var yy = y * _s + _y;
var jun;
2024-08-08 06:57:51 +02:00
if(in_cache_len != array_length(inputDisplayList) || out_cache_len != array_length(outputs)) {
2024-04-30 05:57:34 +02:00
refreshNodeDisplay();
in_cache_len = array_length(inputDisplayList);
2024-08-08 06:57:51 +02:00
out_cache_len = array_length(outputs);
2024-04-30 05:57:34 +02:00
}
var _iny = yy + (junction_draw_hei_y * 0.5) * _s;
2024-08-08 06:57:51 +02:00
for( var i = 0, n = array_length(inputs); i < n; i++ ) {
inputs[i].x = xx;
inputs[i].y = _iny;
}
2024-04-30 05:57:34 +02:00
for(var i = 0; i < in_cache_len; i++) {
jun = inputDisplayList[i];
jun.x = xx;
jun.y = _iny;
_iny += junction_draw_hei_y * _s;
}
}
static drawNodeBase = function(xx, yy, mx, my, _s) {
2024-08-03 12:43:09 +02:00
if(draw_graph_culled && !init_size) return;
var color = getInputData(0);
2023-11-25 08:54:35 +01:00
var txt = getInputData(1);
if(txt == "") txt = "..."
2024-08-03 12:43:09 +02:00
draw_simple = string_pos("<", txt) == 0;
2023-11-25 08:54:35 +01:00
var sty = getInputData(2);
2024-01-08 08:10:50 +01:00
var alp = _color_get_alpha(color);
2023-11-25 08:54:35 +01:00
var wid = getInputData(4);
var posi = getInputData(5);
2024-08-03 12:43:09 +02:00
line_h = getInputData(6);
2023-11-25 08:54:35 +01:00
pos_x = posi[0];
pos_y = posi[1];
2022-01-29 14:25:18 +01:00
font = f_p1;
switch(sty) {
2023-11-25 08:54:35 +01:00
case 0 : font = f_sdf; fsize = 20 / 32; break;
case 1 : font = f_sdf; fsize = 0.5; break;
case 2 : font = f_sdf_medium; fsize = 0.5; break;
}
2022-01-29 14:25:18 +01:00
var ww = 0;
var hh = 0;
2023-07-30 13:56:22 +02:00
2022-01-29 14:25:18 +01:00
var tx = xx + 4;
var ty = yy + 4;
2023-07-30 13:56:22 +02:00
if(WIDGET_CURRENT == ta_editor) {
2023-11-25 08:54:35 +01:00
switch(sty) {
case 0 : ta_editor.font = f_h3; break;
case 1 : ta_editor.font = f_h5; break;
case 2 : ta_editor.font = f_p1; break;
}
2023-07-30 13:56:22 +02:00
ta_editor.draw(tx, ty, wid * _s, 0, txt, [ mx, my ] );
} else {
2024-04-30 05:57:34 +02:00
if(_prev_text != txt) line_update(txt, wid);
2023-07-30 13:56:22 +02:00
draw_set_alpha(alp);
draw_set_text(font, fa_left, fa_top, color);
for( var i = 0, n = array_length(_lines); i < n; i++ ) {
var _line = _lines[i];
2024-08-03 12:43:09 +02:00
if(_line == "/") {
hh += 8;
ty += 8 * _s;
continue;
}
var _h = line_get_height(font) * fsize + line_h;
2023-11-25 08:54:35 +01:00
var _w = draw_text_style(tx, ty, _line, _s, mx, my);
2023-07-30 13:56:22 +02:00
ww = max(ww, _w);
hh += _h;
ty += _h * _s;
}
draw_set_alpha(1);
2024-08-08 06:57:51 +02:00
if(inputs[1].value_from == noone && PANEL_GRAPH.node_hovering == self && PANEL_GRAPH.getFocusingNode() == self) {
2023-07-30 13:56:22 +02:00
if(point_in_rectangle(mx, my, xx, yy, xx + ww + 8, yy + hh + 8) && DOUBLE_CLICK) {
ta_editor._current_text = txt;
ta_editor.activate();
}
}
2022-01-29 14:25:18 +01:00
}
draw_scale = _s;
2022-01-29 14:25:18 +01:00
w = ww + 8;
h = hh + 8;
2024-08-03 12:43:09 +02:00
init_size = false;
}
static drawJunctions = function(_x, _y, _mx, _my, _s) {
2024-04-30 05:57:34 +02:00
if(!active) return;
var hover = noone;
draw_set_circle_precision(16);
for(var i = 0, n = array_length(inputDisplayList); i < n; i++) {
var jun = inputDisplayList[i];
if(jun.drawJunction_fast(_s, _mx, _my))
2024-04-30 05:57:34 +02:00
hover = jun;
}
return hover;
}
2024-04-30 05:57:34 +02:00
2024-08-03 12:43:09 +02:00
static update = function() {
init_size = true;
}
static drawNode = function(_x, _y, _mx, _my, _s) {
var xx = x * _s + _x;
var yy = y * _s + _y;
2024-08-03 12:43:09 +02:00
button_reactive_update();
drawNodeBase(xx, yy, _mx, _my, _s);
if(active_draw_index > -1) {
draw_sprite_stretched_ext(bg_spr, 1, xx, yy, w * _s, h * _s, COLORS._main_accent, 1);
active_draw_index = -1;
}
2024-04-30 05:57:34 +02:00
return drawJunctions(xx, yy, _mx, _my, _s);
}
}