Class: Rubygame::Events::JoystickHatMoved

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygame/events/joystick_events.rb

Overview

JoystickHatMoved is an event that occurs when a joystick’s hat switch has changed direction.

A joystick hat switch is a round switch that can be pressed in 8 possible directions: up, down, left, right, or the four diagonal directions. (Some hat switches support extra diagonal directions, but only those 8 directions are supported by Rubygame.)

Constant Summary collapse

@@direction_map =

Mapping direction symbol to horizontal and vertical parts

{
  :up         => [ 0, -1],
  :up_right   => [ 1, -1],
  :right      => [ 1,  0],
  :down_right => [ 1,  1],
  :down       => [ 0,  1],
  :down_left  => [-1,  1],
  :left       => [-1,  0],
  :up_left    => [-1, -1],
  nil         => [ 0,  0]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(joystick_id, hat, direction) ⇒ JoystickHatMoved

Creates a new JoystickHatMoved instance.

joystick_id

an integer identifying which joystick changed. The first joystick is 0.

hat

an integer identifying which hat switch changed. The first hat switch on each joystick is 0.

direction

a symbol telling the direction the hat switch is being pressed. The direction is either nil or one of these 8 symbols:

:up :up_right :right :down_right :down :down_left :left :up_left



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rubygame/events/joystick_events.rb', line 285

def initialize( joystick_id, hat, direction )

  unless joystick_id.kind_of?(Fixnum) and joystick_id >= 0
    raise ArgumentError, "joystick_id must be an integer >= 0"
  end

  @joystick_id = joystick_id

  unless hat.kind_of?(Fixnum) and hat >= 0
    raise ArgumentError, "hat must be an integer >= 0"
  end

  @hat = hat

  unless @@direction_map.keys.include? direction
    raise ArgumentError, 
          "invalid direction '%s'. "%[direction.inspect] +\
          "Check the docs for valid directions."
  end

  @direction = direction

  @horizontal, @vertical = @@direction_map[direction]

end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



247
248
249
# File 'lib/rubygame/events/joystick_events.rb', line 247

def direction
  @direction
end

#hatObject (readonly)

Returns the value of attribute hat.



247
248
249
# File 'lib/rubygame/events/joystick_events.rb', line 247

def hat
  @hat
end

#horizontalObject (readonly)

Returns the value of attribute horizontal.



248
249
250
# File 'lib/rubygame/events/joystick_events.rb', line 248

def horizontal
  @horizontal
end

#joystick_idObject (readonly)

Returns the value of attribute joystick_id.



247
248
249
# File 'lib/rubygame/events/joystick_events.rb', line 247

def joystick_id
  @joystick_id
end

#verticalObject (readonly)

Returns the value of attribute vertical.



248
249
250
# File 'lib/rubygame/events/joystick_events.rb', line 248

def vertical
  @vertical
end

Instance Method Details

#center?Boolean

True if the hat is in the center (not pressed in any direction).

Returns:

  • (Boolean)


314
315
316
# File 'lib/rubygame/events/joystick_events.rb', line 314

def center?
  @direction == nil
end

#down?Boolean

True if the hat is pressed down, down-right, or down-left.

Returns:

  • (Boolean)


335
336
337
# File 'lib/rubygame/events/joystick_events.rb', line 335

def down?
  @vertical == 1
end

#left?Boolean

True if the hat is pressed left, up-left, or down-left.

Returns:

  • (Boolean)


320
321
322
# File 'lib/rubygame/events/joystick_events.rb', line 320

def left?
  @horizontal == -1
end

#right?Boolean

True if the hat is pressed right, up-right, or down-right.

Returns:

  • (Boolean)


325
326
327
# File 'lib/rubygame/events/joystick_events.rb', line 325

def right?
  @horizontal == 1
end

#up?Boolean

True if the hat is pressed up, up-right, or up-left.

Returns:

  • (Boolean)


330
331
332
# File 'lib/rubygame/events/joystick_events.rb', line 330

def up?
  @vertical == -1
end