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