Pixel-Composer/scripts/node_struct/node_struct.gml

161 lines
4.4 KiB
Text
Raw Normal View History

2023-03-11 01:40:17 +01:00
function Node_Struct(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Struct";
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-03-11 01:40:17 +01:00
2024-08-20 10:15:53 +02:00
size_adjust_tool = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
2024-05-24 07:44:36 +02:00
var _h = ui(48);
var bw = _w / 2 - ui(4);
var bh = ui(36);
if(buttonTextIconInstant(true, THEME.button_hide, _x, _y + ui(8), bw, bh, _m, _focus, _hover, "", THEME.add, __txt("Add"), COLORS._main_value_positive) == 2)
addInput();
var amo = attributes.size;
if(buttonTextIconInstant(attributes.size > 0, THEME.button_hide, _x + _w - bw, _y + ui(8), bw, bh, _m, _focus, _hover, "", THEME.minus, __txt("Remove"), COLORS._main_value_negative) == 2)
2024-08-08 06:57:51 +02:00
deleteInput(array_length(inputs) - data_length);
2024-05-24 07:44:36 +02:00
return _h;
2024-08-20 10:15:53 +02:00
});
2024-05-24 07:44:36 +02:00
input_display_list = [ size_adjust_tool, ];
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Struct", self, VALUE_TYPE.struct, {}));
2023-03-11 01:40:17 +01:00
static createNewInput = function(list = inputs) {
var index = array_length(list);
var bDel = button(function() { node.deleteInput(index); })
.setIcon(THEME.minus_16, 0, COLORS._main_icon);
2023-03-11 01:40:17 +01:00
list[index + 0] = nodeValue_Text("Key", self, "" )
.setDisplay(VALUE_DISPLAY.text_box, { side_button : bDel })
.setAnimable(false);
bDel.setContext(list[index + 0]);
2023-03-11 01:40:17 +01:00
list[index + 1] = nodeValue("value", self, CONNECT_TYPE.input, VALUE_TYPE.any, 0 )
.setVisible(false, false);
2024-05-24 07:44:36 +02:00
return list[index + 0];
}
setDynamicInput(2, false);
static addInput = function() {
var index = array_length(inputs);
2023-03-11 01:40:17 +01:00
attributes.size++;
createNewInput();
2023-03-11 01:40:17 +01:00
if(!UNDO_HOLDING) {
var _inputs = array_create(data_length);
2024-05-24 07:44:36 +02:00
for(var i = 0; i < data_length; i++)
_inputs[i] = inputs[index + i];
2024-05-24 07:44:36 +02:00
recordAction(ACTION_TYPE.custom, function(data, undo) {
if(undo) deleteInput(data.index);
else insertInput(data.index, data.inputs);
}, { index, inputs : _inputs });
2024-05-24 07:44:36 +02:00
}
2023-03-11 01:40:17 +01:00
onInputResize();
}
static deleteInput = function(index) {
if(!UNDO_HOLDING) {
var _inputs = array_create(data_length);
for(var i = 0; i < data_length; i++)
_inputs[i] = inputs[index + i];
2024-05-24 07:44:36 +02:00
recordAction(ACTION_TYPE.custom, function(data, undo) {
if(undo) insertInput(data.index, data.inputs);
else deleteInput(data.index);
}, { index, inputs : _inputs });
2024-05-24 07:44:36 +02:00
}
attributes.size--;
for(var i = data_length - 1; i >= 0; i--)
array_delete(inputs, index + i, 1);
onInputResize();
}
2023-03-11 01:40:17 +01:00
static insertInput = function(index, _inputs) {
attributes.size++;
for(var i = 0; i < data_length; i++)
array_insert(inputs, index + i, _inputs[i]);
2024-05-24 07:44:36 +02:00
onInputResize();
}
static refreshDynamicInput = function() {
input_display_list = array_clone(input_display_list_raw);
for( var i = 0; i < array_length(inputs); i++ ) {
inputs[i].index = i;
array_push(input_display_list, i);
}
getJunctionList();
}
2023-03-11 01:40:17 +01:00
static onValueUpdate = function(index = 0) {
if(LOADING || APPENDING) return;
2023-03-21 03:01:53 +01:00
if(index < 0) return;
2024-05-23 14:40:30 +02:00
2024-05-24 07:44:36 +02:00
if(safe_mod(index - input_fix_len, data_length) == 0) {
2024-08-08 06:57:51 +02:00
inputs[index + 1].setVisible(false, true);
inputs[index + 1].name = $"{getInputData(index)} value";
2024-05-24 07:44:36 +02:00
}
2023-03-11 01:40:17 +01:00
}
2023-09-26 14:35:25 +02:00
static step = function() {
2024-08-08 06:57:51 +02:00
for(var i = input_fix_len; i < array_length(inputs); i += data_length) {
var inp = inputs[i + 1];
var typ = inp.value_from == noone? VALUE_TYPE.any : inp.value_from.type;
2023-10-07 16:23:40 +02:00
inp.setType(typ);
2023-03-11 01:40:17 +01:00
}
}
2023-09-26 14:35:25 +02:00
static update = function() {
2023-03-11 01:40:17 +01:00
var str = {};
2024-08-08 06:57:51 +02:00
for(var i = input_fix_len; i < array_length(inputs); i += data_length) {
var key = getInputData(i + 0);
var val = getInputData(i + 1);
2024-08-08 06:57:51 +02:00
var frm = inputs[i + 1].value_from;
2023-03-11 01:40:17 +01:00
2024-05-23 14:40:30 +02:00
if(key == "") continue;
2023-06-13 14:42:06 +02:00
if(frm != noone && frm.type == VALUE_TYPE.surface)
2023-03-21 03:01:53 +01:00
str[$ key] = new Surface(val);
else if(frm != noone && frm.type == VALUE_TYPE.buffer)
str[$ key] = new Buffer(val);
2023-03-21 03:01:53 +01:00
else
str[$ key] = val;
2023-03-11 01:40:17 +01:00
}
2024-08-08 06:57:51 +02:00
outputs[0].setValue(str);
2023-03-11 01:40:17 +01:00
}
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
var bbox = drawGetBbox(xx, yy, _s);
draw_set_text(f_sdf, fa_left, fa_center, COLORS._main_text);
2023-03-11 01:40:17 +01:00
2024-08-08 06:57:51 +02:00
for(var i = input_fix_len; i < array_length(inputs); i += data_length) {
2024-08-20 10:15:53 +02:00
var key = getInputData(i, "");
2024-08-08 06:57:51 +02:00
var val = inputs[i + 1];
2024-05-24 07:44:36 +02:00
if(!val.visible) continue;
var _ss = min(_s * .4, string_scale(key, bbox.w - 12 * _s, 9999));
2023-03-11 01:40:17 +01:00
draw_set_color(value_color(val.type));
draw_text_transformed(bbox.x0 + 6 * _s, val.y, key, _ss, _ss, 0);
2023-03-11 01:40:17 +01:00
}
}
static doApplyDeserialize = function() {
refreshDynamicInput();
}
}