Pixel-Composer/objects/o_dialog_textbox_autocomplete/Create_0.gml

108 lines
2.6 KiB
Plaintext
Raw Normal View History

2023-07-12 16:28:32 +02:00
/// @description
event_inherited();
2023-07-12 16:28:32 +02:00
#region data
depth = -9999;
dialog_x = 0;
dialog_y = 0;
dialog_w = 300;
dialog_h = 160;
selecting = 0;
textbox = noone;
prompt = "";
data = [];
destroy_on_escape = false;
destroy_on_click_out = false;
2023-11-10 11:32:46 +01:00
function activate(textbox) {
2023-11-08 08:38:04 +01:00
INLINE
self.textbox = textbox;
self.selecting = 0;
}
function deactivate(textbox) {
INLINE
if(textbox != self.textbox) return;
self.textbox = noone;
}
2023-07-12 16:28:32 +02:00
sc_content = new scrollPane(dialog_w, dialog_h, function(_y, _m) {
draw_clear_alpha(COLORS.panel_bg_clear, 0);
var hght = line_get_height(f_p0, 8);
var _dw = sc_content.surface_w;
var _h = 0;
var _ly = _y;
for(var i = 0; i < array_length(data); i++) {
var _dat = data[i];
if(sHOVER && point_in_rectangle(_m[0], _m[1], 0, _ly + 1, _dw, _ly + hght - 1)) {
selecting = i;
2023-07-12 16:28:32 +02:00
if(mouse_press(mb_left))
applyAutoComplete(_dat[3]);
}
2023-07-12 16:28:32 +02:00
if(selecting == i) {
WIDGET_TAB_BLOCK = true;
2023-07-12 16:28:32 +02:00
draw_sprite_stretched_ext(THEME.textbox, 3, 0, _ly, _dw, hght, COLORS.dialog_menubox_highlight, 1);
if(keyboard_check_pressed(vk_tab) || keyboard_check_pressed(vk_enter))
2023-07-12 16:28:32 +02:00
applyAutoComplete(_dat[3]);
}
var icn = _dat[0][0];
var ss = 16 / sprite_get_width(icn);
draw_sprite_ext(icn, _dat[0][1], ui(4 + 12), _ly + hght / 2, ss, ss, 0, c_white, 1);
draw_set_text(f_p2, fa_right, fa_center, COLORS._main_text_sub);
draw_text_cut(_dw - ui(8), _ly + hght / 2 - ui(2), _dat[2], _dw);
draw_set_text(f_p0, fa_left, fa_center, COLORS._main_text);
draw_text_cut(ui(4 + 24 + 4), _ly + hght / 2 - ui(2), _dat[1], _dw);
_ly += hght;
_h += hght;
}
if(keyboard_check_pressed(vk_up)) {
selecting--;
if(selecting < 0) selecting = array_length(data) - 1;
sc_content.scroll_y_to = -(selecting - 2) * hght;
}
if(keyboard_check_pressed(vk_down)) {
selecting = safe_mod(selecting + 1, array_length(data));
sc_content.scroll_y_to = -(selecting - 2) * hght;
}
return _h;
});
function applyAutoComplete(rep) {
2023-09-15 20:12:02 +02:00
var _totAmo = string_length(textbox._input_text);
var _prmAmo = string_length(prompt);
var _repAmo = string_length(rep);
2023-07-12 16:28:32 +02:00
2023-09-15 20:12:02 +02:00
var _sPreC = string_copy(textbox._input_text, 1, textbox.cursor - _prmAmo);
var _sPosC = string_copy(textbox._input_text, textbox.cursor + 1, _totAmo - textbox.cursor);
2023-07-12 16:28:32 +02:00
2023-09-15 20:12:02 +02:00
textbox._input_text = $"{_sPreC}{rep}{_sPosC}";
textbox.cursor += _repAmo - _prmAmo;
2023-07-12 16:28:32 +02:00
textbox.cut_line();
2023-11-26 13:16:38 +01:00
textbox.autocomplete_delay = 0;
2023-11-08 08:38:04 +01:00
textbox = noone;
2023-11-26 13:16:38 +01:00
prompt = "";
data = [];
2023-07-12 16:28:32 +02:00
}
#endregion