Pixel-Composer/scripts/button/button.gml

182 lines
4.8 KiB
Text
Raw Normal View History

2023-12-22 14:46:54 +01:00
function button(_onClick, _icon = noone) { INLINE 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;
2023-03-19 09:17:39 +01:00
text = "";
2022-01-13 05:24:03 +01:00
tooltip = "";
2023-03-19 09:17:39 +01:00
blend = c_white;
2022-01-13 05:24:03 +01:00
onClick = _onClick;
2023-05-03 21:42:17 +02:00
triggered = false;
2023-07-23 20:21:35 +02:00
activate_on_press = false;
clicked = false;
2024-01-12 05:03:35 +01:00
pressed = false;
2023-08-08 20:33:17 +02:00
toggled = false;
context = noone;
2023-08-08 20:33:17 +02:00
static setContext = function(struct) {
2024-05-24 07:44:36 +02:00
onClick = method(struct, onClick);
return self;
}
2024-05-24 07:44:36 +02:00
static setLua = function(_lua_thread, _lua_key, _lua_func) {
2023-05-07 20:55:13 +02:00
lua_thread = _lua_thread;
lua_thread_key = _lua_key;
onClick = method(self, _lua_func);
}
2023-05-07 20:55:13 +02:00
static trigger = function() {
2023-07-23 20:21:35 +02:00
clicked = true;
2023-05-07 20:55:13 +02:00
if(!is_callable(onClick))
return noone;
2023-05-03 21:42:17 +02:00
triggered = true;
2023-01-17 08:11:55 +01:00
onClick();
}
2023-01-17 08:11:55 +01:00
static isTriggered = function() {
2023-05-03 21:42:17 +02:00
var t = triggered;
triggered = false;
return t;
}
2023-05-03 21:42:17 +02:00
static setIcon = function(_icon, _index = 0, _blend = c_white) {
2022-12-16 09:18:09 +01:00
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;
}
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.m);
}
2023-07-30 19:56:53 +02:00
static draw = function(_x, _y, _w, _h, _m, spr = THEME.button_def, blend = c_white) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2024-03-28 14:18:02 +01:00
clicked = false;
hovering = hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h);
2023-07-23 20:21:35 +02:00
2023-03-19 09:17:39 +01:00
var b = colorMultiply(self.blend, blend);
2022-01-13 05:24:03 +01:00
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h)) {
2023-08-08 20:33:17 +02:00
draw_sprite_stretched_ext(spr, toggled? 2 : 1, _x, _y, _w, _h, b, 1);
2024-03-28 14:18:02 +01:00
2024-01-12 05:03:35 +01:00
if(!activate_on_press && pressed && mouse_release(mb_left, active))
2023-07-23 20:21:35 +02:00
trigger();
2024-01-12 05:03:35 +01:00
if(mouse_press(mb_left, active)) {
pressed = true;
if(activate_on_press) trigger();
}
2023-04-08 20:06:27 +02:00
if(mouse_click(mb_left, active)) {
draw_sprite_stretched_ext(spr, 2, _x, _y, _w, _h, b, 1);
draw_sprite_stretched_ext(spr, 3, _x, _y, _w, _h, COLORS._main_accent, 1);
}
2022-01-13 05:24:03 +01:00
if(tooltip != "") TOOLTIP = tooltip;
} else {
2023-08-08 20:33:17 +02:00
draw_sprite_stretched_ext(spr, toggled? 2 : 0, _x, _y, _w, _h, b, 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
2023-01-25 06:49:00 +01:00
var aa = interactable * 0.25 + 0.75;
if(icon) {
2023-10-03 11:27:36 +02:00
var ind = is_array(icon_index)? icon_index[0]() : icon_index;
draw_sprite_ui_uniform(icon, ind, _x + _w / 2, _y + _h / 2,, icon_blend, aa);
}
2022-01-13 05:24:03 +01:00
if(text != "") {
2023-01-25 06:49:00 +01:00
draw_set_alpha(aa);
2024-03-26 04:03:45 +01:00
draw_set_text(font, fa_center, fa_center, COLORS._main_text);
draw_text_add(_x + _w / 2, _y + _h / 2, text);
2023-01-25 06:49:00 +01:00
draw_set_alpha(1);
2022-01-13 05:24:03 +01:00
}
if(WIDGET_CURRENT == self) 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
2024-01-12 05:03:35 +01:00
if(mouse_release(mb_left)) pressed = false;
2023-07-23 20:21:35 +02:00
return _h;
}
2024-03-31 11:10:14 +02:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new buttonClass(onClick);
cln.icon = icon;
cln.icon_blend = icon_blend;
cln.icon_index = icon_index;
cln.text = text;
cln.tooltip = tooltip;
cln.blend = blend;
return cln;
}
2022-01-13 05:24:03 +01:00
}
2024-05-23 10:59:39 +02: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, _icon_scale = 1) {
2022-01-13 05:24:03 +01:00
var res = 0;
2023-02-23 07:02:19 +01:00
var cc = is_array(_icon_blend)? _icon_blend[0] : _icon_blend;
2022-01-13 05:24:03 +01:00
if(_hvr && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h)) {
2023-02-23 07:02:19 +01:00
if(is_array(_icon_blend))
cc = _icon_blend[1];
2022-01-13 05:24:03 +01:00
res = 1;
2023-02-28 09:43:01 +01:00
if(spr) draw_sprite_stretched(spr, 1, _x, _y, _w, _h);
2023-06-04 18:28:29 +02:00
if(_tip != "") TOOLTIP = _tip;
2023-01-17 08:11:55 +01:00
2023-10-04 11:28:13 +02:00
if(mouse_press(mb_left, _act)) res = 2;
if(mouse_press(mb_right, _act)) res = 3;
2023-01-17 08:11:55 +01:00
2023-10-04 11:28:13 +02:00
if(mouse_release(mb_left, _act)) res = -2;
if(mouse_release(mb_right, _act)) res = -3;
2023-02-23 07:02:19 +01:00
2023-04-08 20:06:27 +02:00
if(spr && mouse_click(mb_left, _act)) {
2022-01-13 05:24:03 +01:00
draw_sprite_stretched(spr, 2, _x, _y, _w, _h);
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 3, _x, _y, _w, _h, COLORS._main_accent, 1);
}
2023-02-28 09:43:01 +01:00
} else if(spr)
2022-01-13 05:24:03 +01:00
draw_sprite_stretched(spr, 0, _x, _y, _w, _h);
2024-07-04 03:11:32 +02:00
if(_icon) draw_sprite_ui_uniform(_icon, _icon_index, _x + _w / 2, _y + _h / 2, _icon_scale, cc, _icon_alpha == 1 || res == 0? _icon_alpha : _icon_alpha + 0.4);
2022-01-13 05:24:03 +01:00
return res;
2024-05-23 10:59:39 +02:00
}
2024-05-24 07:44:36 +02:00
function buttonTextIconInstant(active, spr, _x, _y, _w, _h, _m, _act, _hvr, _tip = "", _icon = noone, _icon_label = "", _icon_blend = COLORS._main_icon_light, _icon_alpha = 1) {
var _b = 0;
if(active) _b = buttonInstant(spr, _x, _y, _w, _h, _m, _act, _hvr, _tip);
2024-05-23 10:59:39 +02:00
2024-05-24 07:44:36 +02:00
draw_set_text(f_p1, fa_left, fa_center, active? COLORS._main_icon_light : COLORS._main_icon);
2024-05-23 10:59:39 +02:00
var bxc = _x + _w / 2 - (string_width(_icon_label) + ui(64)) / 2;
var byc = _y + _h / 2;
2024-07-15 10:43:30 +02:00
if(_icon) draw_sprite_ui(_icon, 0, bxc + ui(24), byc, 1, 1, 0, _icon_blend, _icon_alpha * (0.5 + 0.5 * active));
2024-05-24 07:44:36 +02:00
draw_text_add(bxc + ui(48), byc, _icon_label);
2024-05-23 10:59:39 +02:00
return _b;
}