aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Richter <vrld@vrld.org>2012-02-22 23:29:00 +0100
committerMatthias Richter <vrld@vrld.org>2012-02-22 23:29:00 +0100
commitd6ed0f93011a2a877ccea4955d4db071710594f5 (patch)
tree9b6bcc06376d20d5f0854eff7522056a16cd8c9c
parent154c93e0f2ec10fb737e95324747606eb0bb6181 (diff)
downloadQuickie-d6ed0f93011a2a877ccea4955d4db071710594f5.tar.gz
Quickie-d6ed0f93011a2a877ccea4955d4db071710594f5.tar.bz2
Quickie-d6ed0f93011a2a877ccea4955d4db071710594f5.tar.xz
Quickie-d6ed0f93011a2a877ccea4955d4db071710594f5.zip
Replace keyboard.controls() with generic binding scheme.
-rw-r--r--core.lua51
1 files changed, 17 insertions, 34 deletions
diff --git a/core.lua b/core.lua
index 1f22a79..a93ed1d 100644
--- a/core.lua
+++ b/core.lua
@@ -19,7 +19,12 @@ local function hasKeyFocus(id) return context.keyfocus == id end
-- input
local mouse = {x = 0, y = 0, down = false}
-local keyboard = {key = nil, code = -1, ctrl = {down = {key = "tab", code = 9}, up = {key = "tab", code = 0}}}
+local keyboard = {key = nil, code = -1}
+keyboard.cycle = {
+ -- binding = {key = key, modifier1, modifier2, ...} XXX: modifiers are OR-ed!
+ prev = {key = 'tab', 'lshift', 'rshift'},
+ next = {key = 'tab'},
+}
function mouse.inRect(x,y,w,h)
return mouse.x >= x and mouse.x <= x+w and mouse.y >= y and mouse.y <= y+h
@@ -43,44 +48,26 @@ function keyboard.pressed(key, code)
keyboard.code = code
end
-function keyboard.controls(up, down, upCode, downCode)
- keyboard.ctrl.up.key = up
- keyboard.ctrl.down.key = down
- keyboard.ctrl.up.code = upCode or -1
- keyboard.ctrl.down.code = downCode or -1
-end
-
-
function keyboard.tryGrab(id)
if not context.keyfocus then
context.keyfocus = id
end
end
+function keyboard.isBindingDown(bind)
+ local modifiersDown = #bind == 0 or love.keyboard.isDown(unpack(bind))
+ return keyboard.key == bind.key and modifiersDown
+end
+
local function makeTabable(id)
keyboard.tryGrab(id)
if hasKeyFocus(id) then
- if keyboard.ctrl.up.code ~= -1 then
- if keyboard.key == keyboard.ctrl.up.key and keyboard.code == keyboard.ctrl.up.code then
- setKeyFocus(context.lastwidget)
- keyboard.key = nil
- end
- else
- if keyboard.key == keyboard.ctrl.up.key then
- setKeyFocus(context.lastwidget)
- keyboard.key = nil
- end
- end
- if keyboard.ctrl.down.code ~= -1 then
- if keyboard.key == keyboard.ctrl.down.key and keyboard.code == keyboard.ctrl.down.code then
- setKeyFocus(nil)
- keyboard.key = nil
- end
- else
- if keyboard.key == keyboard.ctrl.down.key then
- setKeyFocus(nil)
- keyboard.key = nil
- end
+ if keyboard.isBindingDown(keyboard.cycle.prev) then
+ setKeyFocus(context.lastwidget)
+ keyboard.key = nil
+ elseif keyboard.isBindingDown(keyboard.cycle.next) then
+ setKeyFocus(nil)
+ keyboard.key = nil
end
end
context.lastwidget = id
@@ -145,10 +132,6 @@ local function draw()
mouse.x, mouse.y = love.mouse.getPosition()
mouse.down = love.mouse.isDown('l')
- -- clear keyboard focus if nobody wants it
- if keyboard.key == 'tab' then
- context.keyboardfocus = nil
- end
keyboard.key, keyboard.code = nil, -1
end