Pixel-Composer/scripts/node_switch/node_switch.gml

210 lines
6.2 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Switch(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-01-04 02:30:04 +01:00
name = "Switch";
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-01-17 08:11:55 +01:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Text("Index", self, "" ))
2023-02-14 05:32:32 +01:00
.setVisible(true, true)
.rejectArray();
2023-01-04 02:30:04 +01:00
2024-08-20 10:15:53 +02:00
newInput(1, nodeValue("Default value", self, CONNECT_TYPE.input, VALUE_TYPE.any, 0 ))
2023-01-04 02:30:04 +01:00
.setVisible(false, true);
2024-05-24 07:44:36 +02:00
size_adjust_tool = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) { #region
var _h = ui(48);
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
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();
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
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-23 10:59:39 +02:00
2024-05-24 07:44:36 +02:00
return _h;
}); #endregion
2023-01-04 02:30:04 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Result", self, VALUE_TYPE.any, 0));
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
input_display_list = [ 0, 1,
["Cases", false], size_adjust_tool
]
#region //////////////////////////////// Dynamic IO ////////////////////////////////
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
static createNewInput = function(list = inputs) {
var index = array_length(list);
2024-05-24 07:44:36 +02:00
var bDel = button(function() { node.deleteInput(index); })
.setIcon(THEME.minus_16, 0, COLORS._main_icon);
list[index + 0] = nodeValue_Text("Case", self, "" )
2024-05-24 07:44:36 +02:00
.setDisplay(VALUE_DISPLAY.text_box, { side_button : bDel })
.setAnimable(false);
bDel.setContext(list[index + 0]);
2024-05-24 07:44:36 +02:00
2024-08-20 10:15:53 +02:00
list[index + 1] = nodeValue("value", self, CONNECT_TYPE.input, VALUE_TYPE.any, 0 )
2024-05-24 07:44:36 +02:00
.setVisible(false, false);
return list[index + 0];
2024-05-24 07:44:36 +02:00
}
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
setDynamicInput(2, false);
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
static addInput = function() {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
2024-05-24 07:44:36 +02:00
attributes.size++;
createNewInput();
if(!UNDO_HOLDING) {
var _inputs = array_create(data_length);
for(var i = 0; i < data_length; i++)
2024-08-08 06:57:51 +02:00
_inputs[i] = inputs[index + i];
2023-01-04 02:30:04 +01:00
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 });
2023-01-04 02:30:04 +01:00
}
2024-05-24 07:44:36 +02:00
onInputResize();
2023-01-04 02:30:04 +01:00
}
2024-05-24 07:44:36 +02:00
static deleteInput = function(index) {
if(!UNDO_HOLDING) {
var _inputs = array_create(data_length);
for(var i = 0; i < data_length; i++)
2024-08-08 06:57:51 +02:00
_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 });
}
attributes.size--;
for(var i = data_length - 1; i >= 0; i--)
2024-08-08 06:57:51 +02:00
array_delete(inputs, index + i, 1);
2024-05-24 07:44:36 +02:00
onInputResize();
}
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
static insertInput = function(index, _inputs) {
attributes.size++;
for(var i = 0; i < data_length; i++)
2024-08-08 06:57:51 +02:00
array_insert(inputs, index + i, _inputs[i]);
2024-05-24 07:44:36 +02:00
onInputResize();
}
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
static refreshDynamicInput = function() {
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(inputs); i++ ) {
inputs[i].index = i;
2024-05-24 07:44:36 +02:00
array_push(input_display_list, i);
}
getJunctionList();
}
2023-01-04 02:30:04 +01:00
2024-05-24 07:44:36 +02:00
#endregion //////////////////////////////// Dynamic IO ////////////////////////////////
2023-10-27 13:55:31 +02:00
static onValueFromUpdate = function(index) { #region
2023-01-09 03:14:20 +01:00
if(LOADING || APPENDING) return;
2024-05-23 14:40:30 +02:00
if(index < 0) return;
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
inputs[1].setType(inputs[1].value_from? inputs[1].value_from.type : VALUE_TYPE.any);
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
inputs[i + 1].setType(VALUE_TYPE.any);
if(inputs[i + 1].value_from != noone)
inputs[i + 1].setType(inputs[i + 1].value_from.type);
2023-01-09 03:14:20 +01:00
}
2023-10-27 13:55:31 +02:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-10-27 13:55:31 +02:00
static onValueUpdate = function(index = 0) { #region
2023-01-09 03:14:20 +01:00
if(index < input_fix_len) return;
if(LOADING || APPENDING) return;
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-01-09 03:14:20 +01:00
refreshDynamicInput();
2023-10-27 13:55:31 +02:00
} #endregion
static step = function() { #region
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];
if(_inp.value_from == noone) continue;
2023-10-27 13:55:31 +02:00
_inp.setType(_inp.value_from.type);
}
} #endregion
2023-01-09 03:14:20 +01:00
2023-10-27 13:55:31 +02:00
static update = function(frame = CURRENT_FRAME) { #region
var sele = getInputData(0);
var _res = getInputData(1);
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
outputs[0].setType(inputs[1].value_from? inputs[1].value_from.type : VALUE_TYPE.any);
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
var _cas = getInputData(i + 0);
var _val = getInputData(i + 1);
2023-01-04 02:30:04 +01:00
if(sele == _cas) {
_res = _val;
2024-08-08 06:57:51 +02:00
var _typ = inputs[i + 1].value_from? inputs[i + 1].value_from.type : inputs[i + 1].type;
outputs[0].setType(_typ);
2023-01-04 02:30:04 +01:00
}
}
2024-08-08 06:57:51 +02:00
outputs[0].setValue(_res);
2023-10-27 13:55:31 +02:00
} #endregion
2023-01-04 02:30:04 +01:00
2023-10-27 13:55:31 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2024-08-08 06:57:51 +02:00
var frm = inputs[1];
var sele = getInputData(0);
var _res = getInputData(1);
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
var _cas = getInputData(i + 0);
2024-08-08 06:57:51 +02:00
if(sele == _cas) frm = inputs[i + 1];
2023-01-04 02:30:04 +01:00
}
2024-08-08 06:57:51 +02:00
var to = outputs[0];
2023-01-04 02:30:04 +01:00
var c0 = value_color(frm.type);
draw_set_color(c0);
draw_set_alpha(0.5);
draw_line_width(frm.x, frm.y, to.x, to.y, _s * 4);
draw_set_alpha(1);
draw_set_text(f_sdf, fa_left, fa_center);
var bbox = drawGetBbox(xx, yy, _s);
2024-08-08 06:57:51 +02:00
if(inputs[1].visible) {
var str = string("default");
var ss = min(_s * 0.4, string_scale(str, bbox.w - 16 * _s, 999));
2024-08-08 06:57:51 +02:00
draw_set_color(value_color(inputs[1].type));
draw_text_transformed(bbox.x0 + 8 * _s, inputs[1].y, str, ss, ss, 0);
}
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
if(!inputs[i + 1].visible) continue;
2024-05-24 07:44:36 +02:00
var str = string(getInputData(i, ""));
if(str == "") continue;
var ss = min(_s * 0.4, string_scale(str, bbox.w - 16 * _s, 999));
2024-08-08 06:57:51 +02:00
draw_set_color(value_color(inputs[i + 1].type));
draw_text_transformed(bbox.x0 + 8 * _s, inputs[i + 1].y, str, ss, ss, 0);
}
2023-10-27 13:55:31 +02:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-10-27 13:55:31 +02:00
static doApplyDeserialize = function() { refreshDynamicInput(); }
2023-01-04 02:30:04 +01:00
}