Pixel-Composer/scripts/textBox/textBox.gml

457 lines
12 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
enum TEXTBOX_INPUT {
text,
number,
float
}
function textBox(_input, _onModify) constructor {
active = false;
hover = false;
align = fa_right;
hide = false;
2022-11-03 11:44:49 +01:00
font = noone;
2022-12-10 05:06:01 +01:00
color = COLORS._main_text;
2022-01-13 05:24:03 +01:00
no_empty = true;
auto_update = false;
slidable = false;
sliding = false;
slide_mx = 0;
2022-12-19 13:35:30 +01:00
slide_my = 0;
2022-01-13 05:24:03 +01:00
slide_speed = 1 / 16;
2022-01-26 13:02:30 +01:00
starting_char = 1;
2022-01-13 05:24:03 +01:00
input = _input;
onModify = _onModify;
_input_text = "";
2022-12-18 03:20:38 +01:00
_last_text = "";
2022-01-13 05:24:03 +01:00
cursor = 0;
cursor_pos = 0;
cursor_pos_to = 0;
cursor_select = -1;
click_block = 0;
2022-09-21 06:09:40 +02:00
sprite_index = -1;
2022-01-24 02:21:25 +01:00
static apply = function() {
2022-01-13 05:24:03 +01:00
var _input_text_current = _input_text;
switch(input) {
case TEXTBOX_INPUT.number :
_input_text_current = evaluateFunction(_input_text);
_input_text_current = _input_text_current == ""? 0 : round(_input_text_current);
break;
case TEXTBOX_INPUT.float :
_input_text_current = evaluateFunction(_input_text);
break;
}
if(no_empty && _input_text_current == "")
2022-12-18 03:20:38 +01:00
_input_text_current = _last_text;
2022-01-13 05:24:03 +01:00
if(onModify)
onModify(_input_text_current);
}
2022-01-24 02:21:25 +01:00
static move_cursor = function(delta) {
2022-01-13 05:24:03 +01:00
var ll = string_length(_input_text) + 1;
cursor = safe_mod(cursor + delta + ll, ll);
}
2022-12-18 03:20:38 +01:00
static getDisplayText = function(val) {
if(input == TEXTBOX_INPUT.text) return val;
return string(val);
}
2022-01-24 02:21:25 +01:00
static editText = function() {
2022-01-13 05:24:03 +01:00
#region text editor
2022-12-12 09:08:03 +01:00
if(keyboard_check(vk_control) && keyboard_check_pressed(ord("A"))) {
2022-11-21 06:38:44 +01:00
cursor_select = 0;
cursor = string_length(_input_text);
2022-12-12 09:08:03 +01:00
} else if(keyboard_check(vk_control) && (keyboard_check_pressed(ord("C")) || keyboard_check_pressed(ord("X")))) {
2022-11-21 06:38:44 +01:00
if(cursor_select != -1) {
var minc = min(cursor, cursor_select);
var maxc = max(cursor, cursor_select);
clipboard_set_text(string_copy(_input_text, minc, maxc - minc));
2022-01-13 05:24:03 +01:00
}
} else {
2022-12-12 09:08:03 +01:00
if(keyboard_check(vk_control) && keyboard_check_pressed(ord("V")))
KEYBOARD_STRING = clipboard_get_text();
2022-11-21 06:38:44 +01:00
2022-01-13 05:24:03 +01:00
if(keyboard_check_pressed(vk_escape) || keyboard_check_pressed(vk_enter)) {
} else if(keyboard_check_pressed(vk_backspace)) {
if(cursor_select == -1) {
var str_before = string_copy(_input_text, 1, cursor - 1);
var str_after = string_copy(_input_text, cursor + 1, string_length(_input_text) - cursor);
_input_text = str_before + str_after;
} else {
var minc = min(cursor, cursor_select);
var maxc = max(cursor, cursor_select);
var str_before = string_copy(_input_text, 1, minc);
2022-01-26 13:02:30 +01:00
var str_after = string_copy(_input_text, maxc + 1, string_length(_input_text) - maxc);
2022-01-13 05:24:03 +01:00
2022-01-26 13:02:30 +01:00
cursor = minc + 1;
2022-01-13 05:24:03 +01:00
_input_text = str_before + str_after;
}
cursor_select = -1;
move_cursor(-1);
2022-11-21 06:38:44 +01:00
} else if(keyboard_check_pressed(vk_delete) || (keyboard_check_pressed(ord("X")) && keyboard_check(vk_control) && cursor_select != -1)) {
2022-01-13 05:24:03 +01:00
if(cursor_select == -1) {
var str_before = string_copy(_input_text, 1, cursor);
var str_after = string_copy(_input_text, cursor + 2, string_length(_input_text) - cursor - 1);
2022-09-21 06:09:40 +02:00
2022-01-13 05:24:03 +01:00
_input_text = str_before + str_after;
} else {
var minc = min(cursor, cursor_select);
var maxc = max(cursor, cursor_select);
var str_before = string_copy(_input_text, 1, minc);
2022-01-26 13:02:30 +01:00
var str_after = string_copy(_input_text, maxc + 1, string_length(_input_text) - maxc);
2022-01-13 05:24:03 +01:00
2022-01-26 13:02:30 +01:00
cursor = minc;
2022-01-13 05:24:03 +01:00
_input_text = str_before + str_after;
}
cursor_select = -1;
2022-12-12 09:08:03 +01:00
} else if(KEYBOARD_STRING != "") {
var ch = KEYBOARD_STRING;
2022-01-13 05:24:03 +01:00
if(cursor_select == -1) {
var str_before = string_copy(_input_text, 1, cursor);
var str_after = string_copy(_input_text, cursor + 1, string_length(_input_text) - cursor);
_input_text = str_before + ch + str_after;
move_cursor(string_length(ch));
} else {
var minc = min(cursor, cursor_select);
var maxc = max(cursor, cursor_select);
var str_before = string_copy(_input_text, 1, minc);
var str_after = string_copy(_input_text, maxc + 1, string_length(_input_text) - maxc);
_input_text = str_before + ch + str_after;
2022-01-26 13:02:30 +01:00
cursor = minc + string_length(ch);
2022-01-13 05:24:03 +01:00
}
cursor_select = -1;
}
}
2022-12-12 09:08:03 +01:00
KEYBOARD_STRING = "";
2022-01-13 05:24:03 +01:00
keyboard_lastkey = -1;
#endregion
if(keyboard_check_pressed(vk_escape)) {
2022-12-18 03:20:38 +01:00
_input_text = _last_text;
2022-01-13 05:24:03 +01:00
apply();
TEXTBOX_ACTIVE = noone;
} else if(keyboard_check_pressed(vk_enter)) {
apply();
TEXTBOX_ACTIVE = noone;
2022-11-22 14:25:39 +01:00
} else if(auto_update && keyboard_check_pressed(vk_anykey)) {
apply();
2022-01-13 05:24:03 +01:00
}
}
2022-01-24 02:21:25 +01:00
static display_text = function(_x, _y, _text, _w, _format, _m = -1) {
2022-01-13 05:24:03 +01:00
var _xx = _x, _ch, _chw;
var target = -999;
switch(_format) {
case VALUE_DISPLAY._default :
2022-12-10 05:06:01 +01:00
draw_set_text(font == noone? f_p0 : font, fa_left, fa_center, color);
2022-01-13 05:24:03 +01:00
draw_text(_x, _y, _text);
break;
case VALUE_DISPLAY.export_format :
2022-12-10 05:06:01 +01:00
draw_set_text(font == noone? f_p0 : font, fa_left, fa_center, color);
2022-01-13 05:24:03 +01:00
var _x0 = _x, ch = "", len = string_length(_text), i = 1;
var cc = draw_get_color();
var str = "", _comm = false;
while(i <= len) {
ch = string_char_at(_text, i);
if(ch == "%")
_comm = true;
if(!_comm) {
draw_text(_x0, _y, ch);
_x0 += string_width(ch);
} else {
str += ch;
switch(ch) {
2022-11-18 03:20:31 +01:00
case "d" : draw_set_color(COLORS.widget_text_dec_d); break;
case "n" : draw_set_color(COLORS.widget_text_dec_n); break;
case "e" : draw_set_color(COLORS.widget_text_dec_e); break;
case "f" : draw_set_color(COLORS.widget_text_dec_f); break;
case "i" : draw_set_color(COLORS.widget_text_dec_i); break;
2022-01-13 05:24:03 +01:00
}
switch(ch) {
case "d" : case "n" : case "e" : case "f" : case "i" :
draw_text(_x0, _y, str);
_x0 += string_width(str);
_comm = false;
str = "";
draw_set_color(cc);
break;
}
}
i++;
}
draw_text(_x0, _y, str);
break;
}
if(!sliding && _m != -1) {
2022-01-13 05:24:03 +01:00
for( var i = 1; i <= string_length(_text); i++ ) {
_ch = string_char_at(_text, i);
_chw = string_width(_ch);
if(_m < _xx + _chw / 2) {
target = i - 1;
break;
} else if(_m < _xx + _chw) {
target = i;
break;
}
_xx += _chw;
}
}
if(target != -999) {
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active) || click_block == 1) {
2022-01-13 05:24:03 +01:00
cursor_select = -1;
cursor = target;
click_block = 0;
2022-12-10 05:06:01 +01:00
} else if(mouse_click(mb_left, active) && cursor != target) {
2022-01-13 05:24:03 +01:00
cursor_select = target;
}
}
}
2022-11-03 11:44:49 +01:00
static draw = function(_x, _y, _w, _h, _text, _m, _format = VALUE_DISPLAY._default, halign = fa_left, valign = fa_top) {
switch(halign) {
case fa_left: _x = _x; break;
case fa_center: _x = _x - _w / 2; break;
case fa_right: _x = _x - _w; break;
}
2022-01-26 13:02:30 +01:00
2022-11-03 11:44:49 +01:00
switch(valign) {
case fa_top: _y = _y; break;
case fa_center: _y = _y - _h / 2; break;
case fa_bottom: _y = _y - _h; break;
}
2022-11-18 03:20:31 +01:00
draw_set_text(font == noone? f_p0 : font, fa_left, fa_top);
2022-01-26 13:02:30 +01:00
var hh = _h;
2022-11-03 11:44:49 +01:00
var tx = _x;
2022-01-13 05:24:03 +01:00
switch(align) {
2022-11-03 11:44:49 +01:00
case fa_left : tx = _x + ui(8); break;
2022-01-13 05:24:03 +01:00
case fa_center : tx = _x + _w / 2; break;
2022-11-03 11:44:49 +01:00
case fa_right : tx = _x + _w - ui(8); break;
2022-01-13 05:24:03 +01:00
}
if(sliding > 0) {
var dx = _m[0] - slide_mx;
2022-12-19 13:35:30 +01:00
var dy = slide_my - _m[1];
if(sliding == 1 && (abs(dx) > 16 || abs(dy) > 16)) {
2022-01-13 05:24:03 +01:00
sliding = 2;
2022-12-19 13:35:30 +01:00
slide_mx = _m[0];
slide_my = _m[1];
}
2022-01-13 05:24:03 +01:00
if(sliding == 2) {
2022-12-19 13:35:30 +01:00
var spd = (abs(dx) > abs(dy)? dx : dy) * slide_speed;
if(keyboard_check(vk_alt))
spd /= 10;
if(keyboard_check(vk_control))
spd *= 10;
2022-12-19 13:35:30 +01:00
var _ip = _input_text;
_input_text = _input_text + spd;
2022-01-13 05:24:03 +01:00
switch(input) {
2022-12-18 03:20:38 +01:00
case TEXTBOX_INPUT.number : _input_text = round(_input_text); break;
}
2022-01-13 05:24:03 +01:00
2022-12-19 13:35:30 +01:00
if(_input_text != _ip) {
slide_mx = _m[0];
slide_my = _m[1];
}
2022-11-22 14:25:39 +01:00
apply();
UNDO_HOLDING = true;
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left)) {
2022-11-22 14:25:39 +01:00
UNDO_HOLDING = false;
2022-01-13 05:24:03 +01:00
TEXTBOX_ACTIVE = noone;
}
}
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left))
2022-01-13 05:24:03 +01:00
sliding = 0;
}
if(self == TEXTBOX_ACTIVE) {
2022-11-18 03:20:31 +01:00
draw_sprite_stretched(THEME.textbox, sprite_index == -1? 2 : sprite_index, _x, _y, _w, hh);
2022-01-13 05:24:03 +01:00
editText();
#region cursor
if(keyboard_check_pressed(vk_left)) {
if(keyboard_check(vk_shift)) {
if(cursor_select == -1)
cursor_select = cursor;
} else
cursor_select = -1;
move_cursor(-1);
}
if(keyboard_check_pressed(vk_right)) {
if(keyboard_check(vk_shift)) {
if(cursor_select == -1)
cursor_select = cursor;
} else
cursor_select = -1;
move_cursor(1);
}
#endregion
#region multiplier
2022-11-03 11:44:49 +01:00
if(_w > ui(80) && (input == TEXTBOX_INPUT.number || input == TEXTBOX_INPUT.float)) {
2022-11-18 03:20:31 +01:00
draw_set_text(f_p0b, fa_left, fa_center, COLORS._main_text_sub);
2022-01-13 05:24:03 +01:00
draw_set_alpha(0.5);
2022-12-10 05:06:01 +01:00
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + ui(32), _y + hh)) {
2022-01-13 05:24:03 +01:00
draw_set_alpha(1);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
var ktxt = _input_text;
if(input == TEXTBOX_INPUT.number) {
if(keyboard_check(vk_alt)) _input_text = string(ceil(toNumber(ktxt) / 2));
else _input_text = string(ceil(toNumber(ktxt) * 2));
} else {
if(keyboard_check(vk_alt)) _input_text = string(toNumber(ktxt) / 2);
else _input_text = string(toNumber(ktxt) * 2);
}
2022-11-22 14:25:39 +01:00
apply();
2022-01-13 05:24:03 +01:00
}
}
if(keyboard_check(vk_alt))
2022-11-03 11:44:49 +01:00
draw_text(_x + ui(8), _y + hh / 2, "/2");
2022-01-13 05:24:03 +01:00
else
2022-11-03 11:44:49 +01:00
draw_text(_x + ui(8), _y + hh / 2, "x2");
2022-01-13 05:24:03 +01:00
draw_set_alpha(1);
}
#endregion
#region draw
2022-12-18 03:20:38 +01:00
var disp_text = getDisplayText(_input_text);
var ss = string_cut(disp_text, _w - ui(16));
2022-11-18 03:20:31 +01:00
draw_set_text(font == noone? f_p0 : font, fa_left, fa_top);
2022-01-13 05:24:03 +01:00
var ww = string_width(ss);
switch(align) {
case fa_left : break;
case fa_center : tx -= ww / 2; break;
case fa_right : tx -= ww; break;
}
var cs = string_copy(ss, 1, cursor);
var c_w = string_width(cs);
var c_h = string_height(ss);
var c_y0 = _y + _h / 2 - c_h / 2;
var c_y1 = _y + _h / 2 + c_h / 2;
cursor_pos_to = tx + c_w;
2022-12-10 05:06:01 +01:00
cursor_pos = cursor_pos == 0? cursor_pos_to : lerp_float(cursor_pos, cursor_pos_to, 4);
2022-01-13 05:24:03 +01:00
if(cursor_select > -1) {
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.widget_text_highlight);
2022-12-18 03:20:38 +01:00
var x1 = tx + string_width(string_copy(disp_text, 1, cursor_select));
2022-01-13 05:24:03 +01:00
2022-11-03 11:44:49 +01:00
draw_roundrect_ext(cursor_pos, c_y0, x1, c_y1, ui(8), ui(8), 0);
2022-01-13 05:24:03 +01:00
}
var _mx = -1;
2022-01-26 13:02:30 +01:00
var _my = -1;
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_any, active) && hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + hh)) {
2022-01-13 05:24:03 +01:00
_mx = _m[0];
2022-01-26 13:02:30 +01:00
_my = _m[1];
2022-01-13 05:24:03 +01:00
}
2022-11-03 11:44:49 +01:00
display_text(tx, _y + _h / 2, ss, _w - ui(4), _format, _mx);
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS._main_text_accent);
2022-01-13 05:24:03 +01:00
draw_line_width(cursor_pos, c_y0, cursor_pos, c_y1, 2);
#endregion
2022-12-10 05:06:01 +01:00
if(!point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + hh) && mouse_press(mb_left)) {
2022-01-13 05:24:03 +01:00
apply();
TEXTBOX_ACTIVE = noone;
}
} else {
2022-12-18 03:20:38 +01:00
var ss = string_cut(getDisplayText(_text), _w - 16);
2022-11-18 03:20:31 +01:00
draw_set_text(font == noone? f_p0 : font, fa_left, fa_center);
2022-01-13 05:24:03 +01:00
var ww = string_width(ss);
switch(align) {
case fa_left : break;
case fa_center : tx -= ww / 2; break;
case fa_right : tx -= ww; break;
}
2022-01-26 13:02:30 +01:00
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + hh)) {
2022-01-13 05:24:03 +01:00
if(hide)
2022-11-18 03:20:31 +01:00
draw_sprite_stretched_ext(THEME.textbox, 1, _x, _y, _w, hh, c_white, 0.5);
2022-01-13 05:24:03 +01:00
else
2022-11-18 03:20:31 +01:00
draw_sprite_stretched(THEME.textbox, 1, _x, _y, _w, hh);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
TEXTBOX_ACTIVE = self;
click_block = 1;
2022-12-12 09:08:03 +01:00
KEYBOARD_STRING = "";
2022-01-13 05:24:03 +01:00
keyboard_lastkey = -1;
2022-12-18 03:20:38 +01:00
_input_text = _text;
_last_text = _text;
2022-01-13 05:24:03 +01:00
}
} else if(!hide) {
2022-11-18 03:20:31 +01:00
draw_sprite_stretched(THEME.textbox, 0, _x, _y, _w, hh);
2022-01-13 05:24:03 +01:00
}
2022-11-03 11:44:49 +01:00
display_text(tx, _y + _h / 2, ss, _w - ui(4), _format);
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
if(_w > ui(64) && slidable) {
2022-11-18 03:20:31 +01:00
draw_sprite_ui_uniform(THEME.text_slider, 0, _x + ui(20), _y + hh / 2, 1, COLORS._main_icon, 0.5);
2022-01-13 05:24:03 +01:00
2022-01-26 13:02:30 +01:00
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + hh)) {
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
sliding = 1;
2022-12-19 13:35:30 +01:00
2022-01-13 05:24:03 +01:00
slide_mx = _m[0];
2022-12-19 13:35:30 +01:00
slide_my = _m[1];
2022-01-13 05:24:03 +01:00
}
}
}
}
hover = false;
active = false;
2022-01-26 13:02:30 +01:00
2022-09-21 06:09:40 +02:00
sprite_index = -1;
2022-01-26 13:02:30 +01:00
return hh;
2022-01-13 05:24:03 +01:00
}
}