Pixel-Composer/scripts/node_array/node_array.gml

199 lines
5.3 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Array(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2022-01-19 06:11:17 +01:00
name = "Array";
2024-11-23 07:26:39 +01:00
setDimension(96, 32);
2022-01-13 05:24:03 +01:00
2024-05-23 10:59:39 +02:00
attributes.spread_value = false;
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Enum_Scroll("Type", self, 0, { data: [ "Any", "Surface", "Number", "Color", "Text" ], update_hover: false }))
2023-02-14 05:32:32 +01:00
.rejectArray();
2023-01-25 06:49:00 +01:00
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Bool("Spread array", self, false, "Unpack array and push the contents into the output one by one." ))
2023-04-11 20:29:20 +02:00
.rejectArray();
array_adjust_tool = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
2023-01-25 06:49:00 +01:00
var _h = ui(48);
var bw = _w / 2 - ui(4);
var bh = ui(36);
2024-05-24 07:44:36 +02:00
if(buttonTextIconInstant(true, THEME.button_hide, _x, _y + ui(8), bw, bh, _m, _focus, _hover, "", THEME.add, __txt("Add"), COLORS._main_value_positive) == 2) {
2024-08-08 06:57:51 +02:00
attributes.size = max(attributes.size, (array_length(inputs) - input_fix_len) / data_length ) + 1;
2024-05-24 07:44:36 +02:00
onInputResize();
2023-01-25 06:49:00 +01:00
}
var act = attributes.size > 0;
if(buttonTextIconInstant(act, THEME.button_hide, _x + _w - bw, _y + ui(8), bw, bh, _m, _focus, _hover, "", THEME.minus, __txt("Remove"), COLORS._main_value_negative) == 2) {
2024-05-24 07:44:36 +02:00
attributes.size--;
onInputResize();
2023-01-25 06:49:00 +01:00
}
return _h;
});
2023-01-25 06:49:00 +01:00
2024-05-18 13:46:01 +02:00
input_display_list = [ 0, 1, ["Contents", false], array_adjust_tool, ];
2023-01-25 06:49:00 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Array", self, VALUE_TYPE.any, []));
2023-01-25 06:49:00 +01:00
2024-05-23 10:59:39 +02:00
static createNewInput = function() {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
2024-05-23 10:59:39 +02:00
var _typ = getType();
2024-08-20 10:15:53 +02:00
newInput(index, nodeValue("Input", self, CONNECT_TYPE.input, _typ, -1 ))
2024-05-23 10:59:39 +02:00
.setVisible(true, true);
array_push(input_display_list, index);
2024-08-08 06:57:51 +02:00
return inputs[index];
2024-05-24 07:44:36 +02:00
}
setDynamicInput(1);
2023-04-10 20:02:59 +02:00
static getType = function() {
var _type = getInputData(0);
2023-01-25 06:49:00 +01:00
switch(_type) {
2023-11-27 11:40:28 +01:00
case 1 : return VALUE_TYPE.surface;
case 2 : return VALUE_TYPE.float;
case 3 : return VALUE_TYPE.color;
case 4 : return VALUE_TYPE.text;
2023-02-14 05:32:32 +01:00
default : return VALUE_TYPE.any;
2023-01-25 06:49:00 +01:00
}
}
2023-02-14 05:32:32 +01:00
static refreshDynamicInput = function() {
2024-08-08 06:57:51 +02:00
var _l = [];
2024-05-23 10:59:39 +02:00
var amo = attributes.size;
2023-01-25 06:49:00 +01:00
2024-08-08 06:57:51 +02:00
for( var i = 0; i < array_length(inputs); i++ ) {
var _inp = inputs[i];
2024-05-23 10:59:39 +02:00
if(i < input_fix_len + amo || _inp.hasJunctionFrom())
2024-08-08 06:57:51 +02:00
array_push(_l, _inp);
2022-01-13 05:24:03 +01:00
}
2022-01-19 06:11:17 +01:00
2024-05-23 10:59:39 +02:00
var _add = amo - getInputAmount();
2024-08-08 06:57:51 +02:00
repeat(_add) array_push(_l, createNewInput());
2023-01-25 06:49:00 +01:00
2024-05-18 13:46:01 +02:00
input_display_list = array_clone(input_display_list_raw);
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(_l); i++ ) {
_l[i].index = i;
2023-01-25 06:49:00 +01:00
array_push(input_display_list, i);
}
2022-01-19 06:11:17 +01:00
2024-08-08 06:57:51 +02:00
2022-01-19 06:11:17 +01:00
inputs = _l;
2024-04-20 05:42:23 +02:00
getJunctionList();
2024-05-23 10:59:39 +02:00
setHeight();
}
2023-01-25 06:49:00 +01:00
static updateType = function(resetVal = false) {
2023-11-27 11:40:28 +01:00
var _typ = getType();
2024-06-01 14:08:24 +02:00
if(getInputAmount() <= 0) return;
2023-12-06 05:09:39 +01:00
2024-08-08 06:57:51 +02:00
if(_typ == VALUE_TYPE.any && inputs[input_fix_len].value_from)
outputs[0].setType(inputs[input_fix_len].value_from.type);
2023-12-06 05:09:39 +01:00
else
2024-08-08 06:57:51 +02:00
outputs[0].setType(_typ);
2023-11-27 11:40:28 +01:00
2024-08-08 06:57:51 +02:00
for( var i = array_length(inputs) - 1; i >= input_fix_len; i-- ) {
if(resetVal) inputs[i].resetValue();
2023-11-27 11:40:28 +01:00
2024-08-08 06:57:51 +02:00
if(inputs[i].value_from == noone) {
inputs[i].setType(_typ);
inputs[i].resetDisplay();
2024-04-20 05:42:23 +02:00
2024-08-08 06:57:51 +02:00
} else if (value_bit(inputs[i].value_from.type) & value_bit(_typ) != 0) {
inputs[i].setType(inputs[i].value_from.type);
inputs[i].resetDisplay();
2024-04-20 05:42:23 +02:00
2023-11-27 11:40:28 +01:00
} else {
2024-08-08 06:57:51 +02:00
inputs[i].removeFrom();
2023-11-27 11:40:28 +01:00
}
}
// w = _typ == VALUE_TYPE.surface? 128 : 96;
2023-11-27 11:40:28 +01:00
refreshDynamicInput();
}
2023-11-27 11:40:28 +01:00
static onValueUpdate = function(index = 0) {
2023-11-27 11:40:28 +01:00
if(LOADING || APPENDING) return;
if(index == 0) { updateType(true); return; }
if(index == 1) return;
2023-05-07 20:55:13 +02:00
2023-02-14 05:32:32 +01:00
refreshDynamicInput();
}
2022-01-19 06:11:17 +01:00
static onValueFromUpdate = function(index) {
2023-01-09 03:14:20 +01:00
if(LOADING || APPENDING) return;
refreshDynamicInput();
2023-11-27 11:40:28 +01:00
var _typ = getType();
if(_typ != VALUE_TYPE.any) return;
2024-08-08 06:57:51 +02:00
if(index >= array_length(inputs)) return;
2023-11-27 11:40:28 +01:00
2024-08-08 06:57:51 +02:00
inputs[index].setType(inputs[index].value_from? inputs[index].value_from.type : _typ);
inputs[index].resetDisplay();
}
2023-01-09 03:14:20 +01:00
static update = function(frame = CURRENT_FRAME) {
2024-10-03 12:20:00 +02:00
var spd = getInputData(1);
2023-02-14 05:32:32 +01:00
var _typ = getType();
2023-11-27 11:40:28 +01:00
var res = [];
var ind = 0;
2022-01-19 06:11:17 +01:00
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i++ ) {
var val = getInputData(i);
2023-04-10 20:02:59 +02:00
2023-11-22 05:17:13 +01:00
if(is_array(val) && spd) array_append(res, val);
else array_push(res, val);
2024-05-23 10:59:39 +02:00
2024-08-08 06:57:51 +02:00
if(_typ == VALUE_TYPE.any && inputs[i].value_from)
outputs[0].setType(inputs[i].value_from.type);
2022-01-19 06:11:17 +01:00
}
2024-08-08 06:57:51 +02:00
outputs[0].setValue(res);
}
2022-01-19 06:11:17 +01:00
static postConnect = function() { updateType(false); }
2024-08-20 10:15:53 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
var bbox = drawGetBbox(xx, yy, _s);
2024-10-06 11:23:58 +02:00
var jh = (junction_draw_hei_y - 4) * _s;
2024-08-20 10:15:53 +02:00
2024-10-06 11:23:58 +02:00
var x0 = bbox.x0 + 6 * _s;
var ww = bbox.w - 12 * _s;
2024-08-20 10:15:53 +02:00
for(var i = input_fix_len; i < array_length(inputs); i += data_length) {
var val = inputs[i];
if(!val.isVisible()) continue;
2024-08-20 10:15:53 +02:00
2024-10-06 11:23:58 +02:00
var key = getInputData(i, "");
2024-08-20 10:15:53 +02:00
2024-10-06 11:23:58 +02:00
switch(val.type) {
case VALUE_TYPE.integer :
case VALUE_TYPE.float :
case VALUE_TYPE.boolean :
case VALUE_TYPE.text :
case VALUE_TYPE.path :
draw_set_text(f_sdf, fa_left, fa_center, value_color(val.type));
var _ss = min(_s * .4, string_scale(key, ww, 9999));
draw_text_transformed(x0, val.y, key, _ss, _ss, 0);
break;
case VALUE_TYPE.color :
if(is_array(key)) drawPalette(key, x0, val.y - jh / 2, ww, jh);
else drawColor(key, x0, val.y - jh / 2, ww, jh);
break;
case VALUE_TYPE.gradient :
if(is(key, gradientObject))
key.draw(key, x0, val.y - jh / 2, ww, jh);
break;
}
2024-08-20 10:15:53 +02:00
}
}
2022-01-13 05:24:03 +01:00
}