Pixel-Composer/scripts/matrixGrid/matrixGrid.gml

158 lines
4.4 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;
2023-09-15 20:12:02 +02:00
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
}
2024-03-28 14:18:02 +01:00
static isHovering = function() {
for( var i = 0, n = array_length(tb); i < n; i++ ) if(tb[i].isHovering()) return true;
return false;
}
2023-07-30 19:56:53 +02:00
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
2024-03-26 04:03:45 +01:00
for(var i = 0; i < inputs; 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
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
2023-09-15 20:12:02 +02:00
var ww = _w / size;
2023-01-04 02:30:04 +01:00
2024-03-26 04:03:45 +01:00
draw_sprite_stretched_ext(THEME.textbox, 3, _x, _y, ww * size, _h * size, c_white, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _x, _y, ww * size, _h * size, c_white, 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() { #region
var cln = new matrixGrid(type, size, onModify, unit);
return cln;
} #endregion
2023-01-04 02:30:04 +01:00
}