aboutsummaryrefslogtreecommitdiff
path: root/src/fw/input.odin
blob: 2c150178d760465407605197b2b7abc84b556e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package fw

import "core:c"
import "vendor:glfw"

Keyboard_Input :: union {
  Key,
  Mouse_Button,
}

Controller_Input :: union {
}

Keybind :: struct {
  input: Keyboard_Input,
  pressed: bool,
  just_pressed: bool,
}

key_down_last_frame: #sparse [Key]bool
key_just_down: #sparse [Key]bool

mouse_down_last_frame: #sparse [Mouse_Button]bool
mouse_just_down: #sparse [Mouse_Button]bool

@(private)
_update_input :: proc() {
  for key in Key {
    status := glfw.GetKey(window, c.int(key))
    key_pressed := status == glfw.PRESS
    key_just_down[key] = key_pressed && !key_down_last_frame[key]

    key_down_last_frame[key] = key_pressed
  }

  for mb in Mouse_Button {
    status := glfw.GetMouseButton(window, c.int(mb))
    mouse_pressed := status == glfw.PRESS
    mouse_just_down[mb] = mouse_pressed && !mouse_down_last_frame[mb]

    mouse_down_last_frame[mb] = mouse_pressed
  }
}

is_keybind_down :: proc(keybind: Keybind) -> bool {
  switch val in keybind.input {
  case Key:
    return glfw.GetKey(window, c.int(val)) == glfw.PRESS
  case Mouse_Button:
    return glfw.GetMouseButton(window, c.int(val)) == glfw.PRESS
  }

  assert(false)
  return false
}

is_keybind_just_down :: proc(keybind: Keybind) -> bool {
  switch val in keybind.input {
  case Key: return key_just_down[val]
  case Mouse_Button: return mouse_just_down[val]
  }

  assert(false)
  return false
}

get_mouse_pos :: proc() -> (mouse_pos: Vec2) {
  w_width, w_height := glfw.GetWindowSize(window)
  mx, my := glfw.GetCursorPos(window)

  mouse_pos = Vec2{f32(mx), f32(my)}
  mouse_pos /= Vec2{f32(w_width), f32(w_height)}
  mouse_pos *= Vec2{f32(framebuf.color.size.x), f32(framebuf.color.size.y)}
  mouse_pos += renderer.camera_pos
  return
}