Pixel-Composer/scripts/node_struct_get/node_struct_get.gml

80 lines
2.1 KiB
Plaintext
Raw Normal View History

2023-03-11 01:40:17 +01:00
function Node_Struct_Get(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Struct Get";
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-03-11 01:40:17 +01:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Struct("Struct", self, {}))
2023-03-11 01:40:17 +01:00
.setVisible(true, true);
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Text("Key", self, ""));
2023-03-11 01:40:17 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Value", self, VALUE_TYPE.struct, {}));
2023-03-11 01:40:17 +01:00
static getStructValue = function(str, keys) {
2023-10-27 15:42:17 +02:00
var _pnt = str, val = 0;
if(!is_struct(_pnt)) return [ VALUE_TYPE.any, val ];
2023-03-11 01:40:17 +01:00
for( var j = 0; j < array_length(keys); j++ ) {
var k = keys[j];
2023-10-27 15:42:17 +02:00
if(!variable_struct_exists(_pnt, k))
return [ VALUE_TYPE.float, 0 ];
2023-03-11 01:40:17 +01:00
2023-10-27 15:42:17 +02:00
val = variable_struct_get(_pnt, k);
2023-03-11 01:40:17 +01:00
if(j == array_length(keys) - 1) {
2023-03-21 03:01:53 +01:00
if(is_struct(val)) {
2023-10-27 15:42:17 +02:00
if(is_instanceof(val, Surface))
return [ VALUE_TYPE.surface, val.get() ];
else if(is_instanceof(val, Buffer))
return [ VALUE_TYPE.buffer, val.buffer ];
else
return [ VALUE_TYPE.struct, val ];
2023-03-21 03:01:53 +01:00
} else if(is_array(val) && array_length(val))
2023-10-27 15:42:17 +02:00
return [ is_string(val[0])? VALUE_TYPE.text : VALUE_TYPE.float, val ];
2023-03-11 01:40:17 +01:00
else
2023-10-27 15:42:17 +02:00
return [ is_string(val)? VALUE_TYPE.text : VALUE_TYPE.float, val ];
2023-03-11 01:40:17 +01:00
}
2023-10-27 15:42:17 +02:00
if(is_struct(val)) _pnt = val;
2023-03-11 01:40:17 +01:00
else break;
}
2023-10-27 15:42:17 +02:00
return [ VALUE_TYPE.any, val ];
}
2023-10-27 15:42:17 +02:00
static update = function() {
2023-10-27 15:42:17 +02:00
var str = getInputData(0);
var key = getInputData(1);
var keys = string_splice(key, ".");
if(is_array(str)) {
var typ = VALUE_TYPE.any;
var val = array_create(array_length(str));
for( var i = 0, n = array_length(str); i < n; i++ ) {
var _str = str[i];
var _v = getStructValue(_str, keys);
typ = _v[0];
val[i] = _v[1];
}
2024-08-08 06:57:51 +02:00
outputs[0].setType(typ);
outputs[0].setValue(val);
2023-10-27 15:42:17 +02:00
} else {
var val = getStructValue(str, keys);
2024-08-08 06:57:51 +02:00
outputs[0].setType(val[0]);
outputs[0].setValue(val[1]);
2023-10-27 15:42:17 +02:00
}
}
2023-03-11 01:40:17 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-03-11 01:40:17 +01:00
var bbox = drawGetBbox(xx, yy, _s);
var str = getInputData(1);
2023-03-11 01:40:17 +01:00
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
draw_text_bbox(bbox, str);
}
2023-03-11 01:40:17 +01:00
}