Pixel-Composer/scripts/node_lua_compute/node_lua_compute.gml

197 lines
5.8 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Lua_Compute(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-01-25 06:49:00 +01:00
name = "Lua Compute";
2024-01-23 14:40:03 +01:00
update_on_frame = true;
2023-01-17 08:11:55 +01:00
2024-08-08 06:57:51 +02:00
inputs[0] = nodeValue_Text("Function name", self, "render" + string(irandom_range(100000, 999999)));
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
inputs[1] = nodeValue_Enum_Scroll("Return type", self, 0, { data: [ "Number", "String", "Struct" ], update_hover: false });
2023-02-19 13:49:20 +01:00
2024-08-08 06:57:51 +02:00
inputs[2] = nodeValue_Text("Lua code", self, "", o_dialog_lua_reference)
2023-09-07 20:59:14 +02:00
.setDisplay(VALUE_DISPLAY.codeLUA);
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
inputs[3] = nodeValue("Execution thread", self, JUNCTION_CONNECT.input, VALUE_TYPE.node, noone)
2023-01-04 02:30:04 +01:00
.setVisible(false, true);
2024-08-08 06:57:51 +02:00
inputs[4] = nodeValue_Bool("Execute on frame", self, true)
2023-03-11 01:40:17 +01:00
2024-08-08 06:57:51 +02:00
outputs[0] = nodeValue_Output("Execution thread", self, VALUE_TYPE.node, noone );
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
outputs[1] = nodeValue_Output("Return value", self, VALUE_TYPE.surface, noone);
2023-01-04 02:30:04 +01:00
2023-09-15 20:12:02 +02:00
argumentRenderer(global.lua_arguments);
2023-01-04 02:30:04 +01:00
2024-08-18 05:10:39 +02:00
lb_pre = new Inspector_Label("", _f_code_s);
lb_pos = new Inspector_Label("", _f_code_s);
2023-03-11 01:40:17 +01:00
input_display_list = [ 3, 4,
2023-01-09 03:14:20 +01:00
["Function", false], 0, 1,
2023-01-04 02:30:04 +01:00
["Arguments", false], argument_renderer,
2024-08-18 05:10:39 +02:00
["Script", false], lb_pre, 2, lb_pos,
2023-01-04 02:30:04 +01:00
["Inputs", true],
2023-01-09 03:14:20 +01:00
];
2023-08-05 14:00:33 +02:00
2023-01-04 02:30:04 +01:00
argument_name = [];
argument_val = [];
lua_state = lua_create();
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 05:10:39 +02:00
newInput(index + 0, nodeValue_Text("Argument name", self, "" ));
2023-11-23 02:32:26 +01:00
2024-08-18 05:10:39 +02:00
newInput(index + 1, nodeValue_Enum_Scroll("Argument type", self, 0 , { data: [ "Number", "String", "Surface", "Struct" ], update_hover: false }));
2024-08-08 06:57:51 +02:00
inputs[index + 1].editWidget.interactable = false;
2023-11-23 02:32:26 +01:00
2024-08-18 05:10:39 +02:00
newInput(index + 2, nodeValue("Argument value", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0 ))
2023-11-23 02:32:26 +01:00
.setVisible(true, true);
2024-08-08 06:57:51 +02:00
inputs[index + 2].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(3, false);
if(!LOADING && !APPENDING) createNewInput();
2023-01-04 02:30:04 +01:00
2024-08-18 05:10:39 +02:00
static getState = function() {
2024-08-08 06:57:51 +02:00
if(inputs[3].value_from == noone) return lua_state;
return inputs[3].value_from.node.getState();
2024-08-18 05:10:39 +02:00
}
2023-01-04 02:30:04 +01:00
2024-08-18 05:10:39 +02:00
static refreshDynamicInput = function() {
2024-08-08 06:57:51 +02:00
var _in = [];
2023-01-04 02:30:04 +01:00
for( var i = 0; i < input_fix_len; i++ )
2024-08-08 06:57:51 +02:00
array_push(_in, inputs[i]);
2023-01-04 02:30:04 +01: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 ) {
2024-08-18 05:10:39 +02:00
if(getInputDataForce(i) != "") {
2024-08-08 06:57:51 +02:00
array_push(_in, inputs[i + 0]);
array_push(_in, inputs[i + 1]);
array_push(_in, inputs[i + 2]);
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
inputs[i + 1].editWidget.interactable = true;
if(inputs[i + 2].editWidget != noone)
inputs[i + 2].editWidget.interactable = true;
2023-01-17 08:11:55 +01:00
2023-01-04 02:30:04 +01:00
array_push(input_display_list, i + 2);
} else {
2024-08-08 06:57:51 +02:00
delete inputs[i + 0];
delete inputs[i + 1];
delete inputs[i + 2];
2023-01-04 02:30:04 +01:00
}
}
2024-08-08 06:57:51 +02:00
for( var i = 0; i < array_length(_in); i++ )
_in[i].index = i;
2023-01-04 02:30:04 +01:00
inputs = _in;
2024-05-23 10:59:39 +02:00
refreshInputType();
2023-01-04 02:30:04 +01:00
createNewInput();
2024-08-18 05:10:39 +02:00
}
2023-01-04 02:30:04 +01:00
2024-08-18 05:10:39 +02:00
static refreshInputType = function() {
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
2024-05-23 10:59:39 +02:00
var type = getInputData(i + 1);
switch(type) {
2024-08-18 05:10:39 +02:00
case 0 : inputs[i + 2].setType(VALUE_TYPE.float); break;
case 1 : inputs[i + 2].setType(VALUE_TYPE.text); break;
2024-08-08 06:57:51 +02:00
case 2 : inputs[i + 2].setType(VALUE_TYPE.surface); break;
case 3 : inputs[i + 2].setType(VALUE_TYPE.struct); break;
2024-05-23 10:59:39 +02:00
}
2024-08-08 06:57:51 +02:00
inputs[i + 2].setDisplay(VALUE_DISPLAY._default);
2024-05-23 10:59:39 +02:00
}
2024-08-18 05:10:39 +02:00
}
2024-05-23 10:59:39 +02:00
2024-08-18 05:10:39 +02:00
static onValueUpdate = function(index = 0) {
2023-01-09 03:14:20 +01:00
if(LOADING || APPENDING) return;
2024-05-23 10:59:39 +02:00
var _ind = (index - input_fix_len) % data_length;
if(_ind == 0) refreshDynamicInput();
else if(_ind == 1) refreshInputType();
2024-08-18 05:10:39 +02:00
}
2023-01-09 03:14:20 +01:00
2024-08-18 05:10:39 +02:00
static step = function() {
2024-01-23 14:40:03 +01:00
var _type = getInputData(1);
switch(_type) {
2024-08-08 06:57:51 +02:00
case 0 : outputs[1].setType(VALUE_TYPE.float); break;
case 1 : outputs[1].setType(VALUE_TYPE.text); break;
case 2 : outputs[1].setType(VALUE_TYPE.struct); break;
2024-01-23 14:40:03 +01:00
}
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs) - data_length; i += data_length ) {
var name = getInputData(i + 0);
2024-08-08 06:57:51 +02:00
inputs[i + 2].name = name;
2023-02-14 05:32:32 +01:00
}
2024-08-18 05:10:39 +02:00
}
2023-02-14 05:32:32 +01:00
2024-08-18 05:10:39 +02:00
static update = function(frame = CURRENT_FRAME) {
var _func = getInputData(0);
var _dimm = getInputData(1);
var _exec = getInputData(4);
2024-01-23 14:40:03 +01:00
update_on_frame = _exec;
2023-01-04 02:30:04 +01:00
2023-01-25 06:49:00 +01:00
argument_val = [];
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs) - data_length; i += data_length )
2024-08-18 05:10:39 +02:00
array_push(argument_val, getInputData(i + 2));
2023-01-04 02:30:04 +01:00
2023-03-08 07:35:51 +01:00
lua_projectData(getState());
2024-01-23 14:40:03 +01:00
addCode();
2023-03-08 07:35:51 +01:00
2023-01-04 02:30:04 +01:00
var res = 0;
2024-01-23 14:40:03 +01:00
try { res = lua_call_w(getState(), _func, argument_val); }
catch(e) { noti_warning(exception_print(e),, self); }
2023-01-04 02:30:04 +01:00
2024-08-08 06:57:51 +02:00
outputs[1].setValue(res);
2024-08-18 05:10:39 +02:00
}
2023-01-04 02:30:04 +01:00
2024-08-18 05:10:39 +02:00
static addCode = function() {
var _func = getInputData(0);
var _code = getInputData(2);
2023-01-04 02:30:04 +01:00
argument_name = [];
2024-08-18 05:10:39 +02:00
for( var i = input_fix_len; i < array_length(inputs) - data_length; i += data_length )
if(getInputData(i) != "") array_push(argument_name, getInputData(i));
2023-01-04 02:30:04 +01:00
2024-01-23 14:40:03 +01:00
var lua_code = $"function {_func}(";
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(argument_name); i < n; i++ ) {
2023-01-04 02:30:04 +01:00
if(i) lua_code += ", "
lua_code += argument_name[i];
}
2024-08-18 05:10:39 +02:00
lb_pre.text = lua_code + ")";
lb_pos.text = "end";
lua_code += $")\n{_code}\nend";
2023-01-04 02:30:04 +01:00
2024-01-23 14:40:03 +01:00
lua_add_code(getState(), lua_code);
2024-08-18 05:10:39 +02:00
}
2023-01-04 02:30:04 +01:00
2024-08-18 05:10:39 +02:00
static doApplyDeserialize = function() {
2023-01-09 03:14:20 +01:00
refreshDynamicInput();
2023-02-14 05:32:32 +01:00
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs) - data_length; i += data_length ) {
var name = getInputData(i + 0);
var type = getInputData(i + 1);
2023-02-14 05:32:32 +01:00
2024-08-08 06:57:51 +02:00
inputs[i + 2].name = name;
2023-02-14 05:32:32 +01:00
switch(type) {
2024-08-18 05:10:39 +02:00
case 0 : inputs[i + 2].setType(VALUE_TYPE.float); break;
case 1 : inputs[i + 2].setType(VALUE_TYPE.text); break;
2024-08-08 06:57:51 +02:00
case 2 : inputs[i + 2].setType(VALUE_TYPE.surface); break;
case 3 : inputs[i + 2].setType(VALUE_TYPE.struct); break;
2023-02-14 05:32:32 +01:00
}
2024-08-08 06:57:51 +02:00
inputs[i + 2].setDisplay(VALUE_DISPLAY._default);
2023-02-14 05:32:32 +01:00
}
2023-11-23 02:32:26 +01:00
2024-05-23 14:40:30 +02:00
}
2024-08-18 05:10:39 +02:00
static onDestroy = function() { lua_state_destroy(lua_state); }
static onRestore = function() { lua_state = lua_create(); }
2023-01-04 02:30:04 +01:00
}