From 9ac4325a005c1b5f21e3b4f3f57920ba828e4048 Mon Sep 17 00:00:00 2001 From: E1e5en Date: Fri, 18 Sep 2020 08:37:44 +0300 Subject: [PATCH] =?UTF-8?q?On=20Tap=20Screen=20-=20node=20for=20handling?= =?UTF-8?q?=20touches=20(double,=20triple=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Input parameters: Duration - time during which the number of touches is counted (default value is 0.3 seconds); Interval - the time that must elapse between touches (default value is 0); Repeat - the number of touches that need to be done to execute the event (default value is 2). Output parameters: Done - event for a successful completed action; Fail - an event for a failed action; Tap - event on each touch; Tap Number - how many times the user has already touched the screen; Coords - coordinates of the touch. --- Sources/armory/logicnode/OnTapScreen.hx | 108 ++++++++++++++++++ .../arm/logicnode/input/LN_on_tap_screen.py | 25 ++++ 2 files changed, 133 insertions(+) create mode 100644 Sources/armory/logicnode/OnTapScreen.hx create mode 100644 blender/arm/logicnode/input/LN_on_tap_screen.py diff --git a/Sources/armory/logicnode/OnTapScreen.hx b/Sources/armory/logicnode/OnTapScreen.hx new file mode 100644 index 0000000000..d5b0c2bc39 --- /dev/null +++ b/Sources/armory/logicnode/OnTapScreen.hx @@ -0,0 +1,108 @@ +package armory.logicnode; + +import iron.math.Vec4; + +class OnTapScreen extends LogicNode { + + var duration = 0.3; + var interval = 0.0; + var repeat = 2; + + var timer_run = false; + var timer_duration = 0.0; + var timer_interval = 0.0; + var count_taps = 0; + var coords_last_tap = new Vec4(); + + // New (constructor) + public function new(tree: LogicTree) { + super(tree); + tree.notifyOnUpdate(update); + } + + // Clear var + function clear_var() { + timer_run = false; + count_taps = 0; + timer_duration = 0.0; + timer_interval = 0.0; + } + + // Save vector coords + function save_vector_coord(x: Float, y: Float) { + coords_last_tap.x = x; + coords_last_tap.y = y; + } + + // Update + function update() { + var surface = iron.system.Input.getSurface(); + // In parameters + if (surface.started() == true) { + duration = inputs[0].get(); + interval = inputs[1].get(); + repeat = inputs[2].get(); + } + // Check + if ((repeat <= 0) || (duration <= 0)) { + clear_var(); + return; + } + // timer_duration check + if (timer_run == true) { + timer_duration += iron.system.Time.delta; + if (interval > 0) timer_interval += iron.system.Time.delta; + } + // First Tap, start timer + if ((surface.started() == true) && (count_taps == 0)) { + clear_var(); + count_taps += 1; + save_vector_coord(surface.x, surface.y); + runOutput(2); // action Tap + timer_run = true; + } else { + // Next Taps + if ((surface.started() == true) && (timer_duration < duration)) { + if (interval > 0) { + if (timer_interval >= interval) { + count_taps += 1; + save_vector_coord(surface.x, surface.y); + runOutput(2); // action Tap + timer_interval = 0; + } + } else { + count_taps += 1; + save_vector_coord(surface.x, surface.y); + runOutput(2); // action Tap + } + } + // Time passed + if (timer_duration >= duration) { + // Taps completed + if (count_taps >= repeat) { + save_vector_coord(surface.x, surface.y); + runOutput(0); // action Done + return; + } + clear_var(); + runOutput(1); // action Fail + } else if (count_taps >= repeat) { + // Taps completed + save_vector_coord(surface.x, surface.y); + runOutput(0); // action Done + clear_var(); + } + } + } + + // Get - out + override function get(from: Int): Dynamic { + switch (from) { + // Out value - Tap Number + case 3: return count_taps; + // Out value - Coords last tap + case 4: return coords_last_tap; + } + return null; + } +} \ No newline at end of file diff --git a/blender/arm/logicnode/input/LN_on_tap_screen.py b/blender/arm/logicnode/input/LN_on_tap_screen.py new file mode 100644 index 0000000000..07cdb0c6a6 --- /dev/null +++ b/blender/arm/logicnode/input/LN_on_tap_screen.py @@ -0,0 +1,25 @@ +from arm.logicnode.arm_nodes import * + +# Class OnTapScreen +class OnTapScreen(ArmLogicTreeNode): + """On Tap Screen Node""" + bl_idname = 'LNOnTapScreen' + bl_label = 'On Tap Screen' + arm_version = 1 + + def init(self, context): + super(OnTapScreen, self).init(context) + self.add_input('NodeSocketFloat', 'Duration') + self.inputs[-1].default_value = 0.3 + self.add_input('NodeSocketFloat', 'Interval') + self.inputs[-1].default_value = 0.0 + self.add_input('NodeSocketInt', 'Repeat') + self.inputs[-1].default_value = 2 + self.add_output('ArmNodeSocketAction', 'Done') + self.add_output('ArmNodeSocketAction', 'Fail') + self.add_output('ArmNodeSocketAction', 'Tap') + self.add_output('NodeSocketInt', 'Tap Number') + self.add_output('NodeSocketVector', 'Coords') + +# Add Node +add_node(OnTapScreen, category=PKG_AS_CATEGORY, section='Input') \ No newline at end of file