Module: Input

Included in:
GamePlay::Base
Defined in:
scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb

Overview

Module responsive of giving information about user Inputs

The virtual keys of the Input module are : :A, :B, :X, :Y, :L, :R, :L2, :R2, :L3, :R3, :START, :SELECT, :HOME, :UP, :DOWN, :LEFT, :RIGHT

Constant Summary collapse

Keyboard =

Alias for the Keyboard module

Sf::Keyboard
DEAD_ZONE =

Range giving dead zone of axis

-20..20
NON_TRIGGER_ZONE =

Range outside of which a trigger is considered on an exis

-50..50
AXIS_SENSITIVITY =

Sensitivity in order to take a trigger in account on joystick movement

10
REPEAT_COOLDOWN =

Cooldown delta of Input.repeat?

0.25
REPEAT_SPACE =

Time between each signals of Input.repeat? after cooldown

0.08
Keys =

List of keys the input knows

{
  A: [Sf::Keyboard::C, Sf::Keyboard::Space, Sf::Keyboard::Enter, Sf::Keyboard::C, -1],
  B: [Sf::Keyboard::X, Sf::Keyboard::Backspace, Sf::Keyboard::Escape, Sf::Keyboard::RShift, -2],
  X: [Sf::Keyboard::V, Sf::Keyboard::Num3, Sf::Keyboard::Slash, Sf::Keyboard::V, -3],
  Y: [Sf::Keyboard::B, Sf::Keyboard::Num1, Sf::Keyboard::Quote, Sf::Keyboard::B, -4],
  L: [Sf::Keyboard::F, Sf::Keyboard::F, Sf::Keyboard::LBracket, Sf::Keyboard::F, -5],
  R: [Sf::Keyboard::G, Sf::Keyboard::G, Sf::Keyboard::RBracket, Sf::Keyboard::G, -6],
  L2: [Sf::Keyboard::R, Sf::Keyboard::R, Sf::Keyboard::R, Sf::Keyboard::R, -7],
  R2: [Sf::Keyboard::T, Sf::Keyboard::T, Sf::Keyboard::T, Sf::Keyboard::T, -8],
  L3: [Sf::Keyboard::Num4, Sf::Keyboard::Y, Sf::Keyboard::Y, Sf::Keyboard::Y, -9],
  R3: [Sf::Keyboard::Num5, Sf::Keyboard::U, Sf::Keyboard::U, Sf::Keyboard::U, -10],
  START: [Sf::Keyboard::J, Sf::Keyboard::RControl, Sf::Keyboard::J, Sf::Keyboard::J, -8],
  SELECT: [Sf::Keyboard::H, Sf::Keyboard::LControl, Sf::Keyboard::H, Sf::Keyboard::L, -7],
  HOME: [Sf::Keyboard::M, Sf::Keyboard::LSystem, Sf::Keyboard::RSystem, Sf::Keyboard::M, 255],
  UP: [Sf::Keyboard::Up, Sf::Keyboard::Z, Sf::Keyboard::W, Sf::Keyboard::Numpad8, -13],
  DOWN: [Sf::Keyboard::Down, Sf::Keyboard::S, Sf::Keyboard::S, Sf::Keyboard::Numpad2, -14],
  LEFT: [Sf::Keyboard::Left, Sf::Keyboard::Q, Sf::Keyboard::A, Sf::Keyboard::Numpad4, -15],
  RIGHT: [Sf::Keyboard::Right, Sf::Keyboard::D, Sf::Keyboard::D, Sf::Keyboard::Numpad6, -16]
}
ALIAS_KEYS =

List of key ALIAS

{ up: :UP, down: :DOWN, left: :LEFT, right: :RIGHT, a: :A, b: :B, x: :X, y: :Y, start: :START, select: :SELECT }
AXIS_MAPPING =

List of Axis mapping (axis => key_neg, key_pos)

{
  Sf::Joystick::Z => %i[R2 L2]
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.main_joyInteger

Get the main joystick

Returns:



74
75
76
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 74

def main_joy
  @main_joy
end

.x_axis

Get the X axis



76
77
78
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 76

def x_axis
  @x_axis
end

.x_joy_axis

Get the Joystick X axis



80
81
82
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 80

def x_joy_axis
  @x_joy_axis
end

.y_axis

Get the Y axis



78
79
80
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 78

def y_axis
  @y_axis
end

.y_joy_axis

Get the Joystick Y axis



82
83
84
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 82

def y_joy_axis
  @y_joy_axis
end

Class Method Details

.dir4Integer

Get the 4 direction status

Returns:

  • (Integer)

    2 = down, 4 = left, 6 = right, 8 = up, 0 = none



86
87
88
89
90
91
92
93
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 86

def dir4
  return 6 if press?(:RIGHT)
  return 4 if press?(:LEFT)
  return 2 if press?(:DOWN)
  return 8 if press?(:UP)

  return 0
end

.dir8Integer

Get the 8 direction status

Returns:

  • (Integer)

    see NumPad to know direction



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 97

def dir8
  if press?(:DOWN)
    return 1 if press?(:LEFT)
    return 3 if press?(:RIGHT)

    return 2
  elsif press?(:UP)
    return 7 if press?(:LEFT)
    return 9 if press?(:RIGHT)

    return 8
  end

  return dir4
end

.get_textString?

Get the last entered text

Returns:



115
116
117
118
119
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 115

def get_text
  return nil unless Graphics.focus?

  return @last_text
end

.joy_axis_position(id, axis) ⇒ Integer

Get the axis position of a joystick

Parameters:

Returns:



125
126
127
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 125

def joy_axis_position(id, axis)
  Sf::Joystick.axis_position(id, axis)
end

.press?(key) ⇒ Boolean

Tell if a key is pressed

Parameters:

  • key (Symbol)

    name of the key

Returns:

  • (Boolean)


132
133
134
135
136
137
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 132

def press?(key)
  return false unless Graphics.focus?

  key = ALIAS_KEYS[key] || key unless Keys[key]
  return @current_state[key]
end

.register_events(window)

Register all events in the window

Parameters:



193
194
195
196
197
198
199
200
201
202
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 193

def register_events(window)
  window.on_text_entered = proc { |text| on_text_entered(text) }
  window.on_key_pressed = proc { |key, alt| on_key_down(key, alt) }
  window.on_key_released = proc { |key| on_key_up(key) }
  window.on_joystick_button_pressed = proc { |id, button| on_joystick_button_pressed(id, button) }
  window.on_joystick_button_released = proc { |id, button| on_joystick_button_released(id, button) }
  window.on_joystick_connected = proc { |id| on_joystick_connected(id) }
  window.on_joystick_disconnected = proc { |id| on_joystick_disconnected(id) }
  window.on_joystick_moved = proc { |id, axis, position| on_axis_moved(id, axis, position) }
end

.released?(key) ⇒ Boolean

Tell if a key was released

Parameters:

  • key (Symbol)

    name of the key

Returns:

  • (Boolean)


152
153
154
155
156
157
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 152

def released?(key)
  return false unless Graphics.focus?

  key = ALIAS_KEYS[key] || key unless Keys[key]
  return @last_state[key] && !@current_state[key]
end

.repeat?(key) ⇒ Boolean

Tell if a key is repeated (0.25s then each 0.08s)

Parameters:

  • key (Symbol)

    name of the key

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 162

def repeat?(key)
  return false unless Graphics.focus?

  key = ALIAS_KEYS[key] || key unless Keys[key]
  return false unless @current_state[key]

  # Note: we cannot compare with Graphics.current_time because its updated after the events
  return true if trigger?(key)

  delta = Graphics.current_time - @last_down_times[key]
  return false if delta < REPEAT_COOLDOWN
  return false if @last_down_times[key] > Graphics.current_time

  return true
end

.swap_states

Swap the states (each time input gets updated)



179
180
181
182
183
184
185
186
187
188
189
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 179

def swap_states
  @last_state.merge!(@current_state)

  @last_down_times.each do |key, value|
    next unless repeat?(key)

    delta = Graphics.current_time - value
    @last_down_times[key] = Graphics.current_time - (REPEAT_COOLDOWN - REPEAT_SPACE) if delta >= REPEAT_COOLDOWN
  end
  @last_text = nil
end

.trigger?(key) ⇒ Boolean

Tell if a key was triggered

Parameters:

  • key (Symbol)

    name of the key

Returns:

  • (Boolean)


142
143
144
145
146
147
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Input.rb', line 142

def trigger?(key)
  return false unless Graphics.focus?

  key = ALIAS_KEYS[key] || key unless Keys[key]
  return @current_state[key] && !@last_state[key]
end