Pixel-Composer/scripts/node_array_rearrange/node_array_rearrange.gml

180 lines
4.6 KiB
Plaintext
Raw Normal View History

function Node_Array_Rearrange(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Array Rearrange";
2024-05-02 14:36:59 +02:00
draw_pad_w = 10;
setDimension(96, 48);
2024-08-20 10:15:53 +02:00
newInput(0, nodeValue("Array", self, CONNECT_TYPE.input, VALUE_TYPE.any, 0))
.setArrayDepth(1)
.setVisible(true, true);
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Int("Orders", self, []))
.setArrayDepth(1);
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Array", self, VALUE_TYPE.any, 0))
.setArrayDepth(1);
type = VALUE_TYPE.any;
ordering = noone;
order_i = noone;
order_y = 0;
2024-11-04 06:08:06 +01:00
rearranger = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
2024-08-08 06:57:51 +02:00
var _arr = inputs[0].getValue();
var _ord = inputs[1].getValue();
2024-11-04 06:08:06 +01:00
if(!is_array(_arr)) return 0;
if(!is_array(_ord) || array_length(_ord) != array_length(_arr)) return 0;
var amo = array_length(_arr);
var _fx = _x;
var _fy = _y + ui(8);
2024-08-08 06:57:51 +02:00
var _fh = inputs[0].type == VALUE_TYPE.surface? ui(48) : ui(32);
var _fsh = _fh - ui(8);
var _h = amo * (_fh + ui(4));
var _hov = 0;
for( var i = 0; i < amo; i++ ) {
var _ind = _ord[i];
var _val = _arr[_ind];
_fx = _x;
if(order_i == _ind) _fx += ui(16) * order_y;
var _ffx = _fx + ui(32 + 4);
var _ffy = _fy + ui(4);
draw_sprite_stretched_ext(THEME.ui_panel, 0, _fx, _fy, _w, _fh, CDEF.main_dkblack, 1);
var hv = ordering == noone && _hover && point_in_rectangle(_m[0], _m[1], _fx, _fy, _fx + ui(32), _fy + _fh);
var cc = hv? COLORS._main_icon : COLORS.node_composite_bg;
draw_sprite_ext(THEME.hamburger_s, 0, _fx + ui(16), _fy + _fh / 2, 1, 1, 0, cc, 1);
if(_m[1] > _ffy) _hov = i;
var _ffcx = _fx + _w / 2;
2024-08-08 06:57:51 +02:00
switch(inputs[0].type) {
case VALUE_TYPE.surface :
var _sw = surface_get_width_safe(_val);
var _sh = surface_get_height_safe(_val);
var _ss = min( _fsh / _sw, _fsh / _sh );
_sw *= _ss;
_sh *= _ss;
var _sx = _ffcx - _sw / 2;
var _sy = _ffy + _fsh / 2 - _sh / 2;
draw_sprite_stretched_ext(THEME.ui_panel, 0, _ffcx - _fsh / 2, _ffy, _fsh, _fsh, merge_color(COLORS._main_icon_dark, COLORS.node_composite_bg, 0.25), 1);
draw_surface_ext_safe(_val, _sx, _sy, _ss, _ss);
draw_set_color(COLORS.node_composite_bg);
break;
2024-11-04 06:08:06 +01:00
case VALUE_TYPE.color :
if(is_numeric(_val)) {
drawColor(_val, _fx + ui(40), _ffy + ui(4), _w - ui(40 + 8), _fsh - ui(8));
break;
}
default :
draw_set_text(f_p2, fa_center, fa_center, COLORS._main_text);
draw_text(_ffcx, _ffy + _fsh / 2, string(_val));
break;
}
if(hv && mouse_press(mb_left, _focus)) {
ordering = _ind;
order_i = _ind;
}
_fy += _fh + ui(4);
}
if(ordering != noone) {
order_y = lerp_float(order_y, 1, 5);
array_remove(_ord, ordering);
array_insert(_ord, _hov, ordering);
2024-08-08 06:57:51 +02:00
inputs[1].setValue(_ord);
2024-11-04 06:08:06 +01:00
triggerRender();
if(mouse_release(mb_left)) {
ordering = noone;
order_i = noone;
}
} else
order_y = lerp_float(order_y, 0, 5);
return _h;
2024-11-04 06:08:06 +01:00
});
input_display_list = [ 0, ["Rearranger", false], rearranger ];
2024-11-04 06:08:06 +01:00
static onValueFromUpdate = function(index = 0) {
if(LOADING || APPENDING) return;
2024-08-08 06:57:51 +02:00
var _arr = inputs[0].getValue();
2024-11-04 06:08:06 +01:00
var _val = array_create_ext(array_length(_arr), function(i) /*=>*/ {return i});
2024-08-08 06:57:51 +02:00
inputs[1].setValue(_val);
2024-11-04 06:08:06 +01:00
}
2024-11-04 06:08:06 +01:00
static step = function() {
var _typ = VALUE_TYPE.any;
2024-08-08 06:57:51 +02:00
if(inputs[0].value_from != noone) _typ = inputs[0].value_from.type;
2024-08-08 06:57:51 +02:00
inputs[0].setType(_typ);
outputs[0].setType(_typ);
if(type != _typ) {
2024-03-28 14:18:02 +01:00
if(_typ == VALUE_TYPE.surface)
setDimension(128, 128);
else
setDimension(96, 32 + 24);
type = _typ;
will_setHeight = true;
}
2024-11-04 06:08:06 +01:00
}
2024-11-04 06:08:06 +01:00
static update = function(frame = CURRENT_FRAME) {
var _arr = getInputData(0);
var _ord = getInputData(1);
if(!is_array(_arr)) return;
2024-11-04 06:08:06 +01:00
if(!is_array(_ord) || array_length(_ord) != array_length(_arr)) {
_ord = array_create_ext(array_length(_arr), function(i) /*=>*/ {return i});
inputs[1].setValue(_ord);
}
var res = [];
for( var i = 0; i < array_length(_arr); i++ ) {
2024-03-31 05:36:11 +02:00
var _ind = array_safe_get_fast(_ord, i, i);
res[i] = array_safe_get_fast(_arr, _ind);
}
2024-08-08 06:57:51 +02:00
outputs[0].setValue(res);
2024-11-04 06:08:06 +01:00
}
2024-05-02 14:36:59 +02:00
2024-11-04 06:08:06 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2024-05-02 14:36:59 +02:00
var bbox = drawGetBbox(xx, yy, _s);
2024-11-04 06:08:06 +01:00
var val = outputs[0].getValue();
switch(inputs[0].type) {
case VALUE_TYPE.surface :
break;
case VALUE_TYPE.color :
drawPalette(val, bbox.x0, bbox.y0, bbox.w, bbox.h);
break;
default :
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
draw_text_bbox(bbox, val);
}
}
2024-05-02 14:36:59 +02:00
}