Pixel-Composer/scripts/buttonGroup/buttonGroup.gml

159 lines
4.2 KiB
Text
Raw Normal View History

2023-07-30 13:56:22 +02:00
function buttonGroup(_data, _onClick) : widget() constructor {
2022-01-13 05:24:03 +01:00
data = _data;
onClick = _onClick;
2024-04-18 10:04:08 +02:00
size = array_length(data);
2024-03-26 04:03:45 +01:00
2023-01-17 08:11:55 +01:00
display_button = false;
2024-03-26 04:03:45 +01:00
buttonSpr = [ THEME.button_left, THEME.button_middle, THEME.button_right ];
fColor = COLORS._main_text;
2022-01-13 05:24:03 +01:00
2024-04-18 07:12:31 +02:00
tooltips = [];
2022-01-13 05:24:03 +01:00
2024-04-18 10:04:08 +02:00
current_selecting = 0;
2024-07-02 10:51:52 +02:00
collapsable = true;
2024-04-18 10:04:08 +02: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-08-31 19:56:23 +02:00
static setFont = function(font) {
self.font = font;
return self;
}
2024-07-02 10:51:52 +02:00
static setTooltips = function(tt) { tooltips = tt; return self; }
static setCollape = function(cc) { collapsable = cc; return self; }
2024-04-18 07:12:31 +02:00
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);
}
2023-07-30 19:56:53 +02:00
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
2024-03-29 05:20:49 +01:00
sb_small.setParam(params);
2024-03-26 04:03:45 +01:00
for(var i = 0; i < array_length(data); i++)
buttons[i].setParam(params);
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);
}
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;
2023-08-24 11:59:05 +02:00
while(is_array(current_selecting))
2024-03-31 05:36:11 +02:00
current_selecting = array_safe_get_fast(current_selecting, 0);
2024-03-28 14:18:02 +01:00
hovering = hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h);
2023-01-17 08:11:55 +01:00
2024-06-26 06:38:57 +02:00
var amo = array_length(data);
var _tw = _w;
var _mx = false, _t = 0;
var _sw = _h + ui(8);
var tamo = amo;
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
var total_width = 0;
2024-03-26 04:03:45 +01:00
draw_set_font(font);
2022-01-13 05:24:03 +01:00
for(var i = 0; i < amo; i++) {
2024-06-26 06:38:57 +02:00
var _d = data[i];
if(is_string(_d)) {
_t = 1;
total_width += ui(32) + string_width(_d);
} else if(sprite_exists(_d)) {
if(_t) _mx = true;
if(_mx) tamo--;
total_width += _sw;
_tw -= _sw;
}
2022-12-10 05:06:01 +01:00
}
2024-07-02 10:51:52 +02:00
display_button = !collapsable || total_width < _w;
2024-06-26 06:38:57 +02:00
var ww = (_mx? _tw : _w) / tamo;
2023-01-17 08:11:55 +01:00
if(display_button) {
2023-08-31 19:56:23 +02:00
var bx = _x;
var draw_sel = noone;
2023-08-31 19:56:23 +02:00
2022-12-10 05:06:01 +01:00
for(var i = 0; i < amo; i++) {
2024-06-26 06:38:57 +02:00
var _d = data[i];
2023-06-21 20:36:53 +02:00
buttons[i].setFocusHover(active, hover);
2024-04-18 07:12:31 +02:00
buttons[i].tooltip = array_safe_get(tooltips, i, "");
2023-08-31 19:56:23 +02:00
2024-06-26 06:38:57 +02:00
var bww = !is_string(_d) && sprite_exists(_d) && _mx? _sw : ww;
2023-03-19 09:17:39 +01:00
var spr = i == 0 ? buttonSpr[0] : (i == amo - 1? buttonSpr[2] : buttonSpr[1]);
2023-10-31 05:30:42 +01:00
2023-04-08 20:06:27 +02:00
if(_selecting == i) {
draw_sprite_stretched_ext(spr, 2, floor(bx), _y, ceil(bww), _h, boxColor);
2023-09-07 20:59:14 +02:00
draw_sel = [spr, bx];
2023-07-23 20:21:35 +02:00
} else {
2024-06-26 06:38:57 +02:00
buttons[i].draw(floor(bx), _y, ceil(bww), _h, _m, spr);
2023-07-23 20:21:35 +02:00
if(buttons[i].clicked) onClick(i);
}
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);
2024-06-26 06:38:57 +02:00
draw_text_add(bx + bww / 2, _y + _h / 2, data[i]);
2022-12-10 05:06:01 +01:00
} else if(sprite_exists(data[i])) {
2024-06-26 06:38:57 +02:00
draw_sprite_ui_uniform(data[i], i, bx + bww / 2, _y + _h / 2, 1);
2022-12-10 05:06:01 +01:00
}
2023-08-31 19:56:23 +02:00
2024-06-26 06:38:57 +02:00
bx += bww;
2022-01-13 05:24:03 +01:00
}
2023-02-14 05:32:32 +01:00
if(draw_sel != noone)
draw_sprite_stretched_ext(draw_sel[0], 3, draw_sel[1], _y, ww, _h, COLORS._main_accent, 1);
2023-09-07 20:59:14 +02: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-06-21 20:36:53 +02:00
sb_small.setFocusHover(active, hover);
2024-07-02 13:25:59 +02:00
sb_small.draw(_x, _y, _w, _h, array_safe_get(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();
2023-07-30 19:56:53 +02:00
return h;
2022-01-13 05:24:03 +01:00
}
2024-03-31 11:10:14 +02:00
static clone = function() { #region
var cln = new buttonGroup(data, onClick);
return cln;
} #endregion
2022-01-13 05:24:03 +01:00
}