Pixel-Composer/scripts/scrollBox/scrollBox.gml

147 lines
4 KiB
Text
Raw Normal View History

2024-01-28 09:53:41 +01:00
function scrollItem(name, spr = noone, spr_ind = 0, spr_blend = COLORS._main_icon) constructor {
2023-10-27 13:55:31 +02:00
self.name = name;
self.data = name;
2024-01-28 09:53:41 +01:00
self.spr = spr;
self.spr_ind = spr_ind;
self.spr_blend = spr_blend;
2023-10-27 13:55:31 +02:00
tooltip = "";
}
2023-03-19 09:17:39 +01:00
function scrollBox(_data, _onModify, update_hover = true) : widget() constructor {
2024-01-28 09:53:41 +01:00
self.update_hover = update_hover;
2022-01-13 05:24:03 +01:00
onModify = _onModify;
data_list = _data;
2023-11-17 06:24:31 +01:00
data = _data;
2023-02-17 11:31:33 +01:00
curr_text = 0;
2022-01-13 05:24:03 +01:00
2023-08-19 12:42:50 +02:00
arrow_spr = THEME.scroll_box_arrow;
arrow_ind = 0;
2024-01-28 09:53:41 +01:00
open = false;
2023-01-17 08:11:55 +01:00
open_rx = 0;
open_ry = 0;
2022-01-13 05:24:03 +01:00
2024-01-28 09:53:41 +01:00
align = fa_center;
2024-07-15 10:43:30 +02:00
horizontal = false;
2022-12-16 09:18:09 +01:00
extra_button = noone;
2024-07-15 10:43:30 +02:00
padding = ui(8);
item_pad = ui(8);
2024-07-15 10:43:30 +02:00
static trigger = function() {
2023-02-17 11:31:33 +01:00
if(is_method(data_list)) data = data_list();
else data = data_list;
var ind = array_find(data, curr_text);
2024-01-28 09:53:41 +01:00
open = true;
2023-02-17 11:31:33 +01:00
2024-07-15 10:43:30 +02:00
with(dialogCall(horizontal? o_dialog_scrollbox_horizontal : o_dialog_scrollbox, x + open_rx, y + open_ry)) {
2024-01-28 09:53:41 +01:00
initVal = ind;
2024-07-15 10:43:30 +02:00
font = other.font;
2024-01-28 09:53:41 +01:00
align = other.align;
2024-07-15 10:43:30 +02:00
text_pad = other.padding;
item_pad = other.item_pad;
2023-02-19 13:49:20 +01:00
update_hover = other.update_hover;
2024-07-15 10:43:30 +02:00
initScroll(other);
2023-01-17 08:11:55 +01:00
}
2024-07-15 10:43:30 +02:00
}
2023-01-17 08:11:55 +01:00
2024-07-15 10:43:30 +02:00
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
2024-03-26 04:03:45 +01:00
2023-07-30 19:56:53 +02:00
return draw(params.x, params.y, params.w, params.h, params.data, params.m, params.rx, params.ry);
2024-07-15 10:43:30 +02:00
}
2023-07-30 19:56:53 +02:00
2024-07-15 10:43:30 +02:00
static draw = function(_x, _y, _w, _h, _val, _m = mouse_ui, _rx = 0, _ry = 0) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
open_rx = _rx;
open_ry = _ry;
h = _h;
2023-07-30 19:56:53 +02:00
if(is_method(data_list)) data = data_list();
else data = data_list;
2023-11-17 06:24:31 +01:00
var _selVal = _val;
2023-08-24 11:59:05 +02:00
if(is_array(_val)) return 0;
2024-03-31 05:36:11 +02:00
if(is_numeric(_val)) _selVal = array_safe_get_fast(data, _val);
2023-10-27 13:55:31 +02:00
2023-11-17 06:24:31 +01:00
var _text = is_instanceof(_selVal, scrollItem)? _selVal.name : _selVal;
2024-07-15 10:43:30 +02:00
_text = string_trim_start(_text, ["-", ">", " "]);
2023-02-17 11:31:33 +01:00
curr_text = _text;
2023-01-17 08:11:55 +01:00
w = _w;
2022-12-16 09:18:09 +01:00
if(extra_button != noone) {
2023-06-21 20:36:53 +02:00
extra_button.setFocusHover(active, hover);
2022-12-16 09:18:09 +01:00
extra_button.draw(_x + _w - ui(32), _y + _h / 2 - ui(32 / 2), ui(32), ui(32), _m, THEME.button_hide);
2023-01-17 08:11:55 +01:00
w -= ui(40);
2022-12-16 09:18:09 +01:00
}
if(open) {
2023-01-17 08:11:55 +01:00
resetFocus();
2023-07-30 19:56:53 +02:00
return h;
2022-12-16 09:18:09 +01:00
}
2023-01-17 08:11:55 +01:00
draw_sprite_stretched(THEME.textbox, 3, _x, _y, w, _h);
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + w, _y + _h)) {
draw_sprite_stretched(THEME.textbox, 1, _x, _y, w, _h);
if(mouse_press(mb_left, active))
trigger();
2022-12-16 09:18:09 +01:00
if(mouse_click(mb_left, active))
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(THEME.textbox, 2, _x, _y, w, _h, COLORS._main_accent, 1);
2023-02-14 05:32:32 +01:00
if(is_array(data_list) && key_mod_press(SHIFT)) {
var ind = array_find(data_list, _text);
var len = array_length(data_list);
if(len) {
2023-02-20 10:16:31 +01:00
if(mouse_wheel_down()) onModify(safe_mod(ind + 1 + len, len));
if(mouse_wheel_up()) onModify(safe_mod(ind - 1 + len, len));
2023-02-14 05:32:32 +01:00
}
}
2023-01-17 08:11:55 +01:00
} else {
draw_sprite_stretched_ext(THEME.textbox, 0, _x, _y, w, _h, c_white, 0.5 + 0.5 * interactable);
if(mouse_press(mb_left)) deactivate();
}
2022-01-13 05:24:03 +01:00
2024-03-31 05:36:11 +02:00
var _sps = min(1, _h / 24);
2024-06-12 05:21:27 +02:00
var _ars = min(1, _h / 48);
2024-03-29 05:20:49 +01:00
var _arw = sprite_get_width(arrow_spr) * _ars + ui(8);
2023-11-17 06:24:31 +01:00
var _spr = is_instanceof(_selVal, scrollItem) && _selVal.spr;
2023-08-19 12:42:50 +02:00
2024-01-28 09:53:41 +01:00
var _x0 = _x;
var _x1 = _x + w - _arw;
var _yc = _y + _h / 2;
if(_spr) _x0 += ui(32);
2023-08-19 12:42:50 +02:00
draw_set_text(font, align, fa_center, COLORS._main_text);
2024-03-29 09:13:26 +01:00
if(_h >= line_get_height()) {
draw_set_alpha(0.5 + 0.5 * interactable);
2024-03-31 05:36:11 +02:00
if(align == fa_center) draw_text_add((_x0 + _x1) / 2, _yc, _text, _sps);
2024-07-15 10:43:30 +02:00
else if(align == fa_left) draw_text_add(_x0 + padding, _yc, _text, _sps);
2024-03-29 09:13:26 +01:00
draw_set_alpha(1);
}
2023-01-17 08:11:55 +01:00
2024-03-31 05:36:11 +02:00
if(_spr) draw_sprite_ext(_selVal.spr, _selVal.spr_ind, _x + ui(16) * _sps, _yc, _sps, _sps, 0, _selVal.spr_blend, 1);
2023-10-27 13:55:31 +02:00
2024-03-29 05:20:49 +01:00
draw_sprite_ui_uniform(arrow_spr, arrow_ind, _x1 + _arw / 2, _yc, _ars, COLORS._main_icon, 0.5 + 0.5 * interactable);
2023-01-17 08:11:55 +01:00
if(WIDGET_CURRENT == self)
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(THEME.widget_selecting, 0, _x - ui(3), _y - ui(3), _w + ui(6), _h + ui(6), COLORS._main_accent, 1);
2022-12-16 09:18:09 +01:00
2023-01-17 08:11:55 +01:00
resetFocus();
2023-07-30 19:56:53 +02:00
return h;
2024-07-15 10:43:30 +02:00
}
2024-03-31 11:10:14 +02:00
2024-07-15 10:43:30 +02:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new scrollBox(data, onModify, update_hover);
return cln;
2024-07-15 10:43:30 +02:00
}
2022-01-13 05:24:03 +01:00
}