Pixel-Composer/objects/o_dialog_scrollbox/Create_0.gml

152 lines
3.7 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
/// @description init
event_inherited();
#region
2023-03-23 13:38:50 +01:00
max_h = 640;
align = fa_center;
2022-01-13 05:24:03 +01:00
draggable = false;
destroy_on_click_out = true;
2023-03-23 13:38:50 +01:00
selecting = -1;
scrollbox = noone;
data = [];
initVal = 0;
update_hover = true;
search_string = "";
KEYBOARD_STRING = "";
tb_search = new textBox(TEXTBOX_INPUT.text, function(str) {
search_string = string(str);
filterSearch();
});
tb_search.font = f_p2;
tb_search.color = COLORS._main_text_sub;
tb_search.align = fa_left;
tb_search.auto_update = true;
WIDGET_CURRENT = tb_search;
2022-01-13 05:24:03 +01:00
anchor = ANCHOR.top | ANCHOR.left;
2022-12-16 09:18:09 +01:00
2023-03-23 13:38:50 +01:00
function initScroll(scroll) {
scrollbox = scroll;
2024-03-31 05:36:11 +02:00
dialog_w = max(ui(200), scroll.w);
2023-03-23 13:38:50 +01:00
data = scroll.data;
setSize();
}
function filterSearch() {
if(search_string == "") {
data = scrollbox.data;
setSize();
return;
}
data = [];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(scrollbox.data); i < n; i++ ) {
2023-03-23 13:38:50 +01:00
var val = scrollbox.data[i];
if(val == -1) continue;
if(string_pos(string_lower(search_string), string_lower(val)) > 0)
array_push(data, val);
}
setSize();
}
function setSize() {
2023-05-28 20:00:51 +02:00
var hght = line_get_height(f_p0, 8);
2023-03-23 13:38:50 +01:00
var hh = ui(16 + 24);
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(data); i < n; i++ )
2023-03-23 13:38:50 +01:00
hh += data[i] == -1? ui(8) : hght;
dialog_h = min(max_h, hh);
2023-11-27 11:40:28 +01:00
sc_content.resize(dialog_w, dialog_h - ui(40));
2023-04-05 20:13:27 +02:00
resetPosition();
2023-03-23 13:38:50 +01:00
}
2022-12-16 09:18:09 +01:00
sc_content = new scrollPane(0, 0, function(_y, _m) {
draw_clear_alpha(COLORS.panel_bg_clear, 0);
2023-05-28 20:00:51 +02:00
var hght = line_get_height(f_p0, 8);
2022-12-16 09:18:09 +01:00
var _dw = sc_content.surface_w;
2023-02-17 11:31:33 +01:00
var _h = 0;
var _ly = _y;
2023-03-31 06:59:08 +02:00
var hovering = "";
2022-12-16 09:18:09 +01:00
for(var i = 0; i < array_length(data); i++) {
2023-10-27 13:55:31 +02:00
var _val = data[i];
var txt = is_instanceof(_val, scrollItem)? _val.name : _val;
var _spr = is_instanceof(_val, scrollItem) && _val.spr;
var _tol = is_instanceof(_val, scrollItem) && _val.tooltip != "";
2023-03-31 06:59:08 +02:00
var clickable = !string_starts_with(txt, "-");
if(!clickable)
txt = string_delete(txt, 1, 1);
2023-02-17 11:31:33 +01:00
if(data[i] == -1) {
draw_sprite_stretched(THEME.menu_separator, 0, ui(8), _ly, _dw - ui(16), ui(6));
_ly += ui(8);
_h += ui(8);
continue;
}
2022-12-16 09:18:09 +01:00
2023-03-31 06:59:08 +02:00
if(clickable) {
2023-11-17 06:24:31 +01:00
if(sc_content.hover && point_in_rectangle(_m[0], _m[1], 0, _ly, _dw, _ly + hght - 1)) {
2023-03-31 06:59:08 +02:00
selecting = i;
hovering = data[i];
2023-10-27 13:55:31 +02:00
if(_tol) TOOLTIP = _val.tooltip;
2023-03-31 06:59:08 +02:00
}
2023-01-25 06:49:00 +01:00
2023-03-31 06:59:08 +02:00
if(selecting == i) {
draw_sprite_stretched_ext(THEME.textbox, 3, 0, _ly, _dw, hght, COLORS.dialog_menubox_highlight, 1);
2022-12-16 09:18:09 +01:00
2023-03-31 06:59:08 +02:00
if(sc_content.active && (mouse_press(mb_left) || keyboard_check_pressed(vk_enter))) {
2023-10-27 13:55:31 +02:00
initVal = array_find(scrollbox.data, _val);
2023-03-31 06:59:08 +02:00
instance_destroy();
}
2022-12-16 09:18:09 +01:00
}
}
2023-10-27 13:55:31 +02:00
2023-03-31 06:59:08 +02:00
draw_set_text(f_p0, align, fa_center, clickable? COLORS._main_text : COLORS._main_text_sub);
2024-03-31 05:36:11 +02:00
if(align == fa_center) {
var _xc = _spr? hght + (_dw - hght) / 2 : _dw / 2;
draw_text_cut(_xc, _ly + hght / 2, txt, _dw);
} else if(align == fa_left)
draw_text_cut(ui(8) + _spr * hght, _ly + hght / 2, txt, _dw);
2023-10-27 13:55:31 +02:00
2024-01-28 09:53:41 +01:00
if(_spr) draw_sprite_ext(_val.spr, _val.spr_ind, ui(8) + hght / 2, _ly + hght / 2, 1, 1, 0, _val.spr_blend, 1);
2023-02-17 11:31:33 +01:00
_ly += hght;
_h += hght;
2022-12-16 09:18:09 +01:00
}
2023-02-19 13:49:20 +01:00
if(update_hover) {
UNDO_HOLDING = true;
2023-03-23 13:38:50 +01:00
if(hovering != "")
scrollbox.onModify(array_find(scrollbox.data, hovering));
2023-10-27 13:55:31 +02:00
else if(initVal > -1)
2023-03-23 13:38:50 +01:00
scrollbox.onModify(initVal);
2023-02-19 13:49:20 +01:00
UNDO_HOLDING = false;
}
2023-02-17 11:31:33 +01:00
2023-03-25 12:27:04 +01:00
if(sc_content.active) {
2023-01-25 06:49:00 +01:00
if(keyboard_check_pressed(vk_up)) {
selecting--;
if(selecting < 0) selecting = array_length(data) - 1;
}
if(keyboard_check_pressed(vk_down))
2023-02-20 10:16:31 +01:00
selecting = safe_mod(selecting + 1, array_length(data));
2023-01-25 06:49:00 +01:00
if(keyboard_check_pressed(vk_escape))
instance_destroy();
}
2022-12-16 09:18:09 +01:00
return _h;
});
2022-01-13 05:24:03 +01:00
#endregion