Pixel-Composer/scripts/node_logic/node_logic.gml

175 lines
4.4 KiB
Text
Raw Normal View History

2023-02-14 02:48:33 +01:00
enum LOGIC_OPERATOR {
land,
lor,
lnot,
lnand,
lnor,
lxor
}
2024-02-16 07:44:36 +01:00
#region create
global.node_logic_keys = [ "and", "or", "not", "nand", "nor" , "xor" ];
2023-02-14 02:48:33 +01:00
2024-02-16 07:44:36 +01:00
function Node_create_Logic(_x, _y, _group = noone, _param = {}) {
var query = struct_try_get(_param, "query", "");
var node = new Node_Logic(_x, _y, _group).skipDefault();
2024-02-16 07:44:36 +01:00
var ind = -1;
switch(query) {
default : ind = array_find(global.node_logic_keys, query);
}
2024-08-08 06:57:51 +02:00
if(ind >= 0) node.inputs[0].setValue(ind);
2024-02-16 07:44:36 +01:00
return node;
2023-02-14 02:48:33 +01:00
}
2024-02-16 07:44:36 +01:00
#endregion
2023-02-14 02:48:33 +01:00
2023-02-28 09:43:01 +01:00
function Node_Logic(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-02-14 02:48:33 +01:00
name = "Logic Opr";
color = COLORS.node_blend_number;
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-02-14 02:48:33 +01:00
2024-08-20 10:15:53 +02:00
newInput(0, nodeValue_Enum_Scroll("Type", self, 0, [ new scrollItem("And", s_node_logic, 0),
new scrollItem("Or", s_node_logic, 1),
new scrollItem("Not", s_node_logic, 2),
new scrollItem("Nand", s_node_logic, 3),
new scrollItem("Nor", s_node_logic, 4),
new scrollItem("Xor", s_node_logic, 5), ]))
2023-02-14 02:48:33 +01:00
.rejectArray();
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Result", self, VALUE_TYPE.boolean, false));
2023-02-14 02:48:33 +01:00
2023-08-05 14:00:33 +02:00
static createNewInput = function() {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
var jname = chr(ord("a") + index - 1);
2024-08-18 09:13:41 +02:00
newInput(index, nodeValue_Bool(jname, self, false ))
.setVisible(true, true);
2024-08-08 06:57:51 +02:00
return inputs[index];
2024-05-23 10:59:39 +02:00
} setDynamicInput(1, true, VALUE_TYPE.boolean);
static trimInputs = function(amo) {
2024-08-08 06:57:51 +02:00
if(array_length(inputs) < amo + 1) {
while(array_length(inputs) < amo + 1)
createNewInput();
} else {
2024-08-08 06:57:51 +02:00
while(array_length(inputs) > amo + 1)
array_delete(inputs, amo + 1, 1);
}
}
static onValueUpdate = function(index) {
if(index != 0) return;
var mode = getInputData(0);
switch(mode) {
case LOGIC_OPERATOR.lnot :
trimInputs(1);
2024-05-23 10:59:39 +02:00
auto_input = false;
return;
2024-05-23 10:59:39 +02:00
case LOGIC_OPERATOR.lnand :
case LOGIC_OPERATOR.lnor :
case LOGIC_OPERATOR.lxor :
trimInputs(2);
2024-05-23 10:59:39 +02:00
auto_input = false;
return;
case LOGIC_OPERATOR.land :
case LOGIC_OPERATOR.lor :
2024-05-23 10:59:39 +02:00
auto_input = true;
return;
}
2024-05-23 10:59:39 +02:00
will_setHeight = true;
}
2023-02-14 02:48:33 +01:00
static _eval = function(mode, a, b) {
switch(mode) {
2024-05-23 10:59:39 +02:00
case LOGIC_OPERATOR.land : return bool(a) && bool(b);
case LOGIC_OPERATOR.lor : return bool(a) || bool(b);
2023-02-14 02:48:33 +01:00
case LOGIC_OPERATOR.lnot : return !bool(a);
case LOGIC_OPERATOR.lnand : return !(bool(a) && bool(b));
case LOGIC_OPERATOR.lnor : return !(bool(a) || bool(b));
2024-05-23 10:59:39 +02:00
case LOGIC_OPERATOR.lxor : return bool(a) ^^ bool(b);
2023-02-14 02:48:33 +01:00
}
return false;
}
function evalLogicArray(mode, a, b = false) {
2023-02-14 02:48:33 +01:00
var as = is_array(a);
var bs = is_array(b);
if(!as && !bs)
return _eval(mode, a, b);
var al = as? array_length(a) : 0;
var bl = bs? array_length(b) : 0;
var val = [];
if(!as) a = [ a ];
if(!bs) b = [ b ];
for( var i = 0; i < max(al, bl, cl); i++ )
val[i] = evalLogicArray(mode,
array_safe_get(a, i,, ARRAY_OVERFLOW.loop),
array_safe_get(b, i,, ARRAY_OVERFLOW.loop),
);
return val;
}
2023-10-09 16:07:33 +02:00
static update = function(frame = CURRENT_FRAME) {
var mode = getInputData(0);
var a = getInputData(1);
var val;
switch(mode) {
case LOGIC_OPERATOR.lnot :
val = evalLogicArray(mode, a);
break;
case LOGIC_OPERATOR.lnand :
case LOGIC_OPERATOR.lnor :
case LOGIC_OPERATOR.lxor :
var b = getInputData(2);
val = evalLogicArray(mode, a, b);
break;
case LOGIC_OPERATOR.land :
case LOGIC_OPERATOR.lor :
var val = a;
2024-08-08 06:57:51 +02:00
var to = array_length(inputs);
2024-05-23 10:59:39 +02:00
for( var i = 2; i < to; i++ ) {
var b = getInputData(i);
val = evalLogicArray(mode, val, b);
}
}
2024-08-08 06:57:51 +02:00
outputs[0].setValue(val);
2023-02-14 02:48:33 +01:00
}
2023-03-05 07:16:44 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
draw_set_text(f_sdf, fa_center, fa_center, COLORS._main_text);
2023-02-14 02:48:33 +01:00
var str = "";
switch(getInputData(0)) {
2023-02-14 02:48:33 +01:00
case LOGIC_OPERATOR.land : str = "And"; break;
case LOGIC_OPERATOR.lor : str = "Or"; break;
case LOGIC_OPERATOR.lnot : str = "Not"; break;
case LOGIC_OPERATOR.lnand : str = "Nand"; break;
case LOGIC_OPERATOR.lnor : str = "Nor"; break;
case LOGIC_OPERATOR.lxor : str = "Xor"; break;
default: return;
}
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);
}
}