aboutsummaryrefslogtreecommitdiff
path: root/src/input.lua
blob: 7c625ed878f9b6af9e1e4a9bfe183b7ca34bba22 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
local lk = love.keyboard
local lm = love.mouse

local inputs = {}
local keyEvents = {}
local mouseEvents = {}

---@param triggers table pair like this {type, keycode/name}
function register_input(name, triggers)
  inputs[name] = triggers
end

function input_direction(left, right, up, down)
  return bton(is_input_pressed(right)) - bton(is_input_pressed(left)),
      bton(is_input_pressed(down)) - bton(is_input_pressed(up))
end

function is_input_just_pressed(name)
  local inp = inputs[name]
  local type
  local code

  for _, trig in ipairs(inp) do
    type = trig[1]
    code = trig[2]

    if type == "mouse" then
      if mouseEvents[code] == "Pressed" then return true end
    end
    if type == "key" then
      if keyEvents[code] == "Pressed" then return true end
    end
  end
end

function is_input_just_released(name)
  local inp = inputs[name]
  local type
  local code

  for _, trig in ipairs(inp) do
    type = trig[1]
    code = trig[2]

    if type == "mouse" then
      if mouseEvents[code] == "Released" then return true end
    end
    if type == "key" then
      if keyEvents[code] == "Released" then return true end
    end
  end
end

function is_input_pressed(name)
  local inp = inputs[name]
  local type
  local code

  for _, trig in ipairs(inp) do
    type = trig[1]
    code = trig[2]

    if type == "mouse" then
      if lm.isDown(code) then return true end
    end
    if type == "key" then
      if lk.isDown(code) then return true end
    end
  end
end

function love.keypressed(key)
  keyEvents[key] = "Pressed"
end

function love.keyreleased(key)
  keyEvents[key] = "Released"
end

function love.mousepressed(_, _, btn)
  mouseEvents[btn] = "Pressed"
  local sx, sy = love.mouse.getPosition() --get_mouse_pos()
  im.mousepressed(sx, sy, btn)
end

function love.mousereleased(_, _, btn)
  mouseEvents[btn] = "Released"
  local sx, sy = love.mouse.getPosition() --get_mouse_pos()
  im.mousereleased(sx, sy, btn)
end

function love.mousemoved(_, _, dx, dy)
  local sx, sy = love.mouse.getPosition() --get_mouse_pos()
  -- local scrw, scrh = love.graphics.getDimensions()
  -- local rdx, rdy = dx / scrw * SCR_WIDTH, dy / scrh * SCR_HEIGHT
  im.mousemoved(sx, sy, dx, dy)
end

function love.wheelmoved(...)
  im.wheelmoved(...)
end

function get_mouse_pos()
  local cam = get_active_camera()
  local scrw, scrh = love.graphics.getDimensions()

  local offset_x = (scrw - SCR_WIDTH * WindowScale) / 2
  local offset_y = (scrh - SCR_HEIGHT * WindowScale) / 2

  local mx = (lm.getX() - offset_x) / WindowScale
  local my = (lm.getY() - offset_y) / WindowScale

  mx = mx - SCR_WIDTH / 2
  my = my - SCR_HEIGHT / 2

  mx = mx / cam.zoom
  my = my / cam.zoom

  mx = mx + cam.realx
  my = my + cam.realy

  return mx, my
end

function input_step()
  for key in pairs(keyEvents) do
    keyEvents[key] = nil
  end
  for key in pairs(mouseEvents) do
    mouseEvents[key] = nil
  end
end