Module: Ruby2D::Window::GamepadEvents

Included in:
Ruby2D::Window
Defined in:
lib/ruby2d/window/gamepad_events.rb

Overview

Multi-gamepad input handling. Owns the gamepads collection and the callback the C extension calls for every SDL gamepad event. Holds no ambient single-pad state — every event carries the Gamepad it came from, and polling lives on each Gamepad instance.

Defined Under Namespace

Classes: GamepadAxisData, GamepadButtonData, GamepadConnectData

Constant Summary collapse

DEFAULT_GAMEPAD_MAPPINGS_PATH =

Default location for the user's gamepad mappings file. Loaded automatically from show if it exists.

"#{File.expand_path('~')}/.ruby2d/gamepads.txt".freeze

Instance Method Summary collapse

Instance Method Details

#add_gamepad_mapping(path_or_string) ⇒ Object

Load gamepad mappings from a file or string. Smart-parses the argument: if it points at an existing file, loads it via SDL_AddGamepadMappingsFromFile; otherwise treats it as a single mapping string and forwards to SDL_AddGamepadMapping.



41
42
43
44
45
46
47
# File 'lib/ruby2d/window/gamepad_events.rb', line 41

def add_gamepad_mapping(path_or_string)
  if File.file?(path_or_string)
    Ext.window_load_gamepad_mappings_file(self, path_or_string)
  else
    Ext.window_add_gamepad_mapping(self, path_or_string)
  end
end

#clear_gamepad_frame_stateObject



84
85
86
# File 'lib/ruby2d/window/gamepad_events.rb', line 84

def clear_gamepad_frame_state
  @gamepads.each(&:_clear_frame_state)
end

#gamepad_callback(which, type, data, value, name = nil) ⇒ Object

Called from C for every gamepad event. Type is one of: :connect, :disconnect, :button_down, :button_held, :button_up, :axis. data is the button name (button events), axis name (axis events), or nil (connect/disconnect). value is the dead-zone-applied axis value, or nil. name is the gamepad's display name on connect/ disconnect, otherwise nil.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby2d/window/gamepad_events.rb', line 62

def gamepad_callback(which, type, data, value, name = nil)
  case type
  when :connect
    handle_gamepad_connect(which, name)
  when :disconnect
    handle_gamepad_disconnect(which)
  when :button_down
    handle_gamepad_button_down(which, data)
  when :button_held
    handle_gamepad_button_held(which, data)
  when :button_up
    handle_gamepad_button_up(which, data)
  when :axis
    handle_gamepad_axis(which, data, value)
  end
end

#gamepadsObject

Connected gamepads in connect order. Stable: index 0 is the first-connected pad still present.



33
34
35
# File 'lib/ruby2d/window/gamepad_events.rb', line 33

def gamepads
  @gamepads
end

#init_gamepad_event_storesObject



79
80
81
82
# File 'lib/ruby2d/window/gamepad_events.rb', line 79

def init_gamepad_event_stores
  @gamepads = []
  @gamepads_by_id = {}
end

#load_default_gamepad_mappingsObject

Auto-load mappings from the default user file. Called from show.



50
51
52
53
54
# File 'lib/ruby2d/window/gamepad_events.rb', line 50

def load_default_gamepad_mappings
  return unless File.exist?(DEFAULT_GAMEPAD_MAPPINGS_PATH)

  Ext.window_load_gamepad_mappings_file(self, DEFAULT_GAMEPAD_MAPPINGS_PATH)
end