Pixel-Composer/scripts/vectorBox/vectorBox.gml

181 lines
4.5 KiB
Text
Raw Normal View History

2023-03-19 09:17:39 +01:00
enum DIMENSION {
width,
height
}
2023-05-03 21:42:17 +02:00
function vectorBox(_size, _onModify, _unit = noone) : widget() constructor {
2022-01-13 05:24:03 +01:00
size = _size;
onModify = _onModify;
2022-12-27 04:00:50 +01:00
unit = _unit;
linkable = true;
per_line = false;
2023-05-03 21:42:17 +02:00
current_value = [];
linked = false;
side_button = noone;
2022-01-13 05:24:03 +01:00
link_inactive_color = noone;
2024-04-08 07:13:46 +02:00
tooltip = new tooltipSelector("Axis", [ __txt("Independent"), __txt("Linked") ]);
2022-12-27 04:00:50 +01:00
onModifyIndex = function(val, index) {
2023-05-03 21:42:17 +02:00
var v = toNumber(val);
if(linked) {
2023-02-14 05:32:32 +01:00
var modi = false;
2023-05-07 20:55:13 +02:00
for( var i = 0; i < size; i++ ) {
tb[i]._input_text = v;
if(is_callable(onModify))
modi |= onModify(v, i);
2023-05-07 20:55:13 +02:00
}
2023-02-14 05:32:32 +01:00
return modi;
2022-12-27 04:00:50 +01:00
}
2023-05-07 20:55:13 +02:00
if(is_callable(onModify))
return onModify(v, index);
2023-05-07 20:55:13 +02:00
return noone;
2022-12-27 04:00:50 +01:00
}
2022-01-13 05:24:03 +01:00
axis = [ "x", "y", "z", "w" ];
onModifySingle[0] = function(val) { return onModifyIndex(val, 0); }
onModifySingle[1] = function(val) { return onModifyIndex(val, 1); }
onModifySingle[2] = function(val) { return onModifyIndex(val, 2); }
onModifySingle[3] = function(val) { return onModifyIndex(val, 3); }
2022-01-13 05:24:03 +01:00
2023-04-16 15:26:52 +02:00
for(var i = 0; i < 4; i++) {
2023-05-03 21:42:17 +02:00
tb[i] = new textBox(TEXTBOX_INPUT.number, onModifySingle[i]);
2022-01-13 05:24:03 +01:00
tb[i].slidable = true;
}
static apply = function() {
2024-03-31 11:10:14 +02:00
for( var i = 0; i < size; i++ ) {
tb[i].apply();
current_value[i] = toNumber(tb[i]._input_text);
}
}
2024-03-31 11:10:14 +02:00
static setLinkInactiveColor = function(color) {
link_inactive_color = color;
return self;
}
static setInteract = function(interactable) {
2023-01-25 06:49:00 +01:00
self.interactable = interactable;
2024-03-28 14:18:02 +01:00
if(side_button)
side_button.interactable = interactable;
2023-01-25 06:49:00 +01:00
for( var i = 0; i < size; i++ )
tb[i].interactable = interactable;
}
2023-01-25 06:49:00 +01:00
static register = function(parent = noone) {
2023-01-17 08:11:55 +01:00
for( var i = 0; i < size; i++ )
tb[i].register(parent);
if(side_button != noone)
side_button.register(parent);
2023-01-17 08:11:55 +01:00
if(unit != noone && unit.reference != noone)
unit.triggerButton.register(parent);
}
2023-01-17 08:11:55 +01:00
static isHovering = function() {
2024-03-28 14:18:02 +01:00
for( var i = 0, n = array_length(tb); i < n; i++ ) if(tb[i].isHovering()) return true;
return false;
}
2024-03-28 14:18:02 +01:00
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
for(var i = 0; i < 4; i++) tb[i].setParam(params);
2024-03-26 04:03:45 +01:00
return draw(params.x, params.y, params.w, params.h, params.data, params.display_data, params.m);
}
2023-07-21 12:40:20 +02:00
static draw = function(_x, _y, _w, _h, _data, _display_data, _m) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
2024-03-27 14:18:39 +01:00
h = per_line? (_h + ui(4)) * size - ui(4) : _h;
2023-01-17 08:11:55 +01:00
if(struct_has(_display_data, "linked")) linked = _display_data.linked;
if(struct_has(_display_data, "side_button")) side_button = _display_data.side_button;
tooltip.index = linked;
2024-04-27 05:10:20 +02:00
if(!is_array(_data)) return _h;
if(array_empty(_data)) return _h;
if(is_array(_data[0])) return _h;
2023-08-24 11:59:05 +02:00
2023-05-03 21:42:17 +02:00
current_value = _data;
2024-03-29 05:20:49 +01:00
var sz = min(size, array_length(_data));
var _bs = min(_h, ui(32));
2024-03-29 05:20:49 +01:00
if((_w - _bs) / sz > ui(48)) {
if(side_button) {
side_button.setFocusHover(active, hover);
side_button.draw(_x + _w - _bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m, THEME.button_hide);
_w -= _bs + ui(4);
}
2024-03-29 05:20:49 +01:00
if(unit != noone && unit.reference != noone) {
unit.triggerButton.setFocusHover(iactive, ihover);
unit.draw(_x + _w - _bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m);
_w -= _bs + ui(4);
}
if(linkable) {
var _icon_blend = linked? COLORS._main_accent : (link_inactive_color == noone? COLORS._main_icon : link_inactive_color);
var bx = _x;
var by = _y + _h / 2 - _bs / 2;
if(buttonInstant(THEME.button_hide, bx, by, _bs, _bs, _m, active, hover, tooltip, THEME.value_link, linked, _icon_blend) == 2) {
linked = !linked;
_display_data.linked = linked;
2024-03-29 05:20:49 +01:00
if(linked) {
onModify(_data[0], 0);
onModify(_data[0], 1);
2024-03-29 05:20:49 +01:00
}
}
2024-03-29 05:20:49 +01:00
_x += _bs + ui(4);
_w -= _bs + ui(4);
}
}
2022-12-27 04:00:50 +01:00
var ww = per_line? _w : _w / sz;
2024-03-27 14:18:39 +01:00
if(!per_line) {
draw_sprite_stretched_ext(THEME.textbox, 3, _x, _y, _w, _h, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _x, _y, _w, _h, boxColor, 0.5 + 0.5 * interactable);
2024-03-27 14:18:39 +01:00
}
2023-01-25 06:49:00 +01:00
for(var i = 0; i < sz; i++) {
var bx = per_line? _x : _x + ww * i;
2024-03-27 14:18:39 +01:00
var by = per_line? _y + (_h + ui(4)) * i : _y;
2022-01-13 05:24:03 +01:00
tb[i].setFocusHover(active, hover);
2024-07-03 05:32:12 +02:00
tb[i].labelColor = sep_axis? COLORS.axis[i] : COLORS._main_text_sub;
tb[i].hide = !per_line;
tb[i].label = axis[i];
tb[i].draw(bx, by, ww, _h, _data[i], _m);
2022-01-13 05:24:03 +01:00
}
2023-01-17 08:11:55 +01:00
resetFocus();
2023-07-21 12:40:20 +02:00
return h;
}
2023-05-03 21:42:17 +02:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new vectorBox(size, onModify, unit);
cln.linkable = linkable;
cln.per_line = per_line;
return cln;
}
2022-01-13 05:24:03 +01:00
}