summaryrefslogtreecommitdiffhomepage
path: root/dragon/controller.rb
diff options
context:
space:
mode:
authorAmir Rajan <[email protected]>2020-05-03 20:32:11 -0500
committerAmir Rajan <[email protected]>2020-05-03 20:32:11 -0500
commitac6a6d33c4131f649957fcbd01ad4aec2f43455f (patch)
tree73c29d80eb2e73ba12f09ef563f409624e84a74d /dragon/controller.rb
parenta3ba88253fe6140abc8c069898b7c2762dbf79ef (diff)
downloaddragonruby-game-toolkit-contrib-ac6a6d33c4131f649957fcbd01ad4aec2f43455f.tar.gz
dragonruby-game-toolkit-contrib-ac6a6d33c4131f649957fcbd01ad4aec2f43455f.zip
Synced with v1.8
Diffstat (limited to 'dragon/controller.rb')
-rw-r--r--dragon/controller.rb122
1 files changed, 122 insertions, 0 deletions
diff --git a/dragon/controller.rb b/dragon/controller.rb
new file mode 100644
index 0000000..f67df63
--- /dev/null
+++ b/dragon/controller.rb
@@ -0,0 +1,122 @@
+# coding: utf-8
+# Copyright 2019 DragonRuby LLC
+# MIT License
+# controller.rb has been released under MIT (*only this file*).
+
+module GTK
+ # @gtk
+ class Controller
+ # Access to keys that have been pressed down.
+ #
+ # @return [Controller::Keys]
+ # @gtk
+ attr_reader :key_down
+
+ # Access to keys that have been released up.
+ #
+ # @return [Controller::Keys]
+ # @gtk
+ attr_reader :key_up
+
+ # Access to keys that have been held down.
+ #
+ # @return [Controller::Keys]
+ # @gtk
+ attr_reader :key_held
+
+ # @gtk
+ attr_accessor :left_analog_x_raw,
+ :left_analog_y_raw,
+ :left_analog_x_perc,
+ :left_analog_y_perc,
+ :right_analog_x_raw,
+ :right_analog_y_raw,
+ :right_analog_x_perc,
+ :right_analog_y_perc
+
+
+ def initialize
+ @key_down = Controller::Keys.new
+ @key_up = Controller::Keys.new
+ @key_held = Controller::Keys.new
+ @left_analog_x_raw = 0
+ @left_analog_y_raw = 0
+ @left_analog_x_perc = 0
+ @left_analog_y_perc = 0
+ @right_analog_x_raw = 0
+ @right_analog_y_raw = 0
+ @right_analog_x_perc = 0
+ @right_analog_y_perc = 0
+ end
+
+ def serialize
+ {
+ key_down: @key_down.serialize,
+ key_held: @key_held.serialize,
+ key_up: @key_up.serialize
+ }
+ end
+
+ # Clear all current key presses.
+ #
+ # @return [void]
+ def clear
+ @key_down.clear
+ @key_up.clear
+ @key_held.clear
+ end
+
+ def up
+ @key_up.up || @key_held.up
+ end
+
+ def down
+ @key_up.down || @key_held.down
+ end
+
+ def left
+ @key_up.left || @key_held.left
+ end
+
+ def right
+ @key_up.right || @key_held.right
+ end
+
+ # Activates a key into the down position.
+ #
+ # @param key [Symbol] The key to press down.
+ #
+ # @return [void]
+ def activate_down(key)
+ key_down.activate(key)
+ key_held.deactivate(key)
+ key_up.deactivate(key)
+ end
+
+ # Activates a key into the held down position.
+ #
+ # @param key [Symbol] The key to hold down.
+ #
+ # @return [void]
+ def activate_held(key)
+ key_down.deactivate(key)
+ key_held.activate(key) unless key_held.send(key)
+ key_up.deactivate(key)
+ end
+
+
+ # Activates a key release into the up position.
+ #
+ # @param key [Symbol] The key release up.
+ #
+ # @return [void]
+ def activate_up(key)
+ key_down.deactivate(key)
+ key_held.deactivate(key)
+ key_up.activate(key)
+ end
+
+ include DirectionalInputHelperMethods
+ end
+end
+