Pixel-Composer/scripts/node_lua_compute/node_lua_compute.gml

255 lines
7.2 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";
2023-01-04 02:30:04 +01:00
previewable = false;
2023-01-17 08:11:55 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Function name", self, JUNCTION_CONNECT.input, VALUE_TYPE.text, "render" + string(irandom_range(100000, 999999)));
2023-01-04 02:30:04 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("Return type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2023-03-05 07:16:44 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "Number", "String" ], { update_hover: false });
2023-02-19 13:49:20 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Lua code", self, JUNCTION_CONNECT.input, VALUE_TYPE.text, "", o_dialog_lua_reference)
2023-01-04 02:30:04 +01:00
.setDisplay(VALUE_DISPLAY.code);
2023-02-14 05:32:32 +01: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);
static createNewInput = function() {
var index = ds_list_size(inputs);
2023-02-14 05:32:32 +01:00
inputs[| index + 0] = nodeValue("Argument name", self, JUNCTION_CONNECT.input, VALUE_TYPE.text, "" );
2023-01-04 02:30:04 +01:00
2023-02-14 05:32:32 +01:00
inputs[| index + 1] = nodeValue("Argument type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0 )
2023-03-05 07:16:44 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "Number", "String", "Surface" ], { update_hover: false });
2023-01-17 08:11:55 +01:00
inputs[| index + 1].editWidget.interactable = false;
2023-02-14 05:32:32 +01:00
inputs[| index + 2] = nodeValue("Argument value", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0 )
2023-01-04 02:30:04 +01:00
.setVisible(true, true);
2023-01-17 08:11:55 +01:00
inputs[| index + 2].editWidget.interactable = false;
2023-01-04 02:30:04 +01:00
}
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Execution thread", self, JUNCTION_CONNECT.output, VALUE_TYPE.node, noone );
2023-01-04 02:30:04 +01:00
2023-02-14 05:32:32 +01:00
outputs[| 1] = nodeValue("Return value", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, 0);
2023-01-04 02:30:04 +01:00
luaArgumentRenderer();
2023-01-09 03:14:20 +01:00
input_display_list = [ 3,
["Function", false], 0, 1,
2023-01-04 02:30:04 +01:00
["Arguments", false], argument_renderer,
2023-01-09 03:14:20 +01:00
["Script", false], 2,
2023-01-04 02:30:04 +01:00
["Inputs", true],
2023-01-09 03:14:20 +01:00
];
2023-01-04 02:30:04 +01:00
input_fix_len = ds_list_size(inputs);
input_display_len = array_length(input_display_list);
data_length = 3;
argument_name = [];
argument_val = [];
lua_state = lua_create();
2023-02-14 05:32:32 +01:00
error_notification = noone;
2023-01-04 02:30:04 +01:00
compiled = false;
if(!LOADING && !APPENDING) createNewInput();
static stepBegin = function() {
2023-03-07 14:29:47 +01:00
if(ANIMATOR.frame_progress)
2023-01-04 02:30:04 +01:00
setRenderStatus(false);
setHeight();
doStepBegin();
value_validation[VALIDATION.error] = !compiled;
2023-02-14 05:32:32 +01:00
if(!compiled && error_notification == noone) {
error_notification = noti_error("Lua node [" + string(name) + "] not compiled.");
error_notification.onClick = function() { PANEL_GRAPH.focusNode(self); };
}
if(compiled && error_notification != noone) {
noti_remove(error_notification);
error_notification = noone;
}
2023-01-04 02:30:04 +01:00
var _type = inputs[| 1].getValue();
switch(_type) {
case 0 : outputs[| 1].type = VALUE_TYPE.float; break;
2023-03-07 14:29:47 +01:00
case 1 : outputs[| 1].type = VALUE_TYPE.text; break;
2023-01-04 02:30:04 +01:00
}
}
static getState = function() {
if(inputs[| 3].value_from == noone)
return lua_state;
return inputs[| 3].value_from.node.getState();
}
2023-01-09 03:14:20 +01:00
static refreshDynamicInput = function() {
2023-01-04 02:30:04 +01:00
var _in = ds_list_create();
for( var i = 0; i < input_fix_len; i++ )
ds_list_add(_in, inputs[| i]);
array_resize(input_display_list, input_display_len);
for( var i = input_fix_len; i < ds_list_size(inputs); i += data_length ) {
if(inputs[| i].getValue() != "") {
ds_list_add(_in, inputs[| i + 0]);
ds_list_add(_in, inputs[| i + 1]);
ds_list_add(_in, inputs[| i + 2]);
2023-01-17 08:11:55 +01:00
inputs[| i + 1].editWidget.interactable = true;
inputs[| i + 2].editWidget.interactable = true;
2023-01-04 02:30:04 +01:00
array_push(input_display_list, i + 2);
} else {
delete inputs[| i + 0];
delete inputs[| i + 1];
delete inputs[| i + 2];
}
}
for( var i = 0; i < ds_list_size(_in); i++ )
_in[| i].index = i;
ds_list_destroy(inputs);
inputs = _in;
createNewInput();
}
2023-01-09 03:14:20 +01:00
static onValueFromUpdate = function(index) {
if(index == 0 || index == 2) compiled = false;
}
2023-02-14 05:32:32 +01:00
static onValueUpdate = function(index = 0) {
2023-01-09 03:14:20 +01:00
if(index == 0 || index == 2) compiled = false;
if(index == 3) {
for( var i = 0; i < ds_list_size(outputs[| 0].value_to); i++ ) {
var _j = outputs[| 0].value_to[| i];
if(_j.value_from != outputs[| 0]) continue;
_j.node.compiled = false;
}
compiled = false;
}
if(index < input_fix_len) return;
if(LOADING || APPENDING) return;
2023-02-14 05:32:32 +01:00
compiled = false;
2023-02-20 10:16:31 +01:00
if(safe_mod(index - input_fix_len, data_length) == 1) { //Variable type
2023-01-09 03:14:20 +01:00
var type = inputs[| index].getValue();
2023-02-14 05:32:32 +01:00
2023-01-09 03:14:20 +01:00
switch(type) {
case 0 : inputs[| index + 1].type = VALUE_TYPE.float; break;
case 1 : inputs[| index + 1].type = VALUE_TYPE.text; break;
case 2 : inputs[| index + 1].type = VALUE_TYPE.surface; break;
}
inputs[| index + 1].setDisplay(VALUE_DISPLAY._default);
}
refreshDynamicInput();
}
2023-02-14 05:32:32 +01:00
static step = function() {
for( var i = input_fix_len; i < ds_list_size(inputs) - data_length; i += data_length ) {
var name = inputs[| i + 0].getValue();
inputs[| i + 2].name = name;
}
}
static update = function(frame = ANIMATOR.current_frame) {
2023-01-04 02:30:04 +01:00
if(!compiled) return;
2023-03-07 14:29:47 +01:00
if(!ANIMATOR.is_playing || !ANIMATOR.frame_progress) return;
2023-01-04 02:30:04 +01:00
var _func = inputs[| 0].getValue();
var _dimm = inputs[| 1].getValue();
2023-01-25 06:49:00 +01:00
argument_val = [];
2023-01-04 02:30:04 +01:00
for( var i = input_fix_len; i < ds_list_size(inputs) - data_length; i += data_length ) {
array_push(argument_val, inputs[| i + 2].getValue());
}
2023-03-08 07:35:51 +01:00
if(ANIMATOR.current_frame == 0) { //rerfesh state on the first frame
lua_state_destroy(lua_state);
lua_state = lua_create();
}
lua_projectData(getState());
2023-01-04 02:30:04 +01:00
var res = 0;
try {
res = lua_call_w(getState(), _func, argument_val);
} catch(e) {
2023-01-25 06:49:00 +01:00
noti_warning(exception_print(e),, self);
2023-01-04 02:30:04 +01:00
}
outputs[| 1].setValue(res);
}
2023-02-14 05:32:32 +01:00
static onInspectorUpdate = function() { //compile
2023-01-04 02:30:04 +01:00
var _func = inputs[| 0].getValue();
var _code = inputs[| 2].getValue();
argument_name = [];
for( var i = input_fix_len; i < ds_list_size(inputs) - data_length; i += data_length ) {
array_push(argument_name, inputs[| i + 0].getValue());
}
var lua_code = "function " + _func + "(";
for( var i = 0; i < array_length(argument_name); i++ ) {
if(i) lua_code += ", "
lua_code += argument_name[i];
}
2023-02-14 05:32:32 +01:00
lua_code += ")\n";
2023-01-04 02:30:04 +01:00
lua_code += _code;
2023-02-14 05:32:32 +01:00
lua_code += "\nend";
2023-01-04 02:30:04 +01:00
lua_add_code(getState(), lua_code);
compiled = true;
for( var i = 0; i < ds_list_size(outputs[| 0].value_to); i++ ) {
var _j = outputs[| 0].value_to[| i];
if(_j.value_from != outputs[| 0]) continue;
_j.node.inspectorUpdate();
}
doUpdate();
}
static postDeserialize = function() {
var _inputs = load_map[? "inputs"];
2023-01-09 03:14:20 +01:00
for(var i = input_fix_len; i < ds_list_size(_inputs); i += data_length)
2023-01-04 02:30:04 +01:00
createNewInput();
}
2023-01-09 03:14:20 +01:00
static doApplyDeserialize = function() {
refreshDynamicInput();
2023-02-14 05:32:32 +01:00
for( var i = input_fix_len; i < ds_list_size(inputs) - data_length; i += data_length ) {
var name = inputs[| i + 0].getValue();
var type = inputs[| i + 1].getValue();
inputs[| i + 2].name = name;
switch(type) {
case 0 : inputs[| i + 2].type = VALUE_TYPE.float; break;
case 1 : inputs[| i + 2].type = VALUE_TYPE.text; break;
case 2 : inputs[| i + 2].type = VALUE_TYPE.surface; break;
}
inputs[| i + 2].setDisplay(VALUE_DISPLAY._default);
}
}
static onDestroy = function() {
2023-03-08 07:35:51 +01:00
lua_state_destroy(lua_state);
2023-02-14 05:32:32 +01:00
if(error_notification != noone)
noti_remove(error_notification);
2023-01-09 03:14:20 +01:00
}
2023-01-04 02:30:04 +01:00
}