summaryrefslogtreecommitdiffhomepage
path: root/dragon/controller.rb
blob: f67df6349a7a9a107292e5ba585d336a2dffdfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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