add input spec
s-ol
3 years ago
11 | 11 | for k, v in pairs require 'api' |
12 | 12 | _G[k] = v |
13 | 13 | |
14 | for event in *{'keypressed', 'keyreleased', 'mousepressed', 'mousereleased'} | |
15 | love[event] = (...) -> INPUT[event] INPUT, ... | |
16 | ||
17 | love.draw = SESSION\frame | |
18 | ||
19 | -- Move = (obj, x, y) -> | |
20 | -- if COMMIT | |
21 | -- obj.x = x | |
22 | -- obj.y = y | |
23 | -- return | |
24 | -- | |
25 | -- lg.setColor orng | |
26 | -- lg.line x + obj.w/2, y + obj.h/2, obj\center! | |
27 | -- obj = obj\clone! | |
28 | -- obj.x = x | |
29 | -- obj.y = y | |
30 | -- obj\draw 'line', orng | |
31 | -- | |
32 | -- Add = (obj) -> | |
33 | -- if COMMIT | |
34 | -- table.insert OBJS, obj | |
35 | -- return | |
36 | -- | |
37 | -- obj\draw 'line', gren | |
38 | -- | |
39 | -- class Object | |
40 | -- new: (@name, @x, @y, @w=60, @h=30) => | |
41 | -- | |
42 | -- center: => | |
43 | -- @x + @w/2, @y + @h/2 | |
44 | -- | |
45 | -- draw: (mode, color) => | |
46 | -- rect @x, @y, @w, @h, :mode, :color | |
47 | -- | |
48 | -- clone: => | |
49 | -- Object "", @x, @y, @w, @h | |
50 | -- | |
51 | -- hit: => | |
52 | -- mx, my = lm.getPosition! | |
53 | -- (lm.justDown!) and @x < mx and @x + @w > mx and @y < my and @y + @h > my | |
54 | ||
55 | -- OBJS = { | |
56 | -- Object 'led-1', 50, 50 | |
57 | -- Object 'led-2', 50, 150 | |
58 | -- Object 'led-3', 150, 50 | |
59 | -- Object 'led-3', 250, 150 | |
60 | -- Object 'btn', 150, 150, 40, 40 | |
61 | -- } | |
14 | love.draw = -> | |
15 | SESSION\frame! | |
16 | INPUT\frame! | |
17 | love.keypressed = INPUT\keypressed | |
18 | love.keyreleased = INPUT\keyreleased | |
19 | love.mousemoved = INPUT\mousemoved | |
20 | love.mousepressed = INPUT\mousepressed | |
21 | love.mousereleased = INPUT\mousereleased |
4 | 4 | @last_keys = {} |
5 | 5 | @keys = {} |
6 | 6 | |
7 | @mouse = vec2 love.mouse.getPosition! | |
8 | @last_mouse = @mouse\clone! | |
7 | @last_mouse = vec2! | |
8 | @mouse = @last_mouse\clone! | |
9 | 9 | |
10 | mouse_down: (button=1) => @key_down "mouse-#{button}" | |
11 | mouse_up: (button=1) => @key_up "mouse-#{button}" | |
12 | mouse_held: (button=1) => @key_held "mouse-#{button}" | |
13 | ||
14 | mouse_event: (button=1) => | |
15 | return 'down' if @mouse_down button | |
16 | return 'up' if @mouse_up button | |
17 | return 'held' if @mouse_held button | |
10 | mouse_down: (button=1) => @key_down "mouse-#{button}" | |
11 | mouse_up: (button=1) => @key_up "mouse-#{button}" | |
12 | mouse_held: (button=1) => @key_held "mouse-#{button}" | |
13 | mouse_event: (button=1) => @key_event "mouse-#{button}" | |
18 | 14 | |
19 | 15 | key_down: (key) => @keys[key] and not @last_keys[key] |
20 | 16 | key_up: (key) => not @keys[key] and @last_keys[key] |
21 | 17 | key_held: (key) => @keys[key] |
18 | key_event: (key) => | |
19 | return 'down' if @key_down key | |
20 | return 'up' if @key_up key | |
21 | return 'held' if @key_held key | |
22 | 22 | |
23 | 23 | mouse_pos: => @mouse |
24 | 24 | mouse_delta: => @mouse - @last_mouse |
26 | 26 | keypressed: (key) => @keys[key] = true |
27 | 27 | keyreleased: (key) => @keys[key] = nil |
28 | 28 | |
29 | mousemoved: (x, y) => @mouse.x, @mouse.y = x, y | |
29 | 30 | mousepressed: (_, _, button) => @keys["mouse-#{button}"] = true |
30 | 31 | mousereleased: (_, _, button) => @keys["mouse-#{button}"] = nil |
31 | 32 | |
32 | end_frame: => | |
33 | frame: => | |
33 | 34 | @last_mouse = @mouse |
34 | @mouse = vec2 love.mouse.getPosition! | |
35 | @mouse = @mouse\clone! | |
35 | 36 | |
36 | 37 | @last_keys = @keys |
37 | 38 | @keys = {k,v for k,v in pairs @keys} |
0 | import Input from require 'input' | |
1 | import vec2 from require 'cpml' | |
2 | ||
3 | describe "Input", -> | |
4 | input = Input! | |
5 | ||
6 | it "sane initial state", -> | |
7 | assert.is_equal vec2!, input\mouse_delta! | |
8 | assert.is_falsy input\mouse_down! | |
9 | assert.is_falsy input\mouse_up! | |
10 | assert.is_falsy input\mouse_held! | |
11 | assert.is_falsy input\mouse_event! | |
12 | assert.is_falsy input\key_down 'w' | |
13 | assert.is_falsy input\key_up 'w' | |
14 | assert.is_falsy input\key_held 'w' | |
15 | assert.is_falsy input\key_event 'w' | |
16 | ||
17 | it "processes input events", -> | |
18 | input\mousemoved 0, 0 | |
19 | input\frame! | |
20 | input\mousemoved 20, 20 | |
21 | input\keypressed 'w' | |
22 | input\keypressed 'a' | |
23 | input\mousepressed 21, 21, 1 | |
24 | input\mousepressed 21, 21, 2 | |
25 | ||
26 | it "updates accordingly", -> | |
27 | assert.is_equal (vec2 20, 20), input.mouse | |
28 | assert.is_equal (vec2 20, 20), input\mouse_pos! | |
29 | assert.is_equal (vec2 20, 20), input\mouse_delta! | |
30 | ||
31 | assert.is_true input\mouse_down! | |
32 | assert.is_true input\mouse_held! | |
33 | assert.is_falsy input\mouse_up! | |
34 | assert.is_equal 'down', input\mouse_event! | |
35 | ||
36 | assert.is_true input\mouse_down 2 | |
37 | assert.is_true input\mouse_held 2 | |
38 | assert.is_falsy input\mouse_up 2 | |
39 | assert.is_equal 'down', input\mouse_event 2 | |
40 | ||
41 | assert.is_true input\key_down 'w' | |
42 | assert.is_true input\key_held 'w' | |
43 | assert.is_falsy input\key_up 'w' | |
44 | assert.is_equal 'down', input\key_event 'w' | |
45 | ||
46 | it "keep state after end-of-frame", -> | |
47 | input\frame! | |
48 | ||
49 | assert.is_equal (vec2 20, 20), input.mouse | |
50 | assert.is_equal (vec2 20, 20), input\mouse_pos! | |
51 | assert.is_equal vec2!, input\mouse_delta! | |
52 | ||
53 | assert.is_true input\mouse_held! | |
54 | assert.is_falsy input\mouse_down! | |
55 | assert.is_falsy input\mouse_up! | |
56 | assert.is_equal 'held', input\mouse_event! | |
57 | ||
58 | assert.is_true input\mouse_held 2 | |
59 | assert.is_falsy input\mouse_down 2 | |
60 | assert.is_falsy input\mouse_up 2 | |
61 | assert.is_equal 'held', input\mouse_event 2 | |
62 | ||
63 | assert.is_true input\key_held 'w' | |
64 | assert.is_falsy input\key_down 'w' | |
65 | assert.is_falsy input\key_up 'w' | |
66 | assert.is_equal 'held', input\key_event 'w' | |
67 | ||
68 | it "keeps updating", -> | |
69 | input\keyreleased 'a' | |
70 | input\mousereleased 21, 21, 2 | |
71 | input\mousemoved 0, 40 | |
72 | ||
73 | assert.is_equal (vec2 0, 40), input.mouse | |
74 | assert.is_equal (vec2 0, 40), input\mouse_pos! | |
75 | assert.is_equal (vec2 -20, 20), input\mouse_delta! | |
76 | ||
77 | assert.is_true input\mouse_held! | |
78 | assert.is_falsy input\mouse_down! | |
79 | assert.is_falsy input\mouse_up! | |
80 | assert.is_equal 'held', input\mouse_event! | |
81 | ||
82 | assert.is_true input\mouse_up 2 | |
83 | assert.is_falsy input\mouse_held 2 | |
84 | assert.is_falsy input\mouse_down 2 | |
85 | assert.is_equal 'up', input\mouse_event 2 | |
86 | ||
87 | assert.is_true input\key_held 'w' | |
88 | assert.is_falsy input\key_down 'w' | |
89 | assert.is_falsy input\key_up 'w' | |
90 | assert.is_equal 'held', input\key_event 'w' | |
91 | ||
92 | assert.is_true input\key_up 'a' | |
93 | assert.is_falsy input\key_held 'a' | |
94 | assert.is_falsy input\key_down 'a' | |
95 | assert.is_equal 'up', input\key_event 'a' |