Pixel-Composer/scripts/button/button.gml

96 lines
2.3 KiB
Plaintext
Raw Normal View History

2023-01-01 02:06:02 +01:00
function button(_onClick, _icon = noone) {
return new buttonClass(_onClick, _icon);
2022-01-13 05:24:03 +01:00
}
2023-01-17 08:11:55 +01:00
function buttonClass(_onClick, _icon = noone) : widget() constructor {
2023-01-01 02:06:02 +01:00
icon = _icon;
2022-12-16 09:18:09 +01:00
icon_blend = c_white;
2022-01-13 05:24:03 +01:00
icon_index = 0;
text = "";
tooltip = "";
onClick = _onClick;
2023-01-17 08:11:55 +01:00
static trigger = function() {
if(!onClick) return;
onClick();
}
2022-12-16 09:18:09 +01:00
static setIcon = function(_icon, _index = 0, _blend = c_white) {
icon = _icon;
icon_index = _index;
icon_blend = _blend;
return self;
}
2022-01-13 05:24:03 +01:00
static setText = function(_text) {
text = _text;
return self;
}
static setTooltip = function(_tip) {
tooltip = _tip;
return self;
}
2022-11-18 03:20:31 +01:00
static draw = function(_x, _y, _w, _h, _m, spr = THEME.button, blend = c_white) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2022-01-13 05:24:03 +01:00
var click = false;
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h)) {
draw_sprite_stretched_ext(spr, 1, _x, _y, _w, _h, blend, 1);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2023-01-17 08:11:55 +01:00
trigger();
2022-01-13 05:24:03 +01:00
click = true;
}
2022-12-10 05:06:01 +01:00
if(mouse_click(mb_left, active))
draw_sprite_stretched_ext(spr, 2, _x, _y, _w, _h, blend, 1);
2022-01-13 05:24:03 +01:00
if(tooltip != "") TOOLTIP = tooltip;
} else {
draw_sprite_stretched_ext(spr, 0, _x, _y, _w, _h, blend, 1);
2023-01-17 08:11:55 +01:00
if(mouse_press(mb_left)) deactivate();
2022-01-13 05:24:03 +01:00
}
2023-01-17 08:11:55 +01:00
2022-12-16 09:18:09 +01:00
if(icon) draw_sprite_ui_uniform(icon, icon_index, _x + _w / 2, _y + _h / 2,, icon_blend);
2022-01-13 05:24:03 +01:00
if(text != "") {
2022-11-18 03:20:31 +01:00
draw_set_text(f_p0, fa_center, fa_center, COLORS._main_text);
2022-01-13 05:24:03 +01:00
draw_text(_x + _w / 2, _y + _h / 2, text);
}
2023-01-17 08:11:55 +01:00
if(WIDGET_CURRENT == self)
draw_sprite_stretched(THEME.widget_selecting, 0, _x - ui(3), _y - ui(3), _w + ui(6), _h + ui(6));
resetFocus();
2022-01-13 05:24:03 +01:00
return click;
}
}
2022-11-18 03:20:31 +01:00
function buttonInstant(spr, _x, _y, _w, _h, _m, _act, _hvr, _tip = "", _icon = noone, _icon_index = 0, _icon_blend = COLORS._main_icon, _icon_alpha = 1) {
2022-01-13 05:24:03 +01:00
var res = 0;
if(_hvr && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h)) {
res = 1;
draw_sprite_stretched(spr, 1, _x, _y, _w, _h);
if(_tip != "")
TOOLTIP = _tip;
2023-01-17 08:11:55 +01:00
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, _act))
2022-01-13 05:24:03 +01:00
res = 2;
2023-01-17 08:11:55 +01:00
if(mouse_press(mb_right, _act))
res = 3;
2022-12-10 05:06:01 +01:00
if(mouse_click(mb_left, _act))
2022-01-13 05:24:03 +01:00
draw_sprite_stretched(spr, 2, _x, _y, _w, _h);
2023-01-17 08:11:55 +01:00
} else
2022-01-13 05:24:03 +01:00
draw_sprite_stretched(spr, 0, _x, _y, _w, _h);
2023-01-17 08:11:55 +01:00
if(_icon)
2022-11-03 11:44:49 +01:00
draw_sprite_ui_uniform(_icon, _icon_index, _x + _w / 2, _y + _h / 2, 1, _icon_blend, _icon_alpha);
2022-01-13 05:24:03 +01:00
return res;
}