Class: Engine::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/engine/input.rb

Class Method Summary collapse

Class Method Details

._on_key_down(key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/engine/input.rb', line 45

def self._on_key_down(key)
  keys[key] = :down
  if key == GLFW::KEY_ESCAPE
    Engine.close
  end

  if key == GLFW::KEY_BACKSPACE
    Engine::Debugging.breakpoint { binding.pry }
    # Engine.breakpoint { debugger }
  end

  if key == GLFW::KEY_F
    Engine::Window.toggle_full_screen
  end
end

._on_key_up(key) ⇒ Object



61
62
63
# File 'lib/engine/input.rb', line 61

def self._on_key_up(key)
  keys[key] = :up
end

.initObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/engine/input.rb', line 5

def self.init
  @key_callback = GLFW::create_callback(:GLFWkeyfun) do |window, key, scancode, action, mods|
    Input.key_callback(key, action)
  end
  GLFW.SetKeyCallback(Window.window, @key_callback)

  @cursor_pos_callback = GLFW::create_callback(:GLFWcursorposfun) do |window, x, y|
    Input.mouse_callback(x, y)
  end
  GLFW.SetCursorPosCallback(Window.window, @cursor_pos_callback)

  @mouse_button_callback = GLFW::create_callback(:GLFWmousebuttonfun) do |window, button, action, mods|
    Input.mouse_button_callback(button, action)
  end
  GLFW.SetMouseButtonCallback(Window.window, @mouse_button_callback)
end

.key?(key) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/engine/input.rb', line 22

def self.key?(key)
  keys[key] == :down || keys[key] == :held
end

.key_callback(key, action) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/engine/input.rb', line 65

def self.key_callback(key, action)
  if action == GLFW::PRESS
    _on_key_down(key)
  elsif action == GLFW::RELEASE
    _on_key_up(key)
  end
end

.key_down?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/engine/input.rb', line 26

def self.key_down?(key)
  keys[key] == :down
end

.key_up?(key) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/engine/input.rb', line 30

def self.key_up?(key)
  keys[key] == :up
end

.mouse_button_callback(button, action) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/engine/input.rb', line 79

def self.mouse_button_callback(button, action)
  if action == GLFW::PRESS
    keys[button] = :down
  elsif action == GLFW::RELEASE
    keys[button] = :up
  end
end

.mouse_callback(x, y) ⇒ Object



73
74
75
76
77
# File 'lib/engine/input.rb', line 73

def self.mouse_callback(x, y)
  @mouse_pos_updated = true
  @old_mouse_pos = @mouse_pos
  @mouse_pos = Vector[x, y]
end

.mouse_deltaObject



38
39
40
41
42
43
# File 'lib/engine/input.rb', line 38

def self.mouse_delta
  return Vector[0, 0] if @old_mouse_pos.nil?
  return Vector[0, 0] unless @mouse_pos_updated

  @mouse_pos - @old_mouse_pos
end

.mouse_posObject



34
35
36
# File 'lib/engine/input.rb', line 34

def self.mouse_pos
  @mouse_pos
end

.update_key_statesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/engine/input.rb', line 87

def self.update_key_states
  @mouse_pos_updated = false
  keys.each do |key, state|
    if state == :down
      keys[key] = :held
    elsif state == :up
      keys.delete(key)
    end
  end
end