File size: 818 Bytes
e378a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
--[[

  KeyMap.lua - Keyboard bindings registrations & reactions

  Initialize a new map of key bindings with a table of `key_code` => `binding`,
  where `binding` can be a function or a list table of functions.

  Invoke the `react` method as often as you like to check and handle any
  bindings that match.
]]--

KeyMap = Polo {
  new = function(bindings)
    return {
      bindings = bindings
    }
  end
}

function KeyMap:init()
  self.ctx = self.ctx or ctx
  self.bindings = self.bindings or {}
end

function KeyMap:react()
  for key, binding in pairs(self.bindings) do
    if ImGui.IsKeyPressed(self.ctx, key) then
      if type(binding) == 'function' then
        binding()
      elseif type(binding) == 'table' then
        for _, f in ipairs(binding) do
          f()
        end
      end
    end
  end
end