Pixel-Composer/scripts/node_math/node_math.gml

246 lines
7.2 KiB
Text
Raw Normal View History

enum MATH_OPERATOR {
add,
subtract,
multiply,
divide,
power,
root,
sin,
cos,
tan,
modulo,
floor,
ceiling,
round,
2023-02-14 05:32:32 +01:00
lerp,
abs
}
2024-02-16 07:44:36 +01:00
#region create
global.node_math_keys = [ "add", "subtract", "multiply", "divide", "power", "root", "modulo", "round", "ceiling", "floor", "sin", "cos", "tan", "lerp", "abs" ];
2024-02-16 07:44:36 +01:00
function Node_create_Math(_x, _y, _group = noone, _param = {}) {
var query = struct_try_get(_param, "query", "");
var node = new Node_Math(_x, _y, _group);
switch(query) { #region
2024-05-01 03:40:54 +02:00
case "add" : node.inputs[| 0].setValue(MATH_OPERATOR.add); break;
case "subtract" : node.inputs[| 0].setValue(MATH_OPERATOR.subtract); break;
case "multiply" : node.inputs[| 0].setValue(MATH_OPERATOR.multiply); break;
case "divide" : node.inputs[| 0].setValue(MATH_OPERATOR.divide); break;
case "power" : node.inputs[| 0].setValue(MATH_OPERATOR.power); break;
case "root" : node.inputs[| 0].setValue(MATH_OPERATOR.root); break;
2024-05-01 03:40:54 +02:00
case "sin" : node.inputs[| 0].setValue(MATH_OPERATOR.sin); break;
case "cos" : node.inputs[| 0].setValue(MATH_OPERATOR.cos); break;
case "tan" : node.inputs[| 0].setValue(MATH_OPERATOR.tan); break;
2024-05-01 03:40:54 +02:00
case "modulo" : node.inputs[| 0].setValue(MATH_OPERATOR.modulo); break;
2024-05-01 03:40:54 +02:00
case "floor" : node.inputs[| 0].setValue(MATH_OPERATOR.floor); break;
case "ceiling" : node.inputs[| 0].setValue(MATH_OPERATOR.ceiling); break;
case "round" : node.inputs[| 0].setValue(MATH_OPERATOR.round); break;
2023-02-14 05:32:32 +01:00
2024-05-01 03:40:54 +02:00
case "lerp" : node.inputs[| 0].setValue(MATH_OPERATOR.lerp); break;
case "abs" : node.inputs[| 0].setValue(MATH_OPERATOR.abs); break;
2024-02-16 07:44:36 +01:00
} #endregion
2024-02-16 07:44:36 +01:00
return node;
}
#endregion
2022-01-13 05:24:03 +01:00
2023-02-28 09:43:01 +01:00
function Node_Math(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Math";
2022-11-18 03:20:31 +01:00
color = COLORS.node_blend_number;
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
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)
.setDisplay(VALUE_DISPLAY.enum_scroll, [
/* 0 - 9*/ "Add", "Subtract", "Multiply", "Divide", "Power", "Root", "Sin", "Cos", "Tan", "Modulo",
2023-02-14 05:32:32 +01:00
/*10 - 20*/ "Floor", "Ceil", "Round", "Lerp", "Abs" ])
.rejectArray();
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("a", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0)
2022-01-19 03:05:13 +01:00
.setVisible(true, true);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("b", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0)
2022-01-19 03:05:13 +01:00
.setVisible(true, true);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 3] = nodeValue("Degree angle", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 4] = nodeValue("To integer", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 5] = nodeValue("Amount", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
input_display_list = [
0, 1, 2, 5, 3, 4,
]
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Result", self, JUNCTION_CONNECT.output, VALUE_TYPE.float, 0);
use_mod = 0;
use_deg = false;
2023-10-09 07:36:20 +02:00
static _eval = function(a, b, c = 0) { #region
2023-02-14 05:32:32 +01:00
switch(use_mod) {
case MATH_OPERATOR.add : return a + b;
case MATH_OPERATOR.subtract : return a - b;
case MATH_OPERATOR.multiply : return a * b;
case MATH_OPERATOR.divide : return b == 0? 0 : a / b;
2022-12-16 09:18:09 +01:00
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.power : return power(a, b);
case MATH_OPERATOR.root : return b == 0? 0 : power(a, 1 / b);
2022-09-27 06:37:28 +02:00
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.sin : return sin(use_deg? degtorad(a) : a) * b;
case MATH_OPERATOR.cos : return cos(use_deg? degtorad(a) : a) * b;
case MATH_OPERATOR.tan : return tan(use_deg? degtorad(a) : a) * b;
case MATH_OPERATOR.modulo : return safe_mod(a, b);
2022-09-27 06:37:28 +02:00
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.floor : return floor(a);
case MATH_OPERATOR.ceiling : return ceil(a);
case MATH_OPERATOR.round : return round(a);
2022-12-16 09:18:09 +01:00
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.lerp : return lerp(a, b, c);
case MATH_OPERATOR.abs : return abs(a);
2022-09-27 06:37:28 +02:00
}
return 0;
2023-10-09 07:36:20 +02:00
} #endregion
2022-09-27 06:37:28 +02:00
2023-10-09 07:36:20 +02:00
static step = function() { #region
var mode = getInputData(0);
2022-12-10 05:06:01 +01:00
switch(mode) {
case MATH_OPERATOR.sin :
case MATH_OPERATOR.cos :
case MATH_OPERATOR.tan :
inputs[| 3].setVisible(true);
break;
default:
inputs[| 3].setVisible(false);
break;
}
2023-01-01 02:06:02 +01:00
switch(mode) {
2023-03-05 07:16:44 +01:00
case MATH_OPERATOR.round :
2023-01-01 02:06:02 +01:00
case MATH_OPERATOR.floor :
case MATH_OPERATOR.ceiling :
inputs[| 4].setVisible(true);
var int = getInputData(4);
2023-10-07 16:23:40 +02:00
if(int) outputs[| 0].setType(VALUE_TYPE.integer);
else outputs[| 0].setType(VALUE_TYPE.float);
2023-01-01 02:06:02 +01:00
break;
default:
inputs[| 4].setVisible(false);
2023-03-05 07:16:44 +01:00
2023-10-07 16:23:40 +02:00
outputs[| 0].setType(VALUE_TYPE.float);
2023-01-01 02:06:02 +01:00
break;
}
inputs[| 2].name = "b";
2023-02-14 05:32:32 +01:00
inputs[| 5].setVisible(false);
2022-09-27 06:37:28 +02:00
switch(mode) {
case MATH_OPERATOR.add :
case MATH_OPERATOR.subtract :
case MATH_OPERATOR.multiply :
case MATH_OPERATOR.divide :
case MATH_OPERATOR.power :
case MATH_OPERATOR.root :
case MATH_OPERATOR.modulo :
2022-01-19 06:11:17 +01:00
inputs[| 2].setVisible(true);
2022-01-13 05:24:03 +01:00
break;
case MATH_OPERATOR.sin :
case MATH_OPERATOR.cos :
case MATH_OPERATOR.tan :
2022-09-21 06:09:40 +02:00
inputs[| 2].setVisible(true);
inputs[| 2].name = "Amplitude";
break;
case MATH_OPERATOR.floor :
case MATH_OPERATOR.ceiling :
case MATH_OPERATOR.round :
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.abs :
2022-01-19 06:11:17 +01:00
inputs[| 2].setVisible(false);
2022-01-13 05:24:03 +01:00
break;
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.lerp :
2023-03-02 07:59:14 +01:00
inputs[| 2].setVisible(true);
2023-02-14 05:32:32 +01:00
inputs[| 5].setVisible(true);
break;
2022-11-01 03:06:03 +01:00
default: return;
2022-01-13 05:24:03 +01:00
}
2023-10-09 07:36:20 +02:00
} #endregion
2023-01-01 02:06:02 +01:00
2023-10-09 07:36:20 +02:00
function evalArray(a, b, c = 0) { #region
2023-01-01 02:06:02 +01:00
var as = is_array(a);
var bs = is_array(b);
2023-02-14 05:32:32 +01:00
var cs = is_array(c);
if(!as && !bs && !cs)
return _eval(a, b, c);
if(!as) a = [ a ];
if(!bs) b = [ b ];
if(!cs) c = [ c ];
var al = array_length(a);
var bl = array_length(b);
var cl = array_length(c);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
var amo = max(al, bl, cl);
var val = array_create(amo);
for( var i = 0; i < amo; i++ )
val[i] = evalArray(
array_safe_get(a, i,, ARRAY_OVERFLOW.loop),
array_safe_get(b, i,, ARRAY_OVERFLOW.loop),
array_safe_get(c, i,, ARRAY_OVERFLOW.loop),
);
return val;
2023-10-09 07:36:20 +02:00
} #endregion
2023-02-14 05:32:32 +01:00
2023-10-09 16:07:33 +02:00
static update = function(frame = CURRENT_FRAME) { #region
use_mod = getInputData(0);
var a = getInputData(1);
var b = getInputData(2);
use_deg = getInputData(3);
var c = getInputData(5);
2023-02-14 05:32:32 +01:00
var val = evalArray(a, b, c);
2022-09-27 06:37:28 +02:00
outputs[| 0].setValue(val);
2023-10-09 07:36:20 +02:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-10-09 07:36:20 +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-09-21 06:09:40 +02:00
var str = "";
switch(getInputData(0)) {
case MATH_OPERATOR.add : str = "+"; break;
case MATH_OPERATOR.subtract : str = "-"; break;
case MATH_OPERATOR.multiply : str = "*"; break;
case MATH_OPERATOR.divide : str = "/"; break;
2022-09-21 06:09:40 +02:00
case MATH_OPERATOR.power : str = "pow"; break;
case MATH_OPERATOR.root : str = "root"; break;
case MATH_OPERATOR.sin : str = "sin"; break;
case MATH_OPERATOR.cos : str = "cos"; break;
case MATH_OPERATOR.tan : str = "tan"; break;
case MATH_OPERATOR.modulo : str = "mod"; break;
2022-01-13 05:24:03 +01:00
case MATH_OPERATOR.floor : str = "floor"; break;
case MATH_OPERATOR.ceiling : str = "ceil"; break;
case MATH_OPERATOR.round : str = "round"; break;
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.lerp : str = "lerp"; break;
case MATH_OPERATOR.abs : str = "abs"; break;
2022-11-01 03:06:03 +01:00
default: return;
2022-01-13 05:24:03 +01:00
}
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-09 07:36:20 +02:00
} #endregion
2022-01-13 05:24:03 +01:00
}