aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-11-19 21:00:28 +0000
committers-ol <s-ol@users.noreply.github.com>2019-11-19 21:24:14 +0000
commit219ff2d72f959a61de79c1180150e5eb6e46bda8 (patch)
treeee2e93aa8bae41f3ad43c5ec25a234b71714f534
parentupdate conf.lua (diff)
downloadwatch-cad-219ff2d72f959a61de79c1180150e5eb6e46bda8.tar.gz
watch-cad-219ff2d72f959a61de79c1180150e5eb6e46bda8.zip
add keyboard capturing to input
-rw-r--r--input.moon32
-rw-r--r--spec/input_spec.moon29
2 files changed, 57 insertions, 4 deletions
diff --git a/input.moon b/input.moon
index fe23fa3..13aa7fe 100644
--- a/input.moon
+++ b/input.moon
@@ -1,6 +1,8 @@
+utf8 = require 'utf8'
import vec2 from require 'cpml'
class Input
+ NON_TEXT_KEYS = {k,true for k in *{'escape', 'return', 'clear'}}
new: =>
@last_keys = {}
@keys = {}
@@ -8,10 +10,12 @@ class Input
@last_mouse = vec2!
@mouse = @last_mouse\clone!
+ @text = nil
+
mouse_down: (button=1) => @key_down "mouse-#{button}"
mouse_up: (button=1) => @key_up "mouse-#{button}"
mouse_held: (button=1) => @key_held "mouse-#{button}"
- mouse_event: (button=1) => @key_event "mouse-#{button}"
+ mouse_event: (button=1) => (@key_event "mouse-#{button}") or 'hover'
key_down: (key) => @keys[key] and not @last_keys[key]
key_up: (key) => not @keys[key] and @last_keys[key]
@@ -20,13 +24,35 @@ class Input
return 'down' if @key_down key
return 'up' if @key_up key
return 'held' if @key_held key
- return 'hover'
+
+ text_capture: =>
+ assert not @text, "keyboard already captured!"
+ @text = ''
+
+ text_release: =>
+ with @text
+ @text = nil
mouse_pos: => @mouse
mouse_delta: => @mouse - @last_mouse
- keypressed: (key) => @keys[key] = true
keyreleased: (key) => @keys[key] = nil
+ keypressed: (key) =>
+ if @text and not NON_TEXT_KEYS[key]
+ @textinput nil, key
+ return
+
+ @keys[key] = true
+
+ textinput: (str, key) =>
+ return unless @text
+ if str
+ @text ..= str
+ else
+ switch key
+ when 'backspace'
+ offset = utf8.offset @text, -1
+ @text = string.sub @text, 1, offset - 1 if offset
mousemoved: (x, y) => @mouse.x, @mouse.y = x, y
mousepressed: (_, _, button) => @keys["mouse-#{button}"] = true
diff --git a/spec/input_spec.moon b/spec/input_spec.moon
index 0828871..81f2d73 100644
--- a/spec/input_spec.moon
+++ b/spec/input_spec.moon
@@ -9,7 +9,7 @@ describe "Input", ->
assert.is_falsy input\mouse_down!
assert.is_falsy input\mouse_up!
assert.is_falsy input\mouse_held!
- assert.is_falsy input\mouse_event!
+ assert.is_equal 'hover', input\mouse_event!
assert.is_falsy input\key_down 'w'
assert.is_falsy input\key_up 'w'
assert.is_falsy input\key_held 'w'
@@ -94,3 +94,30 @@ describe "Input", ->
assert.is_falsy input\key_held 'a'
assert.is_falsy input\key_down 'a'
assert.is_equal 'up', input\key_event 'a'
+
+ it "allows capturing the keyboard", ->
+ input\text_capture!
+ assert.has_error -> input\text_capture!
+
+ input\textinput 'Helloo'
+ input\keypressed 'backspace'
+ assert.is_falsy input\key_down 'backspace'
+ input\frame!
+
+ assert.is_equal 'Hello', input.text
+ input\textinput ' World'
+ assert.is_equal 'Hello World', input.text
+ assert.is_falsy input\key_down 'backspace'
+
+ it "doesn't capture meta-keys (return and escape)", ->
+ input\keypressed 'return'
+ input\keypressed 'escape'
+
+ assert.is_true input\key_down 'return'
+ assert.is_true input\key_down 'escape'
+
+ it "returns the input' text after releasing the keyboard", ->
+ assert.is_equal 'Hello World', input\text_release!
+
+ input\keypressed 'backspace'
+ assert.is_true input\key_down 'backspace'