Pixel-Composer/scripts/node_PCX_equation/node_PCX_equation.gml

137 lines
3.6 KiB
Plaintext
Raw Normal View History

2023-09-11 16:08:58 +02:00
function Node_PCX_Equation(_x, _y, _group = noone) : Node_PCX(_x, _y, _group) constructor {
name = "Equation";
2024-05-23 10:59:39 +02:00
setDimension(96, 48);
2023-09-11 16:08:58 +02:00
ast = noone;
2024-08-08 06:57:51 +02:00
inputs[0] = nodeValue_Text("Equation", self, "");
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
outputs[0] = nodeValue_Output("Result", self, VALUE_TYPE.PCXnode, noone );
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
argument_renderer = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
2023-09-11 16:08:58 +02:00
argument_renderer.x = _x;
argument_renderer.y = _y;
argument_renderer.w = _w;
var tx = _x + ui(8);
var ty = _y + ui(8);
var hh = ui(8);
var _th = TEXTBOX_HEIGHT;
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
2023-09-11 16:08:58 +02:00
var _h = 0;
2024-08-08 06:57:51 +02:00
var _jName = inputs[i + 0];
2023-09-11 16:08:58 +02:00
_jName.editWidget.setFocusHover(_focus, _hover);
_jName.editWidget.draw(tx, ty, ui(128), _th, _jName.showValue(), _m, _jName.display_type);
draw_set_text(f_p1, fa_center, fa_top, COLORS._main_text_sub);
draw_text_add(tx + ui(128 + 12), ty + ui(6), "=");
2024-08-08 06:57:51 +02:00
var _jValue = inputs[i + 1];
2023-09-11 16:08:58 +02:00
_jValue.editWidget.setFocusHover(_focus, _hover);
_jValue.editWidget.draw(tx + ui(128 + 24), ty, _w - ui(128 + 24 + 16), _th, _jValue.showValue(), _m);
_h += _th + ui(6);
hh += _h;
ty += _h;
}
argument_renderer.h = hh;
return hh;
2024-08-08 06:57:51 +02:00
});
2023-09-11 16:08:58 +02:00
input_display_list = [
["Function", false], 0,
["Arguments", false], argument_renderer,
["Inputs", true],
]
2024-05-23 10:59:39 +02:00
static createNewInput = function() {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
inputs[index + 0] = nodeValue_Text("Argument name", self, "" )
2024-05-23 10:59:39 +02:00
.setDisplay(VALUE_DISPLAY.text_box);
2024-08-08 06:57:51 +02:00
inputs[index + 1] = nodeValue("Argument value", self, JUNCTION_CONNECT.input, VALUE_TYPE.PCXnode, noone )
2024-05-23 10:59:39 +02:00
.setVisible(true, true);
2024-08-08 06:57:51 +02:00
inputs[index + 1].editWidget.interactable = false;
2024-05-23 10:59:39 +02:00
2024-08-08 06:57:51 +02:00
return inputs[index + 0];
2024-05-23 10:59:39 +02:00
} setDynamicInput(2, false);
2024-08-08 06:57:51 +02:00
argument_renderer.register = function(parent = noone) {
for( var i = input_fix_len; i < array_length(inputs); i++ )
inputs[i].editWidget.register(parent);
}
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
static refreshDynamicInput = function() {
var _in = [];
2023-09-11 16:08:58 +02:00
for( var i = 0; i < input_fix_len; i++ )
2024-08-08 06:57:51 +02:00
array_push(_in, inputs[i]);
2023-09-11 16:08:58 +02:00
array_resize(input_display_list, input_display_len);
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
var varName = getInputData(i);
2023-09-11 16:08:58 +02:00
if(varName != "") {
2024-08-08 06:57:51 +02:00
array_push(_in, inputs[i + 0]);
array_push(_in, inputs[i + 1]);
inputs[i + 1].editWidget.setInteract(true);
inputs[i + 1].name = varName;
2023-09-11 16:08:58 +02:00
array_push(input_display_list, i + 1);
} else {
2024-08-08 06:57:51 +02:00
delete inputs[i + 0];
delete inputs[i + 1];
2023-09-11 16:08:58 +02:00
}
}
2024-08-08 06:57:51 +02:00
for( var i = 0; i < array_length(_in); i++ )
_in[i].index = i;
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
2023-09-11 16:08:58 +02:00
inputs = _in;
createNewInput();
2024-08-08 06:57:51 +02:00
}
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
static onValueUpdate = function(index = 0) {
2023-09-11 16:08:58 +02:00
if(LOADING || APPENDING) return;
if(safe_mod(index - input_fix_len, data_length) == 0) //Variable name
2024-08-08 06:57:51 +02:00
inputs[index + 1].name = getInputData(index);
2023-09-11 16:08:58 +02:00
refreshDynamicInput();
2024-08-08 06:57:51 +02:00
}
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
static update = function() {
var eq = getInputData(0);
2023-09-11 16:08:58 +02:00
var fn = evaluateFunctionTree(eq);
var _fnL = new __funcList();
for( var i = input_fix_len; i < array_length(_data); i += data_length ) {
var _pName = getInputData(i + 0);
var _pVal = getInputData(i + 1);
2023-09-11 16:08:58 +02:00
_fnL.addFunction(new __funcTree("=", _pName, _pVal));
}
_fnL.addFunction(fn);
2024-08-08 06:57:51 +02:00
outputs[0].setValue(fn);
}
2023-09-11 16:08:58 +02:00
2024-08-08 06:57:51 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-09-11 16:08:58 +02:00
draw_set_text(f_h5, fa_center, fa_center, COLORS._main_text);
var str = getInputData(0);
2023-09-11 16:08:58 +02:00
var bbox = drawGetBbox(xx, yy, _s);
var ss = string_scale(str, bbox.w, bbox.h);
draw_text_transformed(bbox.xc, bbox.yc, str, ss, ss, 0);
2024-08-08 06:57:51 +02:00
}
2023-09-11 16:08:58 +02:00
static doApplyDeserialize = function() { refreshDynamicInput(); }
}