Pixel-Composer/scripts/node_processor/node_processor.gml

314 lines
9.4 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
enum ARRAY_PROCESS {
loop,
2023-02-28 09:43:01 +01:00
hold,
expand,
expand_inv,
2022-01-13 05:24:03 +01:00
}
2023-03-02 07:59:14 +01:00
#macro PROCESSOR_OVERLAY_CHECK if(array_length(current_data) != ds_list_size(inputs)) return;
2023-02-28 09:43:01 +01:00
function Node_Processor(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-06-13 14:42:06 +02:00
attributes.array_process = ARRAY_PROCESS.loop;
2022-12-27 13:30:02 +01:00
current_data = [];
inputs_data = [];
inputs_is_array = [];
2023-08-17 16:56:54 +02:00
all_inputs = [];
2023-01-01 02:06:02 +01:00
process_amount = 0;
2023-02-28 09:43:01 +01:00
process_length = [];
2023-07-08 20:29:23 +02:00
dimension_index = 0;
2022-01-13 05:24:03 +01:00
batch_output = false; //Run processData once with all outputs as array.
2022-11-18 03:20:31 +01:00
icon = THEME.node_processor;
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
array_push(attributeEditors, "Array processor");
2023-07-14 20:34:35 +02:00
array_push(attributeEditors, [ "Array process type", function() { return attributes.array_process; },
2023-04-11 20:29:20 +02:00
new scrollBox([ "Loop", "Hold", "Expand", "Expand inverse" ],
function(val) {
2023-06-13 14:42:06 +02:00
attributes.array_process = val;
2023-04-11 20:29:20 +02:00
triggerRender();
}, false) ]);
2023-03-19 09:17:39 +01:00
static getInputData = function(index, def = 0) { return array_safe_get(inputs_data, index, def); }
2023-08-24 11:59:05 +02:00
2023-08-17 16:56:54 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index = 0) { return _outSurf; }
2022-01-13 05:24:03 +01:00
static getSingleValue = function(_index, _arr = 0, output = false) { #region
2023-03-05 07:16:44 +01:00
var _l = output? outputs : inputs;
var _n = _l[| _index];
2022-12-27 13:30:02 +01:00
var _in = _n.getValue();
2022-12-27 04:00:50 +01:00
2023-02-28 09:43:01 +01:00
if(!_n.isArray()) return _in;
2023-06-13 14:42:06 +02:00
switch(attributes.array_process) {
2023-02-28 09:43:01 +01:00
case ARRAY_PROCESS.loop : _index = safe_mod(_arr, array_length(_in)); break;
case ARRAY_PROCESS.hold : _index = min(_arr, array_length(_in) - 1); break;
case ARRAY_PROCESS.expand : _index = floor(_arr / process_length[_index][1]) % process_length[_index][0]; break;
2023-03-05 07:16:44 +01:00
case ARRAY_PROCESS.expand_inv : _index = floor(_arr / process_length[ds_list_size(_l) - 1 - _index][1]) % process_length[_index][0]; break;
2023-02-28 09:43:01 +01:00
}
return array_safe_get(_in, _index);
} #endregion
2022-12-27 13:30:02 +01:00
static getDimension = function(arr = 0) { #region
2022-12-27 13:30:02 +01:00
if(dimension_index == -1) return [1, 1];
var _in = getSingleValue(dimension_index, arr);
2023-01-01 02:06:02 +01:00
if(inputs[| dimension_index].type == VALUE_TYPE.surface && is_surface(_in)) {
2023-09-08 21:37:36 +02:00
var ww = surface_get_width_safe(_in);
var hh = surface_get_height_safe(_in);
2022-12-27 04:00:50 +01:00
return [ww, hh];
2022-01-13 05:24:03 +01:00
}
2022-12-27 13:30:02 +01:00
if(is_array(_in) && array_length(_in) == 2)
return _in;
2022-12-27 04:00:50 +01:00
return [1, 1];
} #endregion
2022-12-27 04:00:50 +01:00
static processDataArray = function(outIndex) { #region
var _output = outputs[| outIndex];
var _out = _output.getValue();
2022-12-18 03:20:38 +01:00
if(process_amount == 1) { #region render single data
if(_output.type == VALUE_TYPE.d3object) //passing 3D vertex call
2023-01-01 02:06:02 +01:00
return _out;
2023-09-26 14:35:25 +02:00
if(_output.type == VALUE_TYPE.surface) { //resize surface
if(dimension_index == -1)
surface_array_free(_out);
else {
var surf = inputs_data[dimension_index];
var _sw = 1, _sh = 1;
if(inputs[| dimension_index].type == VALUE_TYPE.surface) {
if(is_surface(surf)) {
_sw = surface_get_width_safe(surf);
_sh = surface_get_height_safe(surf);
} else
return noone;
} else if(is_array(surf)) {
_sw = array_safe_get(surf, 0, 1);
_sh = array_safe_get(surf, 1, 1);
}
_out = surface_verify(_out, _sw, _sh, attrDepth());
2022-12-27 13:30:02 +01:00
}
2022-12-27 04:00:50 +01:00
}
current_data = inputs_data;
2023-02-14 02:51:14 +01:00
if(active_index > -1 && !inputs_data[active_index]) { // skip
if(inputs[| 0].type == VALUE_TYPE.surface)
return surface_clone(inputs_data[0], _out);
else
return inputs_data[0]
}
2023-08-17 16:56:54 +02:00
var data = processData(_out, inputs_data, outIndex, 0); /// Process data
2023-03-21 03:01:53 +01:00
return data;
2023-08-17 16:56:54 +02:00
} #endregion
2023-01-01 02:06:02 +01:00
2023-08-17 16:56:54 +02:00
#region ++++ array preparation ++++
if(!is_array(_out))
_out = array_create(process_amount);
else if(array_length(_out) != process_amount)
array_resize(_out, process_amount);
#endregion
2022-12-27 04:00:50 +01:00
var _data = array_create(ds_list_size(inputs));
2023-08-24 11:59:05 +02:00
2022-12-27 04:00:50 +01:00
for(var l = 0; l < process_amount; l++) {
for(var i = 0; i < ds_list_size(inputs); i++)
_data[i] = all_inputs[i][l];
2022-12-27 04:00:50 +01:00
if(_output.type == VALUE_TYPE.surface && dimension_index > -1) { #region output surface verification
2022-12-27 13:30:02 +01:00
var surf = _data[dimension_index];
var _sw = 1, _sh = 1;
2023-02-14 02:51:14 +01:00
if(inputs[| dimension_index].type == VALUE_TYPE.surface) {
if(is_surface(surf)) {
2023-09-08 21:37:36 +02:00
_sw = surface_get_width_safe(surf);
_sh = surface_get_height_safe(surf);
2023-02-14 02:51:14 +01:00
} else
return noone;
2022-12-27 13:30:02 +01:00
} else if(is_array(surf)) {
_sw = surf[0];
_sh = surf[1];
}
2023-03-19 09:17:39 +01:00
_out[l] = surface_verify(_out[l], _sw, _sh, attrDepth());
2023-08-17 16:56:54 +02:00
} #endregion
2022-12-27 04:00:50 +01:00
2023-02-14 02:51:14 +01:00
if(l == 0 || l == preview_index)
2022-12-27 13:30:02 +01:00
current_data = _data;
2023-02-14 02:51:14 +01:00
if(active_index > -1 && !_data[active_index]) { // skip
if(inputs[| 0].type == VALUE_TYPE.surface)
_out[l] = surface_clone(_data[0], _out[l]);
else
_out[l] = _data[0];
} else
2023-08-17 16:56:54 +02:00
_out[l] = processData(_out[l], _data, outIndex, l); /// Process data
2022-01-13 05:24:03 +01:00
}
2022-12-27 04:00:50 +01:00
return _out;
} #endregion
2022-01-13 05:24:03 +01:00
static processBatchOutput = function() { #region
for(var i = 0; i < ds_list_size(outputs); i++) {
if(outputs[| i].type != VALUE_TYPE.surface) continue;
var _res = outputs[| i].getValue();
surface_array_free(_res);
outputs[| i].setValue(noone);
}
if(process_amount == 1) {
var data = processData(noone, inputs_data, 0, 0);
for(var i = 0; i < ds_list_size(outputs); i++) {
var _outp = array_safe_get(data, i, undefined);
if(_outp == undefined) continue;
outputs[| i].setValue(_outp);
}
} else {
var _outputs = array_create(ds_list_size(outputs));
for( var l = 0; l < process_amount; l++ ) {
var _data = array_create(ds_list_size(inputs));
for(var i = 0; i < ds_list_size(inputs); i++)
_data[i] = all_inputs[i][l];
2023-09-18 13:54:55 +02:00
var data = processData(0, _data, 0, l);
for(var i = 0; i < ds_list_size(outputs); i++) {
var _outp = array_safe_get(data, i, undefined);
_outputs[i][l] = _outp;
}
}
for( var i = 0, n = ds_list_size(outputs); i < n; i++ )
outputs[| i].setValue(_outputs[i]);
}
} #endregion
static processOutput = function() { #region
2023-09-26 14:35:25 +02:00
var val;
for(var i = 0; i < ds_list_size(outputs); i++) {
if(outputs[| i].process_array) {
val = processDataArray(i);
if(val == undefined) continue;
} else
val = processData(noone, noone, i);
outputs[| i].setValue(val);
}
} #endregion
static getInputs = function() { #region
process_amount = 1;
2023-02-28 09:43:01 +01:00
inputs_data = array_create(ds_list_size(inputs));
inputs_is_array = array_create(ds_list_size(inputs));
2023-02-28 09:43:01 +01:00
process_length = array_create(ds_list_size(inputs));
2022-01-13 05:24:03 +01:00
for(var i = 0; i < ds_list_size(inputs); i++) {
2023-02-28 09:43:01 +01:00
var val = inputs[| i].getValue();
var amo = inputs[| i].arrayLength(val);
2022-12-27 04:00:50 +01:00
2023-09-18 13:54:55 +02:00
if(amo == 0) val = noone; //empty array
if(amo == 1) val = val[0]; //spread single array
amo = max(1, amo);
inputs_data[i] = val;
inputs_is_array[i] = inputs[| i].isArray(val);
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
switch(attributes.array_process) {
2023-02-28 09:43:01 +01:00
case ARRAY_PROCESS.loop :
case ARRAY_PROCESS.hold :
process_amount = max(process_amount, amo);
break;
2023-08-24 11:59:05 +02:00
2023-02-28 09:43:01 +01:00
case ARRAY_PROCESS.expand :
case ARRAY_PROCESS.expand_inv :
2023-09-18 13:54:55 +02:00
process_amount *= amo;
2023-02-28 09:43:01 +01:00
break;
}
2023-09-18 13:54:55 +02:00
process_length[i] = [amo, process_amount];
2023-02-28 09:43:01 +01:00
}
var amoMax = process_amount;
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(process_length); i < n; i++ ) {
2023-02-28 09:43:01 +01:00
amoMax /= process_length[i][0];
process_length[i][1] = amoMax;
2022-12-27 04:00:50 +01:00
}
2022-01-13 05:24:03 +01:00
for(var i = 0; i < ds_list_size(inputs); i++)
all_inputs[i] = array_create(process_amount);
for(var l = 0; l < process_amount; l++) #region input preparation
for(var i = 0; i < ds_list_size(inputs); i++) {
var _in = inputs_data[i];
if(!inputs_is_array[i]) {
all_inputs[i][l] = _in;
continue;
}
if(array_length(_in) == 0) {
all_inputs[i][l] = 0;
continue;
}
var _index = 0;
switch(attributes.array_process) {
case ARRAY_PROCESS.loop : _index = safe_mod(l, array_length(_in)); break;
case ARRAY_PROCESS.hold : _index = min(l, array_length(_in) - 1); break;
case ARRAY_PROCESS.expand : _index = floor(l / process_length[i][1]) % process_length[i][0]; break;
case ARRAY_PROCESS.expand_inv : _index = floor(l / process_length[ds_list_size(inputs) - 1 - i][1]) % process_length[i][0]; break;
}
all_inputs[i][l] = inputs[| i].arrayBalance(_in[_index]);
} #endregion
} #endregion
static update = function(frame = PROJECT.animator.current_frame) { #region
getInputs();
if(batch_output) processBatchOutput();
else processOutput();
postUpdate();
2023-08-17 16:56:54 +02:00
} #endregion
2023-02-28 09:43:01 +01:00
static postUpdate = function() {}
2023-08-17 16:56:54 +02:00
static processSerialize = function(_map) { #region
2023-06-13 14:42:06 +02:00
_map.array_process = attributes.array_process;
2023-08-17 16:56:54 +02:00
} #endregion
2023-02-28 09:43:01 +01:00
2023-08-17 16:56:54 +02:00
static processDeserialize = function() { #region
2023-06-13 14:42:06 +02:00
attributes.array_process = struct_try_get(load_map, "array_process", ARRAY_PROCESS.loop);
2023-08-17 16:56:54 +02:00
} #endregion
2023-07-08 20:29:23 +02:00
///////////////////// CACHE /////////////////////
2023-08-17 16:56:54 +02:00
static cacheCurrentFrameIndex = function(_frame, index) { #region
2023-07-08 20:29:23 +02:00
cacheArrayCheck();
if(PROJECT.animator.current_frame < 0) return;
if(PROJECT.animator.current_frame >= array_length(cached_output)) return;
var prev = cached_output[PROJECT.animator.current_frame];
surface_array_free(array_safe_get(prev, index));
cached_output[PROJECT.animator.current_frame][index] = surface_array_clone(_frame);
array_safe_set(cache_result, PROJECT.animator.current_frame, true);
return cached_output[PROJECT.animator.current_frame];
2023-08-17 16:56:54 +02:00
} #endregion
2023-07-08 20:29:23 +02:00
2023-08-17 16:56:54 +02:00
static getCacheFrameIndex = function(frame = PROJECT.animator.current_frame, index = 0) { #region
2023-07-08 20:29:23 +02:00
if(frame < 0) return false;
if(!cacheExist(frame)) return noone;
var surf = array_safe_get(cached_output, frame);
return array_safe_get(surf, index);
2023-08-17 16:56:54 +02:00
} #endregion
2022-01-13 05:24:03 +01:00
}