Module: DXOpal::Input
- Defined in:
- lib/dxopal/input.rb,
lib/dxopal/input/key_codes.rb
Defined Under Namespace
Modules: KeyCodes, MouseCodes Classes: Touch
Constant Summary collapse
- ON_KEYDOWN_ =
(private) JS keydown event handler
%x{ function(ev){ #{Input._pressing_keys}[ev.code] = #{@@tick}; ev.preventDefault(); ev.stopPropagation(); } }
- ON_KEYUP_ =
(private) JS keyup event handler
%x{ function(ev){ #{Input._pressing_keys}[ev.code] = -#{@@tick}; ev.preventDefault(); ev.stopPropagation(); } }
Class Method Summary collapse
-
._init(canvas) ⇒ Object
Internal setup for Input class.
-
._init_mouse_events ⇒ Object
(internal) initialize mouse events.
-
._init_touch_events ⇒ Object
(internal) initialize touch events.
-
._on_tick ⇒ Object
Called on every frame from Window.
- ._pressing_keys ⇒ Object
- ._update_touch_info ⇒ Object
-
.key_down?(code) ⇒ Boolean
Return true if the key is being pressed.
-
.key_push?(code) ⇒ Boolean
Return true if the key is just pressed.
-
.key_release?(code) ⇒ Boolean
Return true if the key is just released.
-
.keyevent_target ⇒ Object
Return DOM element set by `keyevent_target=`.
-
.keyevent_target=(target) ⇒ Object
Set DOM element to receive keydown/keyup event.
-
.mouse_down?(mouse_code) ⇒ Boolean
Return true if the mouse button is being pressed.
-
.mouse_push?(mouse_code) ⇒ Boolean
Return true if the mouse button is pressed in the last tick.
-
.mouse_release?(mouse_code) ⇒ Boolean
Return true if the mouse button is released in the last tick.
-
.mouse_x ⇒ Object
(also: mouse_pos_x)
Return position of mouse cursor (0, 0) is the top-left corner of the canvas.
- .mouse_y ⇒ Object (also: mouse_pos_y)
-
.new_touches ⇒ Object
Returns newly created touches.
-
.touch_down? ⇒ Boolean
Return true if the touch is being pressed.
-
.touch_push? ⇒ Boolean
Return true if the touch is pressed in the last tick.
-
.touch_release? ⇒ Boolean
Return true if the touch is released in the last tick.
-
.touch_x ⇒ Object
(also: touch_pos_x)
Return position of touch (0, 0) is the top-left corner of the canvas.
- .touch_y ⇒ Object (also: touch_pos_y)
-
.touches ⇒ Object
Returns current touches as an array of Input::Touch.
-
.x(pad_number = 0) ⇒ Object
Return 1 if 'right', -1 if 'left'.
-
.y(pad_number = 0) ⇒ Object
Return 1 if 'down', -1 if 'up'.
Class Method Details
._init(canvas) ⇒ Object
Internal setup for Input class
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dxopal/input.rb', line 15 def self._init(canvas) @@tick = 0 @@pressing_keys = `new Object()` @@mouse_info = `{x: 0, y: 0}` @@pressing_mouse_buttons = `new Object()` @@touch_info = `{x: 0, y: 0}` @@pressing_touch = `new Object()` @@canvas = canvas rect = `canvas.getBoundingClientRect()` @@canvas_x = `rect.left + window.pageXOffset` @@canvas_y = `rect.top + window.pageYOffset` self._init_mouse_events self._init_touch_events self.keyevent_target = `window` unless Input.keyevent_target end |
._init_mouse_events ⇒ Object
(internal) initialize mouse events
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/dxopal/input.rb', line 120 def self._init_mouse_events %x{ document.addEventListener('mousemove', function(ev){ #{@@mouse_info}.x = ev.pageX - #{@@canvas_x}; #{@@mouse_info}.y = ev.pageY - #{@@canvas_y}; }); document.addEventListener('mousedown', function(ev){ #{@@mouse_info}.x = ev.pageX - #{@@canvas_x}; #{@@mouse_info}.y = ev.pageY - #{@@canvas_y}; for (var k=1; k<=16; k<<=1) { if (ev.buttons & k) { #{@@pressing_mouse_buttons}[k] = #{@@tick}; } } }); document.addEventListener('mouseup', function(ev){ #{@@mouse_info}.x = ev.pageX - #{@@canvas_x}; #{@@mouse_info}.y = ev.pageY - #{@@canvas_y}; // ev.button => ev.buttons table = { 0: 1, 1: 4, 2: 2, 3: 8, 4: 16 }; for (var k=1; k<=16; k<<=1) { if (#{@@pressing_mouse_buttons}[k]) { #{@@pressing_mouse_buttons}[table[ev.button]] = -#{@@tick}; } } }); } end |
._init_touch_events ⇒ Object
(internal) initialize touch events
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/dxopal/input.rb', line 185 def self._init_touch_events @@touches = {} @@new_touches = [] %x{ #{@@canvas}.addEventListener('touchmove', function(ev){ ev.preventDefault(); ev.stopPropagation(); #{@@touch_info}.x = ev.changedTouches[0].pageX - #{@@canvas_x}; #{@@touch_info}.y = ev.changedTouches[0].pageY - #{@@canvas_y}; for (var touch of ev.changedTouches) { const id = touch.identifier; const x = touch.pageX - #{@@canvas_x}; const y = touch.pageY - #{@@canvas_y}; #{@@touches[`id`]&._move(`x`, `y`)} } }); #{@@canvas}.addEventListener('touchstart', function(ev){ ev.preventDefault(); ev.stopPropagation(); #{@@touch_info}.x = ev.changedTouches[0].pageX - #{@@canvas_x}; #{@@touch_info}.y = ev.changedTouches[0].pageY - #{@@canvas_y}; #{@@pressing_touch}[0] = #{@@tick}; for (var touch of ev.changedTouches) { const id = touch.identifier; const x = touch.pageX - #{@@canvas_x}; const y = touch.pageY - #{@@canvas_y}; #{ new_touch = Touch.new(`id`, `x`, `y`) @@touches[`id`] = new_touch @@new_touches.push(new_touch) } } }); #{@@canvas}.addEventListener('touchend', function(ev){ ev.preventDefault(); ev.stopPropagation(); #{@@touch_info}.x = ev.changedTouches[0].pageX - #{@@canvas_x}; #{@@touch_info}.y = ev.changedTouches[0].pageY - #{@@canvas_y}; #{@@pressing_touch}[0] = -#{@@tick}; for (var touch of ev.changedTouches) { const id = touch.identifier; #{@@touches[`id`]&._released(@@tick)} } }); } end |
._on_tick ⇒ Object
Called on every frame from Window
34 35 36 37 |
# File 'lib/dxopal/input.rb', line 34 def self._on_tick @@tick += 1 self._update_touch_info end |
._pressing_keys ⇒ Object
12 |
# File 'lib/dxopal/input.rb', line 12 def self._pressing_keys; @@pressing_keys; end |
._update_touch_info ⇒ Object
232 233 234 235 |
# File 'lib/dxopal/input.rb', line 232 def self._update_touch_info # Clear old data @@touches.delete_if{|id, t| t.released? && t._released_at < @@tick-1} end |
.key_down?(code) ⇒ Boolean
Return true if the key is being pressed
60 61 62 |
# File 'lib/dxopal/input.rb', line 60 def self.key_down?(code) return `#{@@pressing_keys}[code] > 0` end |
.key_push?(code) ⇒ Boolean
Return true if the key is just pressed
65 66 67 |
# File 'lib/dxopal/input.rb', line 65 def self.key_push?(code) return `#{@@pressing_keys}[code] == #{@@tick}-1` end |
.key_release?(code) ⇒ Boolean
Return true if the key is just released
70 71 72 |
# File 'lib/dxopal/input.rb', line 70 def self.key_release?(code) return `#{@@pressing_keys}[code] == -(#{@@tick}-1)` end |
.keyevent_target ⇒ Object
Return DOM element set by `keyevent_target=`
113 |
# File 'lib/dxopal/input.rb', line 113 def self.keyevent_target; @@keyevent_target; end |
.keyevent_target=(target) ⇒ Object
Set DOM element to receive keydown/keyup event
By default, `window` is set to this (i.e. all key events are stolen by DXOpal.) If canvas element is set to this, only key events happend on canvas are processed by DXOpal.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dxopal/input.rb', line 95 def self.keyevent_target=(target) if @@keyevent_target %x{ #{@@keyevent_target}.removeEventListener('keydown', #{ON_KEYDOWN_}); #{@@keyevent_target}.removeEventListener('keyup', #{ON_KEYUP_}); } end @@keyevent_target = target %x{ if (#{@@keyevent_target}.tagName == "CANVAS") { #{@@keyevent_target}.setAttribute('tabindex', 0); } #{@@keyevent_target}.addEventListener('keydown', #{ON_KEYDOWN_}); #{@@keyevent_target}.addEventListener('keyup', #{ON_KEYUP_}); } end |
.mouse_down?(mouse_code) ⇒ Boolean
Return true if the mouse button is being pressed
163 164 165 166 |
# File 'lib/dxopal/input.rb', line 163 def self.mouse_down?(mouse_code) raise "missing argument of `mouse_down?'" unless mouse_code return `#{@@pressing_mouse_buttons}[mouse_code] > 0` end |
.mouse_push?(mouse_code) ⇒ Boolean
Return true if the mouse button is pressed in the last tick
169 170 171 172 |
# File 'lib/dxopal/input.rb', line 169 def self.mouse_push?(mouse_code) raise "missing argument of `mouse_push?'" unless mouse_code return `#{@@pressing_mouse_buttons}[mouse_code] == -(#{@@tick}-1)` end |
.mouse_release?(mouse_code) ⇒ Boolean
Return true if the mouse button is released in the last tick
175 176 177 178 |
# File 'lib/dxopal/input.rb', line 175 def self.mouse_release?(mouse_code) raise "missing argument of `mouse_release?'" unless mouse_code return `#{@@pressing_mouse_buttons}[mouse_code] == -(#{@@tick}-1)` end |
.mouse_x ⇒ Object Also known as: mouse_pos_x
Return position of mouse cursor (0, 0) is the top-left corner of the canvas
151 152 153 |
# File 'lib/dxopal/input.rb', line 151 def self.mouse_x return `#{@@mouse_info}.x` end |
.mouse_y ⇒ Object Also known as: mouse_pos_y
154 155 156 |
# File 'lib/dxopal/input.rb', line 154 def self.mouse_y return `#{@@mouse_info}.y` end |
.new_touches ⇒ Object
Returns newly created touches
309 310 311 312 313 |
# File 'lib/dxopal/input.rb', line 309 def self.new_touches ret = @@new_touches @@new_touches = [] ret end |
.touch_down? ⇒ Boolean
Return true if the touch is being pressed
255 256 257 |
# File 'lib/dxopal/input.rb', line 255 def self.touch_down? return `#{@@pressing_touch}[0] > 0` end |
.touch_push? ⇒ Boolean
Return true if the touch is pressed in the last tick
260 261 262 |
# File 'lib/dxopal/input.rb', line 260 def self.touch_push? return `#{@@pressing_touch}[0] == -(#{@@tick}-1)` end |
.touch_release? ⇒ Boolean
Return true if the touch is released in the last tick
265 266 267 |
# File 'lib/dxopal/input.rb', line 265 def self.touch_release? return `#{@@pressing_touch}[0] == -(#{@@tick}-1)` end |
.touch_x ⇒ Object Also known as: touch_pos_x
Return position of touch (0, 0) is the top-left corner of the canvas
243 244 245 |
# File 'lib/dxopal/input.rb', line 243 def self.touch_x return `#{@@touch_info}.x` end |
.touch_y ⇒ Object Also known as: touch_pos_y
246 247 248 |
# File 'lib/dxopal/input.rb', line 246 def self.touch_y return `#{@@touch_info}.y` end |
.touches ⇒ Object
Returns current touches as an array of Input::Touch
304 305 306 |
# File 'lib/dxopal/input.rb', line 304 def self.touches @@touches.values end |
.x(pad_number = 0) ⇒ Object
Return 1 if 'right', -1 if 'left'
40 41 42 43 44 45 |
# File 'lib/dxopal/input.rb', line 40 def self.x(pad_number=0) ret = 0 ret += 1 if key_down?(K_RIGHT) ret -= 1 if key_down?(K_LEFT) ret end |
.y(pad_number = 0) ⇒ Object
Return 1 if 'down', -1 if 'up'
48 49 50 51 52 53 |
# File 'lib/dxopal/input.rb', line 48 def self.y(pad_number=0) ret = 0 ret += 1 if key_down?(K_DOWN) ret -= 1 if key_down?(K_UP) ret end |