Pixel-Composer/scripts/buttonGroup/buttonGroup.gml

101 lines
2.7 KiB
Text
Raw Normal View History

2022-01-13 05:24:03 +01:00
function buttonGroup(_data, _onClick) {
return new buttonGroupClass(_data, _onClick);
}
2023-01-17 08:11:55 +01:00
function buttonGroupClass(_data, _onClick) : widget() constructor {
2022-01-13 05:24:03 +01:00
data = _data;
onClick = _onClick;
2023-01-17 08:11:55 +01:00
display_button = false;
2023-03-19 09:17:39 +01:00
buttonSpr = [ THEME.button_left, THEME.button_middle, THEME.button_right ];
font = f_p0;
fColor = COLORS._main_text;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
current_selecting = 0;
2022-01-13 05:24:03 +01:00
2023-01-25 06:49:00 +01:00
for(var i = 0; i < array_length(data); i++)
buttons[i] = button(-1);
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
sb_small = new scrollBox(data, _onClick);
2023-01-17 08:11:55 +01:00
static trigger = function() {
if(current_selecting + 1 >= array_length(data))
onClick(0);
else
onClick(current_selecting + 1);
}
2023-01-25 06:49:00 +01:00
static setInteract = function(interactable = noone) {
self.interactable = interactable;
for(var i = 0; i < array_length(data); i++)
buttons[i].interactable = interactable;
sb_small.interactable = interactable;
}
2023-01-17 08:11:55 +01:00
static register = function(parent = noone) {
if(display_button) {
array_push(WIDGET_ACTIVE, self);
self.parent = parent;
} else
sb_small.register(parent);
}
2022-12-12 09:08:03 +01:00
static draw = function(_x, _y, _w, _h, _selecting, _m, _rx = 0, _ry = 0) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
current_selecting = _selecting;
2022-01-13 05:24:03 +01:00
var amo = array_length(data);
var ww = _w / amo;
2022-12-10 05:06:01 +01:00
var total_width = 0;
draw_set_font(f_p0);
2022-01-13 05:24:03 +01:00
for(var i = 0; i < amo; i++) {
2022-12-10 05:06:01 +01:00
if(is_string(data[i]))
total_width += string_width(data[i]) + ui(32);
}
2023-01-17 08:11:55 +01:00
display_button = total_width < _w;
if(display_button) {
2022-12-10 05:06:01 +01:00
for(var i = 0; i < amo; i++) {
2023-03-25 12:27:04 +01:00
buttons[i].setActiveFocus(hover, active);
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
var bx = _x + ww * i;
2023-03-19 09:17:39 +01:00
var spr = i == 0 ? buttonSpr[0] : (i == amo - 1? buttonSpr[2] : buttonSpr[1]);
2022-01-13 05:24:03 +01:00
2023-04-08 20:06:27 +02:00
if(_selecting == i) {
2022-12-10 05:06:01 +01:00
draw_sprite_stretched(spr, 2, bx, _y, ww, _h);
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 3, bx, _y, ww, _h, COLORS._main_accent, 1);
} else if(buttons[i].draw(bx, _y, ww, _h, _m, spr))
2023-01-17 08:11:55 +01:00
onClick(i);
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
if(is_string(data[i])) {
2023-03-19 09:17:39 +01:00
draw_set_text(font, fa_center, fa_center, fColor);
2022-12-10 05:06:01 +01:00
draw_text(bx + ww / 2, _y + _h / 2, data[i]);
} else if(sprite_exists(data[i])) {
draw_sprite_ui_uniform(data[i], i, bx + ww / 2, _y + _h / 2);
}
2022-01-13 05:24:03 +01:00
}
2023-02-14 05:32:32 +01:00
if(point_in_rectangle(_m[0], _m[1], _x, _y, _x + w, _y + _h)) {
if(is_array(data) && key_mod_press(SHIFT)) {
var len = array_length(data);
if(len) {
2023-02-20 10:16:31 +01:00
if(mouse_wheel_down()) onClick(safe_mod(_selecting + 1 + len, len));
if(mouse_wheel_up()) onClick(safe_mod(_selecting - 1 + len, len));
2023-02-14 05:32:32 +01:00
}
}
}
2022-12-10 05:06:01 +01:00
} else {
2023-03-25 12:27:04 +01:00
sb_small.setActiveFocus(hover, active);
2022-12-12 09:08:03 +01:00
sb_small.draw(_x, _y, _w, _h, data[_selecting], _m, _rx, _ry);
2022-01-13 05:24:03 +01:00
}
2022-12-10 05:06:01 +01:00
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);
2023-01-17 08:11:55 +01:00
resetFocus();
2022-01-13 05:24:03 +01:00
}
}