Pixel-Composer/scripts/node_statistic/node_statistic.gml

175 lines
4.4 KiB
Plaintext
Raw Normal View History

2022-01-26 06:57:34 +01:00
enum STAT_OPERATOR {
_sum,
_average,
_median,
_max,
_min
}
2024-02-16 07:44:36 +01:00
#region create
global.node_statistic_keys = [ "sum", "mean", "median", "min", "max" ];
array_append(global.node_statistic_keys, [ "average" ]);
2022-01-26 06:57:34 +01:00
2024-02-16 07:44:36 +01: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).skipDefault();
2024-02-16 07:44:36 +01:00
var ind = -1;
switch(query) {
default : ind = array_find(global.node_statistic_keys, query);
}
2024-08-08 06:57:51 +02:00
if(ind >= 0) node.inputs[0].setValue(ind);
2022-01-26 06:57:34 +01:00
2024-02-16 07:44:36 +01:00
return node;
}
#endregion
2022-01-26 06:57:34 +01:00
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";
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-01-17 08:11:55 +01:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Enum_Scroll("Type", self, 0, [ "Sum", "Mean", "Median", "Max", "Min" ]))
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-01-26 06:57:34 +01:00
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 09:13:41 +02:00
newInput(index, nodeValue_Float("Input", self, -1 ))
2023-10-07 09:09:18 +02:00
.setVisible(false, true);
2024-05-23 10:59:39 +02:00
2024-08-08 06:57:51 +02:00
return inputs[index];
2024-05-24 07:44:36 +02:00
}
setDynamicInput(1, true, VALUE_TYPE.float);
2022-01-26 06:57:34 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Statistic", self, VALUE_TYPE.float, -1));
2022-01-26 06:57:34 +01:00
2024-08-14 09:55:53 +02:00
static onValueUpdate = function(index = 0) {
if(index != 0) return;
var _type = inputs[0].getValue();
switch(_type) {
case STAT_OPERATOR._sum : setDisplayName("Sum") break;
case STAT_OPERATOR._average : setDisplayName("Average"); break;
case STAT_OPERATOR._median : setDisplayName("Median"); break;
case STAT_OPERATOR._max : setDisplayName("Max"); break;
case STAT_OPERATOR._min : setDisplayName("Min"); break;
}
}
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;
switch(type) {
case STAT_OPERATOR._sum :
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); 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 :
2024-08-08 06:57:51 +02:00
if(array_length(inputs) <= input_fix_len + 1) {
2022-12-21 02:30:23 +01:00
res = 0;
break;
}
var amo = 0;
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); 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 :
2024-08-08 06:57:51 +02:00
if(array_length(inputs) - 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;
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); 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;
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); 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
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); 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;
}
2024-08-08 06:57:51 +02:00
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
}