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

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_eventsObject

(internal) initialize mouse events



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
148
149
150
# File 'lib/dxopal/input.rb', line 123

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_eventsObject

(internal) initialize touch events



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
231
232
233
# File 'lib/dxopal/input.rb', line 188

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_tickObject

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_keysObject



12
# File 'lib/dxopal/input.rb', line 12

def self._pressing_keys; @@pressing_keys; end

._update_touch_infoObject



235
236
237
238
# File 'lib/dxopal/input.rb', line 235

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


70
71
72
# File 'lib/dxopal/input.rb', line 70

def self.key_release?(code)
  return `#{@@pressing_keys}[code] == -(#{@@tick}-1)`
end

.keyevent_targetObject

Return DOM element set by ‘keyevent_target=`



113
114
115
116
# File 'lib/dxopal/input.rb', line 113

def self.keyevent_target
  return nil unless class_variable_defined?(:@@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

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/dxopal/input.rb', line 166

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

Returns:

  • (Boolean)


172
173
174
175
# File 'lib/dxopal/input.rb', line 172

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

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/dxopal/input.rb', line 178

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_xObject Also known as: mouse_pos_x

Return position of mouse cursor (0, 0) is the top-left corner of the canvas



154
155
156
# File 'lib/dxopal/input.rb', line 154

def self.mouse_x
  return `#{@@mouse_info}.x`
end

.mouse_yObject Also known as: mouse_pos_y



157
158
159
# File 'lib/dxopal/input.rb', line 157

def self.mouse_y
  return `#{@@mouse_info}.y`
end

.new_touchesObject

Returns newly created touches



312
313
314
315
316
# File 'lib/dxopal/input.rb', line 312

def self.new_touches
  ret = @@new_touches
  @@new_touches = []
  ret
end

.touch_down?Boolean

Return true if the touch is being pressed

Returns:

  • (Boolean)


258
259
260
# File 'lib/dxopal/input.rb', line 258

def self.touch_down?
  return `#{@@pressing_touch}[0] > 0`
end

.touch_push?Boolean

Return true if the touch is pressed in the last tick

Returns:

  • (Boolean)


263
264
265
# File 'lib/dxopal/input.rb', line 263

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

Returns:

  • (Boolean)


268
269
270
# File 'lib/dxopal/input.rb', line 268

def self.touch_release?
  return `#{@@pressing_touch}[0] == -(#{@@tick}-1)`
end

.touch_xObject Also known as: touch_pos_x

Return position of touch (0, 0) is the top-left corner of the canvas



246
247
248
# File 'lib/dxopal/input.rb', line 246

def self.touch_x
  return `#{@@touch_info}.x`
end

.touch_yObject Also known as: touch_pos_y



249
250
251
# File 'lib/dxopal/input.rb', line 249

def self.touch_y
  return `#{@@touch_info}.y`
end

.touchesObject

Returns current touches as an array of Input::Touch



307
308
309
# File 'lib/dxopal/input.rb', line 307

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