Pixel-Composer/scripts/matrixGrid/matrixGrid.gml

139 lines
3.3 KiB
Text
Raw Normal View History

2023-09-15 20:12:02 +02:00
function matrixGrid(_type, _size, _onModify, _unit = noone) : widget() constructor {
2024-03-31 11:10:14 +02:00
type = _type;
size = 1;
2023-01-04 02:30:04 +01:00
onModify = _onModify;
unit = _unit;
linked = false;
b_link = button(function() /*=>*/ { linked = !linked; });
2023-01-04 02:30:04 +01:00
b_link.icon = THEME.value_link;
onModifyIndex = function(val, index) {
2023-02-14 05:32:32 +01:00
var modi = false;
2023-01-04 02:30:04 +01:00
if(linked) {
for( var i = 0; i < size * size; i++ )
modi |= onModify(toNumber(val), i);
2023-02-14 05:32:32 +01:00
return modi;
2023-01-04 02:30:04 +01:00
}
return onModify(toNumber(val), index);
2023-01-04 02:30:04 +01:00
}
extras = -1;
static setSize = function(_size) {
if(size == _size) return self;
size = _size;
for(var i = 0; i < size * size; i++) {
tb[i] = new textBox(type, onModifyIndex);
tb[i].onModifyParam = i;
tb[i].slidable = true;
}
return self;
} setSize(_size);
static setInteract = function(interactable = false) {
self.interactable = interactable;
2023-01-25 06:49:00 +01:00
b_link.interactable = interactable;
for( var i = 0; i < size * size; i++ )
2023-01-25 06:49:00 +01:00
tb[i].interactable = interactable;
if(extras)
extras.interactable = interactable;
}
2023-01-17 08:11:55 +01:00
static register = function(parent = noone) {
b_link.register(parent);
for( var i = 0; i < size * size; i++ )
2023-01-17 08:11:55 +01:00
tb[i].register(parent);
if(extras)
extras.register(parent);
if(unit != noone && unit.reference != noone)
unit.triggerButton.register(parent);
}
2024-03-28 14:18:02 +01:00
static isHovering = function() {
for( var i = 0, n = size * size; i < n; i++ ) if(tb[i].isHovering()) return true;
2024-03-28 14:18:02 +01:00
return false;
}
2023-07-30 19:56:53 +02:00
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
for(var i = 0; i < size * size; i++)
2024-03-27 11:51:14 +01:00
tb[i].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.data, params.m);
}
2023-01-04 02:30:04 +01:00
static draw = function(_x, _y, _w, _h, _data, _m) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
2024-03-26 04:03:45 +01:00
h = _h * size;
2023-01-17 08:11:55 +01:00
var ww = _w / size;
2024-03-29 05:20:49 +01:00
var _bs = min(_h, ui(32));
if((_w - _bs) / size > ui(64)) {
if(extras && instanceof(extras) == "buttonClass") {
extras.setFocusHover(active, hover);
extras.draw(_x + _w - _bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m, THEME.button_hide);
_w -= _bs + ui(8);
}
if(unit != noone && unit.reference != noone) {
_w += ui(4);
2023-01-04 02:30:04 +01:00
2024-03-29 05:20:49 +01:00
unit.triggerButton.setFocusHover(iactive, ihover);
unit.draw(_x + _w - _bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m);
_w -= _bs + ui(8);
}
b_link.setFocusHover(active, hover);
b_link.icon_index = linked;
b_link.icon_blend = linked? COLORS._main_accent : COLORS._main_icon;
b_link.tooltip = linked? __txt("Unlink values") : __txt("Link values");
var th = _h * size - ui(8);
var bx = _x;
var by = _y + th / 2 - _bs / 2;
b_link.draw(bx, by, _bs, _bs, _m, THEME.button_hide);
_x += _bs + ui(4);
_w -= _bs + ui(4);
2023-01-04 02:30:04 +01:00
2024-03-29 05:20:49 +01:00
}
2023-01-04 02:30:04 +01:00
draw_sprite_stretched_ext(THEME.textbox, 3, _x, _y, ww * size, _h * size, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _x, _y, ww * size, _h * size, boxColor, 0.5 + 0.5 * interactable);
2023-09-15 20:12:02 +02:00
for(var i = 0; i < size; i++)
for(var j = 0; j < size; j++) {
var ind = i * size + j;
2023-06-21 20:36:53 +02:00
tb[ind].setFocusHover(active, hover);
tb[ind].hide = true;
2023-01-04 02:30:04 +01:00
var bx = _x + ww * j;
2024-03-26 04:03:45 +01:00
var by = _y + _h * i;
2024-03-31 05:36:11 +02:00
var _dat = array_safe_get_fast(_data, ind);
2023-01-04 02:30:04 +01:00
2024-03-26 04:03:45 +01:00
tb[ind].draw(bx, by, ww, _h, _dat, _m);
2023-01-04 02:30:04 +01:00
}
2023-01-17 08:11:55 +01:00
resetFocus();
2023-01-04 02:30:04 +01:00
return h;
2023-01-04 02:30:04 +01:00
}
2024-03-31 11:10:14 +02:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new matrixGrid(type, size, onModify, unit);
return cln;
}
2023-01-04 02:30:04 +01:00
}