Module: DXOpal::Input

Defined in:
opal/dxopal/input.rb

Defined Under Namespace

Modules: MouseCodes

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
# File 'opal/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()`

  rect = `canvas.getBoundingClientRect()`
  @@canvas_x = `rect.left + window.pageXOffset`
  @@canvas_y = `rect.top  + window.pageYOffset`

  self._init_mouse_events
  self.keyevent_target = `window` unless Input.keyevent_target
end

._init_mouse_eventsObject

(internal) initialize mouse events



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'opal/dxopal/input.rb', line 115

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};
      for (var k=1; k<=16; k<<=1) {
        if ((ev.buttons & k) == 0 && #{@@pressing_mouse_buttons}[k]) {
          #{@@pressing_mouse_buttons}[k] = -#{@@tick};
        }
      }
    });
  }
end

._on_tickObject

Called on every frame from Window



30
31
32
# File 'opal/dxopal/input.rb', line 30

def self._on_tick
  @@tick += 1
end

._pressing_keysObject



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

def self._pressing_keys; @@pressing_keys; end

.key_down?(code) ⇒ Boolean

Return true if the key is being pressed

Returns:

  • (Boolean)


55
56
57
# File 'opal/dxopal/input.rb', line 55

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)


60
61
62
# File 'opal/dxopal/input.rb', line 60

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)


65
66
67
# File 'opal/dxopal/input.rb', line 65

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

.keyevent_targetObject

Return DOM element set by `keyevent_target=`



108
# File 'opal/dxopal/input.rb', line 108

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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'opal/dxopal/input.rb', line 90

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)


156
157
158
# File 'opal/dxopal/input.rb', line 156

def self.mouse_down?(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)


161
162
163
# File 'opal/dxopal/input.rb', line 161

def self.mouse_push?(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)


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

def self.mouse_release?(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



144
145
146
# File 'opal/dxopal/input.rb', line 144

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

.mouse_yObject Also known as: mouse_pos_y



147
148
149
# File 'opal/dxopal/input.rb', line 147

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

.x(pad_number = 0) ⇒ Object

Return 1 if 'right', -1 if 'left'



35
36
37
38
39
40
# File 'opal/dxopal/input.rb', line 35

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'



43
44
45
46
47
48
# File 'opal/dxopal/input.rb', line 43

def self.y(pad_number=0)
  ret = 0
  ret += 1 if key_down?(K_DOWN)
  ret -= 1 if key_down?(K_UP)
  ret
end