Pixel-Composer/scripts/node_struct_get/node_struct_get.gml

81 lines
2.3 KiB
Text
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";
w = 96;
inputs[| 0] = nodeValue("Struct", self, JUNCTION_CONNECT.input, VALUE_TYPE.struct, {})
.setVisible(true, true);
inputs[| 1] = nodeValue("Key", self, JUNCTION_CONNECT.input, VALUE_TYPE.text, "");
outputs[| 0] = nodeValue("Struct", self, JUNCTION_CONNECT.output, VALUE_TYPE.struct, {});
2023-10-27 15:42:17 +02:00
static getStructValue = function(str, keys) { #region
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 ];
} #endregion
static update = function() { #region
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];
}
outputs[| 0].setType(typ);
outputs[| 0].setValue(val);
} else {
var val = getStructValue(str, keys);
outputs[| 0].setType(val[0]);
outputs[| 0].setValue(val[1]);
}
} #endregion
2023-03-11 01:40:17 +01:00
2023-10-27 15:42:17 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
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);
2023-03-11 01:40:17 +01:00
var ss = string_scale(str, bbox.w, bbox.h);
draw_text_transformed(bbox.xc, bbox.yc, str, ss, ss, 0);
2023-10-27 15:42:17 +02:00
} #endregion
2023-03-11 01:40:17 +01:00
}