Pixel-Composer/scripts/node_math/node_math.gml

244 lines
6.9 KiB
Plaintext
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
}
2023-02-28 09:43:01 +01:00
function Node_create_Math(_x, _y, _group = noone, _param = "") {
2022-12-13 09:20:36 +01:00
var node = new Node_Math(_x, _y, _group);
switch(_param) {
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;
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;
case "modulo" : node.inputs[| 0].setValue(MATH_OPERATOR.modulo); break;
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
case "lerp" : node.inputs[| 0].setValue(MATH_OPERATOR.lerp); break;
case "abs" : node.inputs[| 0].setValue(MATH_OPERATOR.abs); break;
}
2022-12-13 09:20:36 +01:00
//ds_list_add(PANEL_GRAPH.nodes_list, node);
2022-01-13 05:24:03 +01:00
return node;
}
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;
2022-01-13 05:24:03 +01:00
previewable = false;
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)
.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;
static _eval = function(a, b, c = 0) {
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;
}
2022-12-10 05:06:01 +01:00
static step = function() {
var mode = inputs[| 0].getValue();
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 = inputs[| 4].getValue();
if(int) outputs[| 0].type = VALUE_TYPE.integer;
else outputs[| 0].type = VALUE_TYPE.float;
break;
default:
inputs[| 4].setVisible(false);
2023-03-05 07:16:44 +01:00
outputs[| 0].type = 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-01-01 02:06:02 +01:00
}
2023-02-14 05:32:32 +01:00
function evalArray(a, b, c = 0) {
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;
}
function update(frame = ANIMATOR.current_frame) {
use_mod = inputs[| 0].getValue();
var a = inputs[| 1].getValue();
var b = inputs[| 2].getValue();
use_deg = inputs[| 3].getValue();
var c = inputs[| 5].getValue();
var val = evalArray(a, b, c);
2022-09-27 06:37:28 +02:00
outputs[| 0].setValue(val);
2022-01-13 05:24:03 +01:00
}
2023-03-05 07:16:44 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2022-11-18 03:20:31 +01:00
draw_set_text(f_h3, fa_center, fa_center, COLORS._main_text);
2022-09-21 06:09:40 +02:00
var str = "";
2022-01-13 05:24:03 +01:00
switch(inputs[| 0].getValue()) {
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);
2022-01-13 05:24:03 +01:00
}
}