Pixel-Composer/scripts/vectorRangeBox/vectorRangeBox.gml

172 lines
4.8 KiB
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function vectorRangeBox(_size, _type, _onModify, _unit = noone) : widget() constructor {
2022-01-13 05:24:03 +01:00
size = _size;
2024-03-31 11:10:14 +02:00
type = _type;
2022-01-13 05:24:03 +01:00
onModify = _onModify;
2022-12-27 04:00:50 +01:00
unit = _unit;
linked = false;
ranged = false;
2022-01-13 05:24:03 +01:00
disp_h = 0;
tooltip_ranged = new tooltipSelector("Value Type", [ __txtx("widget_range_constant", "Constant"), __txtx("widget_range_random", "Random Range") ]);
2023-01-17 08:11:55 +01:00
onModifyIndex = function(val, index) {
var v = toNumber(val);
if(!linked && ranged) return onModify(v, index);
var modi = false;
if(linked && !ranged) {
modi |= onModify(v, 0);
modi |= onModify(v, 1);
modi |= onModify(v, 2);
modi |= onModify(v, 3);
} else if(linked) {
modi |= onModify(v, index);
modi |= onModify(v, (index + 2) % 4);
} else if(!ranged) {
modi |= onModify(v, floor(index / 2) * 2 + 0);
modi |= onModify(v, floor(index / 2) * 2 + 1);
2023-01-17 08:11:55 +01:00
}
return modi;
2023-01-17 08:11:55 +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
extras = -1;
for(var i = 0; i < size; i++) {
2022-01-13 05:24:03 +01:00
tb[i] = new textBox(_type, onModifySingle[i]);
tb[i].slidable = true;
2024-03-26 04:03:45 +01:00
tb[i].hide = true;
}
2022-01-13 05:24:03 +01:00
static setInteract = function(interactable = noone) {
2023-01-25 06:49:00 +01:00
self.interactable = interactable;
for( var i = 0; i < size; i++ ) tb[i].interactable = interactable;
}
2023-01-25 06:49:00 +01:00
static register = function(parent = noone) { for( var i = 0; i < size; i++ ) tb[i].register(parent); }
2023-01-17 08:11:55 +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;
}
static drawParam = function(params) {
2024-03-27 11:51:14 +01:00
setParam(params);
for(var i = 0; i < size; 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-30 19:56:53 +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-06-13 07:19:25 +02:00
_data = array_verify(_data, size);
if(struct_has(_display_data, "linked")) linked = _display_data.linked;
if(struct_has(_display_data, "ranged")) ranged = _display_data.ranged;
h = _h * 2 + ui(4);
2023-01-17 08:11:55 +01:00
var _bs = min(_h, ui(32));
2024-03-29 05:20:49 +01:00
if((_w - _bs) / 2 > ui(64)) {
2024-06-13 07:19:25 +02:00
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);
}
var bx = _x;
var by = _y + _h / 2 - _bs / 2;
var bc = linked? COLORS._main_accent : COLORS._main_icon;
2024-06-13 07:19:25 +02:00
2024-12-16 12:03:02 +01:00
if(buttonInstant(THEME.button_hide, bx, by, _bs, _bs, _m, hover, active, __txt("Link axis"), THEME.value_link, linked, bc) == 2) {
2024-03-29 05:20:49 +01:00
linked = !linked;
_display_data.linked = linked;
2024-03-29 05:20:49 +01:00
if(linked) {
onModifyIndex(_data[0], 0);
onModifyIndex(_data[1], 1);
}
}
by += _h + ui(4);
tooltip_ranged.index = ranged;
2024-12-16 12:03:02 +01:00
if(buttonInstant(THEME.button_hide, bx, by, _bs, _bs, _m, hover, active, tooltip_ranged, THEME.value_range, ranged, COLORS._main_icon) == 2) {
ranged = !ranged;
_display_data.ranged = ranged;
if(!ranged) {
onModifyIndex(_data[0], 0);
onModifyIndex(_data[1], 2);
}
}
2024-03-29 05:20:49 +01:00
_x += _bs + ui(4);
_w -= _bs + ui(4);
}
2023-01-17 08:11:55 +01:00
var ww = _w / 2;
2022-01-13 05:24:03 +01:00
for( var j = 0; j < 2; j++ ) {
var by = _y + (_h + ui(4)) * j;
draw_sprite_stretched_ext(THEME.textbox, 3, _x, by, _w, _h, boxColor, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _x, by, _w, _h, boxColor, 0.5 + 0.5 * interactable);
}
if(linked) {
2024-07-19 05:20:18 +02:00
draw_sprite_stretched_ext(THEME.ui_scrollbar, 0, _x + ww / 2 - ui(2), _y + _h / 2, ui(4), _h + ui(4), COLORS._main_accent, .2);
draw_sprite_stretched_ext(THEME.ui_scrollbar, 0, _x + ww + ww / 2 - ui(2), _y + _h / 2, ui(4), _h + ui(4), COLORS._main_accent, .2);
}
if(!ranged) {
2024-07-19 05:20:18 +02:00
draw_sprite_stretched_ext(THEME.ui_scrollbar, 0, _x + ww / 2, _y + _h / 2 - ui(2), ww, ui(4), COLORS._main_accent, .2);
draw_sprite_stretched_ext(THEME.ui_scrollbar, 0, _x + ww / 2, _y + _h + ui(4) + _h / 2 - ui(2), ww, ui(4), COLORS._main_accent, .2);
}
for( var j = 0; j < 2; j++ ) {
var by = _y + (_h + ui(4)) * j;
for( var i = 0; i < 2; i++ ) {
var bx = _x + ww * i;
if(i == 0) tb[j * 2 + i].label = axis[j];
tb[j * 2 + i].setFocusHover(active, hover);
tb[j * 2 + i].draw(bx, by, ww, _h, _data[j * 2 + i], _m);
}
2022-01-13 05:24:03 +01:00
}
2023-01-17 08:11:55 +01:00
resetFocus();
2022-11-21 06:38:44 +01:00
return h;
}
2024-03-31 11:10:14 +02:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new vectorRangeBox(size, type, onModify, unit);
cln.axis = axis;
cln.extras = extras;
return cln;
}
2024-11-24 11:39:17 +01:00
static free = function() {
for( var i = 0, n = array_length(tb); i < n; i++ ) tb[i].free();
}
2022-01-13 05:24:03 +01:00
}