Pixel-Composer/scripts/node_equation/node_equation.gml

190 lines
5.2 KiB
Text
Raw Normal View History

function Node_create_Equation(_x, _y, _group = noone, _param = {}) {
2023-09-22 17:01:56 +02:00
var query = struct_try_get(_param, "query", "");
var node = new Node_Equation(_x, _y, _group).skipDefault();
2023-09-22 17:01:56 +02:00
if(query == "") return node;
2023-03-23 06:57:31 +01:00
2024-08-08 06:57:51 +02:00
node.inputs[0].setValue(query);
2023-03-23 06:57:31 +01:00
var ind = 1;
2023-09-22 17:01:56 +02:00
var amo = string_length(query);
2023-03-23 06:57:31 +01:00
var str = "";
var pres = global.EQUATION_PRES;
var vars = [];
for( var ind = 1; ind <= amo; ind++ ) {
2023-09-22 17:01:56 +02:00
var ch = string_char_at(query, ind);
2023-03-23 06:57:31 +01:00
if(ds_map_exists(pres, ch) || ch == "(" || ch == ")") {
if(str != "" && str != toNumber(str))
array_push_unique(vars, str);
str = "";
} else
str += ch;
}
if(str != "" && str != toNumber(str))
array_push_unique(vars, str);
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(vars); i < n; i++ )
2024-08-08 06:57:51 +02:00
node.inputs[1 + i * 2].setValue(vars[i]);
2023-03-23 06:57:31 +01:00
return node;
}
2023-03-23 06:57:31 +01:00
2023-02-28 09:43:01 +01:00
function Node_Equation(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2023-01-09 03:14:20 +01:00
name = "Equation";
color = COLORS.node_blend_number;
2024-05-02 12:13:35 +02:00
draw_pad_w = 10;
setDimension(96, 48);
2023-08-31 18:49:57 +02:00
ast = [];
2023-01-17 08:11:55 +01:00
2024-05-23 10:59:39 +02:00
attributes.size = 0;
2023-01-09 03:14:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Text("Equation", self, ""));
2023-01-09 03:14:20 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Result", self, VALUE_TYPE.float, 0));
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
argument_renderer = new Inspector_Custom_Renderer(function(_x, _y, _w, _m, _hover, _focus) {
2023-01-17 08:11:55 +01:00
argument_renderer.x = _x;
argument_renderer.y = _y;
argument_renderer.w = _w;
2024-05-23 10:59:39 +02:00
var bw = _w / 2 - ui(4);
var bh = ui(36);
2024-05-24 07:44:36 +02:00
if(buttonTextIconInstant(true, THEME.button_hide, _x, _y + ui(8), bw, bh, _m, _focus, _hover, "", THEME.add, __txt("Add"), COLORS._main_value_positive) == 2) {
2024-05-23 10:59:39 +02:00
attributes.size++;
refreshDynamicInput();
update();
}
var amo = attributes.size;
2024-06-16 05:27:57 +02:00
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-05-24 07:44:36 +02:00
attributes.size--;
2024-05-23 10:59:39 +02:00
refreshDynamicInput();
update();
}
var tx = _x + ui(8);
var ty = _y + bh + ui(16);
var hh = bh + ui(16);
2023-01-09 03:14:20 +01:00
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-01-09 03:14:20 +01:00
var _h = 0;
2024-08-08 06:57:51 +02:00
var _jName = inputs[i + 0];
2023-06-21 20:36:53 +02:00
_jName.editWidget.setFocusHover(_focus, _hover);
2023-01-09 03:14:20 +01:00
_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);
2023-06-10 13:59:45 +02:00
draw_text_add(tx + ui(128 + 12), ty + ui(6), "=");
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
var _jValue = inputs[i + 1];
2023-06-21 20:36:53 +02:00
_jValue.editWidget.setFocusHover(_focus, _hover);
2023-01-09 03:14:20 +01:00
_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;
}
2023-01-17 08:11:55 +01:00
argument_renderer.h = hh;
2023-01-09 03:14:20 +01:00
return hh;
2024-08-08 06:57:51 +02:00
});
2023-01-09 03:14:20 +01:00
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-01-17 08:11:55 +01:00
2023-01-09 03:14:20 +01:00
input_display_list = [
["Function", false], 0,
["Arguments", false], argument_renderer,
2023-03-23 06:57:31 +01:00
["Inputs", true],
2023-01-09 03:14:20 +01:00
]
2024-05-23 10:59:39 +02:00
static createNewInput = function() {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
2024-08-18 09:13:41 +02:00
newInput(index + 0, nodeValue_Text("Argument name", self, "" ))
2024-05-23 10:59:39 +02:00
.setDisplay(VALUE_DISPLAY.text_box);
2024-08-18 09:13:41 +02:00
newInput(index + 1, nodeValue_Float("Argument value", self, 0 ))
2024-05-23 10:59:39 +02:00
.setVisible(true, true);
2024-08-08 06:57:51 +02:00
return inputs[index + 0];
2024-05-23 10:59:39 +02:00
} setDynamicInput(2, false);
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
static refreshDynamicInput = function() {
var _l = [];
2024-05-23 10:59:39 +02:00
var amo = attributes.size;
2023-01-09 03:14:20 +01:00
2024-05-23 10:59:39 +02:00
for(var i = 0; i < input_fix_len; i++ )
2024-08-08 06:57:51 +02:00
array_push(_l, inputs[i]);
2023-01-09 03:14:20 +01:00
2024-05-23 10:59:39 +02:00
for(var i = 0; i < amo; i++ ) {
var _i = input_fix_len + i * data_length;
2023-03-23 06:57:31 +01:00
2024-08-08 06:57:51 +02:00
if(_i >= array_length(_l))
2024-05-23 10:59:39 +02:00
createNewInput();
for(var j = 0; j < data_length; j++)
2024-08-08 06:57:51 +02:00
array_push(_l, inputs[_i + j]);
2023-01-09 03:14:20 +01:00
}
2024-05-23 10:59:39 +02:00
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(_l); i++ ) {
_l[i].index = i;
2024-05-23 10:59:39 +02:00
array_push(input_display_list, i);
}
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(_l) - 1; i += 2 )
inputs[i + 1].setName(inputs[i].getValue());
2024-05-23 10:59:39 +02:00
inputs = _l;
getJunctionList();
setHeight();
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
}
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
static onValueUpdate = function(index = 0) {
2023-01-09 03:14:20 +01:00
if(LOADING || APPENDING) return;
2023-03-23 06:57:31 +01:00
if(safe_mod(index - input_fix_len, data_length) == 0) //Variable name
2024-08-08 06:57:51 +02:00
inputs[index + 1].setName(inputs[index].getValue());
}
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
static processData = function(_output, _data, _output_index, _array_index = 0) {
2023-01-09 03:14:20 +01:00
var eq = _data[0];
var params = {};
for( var i = input_fix_len; i < array_length(_data); i += data_length ) {
var _pName = _data[i + 0];
var _pVal = _data[i + 1];
2023-09-18 13:54:55 +02:00
if(_pName != "")
params[$ _pName] = _pVal;
2023-01-09 03:14:20 +01:00
}
2024-03-31 05:36:11 +02:00
var _tree = array_safe_get_fast(ast, _array_index, noone);
2023-08-31 18:49:57 +02:00
if(_tree == noone || _tree.fx != eq) {
ast[_array_index] = { fx: eq, tree: evaluateFunctionTree(eq) };
_tree = ast[_array_index];
}
if(_tree == noone) return noone;
return _tree.tree.eval(params);
2024-08-08 06:57:51 +02:00
}
2023-01-09 03:14:20 +01:00
2024-08-08 06:57:51 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
var str = getInputData(0);
2023-01-09 03:14:20 +01:00
var bbox = drawGetBbox(xx, yy, _s);
2024-05-02 12:13:35 +02:00
var ss = string_scale(str, bbox.w, bbox.h);
2023-01-09 03:14:20 +01:00
draw_text_transformed(bbox.xc, bbox.yc, str, ss, ss, 0);
2024-08-08 06:57:51 +02:00
}
2023-01-09 03:14:20 +01:00
static postApplyDeserialize = function() { refreshDynamicInput(); }
2023-01-09 03:14:20 +01:00
}