aboutsummaryrefslogtreecommitdiff
path: root/src/fw/input.odin
blob: 3c1236f99d525a504684790113ef768e20f54eab (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
package fw

import "core:c"
import "core:log"

import "vendor:glfw"

Keyboard_Input :: union {
  Key,
  Mouse_Button,
}

Controller_Input :: union {
}

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

update_keybind :: proc(keybind: ^Keybind) {
  pressed: bool

  switch val in keybind.input {
  case Key:
    pressed = glfw.GetKey(window, c.int(val)) == glfw.PRESS
  case Mouse_Button:
    pressed = glfw.GetMouseButton(window, c.int(val)) == glfw.PRESS
  }

  keybind.just_pressed = !keybind.was_pressed && pressed
  keybind.was_pressed = pressed
}

@(require_results)
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
  case:
    log.warn("Keybind input is invalid")
    return false
  }
}

@(require_results)
is_keybind_just_down :: proc(keybind: Keybind) -> bool {
  return keybind.just_pressed
}

@(require_results)
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
}