Pixel-Composer/scripts/node_statistic/node_statistic.gml

196 lines
4.9 KiB
Plaintext
Raw Normal View History

2022-01-26 06:57:34 +01:00
enum STAT_OPERATOR {
_sum,
_average,
_median,
_max,
_min
}
2023-09-22 17:01:56 +02:00
function Node_create_Statistic(_x, _y, _group = noone, _param = {}) {
var query = struct_try_get(_param, "query", "");
var node = new Node_Statistic(_x, _y, _group);
2022-01-26 06:57:34 +01:00
2023-09-22 17:01:56 +02:00
switch(query) {
2022-01-26 06:57:34 +01:00
case "sum" : node.inputs[| 0].setValue(STAT_OPERATOR._sum); break;
case "mean" :
case "average" : node.inputs[| 0].setValue(STAT_OPERATOR._average); break;
case "median" : node.inputs[| 0].setValue(STAT_OPERATOR._median); break;
case "min" : node.inputs[| 0].setValue(STAT_OPERATOR._min); break;
case "max" : node.inputs[| 0].setValue(STAT_OPERATOR._max); break;
}
return node;
}
2023-02-28 09:43:01 +01:00
function Node_Statistic(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2022-01-26 06:57:34 +01:00
name = "Statistic";
w = 96;
2023-01-17 08:11:55 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2022-01-26 06:57:34 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [
2023-02-14 05:32:32 +01:00
"Sum", "Mean", "Median", "Max", "Min"])
.rejectArray();
2022-01-26 06:57:34 +01:00
2023-08-05 14:00:33 +02:00
setIsDynamicInput(1);
2022-01-26 06:57:34 +01:00
2023-10-07 09:09:18 +02:00
static createNewInput = function() { #region
2022-01-26 06:57:34 +01:00
var index = ds_list_size(inputs);
2023-02-14 05:32:32 +01:00
inputs[| index] = nodeValue("Input", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, -1 )
2023-10-07 09:09:18 +02:00
.setVisible(false, true);
} #endregion
2022-12-21 02:30:23 +01:00
if(!LOADING && !APPENDING) createNewInput();
2022-01-26 06:57:34 +01:00
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Statistic", self, JUNCTION_CONNECT.output, VALUE_TYPE.float, -1);
2022-01-26 06:57:34 +01:00
2023-10-07 09:09:18 +02:00
static refreshDynamicInput = function() { #region
2022-01-26 06:57:34 +01:00
var _l = ds_list_create();
for( var i = 0; i < input_fix_len; i++ ) {
_l[| i] = inputs[| i];
}
for( var i = input_fix_len; i < ds_list_size(inputs); i++ ) {
if(inputs[| i].value_from) {
ds_list_add(_l, inputs[| i]);
2023-10-07 09:09:18 +02:00
inputs[| i].setVisible(true, true);
2022-01-26 06:57:34 +01:00
} else {
delete inputs[| i];
}
}
for( var i = 0; i < ds_list_size(_l); i++ ) {
_l[| i].index = i;
}
ds_list_destroy(inputs);
inputs = _l;
createNewInput();
2023-10-07 09:09:18 +02:00
} #endregion
2022-01-26 06:57:34 +01:00
2023-10-07 09:09:18 +02:00
static onValueFromUpdate = function(index) { #region
2023-01-09 03:14:20 +01:00
if(LOADING || APPENDING) return;
refreshDynamicInput();
2023-10-07 09:09:18 +02:00
} #endregion
2023-01-09 03:14:20 +01:00
2023-10-09 16:07:33 +02:00
static update = function(frame = CURRENT_FRAME) { #region
var type = getInputData(0);
2022-01-26 06:57:34 +01:00
var res = 0;
2023-10-07 09:09:18 +02:00
if(ds_list_size(inputs) - 1 == input_fix_len) {
outputs[| 0].setValue(0);
return;
}
2022-01-26 06:57:34 +01:00
switch(type) {
case STAT_OPERATOR._sum :
for( var i = input_fix_len; i < ds_list_size(inputs) - 1; i++ ) {
var val = getInputData(i);
2022-12-21 02:30:23 +01:00
if(is_array(val)) {
for( var j = 0; j < array_length(val); j++ )
res += val[j];
} else
res += val;
2022-01-26 06:57:34 +01:00
}
break;
case STAT_OPERATOR._average :
2022-12-21 02:30:23 +01:00
if(ds_list_size(inputs) <= input_fix_len + 1) {
res = 0;
break;
}
var amo = 0;
for( var i = input_fix_len; i < ds_list_size(inputs) - 1; i++ ) {
var val = getInputData(i);
2022-12-21 02:30:23 +01:00
if(is_array(val)) {
for( var j = 0; j < array_length(val); j++ ) {
res += val[j];
amo++;
}
} else {
2022-01-26 06:57:34 +01:00
res += val;
2022-12-21 02:30:23 +01:00
amo++;
2022-01-26 06:57:34 +01:00
}
}
2022-12-21 02:30:23 +01:00
res /= amo;
2022-01-26 06:57:34 +01:00
break;
case STAT_OPERATOR._median :
2022-12-21 02:30:23 +01:00
if(ds_list_size(inputs) - 1 - input_fix_len == 0) {
2022-01-26 06:57:34 +01:00
res = 0;
break;
}
2022-12-21 02:30:23 +01:00
var vals = [];
var amo = 0;
2022-01-26 06:57:34 +01:00
for( var i = input_fix_len; i < ds_list_size(inputs) - 1; i++ ) {
var val = getInputData(i);
2022-12-21 02:30:23 +01:00
if(is_array(val)) {
for( var j = 0; j < array_length(val); j++ ) {
array_push(vals, val[j]);
amo++;
}
} else {
array_push(vals, val);
amo++;
}
2022-01-26 06:57:34 +01:00
}
2022-12-21 02:30:23 +01:00
if(amo == 1) {
2022-01-26 06:57:34 +01:00
res = vals[0];
break;
}
array_sort(vals, true);
2022-12-21 02:30:23 +01:00
if(amo % 2 == 0)
res = (vals[amo / 2 - 1] + vals[amo / 2]) / 2;
2022-01-26 06:57:34 +01:00
else
2022-12-21 02:30:23 +01:00
res = vals[(amo - 1) / 2];
2022-01-26 06:57:34 +01:00
break;
case STAT_OPERATOR._min :
var _min = 9999999999;
for( var i = input_fix_len; i < ds_list_size(inputs) - 1; i++ ) {
var val = getInputData(i);
2022-12-21 02:30:23 +01:00
if(is_array(val)) {
for( var j = 0; j < array_length(val); j++ )
_min = min(_min, val[j]);
} else
_min = min(_min, val);
2022-01-26 06:57:34 +01:00
}
res = _min;
break;
case STAT_OPERATOR._max :
var _max = -9999999999;
2023-05-22 20:31:55 +02:00
2022-01-26 06:57:34 +01:00
for( var i = input_fix_len; i < ds_list_size(inputs) - 1; i++ ) {
var val = getInputData(i);
2022-12-21 02:30:23 +01:00
if(is_array(val)) {
for( var j = 0; j < array_length(val); j++ )
_max = max(_max, val[j]);
} else
_max = max(_max, val);
2022-01-26 06:57:34 +01:00
}
res = _max;
break;
}
outputs[| 0].setValue(res);
2023-10-07 09:09:18 +02:00
} #endregion
2022-01-26 06:57:34 +01:00
2023-10-07 09:09:18 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
2022-01-26 06:57:34 +01:00
var str = "";
switch(getInputData(0)) {
2022-01-26 06:57:34 +01:00
case STAT_OPERATOR._average : str = "Avg"; break;
case STAT_OPERATOR._sum : str = "Sum"; break;
case STAT_OPERATOR._median : str = "Med"; break;
case STAT_OPERATOR._min : str = "Min"; break;
case STAT_OPERATOR._max : str = "Max"; break;
}
2023-01-04 02:30:04 +01:00
var bbox = drawGetBbox(xx, yy, _s);
var ss = string_scale(str, bbox.w, bbox.h);
draw_text_transformed(bbox.xc, bbox.yc, str, ss, ss, 0);
2023-10-07 09:09:18 +02:00
} #endregion
2022-01-26 06:57:34 +01:00
}