Pixel-Composer/scripts/node_array/node_array.gml

193 lines
5.4 KiB
Plaintext
Raw Normal View History

2022-12-13 09:20:36 +01:00
function Node_Array(_x, _y, _group = -1) : Node(_x, _y, _group) constructor {
2022-01-19 06:11:17 +01:00
name = "Array";
2022-01-13 05:24:03 +01:00
previewable = false;
w = 96;
2023-01-17 08:11:55 +01:00
2023-01-25 06:49:00 +01:00
inputs[| 0] = nodeValue( 0, "Type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0 )
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "Any", "Surface", "Number", "Text" ]);
array_adjust_tool = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
var _h = ui(48);
draw_set_color(COLORS._main_icon);
draw_set_alpha(0.75);
//draw_line_width(_x + ui(8), _y + _h, _x + _w - ui(16), _y + _h, 2);
draw_set_alpha(1);
var bw = _w / 2 - ui(4);
var bh = ui(36);
var bx = _x;
var by = _y + ui(8);
if(buttonInstant(THEME.button_hide, bx, by, bw, bh, _m, _focus, _hover) == 2) {
var amo = ds_list_size(inputs) - input_fix_len;
attributes[? "size"] = amo + 1;
refreshDynamicInput();
update();
}
draw_set_text(f_p1, fa_left, fa_center, COLORS._main_icon_light);
var bxc = bx + bw / 2 - (string_width("Add") + ui(64)) / 2;
var byc = by + bh / 2;
draw_sprite_ui(THEME.add, 0, bxc + ui(24), byc,,,, COLORS._main_icon_light);
draw_text(bxc + ui(48), byc, "Add");
var bx = _x + bw + ui(8);
var amo = attributes[? "size"];
if(amo > 1 && buttonInstant(THEME.button_hide, bx, by, bw, bh, _m, _focus, _hover) == 2) {
var amo = ds_list_size(inputs) - input_fix_len;
attributes[? "size"] = max(amo - 1, 1);
refreshDynamicInput();
update();
}
draw_set_text(f_p1, fa_left, fa_center, COLORS._main_icon_light);
var bxc = bx + bw / 2 - (string_width("Remove") + ui(64)) / 2;
var byc = by + bh / 2;
draw_sprite_ui(THEME.minus, 0, bxc + ui(24), byc,,,, COLORS._main_icon_light, (amo > 1) * 0.5 + 0.5);
draw_set_alpha((amo > 1) * 0.5 + 0.5);
draw_text(bxc + ui(48), byc, "Remove");
draw_set_alpha(1);
return _h;
});
input_display_list = [ 0, array_adjust_tool ];
input_fix_len = ds_list_size(inputs);
input_display_list_len = array_length(input_display_list);
data_length = 1;
outputs[| 0] = nodeValue(0, "Array", self, JUNCTION_CONNECT.output, VALUE_TYPE.any, []);
attributes[? "size"] = 1;
2022-01-13 05:24:03 +01:00
2022-01-19 06:11:17 +01:00
static createNewInput = function() {
var index = ds_list_size(inputs);
2023-01-25 06:49:00 +01:00
var _type = inputs[| 0].getValue();
var _typ = VALUE_TYPE.any;
switch(_type) {
case 1 : _typ = VALUE_TYPE.surface; break;
case 2 : _typ = VALUE_TYPE.float; break;
case 3 : _typ = VALUE_TYPE.text; break;
}
inputs[| index] = nodeValue( index, "Input", self, JUNCTION_CONNECT.input, _typ, -1 )
2022-01-19 06:11:17 +01:00
.setVisible(true, true);
2023-01-25 06:49:00 +01:00
array_push(input_display_list, index);
return inputs[| index];
2022-01-13 05:24:03 +01:00
}
2022-12-21 02:30:23 +01:00
if(!LOADING && !APPENDING) createNewInput();
2022-01-13 05:24:03 +01:00
2023-01-09 03:14:20 +01:00
static refreshDynamicInput = function() {
2022-01-19 06:11:17 +01:00
var _l = ds_list_create();
2023-01-25 06:49:00 +01:00
var amo = attributes[? "size"];
var extra = true;
var lastNode = noone;
2022-01-19 06:11:17 +01:00
for( var i = 0; i < ds_list_size(inputs); i++ ) {
2023-01-25 06:49:00 +01:00
if(i < input_fix_len || i < amo || inputs[| i].value_from)
2022-01-19 06:11:17 +01:00
ds_list_add(_l, inputs[| i]);
2022-12-21 02:30:23 +01:00
else
2022-01-19 06:11:17 +01:00
delete inputs[| i];
2022-01-13 05:24:03 +01:00
}
2022-01-19 06:11:17 +01:00
2023-01-25 06:49:00 +01:00
var _add = amo - (ds_list_size(_l) - input_fix_len);
repeat(_add) {
lastNode = createNewInput();
ds_list_add(_l, lastNode);
}
input_display_list = [];
for( var i = 0; i < ds_list_size(_l); i++ ) {
2022-12-21 02:30:23 +01:00
_l[| i].index = i;
2023-01-25 06:49:00 +01:00
array_push(input_display_list, i);
if(i >= input_fix_len && _l[| i].value_from == noone)
extra = false;
}
array_insert(input_display_list, 1, array_adjust_tool);
2022-01-19 06:11:17 +01:00
ds_list_destroy(inputs);
inputs = _l;
2023-01-25 06:49:00 +01:00
if(extra)
lastNode = createNewInput();
}
static onValueUpdate = function(index) {
if(index != 0) return;
var _type = inputs[| 0].getValue();
var _typ = VALUE_TYPE.any;
switch(_type) {
case 1 : _typ = VALUE_TYPE.surface; break;
case 2 : _typ = VALUE_TYPE.float; break;
case 3 : _typ = VALUE_TYPE.text; break;
}
for( var i = input_fix_len; i < ds_list_size(inputs); i++ ) {
if(_type) inputs[| i].type = _typ;
inputs[| i].resetDisplay();
if(_typ && inputs[| i].value_from && (value_bit(inputs[| i].value_from.type) & value_bit(_typ) == 0))
inputs[| i].removeFrom();
}
2022-01-19 06:11:17 +01:00
}
2023-01-09 03:14:20 +01:00
static onValueFromUpdate = function(index) {
if(LOADING || APPENDING) return;
refreshDynamicInput();
}
2022-01-19 06:11:17 +01:00
static update = function() {
2023-01-25 06:49:00 +01:00
var _type = inputs[| 0].getValue();
var _typ = VALUE_TYPE.any;
switch(_type) {
case 1 : _typ = VALUE_TYPE.surface; break;
case 2 : _typ = VALUE_TYPE.float; break;
case 3 : _typ = VALUE_TYPE.text; break;
}
outputs[| 0].type = _typ;
var res = array_create(ds_list_size(inputs) - input_fix_len - 1);
var ind = 0;
2022-01-19 06:11:17 +01:00
2023-01-25 06:49:00 +01:00
for( var i = input_fix_len; i < ds_list_size(inputs) - 1; i++ ) {
res[ind++] = inputs[| i].getValue();
2022-12-27 04:00:50 +01:00
inputs[| i].type = inputs[| i].value_from? inputs[| i].value_from.type : VALUE_TYPE.any;
2023-01-25 06:49:00 +01:00
if(i == 0 && _type == 0)
2022-12-27 04:00:50 +01:00
outputs[| 0].type = inputs[| i].value_from? inputs[| i].value_from.type : VALUE_TYPE.any;
2022-01-19 06:11:17 +01:00
}
2022-01-13 05:24:03 +01:00
outputs[| 0].setValue(res);
}
2022-01-19 06:11:17 +01:00
static postDeserialize = function() {
var _inputs = load_map[? "inputs"];
2023-01-09 03:14:20 +01:00
for(var i = input_fix_len; i < ds_list_size(_inputs); i += data_length)
2022-01-19 06:11:17 +01:00
createNewInput();
}
2023-01-25 06:49:00 +01:00
static attributeSerialize = function() {
var att = ds_map_create();
ds_map_override(att, attributes);
return att;
}
static attributeDeserialize = function(attr) {
ds_map_override(attributes, attr);
var amo = ds_map_find_value(attributes, "size");
//var _add = amo - (ds_list_size(inputs) - input_fix_len);
//repeat(_add)
// createNewInput();
}
2022-01-13 05:24:03 +01:00
}