Pixel-Composer/scripts/checkbox/checkbox.gml

93 lines
2.3 KiB
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function checkBox(_onClick) : widget() constructor {
onClick = _onClick;
2023-05-03 21:42:17 +02:00
triggered = false;
spr = THEME.checkbox_def;
2022-01-13 05:24:03 +01:00
2023-05-07 20:55:13 +02:00
static setLua = function(_lua_thread, _lua_key, _lua_func) {
lua_thread = _lua_thread;
lua_thread_key = _lua_key;
onClick = method(self, _lua_func);
}
2023-01-17 08:11:55 +01:00
static trigger = function() {
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-05-03 21:42:17 +02:00
static isTriggered = function() {
var t = triggered;
triggered = false;
return t;
}
2023-07-30 19:56:53 +02:00
static drawParam = function(params) {
var ss = params.s;
var x0, y0;
2023-07-30 19:56:53 +02:00
switch(params.halign) {
case fa_left : x0 = params.x; break;
case fa_center : x0 = params.x + (params.w - ss) / 2; break;
case fa_right : x0 = params.x + params.w - ss; break;
2023-07-30 19:56:53 +02:00
}
switch(params.valign) {
case fa_top : y0 = params.y; break;
case fa_center : y0 = params.y + (params.h - ss) / 2; break;
case fa_bottom : y0 = params.y + params.h - ss; break;
}
return draw(x0, y0, params.data, params.m, params.s);
2023-07-30 19:56:53 +02:00
}
2022-11-03 11:44:49 +01:00
static draw = function(_x, _y, _value, _m, ss = ui(28), halign = fa_left, valign = fa_top) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = ss;
h = ss;
2022-12-19 13:35:30 +01:00
var _dx, _dy;
2022-11-03 11:44:49 +01:00
switch(halign) {
2022-12-19 13:35:30 +01:00
case fa_left: _dx = _x; break;
case fa_center: _dx = _x - ss / 2; break;
case fa_right: _dx = _x - ss; break;
2022-11-03 11:44:49 +01:00
}
switch(valign) {
2022-12-19 13:35:30 +01:00
case fa_top: _dy = _y; break;
case fa_center: _dy = _y - ss / 2; break;
case fa_bottom: _dy = _y - ss; break;
2022-11-03 11:44:49 +01:00
}
2022-01-13 05:24:03 +01:00
2023-01-25 06:49:00 +01:00
var aa = interactable * 0.25 + 0.75;
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 0, _dx, _dy, ss, ss, c_white, aa);
2022-01-13 05:24:03 +01:00
2022-12-19 13:35:30 +01:00
if(hover && point_in_rectangle(_m[0], _m[1], _dx, _dy, _dx + ss, _dy + ss)) {
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 1, _dx, _dy, ss, ss, c_white, aa);
2022-01-13 05:24:03 +01:00
2022-12-19 13:35:30 +01:00
if(mouse_press(mb_left, active))
2023-01-17 08:11:55 +01:00
trigger();
} else
if(mouse_press(mb_left)) deactivate();
2023-06-01 19:50:53 +02:00
if(is_array(_value))
draw_sprite_stretched_ext(spr, 3, _dx, _dy + ss / 2 - 8, ss, 16, COLORS._main_accent, aa);
else if(_value)
draw_sprite_stretched_ext(spr, 2, _dx, _dy, ss, ss, COLORS._main_accent, aa);
2023-04-08 20:06:27 +02: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, _dx - ui(3), _dy - ui(3), ss + ui(6), ss + ui(6), COLORS._main_accent, 1);
2022-01-13 05:24:03 +01:00
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 checkBox(onClick);
return cln;
} #endregion
2022-01-13 05:24:03 +01:00
}