Pixel-Composer/scripts/node_math/node_math.gml

256 lines
7.1 KiB
Text
Raw Normal View History

enum MATH_OPERATOR {
add, // 0
subtract, // 1
multiply, // 2
divide, // 3
power, // 4
root, // 5
sin, // 6
cos, // 7
tan, // 8
modulo, // 9
floor, // 10
ceiling, // 11
round, // 12
2023-02-14 05:32:32 +01:00
lerp, // 13
abs, // 14
2024-06-06 09:54:13 +02:00
clamp, // 15
snap, // 16
fract, // 17
}
global.node_math_keys = [ "add", "subtract", "multiply", "divide", "power", "root",
"+", "-", "*", "/", "^",
"sin", "cos", "tan", "modulo", "round",
"ceiling", "floor", "lerp", "abs", "fract",
"clamp", "snap" ];
global.node_math_keys_map = [ MATH_OPERATOR.add, MATH_OPERATOR.subtract, MATH_OPERATOR.multiply, MATH_OPERATOR.divide, MATH_OPERATOR.power, MATH_OPERATOR.root,
MATH_OPERATOR.add, MATH_OPERATOR.subtract, MATH_OPERATOR.multiply, MATH_OPERATOR.divide, MATH_OPERATOR.power,
MATH_OPERATOR.sin, MATH_OPERATOR.cos, MATH_OPERATOR.tan, MATH_OPERATOR.modulo, MATH_OPERATOR.round,
MATH_OPERATOR.ceiling, MATH_OPERATOR.floor, MATH_OPERATOR.lerp, MATH_OPERATOR.abs, MATH_OPERATOR.fract,
MATH_OPERATOR.clamp, MATH_OPERATOR.snap ];
global.node_math_names = [ /* 0 - 9*/ "Add", "Subtract", "Multiply", "Divide", "Power", "Root", "Sin", "Cos", "Tan", "Modulo",
/*10 - 20*/ "Floor", "Ceil", "Round", "Lerp", "Abs", "Clamp", "Snap", "Fract" ];
global.node_math_scroll = array_create_ext(array_length(global.node_math_names), function(i) /*=>*/ {return new scrollItem(global.node_math_names[i], s_node_math_operators, i)});
function Node_create_Math(_x, _y, _group = noone, _param = {}) {
var query = struct_try_get(_param, "query", "");
var node = new Node_Math(_x, _y, _group).skipDefault();
var ind = array_find(global.node_math_keys, query);
if(ind != -1) node.inputs[0].setValue(global.node_math_keys_map[ind]);
return node;
}
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
newInput(0, nodeValue_Enum_Scroll("Type", self, 0, global.node_math_scroll))
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Float("a", self, 0))
2022-01-19 03:05:13 +01:00
.setVisible(true, true);
2023-01-01 02:06:02 +01:00
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Float("b", self, 0))
2022-01-19 03:05:13 +01:00
.setVisible(true, true);
2023-01-01 02:06:02 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Bool("Degree angle", self, true));
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Bool("To integer", self, false));
2023-01-01 02:06:02 +01:00
2024-08-18 06:16:20 +02:00
newInput(5, nodeValue_Float("Amount", self, 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
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Result", self, VALUE_TYPE.float, 0));
2023-02-14 05:32:32 +01:00
use_mod = 0;
use_deg = false;
2024-08-14 09:55:53 +02:00
static onValueUpdate = function(index = 0) {
if(index != 0) return;
var _type = inputs[0].getValue();
setDisplayName(global.node_math_names[_type]);
2024-08-14 09:55:53 +02:00
}
2024-08-06 14:04:41 +02:00
static _eval = function(a, b, c = 0) {
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);
2024-06-06 09:54:13 +02:00
case MATH_OPERATOR.clamp : return clamp(a, b, c);
2024-06-12 06:09:56 +02:00
case MATH_OPERATOR.snap : return value_snap(a, b);
case MATH_OPERATOR.fract : return frac(a);
2022-09-27 06:37:28 +02:00
}
return 0;
2024-08-06 14:04:41 +02:00
}
2022-09-27 06:37:28 +02:00
2024-08-06 14:04:41 +02:00
static step = function() {
var mode = getInputData(0);
2022-12-10 05:06:01 +01:00
inputs[2].setVisible(false, false);
inputs[3].setVisible(false, false);
inputs[5].setVisible(false, false);
2022-12-10 05:06:01 +01:00
switch(mode) {
case MATH_OPERATOR.sin :
case MATH_OPERATOR.cos :
case MATH_OPERATOR.tan :
2024-08-08 06:57:51 +02:00
inputs[3].setVisible(true);
2022-12-10 05:06:01 +01:00
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 :
2024-08-08 06:57:51 +02:00
inputs[4].setVisible(true);
2023-01-01 02:06:02 +01:00
var int = getInputData(4);
2024-08-08 06:57:51 +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:
2024-08-08 06:57:51 +02:00
inputs[4].setVisible(false);
2023-03-05 07:16:44 +01:00
2024-08-08 06:57:51 +02:00
outputs[0].setType(VALUE_TYPE.float);
2023-01-01 02:06:02 +01:00
break;
}
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 :
2024-08-08 06:57:51 +02:00
inputs[2].name = "b";
2024-06-06 09:54:13 +02:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(true, true);
2022-01-13 05:24:03 +01:00
break;
2024-06-06 09:54:13 +02:00
case MATH_OPERATOR.sin :
case MATH_OPERATOR.cos :
case MATH_OPERATOR.tan :
2024-08-08 06:57:51 +02:00
inputs[2].name = "Amplitude";
2024-06-06 09:54:13 +02:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(true, true);
2022-09-21 06:09:40 +02:00
break;
2024-06-06 09:54:13 +02:00
2023-02-14 05:32:32 +01:00
case MATH_OPERATOR.lerp :
2024-08-08 06:57:51 +02:00
inputs[2].name = "To";
inputs[5].name = "Amount";
2024-06-06 09:54:13 +02:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(true, true);
inputs[5].setVisible(true, true);
2024-06-06 09:54:13 +02:00
break;
case MATH_OPERATOR.clamp :
2024-08-08 06:57:51 +02:00
inputs[2].name = "Min";
inputs[5].name = "Max";
2024-06-06 09:54:13 +02:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(true, true);
inputs[5].setVisible(true, true);
2023-02-14 05:32:32 +01:00
break;
2024-06-12 06:09:56 +02:00
case MATH_OPERATOR.snap :
2024-08-08 06:57:51 +02:00
inputs[2].name = "Snap";
2024-06-12 06:09:56 +02:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(true, true);
2024-06-12 06:09:56 +02:00
break;
2022-11-01 03:06:03 +01:00
default: return;
2022-01-13 05:24:03 +01:00
}
2024-08-06 14:04:41 +02:00
}
2023-01-01 02:06:02 +01:00
2024-08-06 14:04:41 +02:00
function evalArray(a, b, c = 0) {
2024-06-06 09:54:13 +02:00
var _as = is_array(a);
var _bs = is_array(b);
var _cs = is_array(c);
2023-02-14 05:32:32 +01:00
2024-06-06 09:54:13 +02:00
if(!_as && !_bs && !_cs)
2023-02-14 05:32:32 +01:00
return _eval(a, b, c);
2024-06-06 09:54:13 +02:00
if(!_as) a = [ a ];
if(!_bs) b = [ b ];
if(!_cs) c = [ c ];
2023-02-14 05:32:32 +01:00
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;
2024-08-06 14:04:41 +02:00
}
2023-02-14 05:32:32 +01:00
2024-08-06 14:04:41 +02:00
static update = function(frame = CURRENT_FRAME) {
use_mod = getInputData(0);
2024-06-06 09:54:13 +02:00
use_deg = getInputData(3);
var a = getInputData(1);
var b = getInputData(2);
var c = getInputData(5);
2023-02-14 05:32:32 +01:00
var val = evalArray(a, b, c);
2024-08-08 06:57:51 +02:00
outputs[0].setValue(val);
2024-08-06 14:04:41 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-06 14:04:41 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
2022-09-21 06:09:40 +02:00
var str = "";
var typ = getInputData(0);
switch(typ) {
case MATH_OPERATOR.add : str = "+"; break;
case MATH_OPERATOR.subtract : str = "-"; break;
case MATH_OPERATOR.multiply : str = "*"; break;
case MATH_OPERATOR.divide : str = "/"; break;
default: str = string_lower(global.node_math_names[typ]);
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);
2024-06-06 09:54:13 +02:00
draw_text_transformed(bbox.xc, bbox.yc, str, ss * 0.8, ss * 0.8, 0);
2024-08-06 14:04:41 +02:00
}
2022-01-13 05:24:03 +01:00
}