Pixel-Composer/scripts/node_logic_operate/node_logic_operate.gml

225 lines
5.6 KiB
Text
Raw Normal View History

2023-02-14 02:48:33 +01:00
enum LOGIC_OPERATOR {
land,
lor,
lnot,
lnand,
lnor,
lxor
}
2023-09-22 17:01:56 +02: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);
2023-02-14 02:48:33 +01:00
2023-09-22 17:01:56 +02:00
switch(query) {
2023-02-14 02:48:33 +01:00
case "and" : node.inputs[| 0].setValue(LOGIC_OPERATOR.land); break;
case "or" : node.inputs[| 0].setValue(LOGIC_OPERATOR.lor); break;
case "not" : node.inputs[| 0].setValue(LOGIC_OPERATOR.lnot); break;
case "nand" : node.inputs[| 0].setValue(LOGIC_OPERATOR.lnand); break;
case "nor" : node.inputs[| 0].setValue(LOGIC_OPERATOR.lnor); break;
case "xor" : node.inputs[| 0].setValue(LOGIC_OPERATOR.lxor); break;
}
return node;
}
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;
previewable = false;
w = 96;
inputs[| 0] = nodeValue("Type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
.setDisplay(VALUE_DISPLAY.enum_scroll, [
/* 0 - 9*/ "And", "Or", "Not", "Nand", "Nor", "Xor" ])
.rejectArray();
inputs[| 1] = nodeValue("a", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false)
.setVisible(true, true);
2023-02-14 02:48:33 +01:00
input_display_list = [
0, 1,
2023-02-14 02:48:33 +01:00
]
outputs[| 0] = nodeValue("Result", self, JUNCTION_CONNECT.output, VALUE_TYPE.boolean, false);
2023-08-05 14:00:33 +02:00
setIsDynamicInput(1);
2023-08-05 14:00:33 +02:00
static createNewInput = function() {
var index = ds_list_size(inputs);
var jname = chr(ord("a") + index - 1);
inputs[| index] = nodeValue(jname, self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false )
.setVisible(true, true);
array_push(input_display_list, index);
return inputs[| index];
}
if!(LOADING || APPENDING)
createNewInput();
static refreshDynamicInput = function() {
var _in = ds_list_create();
for( var i = 0; i < input_fix_len; i++ )
ds_list_add(_in, inputs[| i]);
array_resize(input_display_list, input_display_len);
for( var i = input_fix_len; i < ds_list_size(inputs); i += data_length ) {
if(inputs[| i].value_from) {
ds_list_add(_in, inputs[| i]);
array_push(input_display_list, i);
} else
delete inputs[| i];
}
for( var i = 0; i < ds_list_size(_in); i++ )
_in[| i].index = i;
ds_list_destroy(inputs);
inputs = _in;
createNewInput();
}
static trimInputs = function(amo) {
if(ds_list_size(inputs) < amo + 1) {
while(ds_list_size(inputs) < amo + 1)
createNewInput();
} else {
while(ds_list_size(inputs) > amo + 1)
ds_list_delete(inputs, amo + 1);
}
array_resize(input_display_list, amo + 1);
}
static onValueUpdate = function(index) {
if(index != 0) return;
var mode = getInputData(0);
switch(mode) {
case LOGIC_OPERATOR.lnot :
trimInputs(1);
return;
case LOGIC_OPERATOR.lnand :
case LOGIC_OPERATOR.lnor :
case LOGIC_OPERATOR.lxor :
trimInputs(2);
return;
case LOGIC_OPERATOR.land :
case LOGIC_OPERATOR.lor :
while(ds_list_size(inputs) < 3)
createNewInput();
return;
}
}
static onValueFromUpdate = function(index) {
if(LOADING || APPENDING) return;
var mode = getInputData(0);
switch(mode) {
case LOGIC_OPERATOR.lnot :
trimInputs(1);
return;
case LOGIC_OPERATOR.lnand :
case LOGIC_OPERATOR.lnor :
case LOGIC_OPERATOR.lxor :
trimInputs(2);
return;
}
if(index < input_fix_len) return;
refreshDynamicInput();
}
2023-02-14 02:48:33 +01:00
static _eval = function(mode, a, b) {
switch(mode) {
case LOGIC_OPERATOR.land : return bool(a) && bool(b);
case LOGIC_OPERATOR.lor : return bool(a) || bool(b);
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));
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-09-26 14:35:25 +02:00
static update = function(frame = PROJECT.animator.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;
var to = max(2, ds_list_size(inputs) - 1);
for( var i = 2; i < to; i++ ) {
var b = getInputData(i);
val = evalLogicArray(mode, val, b);
}
}
2023-02-14 02:48:33 +01:00
outputs[| 0].setValue(val);
}
2023-03-05 07:16:44 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-02-14 02:48:33 +01:00
draw_set_text(f_h3, fa_center, fa_center, COLORS._main_text);
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);
}
}