Pixel-Composer/scripts/node_lua_surface/node_lua_surface.gml

261 lines
7.7 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Lua_Surface(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-01-25 06:49:00 +01:00
name = "Lua Surface";
2023-01-04 02:30:04 +01:00
preview_channel = 1;
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-07-21 12:40:20 +02:00
inputs[| 1] = nodeValue("Output dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, DEF_SURF)
2023-01-04 02:30:04 +01:00
.setDisplay(VALUE_DISPLAY.vector);
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-09-07 20:59:14 +02:00
.setDisplay(VALUE_DISPLAY.codeLUA);
2023-01-04 02:30:04 +01:00
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);
2023-03-11 01:40:17 +01:00
inputs[| 4] = nodeValue("Execute on frame", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true)
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("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2023-01-04 02:30:04 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-09-15 20:12:02 +02:00
argumentRenderer(global.lua_arguments);
2023-01-04 02:30:04 +01:00
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,
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-08-05 14:00:33 +02:00
setIsDynamicInput(3, false);
2023-01-04 02:30:04 +01:00
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;
2023-11-23 02:32:26 +01:00
static createNewInput = function() { #region
var index = ds_list_size(inputs);
inputs[| index + 0] = nodeValue("Argument name", self, JUNCTION_CONNECT.input, VALUE_TYPE.text, "" );
inputs[| index + 1] = nodeValue("Argument type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0 )
.setDisplay(VALUE_DISPLAY.enum_scroll, { data: [ "Number", "String", "Surface", "Struct" ], update_hover: false });
inputs[| index + 1].editWidget.interactable = false;
inputs[| index + 2] = nodeValue("Argument value", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0 )
.setVisible(true, true);
inputs[| index + 2].editWidget.interactable = false;
} if(!LOADING && !APPENDING) createNewInput(); #endregion
2023-01-04 02:30:04 +01:00
2023-11-23 02:32:26 +01:00
static stepBegin = function() { #region
2023-07-06 19:49:16 +02:00
if(PROJECT.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-11-23 02:32:26 +01:00
} #endregion
2023-01-04 02:30:04 +01:00
2023-11-23 02:32:26 +01:00
static getState = function() { #region
2023-10-27 13:55:31 +02:00
if(inputs[| 3].isLeaf())
2023-01-04 02:30:04 +01:00
return lua_state;
return inputs[| 3].value_from.node.getState();
2023-11-23 02:32:26 +01:00
} #endregion
2023-01-04 02:30:04 +01:00
2023-11-23 02:32:26 +01:00
static refreshDynamicInput = function() { #region
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(getInputData(i) != "") {
2023-01-04 02:30:04 +01:00
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;
2023-03-11 01:40:17 +01:00
if(inputs[| i + 2].editWidget != noone)
inputs[| i + 2].editWidget.interactable = true;
2023-01-17 08:11:55 +01:00
var type = getInputData(i + 1);
switch(type) {
2023-10-07 16:23:40 +02:00
case 0 : inputs[| i + 2].setType(VALUE_TYPE.float); break;
case 1 : inputs[| i + 2].setType(VALUE_TYPE.text); break;
case 2 : inputs[| i + 2].setType(VALUE_TYPE.surface); break;
case 3 : inputs[| i + 2].setType(VALUE_TYPE.struct); break;
2023-01-09 03:14:20 +01:00
}
inputs[| i + 2].setDisplay(VALUE_DISPLAY._default);
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-11-23 02:32:26 +01:00
} #endregion
2023-01-04 02:30:04 +01:00
2023-11-23 02:32:26 +01:00
static onValueFromUpdate = function(index) { #region
2023-01-09 03:14:20 +01:00
if(index == 0 || index == 2) compiled = false;
2023-11-23 02:32:26 +01:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-11-23 02:32:26 +01:00
static onValueUpdate = function(index = 0) { #region
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(LOADING || APPENDING) return;
2023-02-14 05:32:32 +01:00
compiled = false;
2023-01-09 03:14:20 +01:00
refreshDynamicInput();
2023-11-23 02:32:26 +01:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-11-23 02:32:26 +01:00
static step = function() { #region
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 = getInputData(i + 0);
2023-02-14 05:32:32 +01:00
inputs[| i + 2].name = name;
}
2023-11-23 02:32:26 +01:00
} #endregion
2023-02-14 05:32:32 +01:00
2023-11-23 02:32:26 +01:00
static update = function(frame = CURRENT_FRAME) { #region
2023-01-04 02:30:04 +01:00
if(!compiled) return;
2023-07-06 19:49:16 +02:00
//if(!PROJECT.animator.is_playing || !PROJECT.animator.frame_progress) return;
2023-03-07 14:29:47 +01:00
var _func = getInputData(0);
var _dimm = getInputData(1);
var _exec = getInputData(4);
2023-03-11 01:40:17 +01:00
if(!_exec) return;
2023-01-04 02:30:04 +01:00
argument_val = [];
for( var i = input_fix_len; i < ds_list_size(inputs) - data_length; i += data_length ) {
array_push(argument_val, getInputData(i + 2));
2023-01-04 02:30:04 +01:00
}
2023-10-09 16:07:33 +02:00
//if(CURRENT_FRAME == 0) { //rerfesh state on the first frame
2023-03-11 01:40:17 +01:00
// lua_state_destroy(lua_state);
// lua_state = lua_create();
// addCode();
//}
2023-03-08 07:35:51 +01:00
lua_projectData(getState());
2023-01-04 02:30:04 +01:00
var _outSurf = outputs[| 1].getValue();
2023-03-19 09:17:39 +01:00
_outSurf = surface_verify(_outSurf, _dimm[0], _dimm[1], attrDepth());
2023-01-04 02:30:04 +01:00
surface_set_target(_outSurf);
2023-05-08 10:50:42 +02:00
try { lua_call_w(getState(), _func, argument_val); }
catch(e) { noti_warning(exception_print(e),, self); }
2023-01-04 02:30:04 +01:00
surface_reset_target();
outputs[| 1].setValue(_outSurf);
2023-11-23 02:32:26 +01:00
} #endregion
2023-01-04 02:30:04 +01:00
2023-11-23 02:32:26 +01:00
static addCode = function() { #region
var _func = getInputData(0);
var _code = getInputData(2);
2023-01-04 02:30:04 +01:00
argument_name = [];
for( var i = input_fix_len; i < ds_list_size(inputs) - data_length; i += data_length ) {
array_push(argument_name, getInputData(i + 0));
2023-01-04 02:30:04 +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];
}
2023-01-09 03:14:20 +01:00
lua_code += ")\n";
2023-01-04 02:30:04 +01:00
lua_code += _code;
2023-01-09 03:14:20 +01:00
lua_code += "\nend";
2023-01-04 02:30:04 +01:00
lua_add_code(getState(), lua_code);
2023-11-23 02:32:26 +01:00
} #endregion
2023-03-11 01:40:17 +01:00
insp1UpdateTooltip = __txt("Compile");
insp1UpdateIcon = [ THEME.refresh, 1, COLORS._main_value_positive ];
2023-11-23 02:32:26 +01:00
static onInspector1Update = function() { #region
2023-03-19 09:17:39 +01:00
var thrd = inputs[| 3].value_from;
if(thrd == noone) {
doCompile();
return;
}
2023-03-28 06:58:28 +02:00
thrd.node.onInspector1Update();
2023-11-23 02:32:26 +01:00
} #endregion
2023-03-19 09:17:39 +01:00
2023-11-23 02:32:26 +01:00
static doCompile = function() { #region
2023-03-19 09:17:39 +01:00
addCode();
2023-01-04 02:30:04 +01:00
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;
2023-03-19 09:17:39 +01:00
_j.node.doCompile();
2023-01-04 02:30:04 +01:00
}
doUpdate();
2023-11-23 02:32:26 +01:00
} #endregion
2023-01-04 02:30:04 +01:00
2023-11-23 02:32:26 +01:00
static doApplyDeserialize = function() { #region
2023-01-09 03:14:20 +01:00
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 = getInputData(i + 0);
var type = getInputData(i + 1);
2023-02-14 05:32:32 +01:00
inputs[| i + 2].name = name;
switch(type) {
2023-10-07 16:23:40 +02:00
case 0 : inputs[| i + 2].setType(VALUE_TYPE.float); break;
case 1 : inputs[| i + 2].setType(VALUE_TYPE.text); break;
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
}
inputs[| i + 2].setDisplay(VALUE_DISPLAY._default);
}
2023-11-23 02:32:26 +01:00
doCompile();
} #endregion
2023-02-14 05:32:32 +01:00
2023-11-23 02:32:26 +01:00
static onDestroy = function() { #region
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-11-23 02:32:26 +01:00
} #endregion
2023-01-04 02:30:04 +01:00
}