Module: Input

Defined in:
lib/openrgss/input.rb

Overview

A module that handles input data from a gamepad or keyboard.

Managed by symbols rather than button numbers in RGSS3. (RGSS3)

Constant Summary collapse

Keys =
{
    DOWN:     [SDL::Key::DOWN, SDL::Key::S],
    LEFT:     [SDL::Key::LEFT, SDL::Key::A],
    RIGHT:    [SDL::Key::RIGHT, SDL::Key::D],
    UP:       [SDL::Key::UP, SDL::Key::W],
    A:        [SDL::Key::LSHIFT],
    B:        [SDL::Key::X, SDL::Key::ESCAPE],
    C:        [SDL::Key::Z, SDL::Key::RETURN],
    L:        [SDL::Key::PAGEUP],
    R:        [SDL::Key::PAGEDOWN],
    SHIFT:    [SDL::Key::LSHIFT, SDL::Key::RSHIFT],
    CTRL:     [SDL::Key::LSHIFT, SDL::Key::RSHIFT],
    ALT:      [SDL::Key::LSHIFT, SDL::Key::RSHIFT],
    F5:       [SDL::Key::F5],
    F6:       [SDL::Key::F6],
    F7:       [SDL::Key::F7],
    F8:       [SDL::Key::F8],
    F9:       [SDL::Key::F9],
    SHOW_FPS: [SDL::Key::F2],
    RESET:    [SDL::Key::F12]
}
Entities =
{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.eventsObject

Returns the value of attribute events.



37
38
39
# File 'lib/openrgss/input.rb', line 37

def events
  @events
end

Class Method Details

.dir4Object

Checks the status of the directional buttons, translates the data into a specialized 4-direction input format, and returns the number pad equivalent (2, 4, 6, 8).

If no directional buttons are being pressed (or the equivalent), returns 0.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openrgss/input.rb', line 98

def dir4
  case
  when @status[:DOWN]
    2
  when @status[:LEFT]
    4
  when @status[:RIGHT]
    6
  when @status[:UP]
    8
  else
    0
  end
end

.dir8Object

Checks the status of the directional buttons, translates the data into a specialized 8-direction input format, and returns the number pad equivalent (1, 2, 3, 4, 6, 7, 8, 9).

If no directional buttons are being pressed (or the equivalent), returns 0.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/openrgss/input.rb', line 117

def dir8
  case
  when @status[:DOWN] && @status[:LEFT]
    1
  when @status[:DOWN] && @status[:RIGHT]
    3
  when @status[:DOWN]
    2
  when @status[:UP] && @status[:LEFT]
    7
  when @status[:UP] && @status[:RIGHT]
    9
  when @status[:UP]
    8
  when @status[:LEFT]
    4
  when @status[:RIGHT]
    6
  else
    0
  end
end

.press?(sym) ⇒ Boolean

Determines whether the button corresponding to the symbol sym is currently being pressed.

If the button is being pressed, returns TRUE. If not, returns FALSE.

if Input.press?(:C)
  do_something
end

Returns:

  • (Boolean)


70
71
72
# File 'lib/openrgss/input.rb', line 70

def press?(sym)
  @status[sym]
end

.repeat?(sym) ⇒ Boolean

Determines whether the button corresponding to the symbol sym is currently being pressed again.

Unlike trigger?, takes into account the repeated input of a button being held down continuously.

If the button is being pressed, returns TRUE. If not, returns FALSE.

Returns:

  • (Boolean)


90
91
92
# File 'lib/openrgss/input.rb', line 90

def repeat?(sym)
  @status[sym] and (@status[sym].zero? or (@status[sym] > 10 and (@status[sym] % 4).zero?))
end

.trigger?(sym) ⇒ Boolean

Determines whether the button corresponding to the symbol sym is currently being pressed again.

“Pressed again” is seen as time having passed between the button being not pressed and being pressed.

If the button is being pressed, returns TRUE. If not, returns FALSE.

Returns:

  • (Boolean)


80
81
82
# File 'lib/openrgss/input.rb', line 80

def trigger?(sym)
  @status[sym] and @status[sym].zero?
end

.updateObject

Updates input data. As a general rule, this method is called once per frame.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openrgss/input.rb', line 41

def update
  RGSS.update
  @status.each { |key, value| @status[key] = value.next }
  while event = events.shift
    key = Entities[event.sym]
    Log.debug('key') { event }
    if event.press
      case key
      when :SHOW_FPS
        RGSS.show_fps = !RGSS.show_fps
      when :RESET
        raise RGSSReset
      else
        @status[key] = 0
      end
    else
      @status.delete key
    end
  end
end