Refactor ArrayAddNode + combine it with ArrayAddUnique

This commit is contained in:
Moritz Brückner 2020-04-12 17:33:27 +02:00
parent a4dba65a7a
commit 247e0b4c3b
4 changed files with 25 additions and 57 deletions

View file

@ -2,21 +2,38 @@ package armory.logicnode;
class ArrayAddNode extends LogicNode {
var ar: Array<Dynamic>;
public function new(tree: LogicTree) {
super(tree);
}
override function run(from: Int) {
var ar: Array<Dynamic> = inputs[1].get();
ar = inputs[1].get();
if (ar == null) return;
if (inputs.length > 2) {
for (i in 2...inputs.length) {
// "Modify Original" == `false` -> Copy the input array
if (!inputs[3].get()) {
ar = ar.copy();
}
if (inputs.length > 4) {
for (i in 4...inputs.length) {
var value: Dynamic = inputs[i].get();
ar.push(value);
// "Unique Values" options only supports primitive data types
// for now, a custom indexOf() or contains() method would be
// required to compare values of other types
if (!inputs[2].get() || ar.indexOf(value) == -1) {
ar.push(value);
}
}
}
runOutput(0);
}
override function get(from: Int): Dynamic {
return ar;
}
}

View file

@ -1,22 +0,0 @@
package armory.logicnode;
class ArrayAddUniqueNode extends LogicNode {
public function new(tree: LogicTree) {
super(tree);
}
override function run(from: Int) {
var ar: Array<Dynamic> = inputs[1].get();
if (ar == null) return;
if (inputs.length > 2) {
for (i in 2...inputs.length) {
var value: Dynamic = inputs[i].get();
if (ar.indexOf(value) == -1) ar.push(value);
}
}
runOutput(0);
}
}

View file

@ -15,13 +15,16 @@ class ArrayAddNode(Node, ArmLogicTreeNode):
def init(self, context):
self.inputs.new('ArmNodeSocketAction', 'In')
self.inputs.new('ArmNodeSocketArray', 'Array')
self.inputs.new('NodeSocketBool', 'Unique Values')
self.inputs.new('NodeSocketBool', 'Modify Original').default_value = True
self.inputs.new('NodeSocketShader', 'Value')
self.outputs.new('ArmNodeSocketAction', 'Out')
self.outputs.new('ArmNodeSocketArray', 'Array')
def draw_buttons(self, context, layout):
row = layout.row(align=True)
op = row.operator('arm.node_add_input_value', text='New', icon='PLUS', emboss=True)
op = row.operator('arm.node_add_input_value', text='Add Input', icon='PLUS', emboss=True)
op.node_index = str(id(self))
op.socket_type = 'NodeSocketShader'
op2 = row.operator('arm.node_remove_input_value', text='', icon='X', emboss=True)

View file

@ -1,30 +0,0 @@
import bpy
from bpy.props import *
from bpy.types import Node, NodeSocket
from arm.logicnode.arm_nodes import *
class ArrayAddUniqueNode(Node, ArmLogicTreeNode):
'''Array add unique node'''
bl_idname = 'LNArrayAddUniqueNode'
bl_label = 'Array Add Unique'
bl_icon = 'QUESTION'
def __init__(self):
array_nodes[str(id(self))] = self
def init(self, context):
self.inputs.new('ArmNodeSocketAction', 'In')
self.inputs.new('ArmNodeSocketArray', 'Array')
self.inputs.new('NodeSocketShader', 'Value')
self.outputs.new('ArmNodeSocketAction', 'Out')
def draw_buttons(self, context, layout):
row = layout.row(align=True)
op = row.operator('arm.node_add_input_value', text='New', icon='PLUS', emboss=True)
op.node_index = str(id(self))
op.socket_type = 'NodeSocketShader'
op2 = row.operator('arm.node_remove_input_value', text='', icon='X', emboss=True)
op2.node_index = str(id(self))
add_node(ArrayAddUniqueNode, category='Array')