git.s-ol.nu watch-cad / 219ff2d
add keyboard capturing to input s-ol 2 years ago
2 changed file(s) with 57 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
0 utf8 = require 'utf8'
01 import vec2 from require 'cpml'
12
23 class Input
4 NON_TEXT_KEYS = {k,true for k in *{'escape', 'return', 'clear'}}
35 new: =>
46 @last_keys = {}
57 @keys = {}
79 @last_mouse = vec2!
810 @mouse = @last_mouse\clone!
911
12 @text = nil
13
1014 mouse_down: (button=1) => @key_down "mouse-#{button}"
1115 mouse_up: (button=1) => @key_up "mouse-#{button}"
1216 mouse_held: (button=1) => @key_held "mouse-#{button}"
13 mouse_event: (button=1) => @key_event "mouse-#{button}"
17 mouse_event: (button=1) => (@key_event "mouse-#{button}") or 'hover'
1418
1519 key_down: (key) => @keys[key] and not @last_keys[key]
1620 key_up: (key) => not @keys[key] and @last_keys[key]
1923 return 'down' if @key_down key
2024 return 'up' if @key_up key
2125 return 'held' if @key_held key
22 return 'hover'
26
27 text_capture: =>
28 assert not @text, "keyboard already captured!"
29 @text = ''
30
31 text_release: =>
32 with @text
33 @text = nil
2334
2435 mouse_pos: => @mouse
2536 mouse_delta: => @mouse - @last_mouse
2637
27 keypressed: (key) => @keys[key] = true
2838 keyreleased: (key) => @keys[key] = nil
39 keypressed: (key) =>
40 if @text and not NON_TEXT_KEYS[key]
41 @textinput nil, key
42 return
43
44 @keys[key] = true
45
46 textinput: (str, key) =>
47 return unless @text
48 if str
49 @text ..= str
50 else
51 switch key
52 when 'backspace'
53 offset = utf8.offset @text, -1
54 @text = string.sub @text, 1, offset - 1 if offset
2955
3056 mousemoved: (x, y) => @mouse.x, @mouse.y = x, y
3157 mousepressed: (_, _, button) => @keys["mouse-#{button}"] = true
88 assert.is_falsy input\mouse_down!
99 assert.is_falsy input\mouse_up!
1010 assert.is_falsy input\mouse_held!
11 assert.is_falsy input\mouse_event!
11 assert.is_equal 'hover', input\mouse_event!
1212 assert.is_falsy input\key_down 'w'
1313 assert.is_falsy input\key_up 'w'
1414 assert.is_falsy input\key_held 'w'
9393 assert.is_falsy input\key_held 'a'
9494 assert.is_falsy input\key_down 'a'
9595 assert.is_equal 'up', input\key_event 'a'
96
97 it "allows capturing the keyboard", ->
98 input\text_capture!
99 assert.has_error -> input\text_capture!
100
101 input\textinput 'Helloo'
102 input\keypressed 'backspace'
103 assert.is_falsy input\key_down 'backspace'
104 input\frame!
105
106 assert.is_equal 'Hello', input.text
107 input\textinput ' World'
108 assert.is_equal 'Hello World', input.text
109 assert.is_falsy input\key_down 'backspace'
110
111 it "doesn't capture meta-keys (return and escape)", ->
112 input\keypressed 'return'
113 input\keypressed 'escape'
114
115 assert.is_true input\key_down 'return'
116 assert.is_true input\key_down 'escape'
117
118 it "returns the input' text after releasing the keyboard", ->
119 assert.is_equal 'Hello World', input\text_release!
120
121 input\keypressed 'backspace'
122 assert.is_true input\key_down 'backspace'