Class: XInput

Inherits:
Object
  • Object
show all
Extended by:
FFI::XInput
Includes:
FFI::XInput
Defined in:
lib/ffi-xinput.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ XInput



67
68
69
# File 'lib/ffi-xinput.rb', line 67

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



65
66
67
# File 'lib/ffi-xinput.rb', line 65

def id
  @id
end

Class Method Details

.connected?Boolean



123
124
125
# File 'lib/ffi-xinput.rb', line 123

def self.connected?
  self.state(@id)[:connected]
end

.state(id) ⇒ Object



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
# File 'lib/ffi-xinput.rb', line 83

def self.state(id)
  state = XINPUT_STATE.new
  result = XInputGetState(id, state)
  gamepad = state[:Gamepad]
  
  state = {}
  
  state[:connected] = result == ERROR_SUCCESS
  state[:up] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_UP > 0
  state[:down] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_DOWN > 0
  state[:left] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_LEFT > 0
  state[:right] = gamepad[:wButtons] & XINPUT_GAMEPAD_DPAD_RIGHT > 0
  state[:start] = gamepad[:wButtons] & XINPUT_GAMEPAD_START > 0
  state[:back] = gamepad[:wButtons] & XINPUT_GAMEPAD_BACK > 0
  state[:left_thumb] = gamepad[:wButtons] & XINPUT_GAMEPAD_LEFT_THUMB > 0
  state[:right_thumb] = gamepad[:wButtons] & XINPUT_GAMEPAD_RIGHT_THUMB > 0
  state[:left_shoulder] = gamepad[:wButtons] & XINPUT_GAMEPAD_LEFT_SHOULDER > 0
  state[:right_shoulder] = gamepad[:wButtons] & XINPUT_GAMEPAD_RIGHT_SHOULDER > 0
  state[:a] = gamepad[:wButtons] & XINPUT_GAMEPAD_A > 0
  state[:b] = gamepad[:wButtons] & XINPUT_GAMEPAD_B > 0
  state[:x] = gamepad[:wButtons] & XINPUT_GAMEPAD_X > 0
  state[:y] = gamepad[:wButtons] & XINPUT_GAMEPAD_Y > 0
  state[:left_trigger] = gamepad[:bLeftTrigger].to_f / 0xFF.to_f
  state[:right_trigger] = gamepad[:bRightTrigger].to_f / 0xFF.to_f
  state[:left_thumb_x] = gamepad[:sThumbLX].to_f / 0x8000.to_f
  state[:left_thumb_y] = gamepad[:sThumbLY].to_f / 0x8000.to_f
  state[:right_thumb_x] = gamepad[:sThumbRX].to_f / 0x8000.to_f
  state[:right_thumb_y] = gamepad[:sThumbRY].to_f / 0x8000.to_f
  
  state
end

.vibrate(id, left = 0, right = 0) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/ffi-xinput.rb', line 115

def self.vibrate(id, left = 0, right = 0)
  vibration = XINPUT_VIBRATION.new
  vibration[:wLeftMotorSpeed] = left * 0xFFFF
  vibration[:wRightMotorSpeed] = right * 0xFFFF

  XInputSetState(id, vibration)
end

Instance Method Details

#connected?Boolean



79
80
81
# File 'lib/ffi-xinput.rb', line 79

def connected?
  XInput.state(@id)[:connected]
end

#stateObject



71
72
73
# File 'lib/ffi-xinput.rb', line 71

def state
  XInput.state(@id)
end

#vibrate(left = 0, right = 0) ⇒ Object



75
76
77
# File 'lib/ffi-xinput.rb', line 75

def vibrate(left = 0, right = 0)
  XInput.vibrate(@id, left, right)
end