Pixel-Composer/scripts/matrixGrid/matrixGrid.gml

137 lines
3.8 KiB
Plaintext
Raw Normal View History

2023-09-15 20:12:02 +02:00
function matrixGrid(_type, _size, _onModify, _unit = noone) : widget() constructor {
size = _size;
inputs = size * size;
2023-01-04 02:30:04 +01:00
onModify = _onModify;
unit = _unit;
linked = false;
b_link = button(function() { linked = !linked; });
b_link.icon = THEME.value_link;
onModifyIndex = function(index, val) {
2023-02-14 05:32:32 +01:00
var modi = false;
2023-01-04 02:30:04 +01:00
if(linked) {
2023-09-15 20:12:02 +02:00
for( var i = 0; i < inputs; i++ )
2023-02-14 05:32:32 +01:00
modi |= onModify(i, toNumber(val));
return modi;
2023-01-04 02:30:04 +01:00
}
2023-02-14 05:32:32 +01:00
return onModify(index, toNumber(val));
2023-01-04 02:30:04 +01:00
}
2023-02-14 05:32:32 +01:00
onModifySingle[0] = function(val) { return onModifyIndex(0, val); }
onModifySingle[1] = function(val) { return onModifyIndex(1, val); }
onModifySingle[2] = function(val) { return onModifyIndex(2, val); }
onModifySingle[3] = function(val) { return onModifyIndex(3, val); }
2023-09-15 20:12:02 +02:00
2023-02-14 05:32:32 +01:00
onModifySingle[4] = function(val) { return onModifyIndex(4, val); }
onModifySingle[5] = function(val) { return onModifyIndex(5, val); }
onModifySingle[6] = function(val) { return onModifyIndex(6, val); }
onModifySingle[7] = function(val) { return onModifyIndex(7, val); }
2023-09-15 20:12:02 +02:00
onModifySingle[ 8] = function(val) { return onModifyIndex( 8, val); }
onModifySingle[ 9] = function(val) { return onModifyIndex( 9, val); }
onModifySingle[10] = function(val) { return onModifyIndex(10, val); }
onModifySingle[11] = function(val) { return onModifyIndex(11, val); }
2023-01-04 02:30:04 +01:00
onModifySingle[12] = function(val) { return onModifyIndex(12, val); }
onModifySingle[13] = function(val) { return onModifyIndex(13, val); }
onModifySingle[14] = function(val) { return onModifyIndex(14, val); }
onModifySingle[15] = function(val) { return onModifyIndex(15, val); }
2023-01-04 02:30:04 +01:00
extras = -1;
for(var i = 0; i < inputs; i++) {
tb[i] = new textBox(_type, onModifySingle[i]);
tb[i].slidable = true;
}
2023-01-25 06:49:00 +01:00
static setInteract = function(interactable = noone) {
self.interactable = interactable;
b_link.interactable = interactable;
2023-09-15 20:12:02 +02:00
for( var i = 0; i < inputs; 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);
2023-09-15 20:12:02 +02:00
for( var i = 0; i < inputs; 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);
}
2023-02-14 05:32:32 +01:00
static setSlideSpeed = function(speed) {
2023-09-15 20:12:02 +02:00
for(var i = 0; i < inputs; i++)
tb[i].setSlidable(speed);
2023-02-14 05:32:32 +01:00
}
2023-07-30 19:56:53 +02:00
static drawParam = function(params) {
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;
var hh = TEXTBOX_HEIGHT + ui(8);
h = hh * size - ui(8);
2023-01-17 08:11:55 +01:00
2023-01-04 02:30:04 +01:00
if(extras && instanceof(extras) == "buttonClass") {
2023-06-21 20:36:53 +02:00
extras.setFocusHover(active, hover);
2023-01-04 02:30:04 +01:00
extras.draw(_x + _w - ui(32), _y + _h / 2 - ui(32 / 2), ui(32), ui(32), _m, THEME.button_hide);
_w -= ui(40);
}
if(unit != noone && unit.reference != noone) {
_w += ui(4);
2023-06-21 20:36:53 +02:00
unit.triggerButton.setFocusHover(iactive, ihover);
2023-01-04 02:30:04 +01:00
unit.draw(_x + _w - ui(32), _y + _h / 2 - ui(32 / 2), ui(32), ui(32), _m);
_w -= ui(40);
}
2023-06-21 20:36:53 +02:00
b_link.setFocusHover(active, hover);
2023-01-04 02:30:04 +01:00
b_link.icon_index = linked;
b_link.icon_blend = linked? COLORS._main_accent : COLORS._main_icon;
2023-06-04 18:28:29 +02:00
b_link.tooltip = linked? __txt("Unlink values") : __txt("Link values");
2023-01-04 02:30:04 +01:00
2023-09-15 20:12:02 +02:00
var th = hh * size - ui(8);
2023-01-04 02:30:04 +01:00
var bx = _x;
var by = _y + th / 2 - ui(32 / 2);
b_link.draw(bx + ui(4), by + ui(4), ui(24), ui(24), _m, THEME.button_hide);
_x += ui(28);
_w -= ui(28);
2023-09-15 20:12:02 +02:00
var ww = _w / size;
2023-01-04 02:30:04 +01:00
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);
2023-01-04 02:30:04 +01:00
var bx = _x + ww * j;
var by = _y + hh * i;
var _dat = array_safe_get(_data, ind);
2023-01-04 02:30:04 +01:00
tb[ind].draw(bx + ui(8), by, ww - ui(8), TEXTBOX_HEIGHT, _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
}
}