From 4e345ac90024d8b5f244d855c0cc2280380427f1 Mon Sep 17 00:00:00 2001 From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54> Date: Tue, 12 Jan 2010 15:27:05 +0000 Subject: [PATCH] convert dialogs to keybind git-svn-id: http://svn.net-core.org/repos/t-engine4@232 51575b47-30f0-44d4-a5cc-537603b46e54 --- game/data/keybinds/actions.lua | 21 ++++++++++ game/engine/Dialog.lua | 9 +++-- game/engine/Key.lua | 8 ++++ game/engine/KeyBind.lua | 10 ++++- game/engine/dialogs/ShowEquipment.lua | 13 ++++--- game/engine/dialogs/ShowInventory.lua | 13 ++++--- game/engine/dialogs/ShowPickupFloor.lua | 13 ++++--- game/engine/dialogs/UseTalents.lua | 39 ++++++++++--------- .../tome/dialogs/LevelupStatsDialog.lua | 15 +++---- .../tome/dialogs/LevelupTalentsDialog.lua | 15 +++---- game/modules/tome/dialogs/Quit.lua | 17 ++++---- 11 files changed, 109 insertions(+), 64 deletions(-) diff --git a/game/data/keybinds/actions.lua b/game/data/keybinds/actions.lua index c41a6e9fdb..ef633059e9 100644 --- a/game/data/keybinds/actions.lua +++ b/game/data/keybinds/actions.lua @@ -52,3 +52,24 @@ defineAction{ group = "actions", name = "Switch graphical modes", } + +defineAction{ + default = { "uni:?" }, + type = "HELP", + group = "actions", + name = "Help", +} + +defineAction{ + default = { "sym:13:false:false:false:false", "sym:271:false:false:false:false" }, + type = "ACCEPT", + group = "actions", + name = "Accept action", +} + +defineAction{ + default = { "sym:27:false:false:false:false" }, + type = "EXIT", + group = "actions", + name = "Exit menu", +} diff --git a/game/engine/Dialog.lua b/game/engine/Dialog.lua index cfe0a97c9e..d4dd056726 100644 --- a/game/engine/Dialog.lua +++ b/game/engine/Dialog.lua @@ -1,6 +1,6 @@ require "engine.class" require "engine.Tiles" -require "engine.KeyCommand" +require "engine.KeyBind" --- Handles dialog windows module(..., package.seeall, class.make) @@ -75,10 +75,11 @@ end function _M:drawDialog(s) end -function _M:keyCommands(t) +function _M:keyCommands(t, b) self.old_key = game.key - game.key = engine.KeyCommand.new() - game.key:addCommands(t) + game.key = engine.KeyBind.new() + if t then game.key:addCommands(t) end + if b then game.key:addBinds(b) end game.key:setCurrent() self.key = game.key end diff --git a/game/engine/Key.lua b/game/engine/Key.lua index 0dc0d1a927..8c1c32faf4 100644 --- a/game/engine/Key.lua +++ b/game/engine/Key.lua @@ -275,3 +275,11 @@ _UNDO = 322 -- Atari keyboard has Undo __DEFAULT = -10000 __TEXTINPUT = -10001 + +-- Reverse sym calc +_M.sym_to_name = {} +for k, e in pairs(_M) do + if type(k) == "string" and type(e) == "number" then + _M.sym_to_name[e] = k + end +end diff --git a/game/engine/KeyBind.lua b/game/engine/KeyBind.lua index 2e192d0c38..b33be97fe1 100644 --- a/game/engine/KeyBind.lua +++ b/game/engine/KeyBind.lua @@ -95,7 +95,15 @@ end -- @param mods a table with the mod keys needed, i.e: {"ctrl", "alt"} -- @param fct the function to call when the key is pressed function _M:addBinds(t) + local later = {} for virtual, fct in pairs(t) do - self:addBind(virtual, fct) + if type(fct) == "function" then + self:addBind(virtual, fct) + else + later[virtual] = fct + end + end + for virtual, fct in pairs(later) do + self:addBind(virtual, self.virtuals[fct]) end end diff --git a/game/engine/dialogs/ShowEquipment.lua b/game/engine/dialogs/ShowEquipment.lua index 06b271cea0..5ebb5d0fdd 100644 --- a/game/engine/dialogs/ShowEquipment.lua +++ b/game/engine/dialogs/ShowEquipment.lua @@ -15,18 +15,19 @@ function _M:init(title, actor, filter, action) self.scroll = 1 self.max = math.floor((self.ih - 5) / self.font_h) - 1 - self:keyCommands{ - _UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, - _DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, - _RETURN = function() self:use() end, - _ESCAPE = function() game:unregisterDialog(self) end, + self:keyCommands({ __TEXTINPUT = function(c) if self.chars[c] then self.sel = self.chars[c] self:use() end end, - } + }, { + MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, + MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, + ACCEPT = function() self:use() end, + EXIT = function() game:unregisterDialog(self) end, + }) self:mouseZones{ { x=2, y=5, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty) self.sel = util.bound(self.scroll + math.floor(ty / self.font_h), 1, #self.list) diff --git a/game/engine/dialogs/ShowInventory.lua b/game/engine/dialogs/ShowInventory.lua index f4f802d885..69a6d0cbaa 100644 --- a/game/engine/dialogs/ShowInventory.lua +++ b/game/engine/dialogs/ShowInventory.lua @@ -15,18 +15,19 @@ function _M:init(title, inven, filter, action) self.scroll = 1 self.max = math.floor((self.ih - 5) / self.font_h) - 1 - self:keyCommands{ - _UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, - _DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, - _RETURN = function() self:use() end, - _ESCAPE = function() game:unregisterDialog(self) end, + self:keyCommands({ __TEXTINPUT = function(c) if c:find("^[a-z]$") then self.sel = util.bound(1 + string.byte(c) - string.byte('a'), 1, #self.list) self:use() end end, - } + },{ + MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, + MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, + ACCEPT = function() self:use() end, + EXIT = function() game:unregisterDialog(self) end, + }) self:mouseZones{ { x=2, y=5, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty) self.sel = util.bound(self.scroll + math.floor(ty / self.font_h), 1, #self.list) diff --git a/game/engine/dialogs/ShowPickupFloor.lua b/game/engine/dialogs/ShowPickupFloor.lua index 4e1f3cc3f3..ed610862ff 100644 --- a/game/engine/dialogs/ShowPickupFloor.lua +++ b/game/engine/dialogs/ShowPickupFloor.lua @@ -15,11 +15,7 @@ function _M:init(title, x, y, filter, action) self.scroll = 1 self.max = math.floor((self.ih - 5) / self.font_h) - 1 - self:keyCommands{ - _UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, - _DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, - _RETURN = function() self:use() end, - _ESCAPE = function() game:unregisterDialog(self) end, + self:keyCommands({ _ASTERISK = function() while self:use() do end end, __TEXTINPUT = function(c) if c:find("^[a-z]$") then @@ -27,7 +23,12 @@ function _M:init(title, x, y, filter, action) self:use() end end, - } + },{ + MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, + MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) end, + ACCEPT = function() self:use() end, + EXIT = function() game:unregisterDialog(self) end, + }) self:mouseZones{ { x=2, y=5, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty) self.sel = util.bound(self.scroll + math.floor(ty / self.font_h), 1, #self.list) diff --git a/game/engine/dialogs/UseTalents.lua b/game/engine/dialogs/UseTalents.lua index 15bfabb3d9..e4f6f6be47 100644 --- a/game/engine/dialogs/UseTalents.lua +++ b/game/engine/dialogs/UseTalents.lua @@ -14,31 +14,32 @@ function _M:init(actor) self.scroll = 1 self.max = math.floor((self.ih - 5) / self.font_h) - 1 - self:keyCommands{ - _1 = function() self:defineHotkey(1) end, - _2 = function() self:defineHotkey(2) end, - _3 = function() self:defineHotkey(3) end, - _4 = function() self:defineHotkey(4) end, - _5 = function() self:defineHotkey(5) end, - _6 = function() self:defineHotkey(6) end, - _7 = function() self:defineHotkey(7) end, - _8 = function() self:defineHotkey(8) end, - _9 = function() self:defineHotkey(9) end, - _0 = function() self:defineHotkey(10) end, - _RIGHTPAREN = function() self:defineHotkey(11) end, - _EQUALS = function() self:defineHotkey(12) end, - - _UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, - _DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, - _RETURN = function() self:use() end, - _ESCAPE = function() game:unregisterDialog(self) end, + self:keyCommands({ __TEXTINPUT = function(c) if c:find("^[a-z]$") then self.sel = util.bound(1 + string.byte(c) - string.byte('a'), 1, #self.list) self:use() end end, - } + },{ + HOTKEY_1 = function() self:defineHotkey(1) end, + HOTKEY_2 = function() self:defineHotkey(2) end, + HOTKEY_3 = function() self:defineHotkey(3) end, + HOTKEY_4 = function() self:defineHotkey(4) end, + HOTKEY_5 = function() self:defineHotkey(5) end, + HOTKEY_6 = function() self:defineHotkey(6) end, + HOTKEY_7 = function() self:defineHotkey(7) end, + HOTKEY_8 = function() self:defineHotkey(8) end, + HOTKEY_9 = function() self:defineHotkey(9) end, + HOTKEY_10 = function() self:defineHotkey(10) end, + HOTKEY_11 = function() self:defineHotkey(11) end, + HOTKEY_12 = function() self:defineHotkey(12) end, + + MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, + MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, + ACCEPT = function() self:use() end, + EXIT = function() game:unregisterDialog(self) end, + }) self:mouseZones{ { x=2, y=5, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty) self.sel = util.bound(self.scroll + math.floor(ty / self.font_h), 1, #self.list) diff --git a/game/modules/tome/dialogs/LevelupStatsDialog.lua b/game/modules/tome/dialogs/LevelupStatsDialog.lua index 746f5b732c..52ba76fd4d 100644 --- a/game/modules/tome/dialogs/LevelupStatsDialog.lua +++ b/game/modules/tome/dialogs/LevelupStatsDialog.lua @@ -11,12 +11,13 @@ function _M:init(actor) self.sel = 1 - self:keyCommands{ - _UP = function() self.changed = true; self.sel = util.boundWrap(self.sel - 1, 1, 6) end, - _DOWN = function() self.changed = true; self.sel = util.boundWrap(self.sel + 1, 1, 6) end, - _LEFT = function() self.changed = true; self:incStat(-1) end, - _RIGHT = function() self.changed = true; self:incStat(1) end, - _ESCAPE = function() + self:keyCommands(nil, { + MOVE_UP = function() self.changed = true; self.sel = util.boundWrap(self.sel - 1, 1, 6) end, + MOVE_DOWN = function() self.changed = true; self.sel = util.boundWrap(self.sel + 1, 1, 6) end, + MOVE_LEFT = function() self.changed = true; self:incStat(-1) end, + MOVE_RIGHT = function() self.changed = true; self:incStat(1) end, + ACCEPT = "EXIT", + EXIT = function() game:unregisterDialog(self) -- if talents to spend, do it now @@ -25,7 +26,7 @@ function _M:init(actor) game:registerDialog(dt) end end, - } + }) self:mouseZones{ { x=2, y=25, w=130, h=self.font_h*6, fct=function(button, x, y, xrel, yrel, tx, ty) self.changed = true diff --git a/game/modules/tome/dialogs/LevelupTalentsDialog.lua b/game/modules/tome/dialogs/LevelupTalentsDialog.lua index d666e97983..5b9350244a 100644 --- a/game/modules/tome/dialogs/LevelupTalentsDialog.lua +++ b/game/modules/tome/dialogs/LevelupTalentsDialog.lua @@ -14,13 +14,14 @@ function _M:init(actor) self.scroll = 1 self.max = math.floor((self.ih - 45) / self.font_h) - 1 - self:keyCommands{ - _UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, - _DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, - _LEFT = function() self:learn(false) self.changed = true end, - _RIGHT = function() self:learn(true) self.changed = true end, - _ESCAPE = function() game:unregisterDialog(self) end, - } + self:keyCommands(nil, { + MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, + MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end, + MOVE_LEFT = function() self:learn(false) self.changed = true end, + MOVE_RIGHT = function() self:learn(true) self.changed = true end, + ACCEPT = "EXIT", + EXIT = function() game:unregisterDialog(self) end, + }) self:mouseZones{ { x=2, y=45, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty) self.changed = true diff --git a/game/modules/tome/dialogs/Quit.lua b/game/modules/tome/dialogs/Quit.lua index 5fbeb63b88..85c201fa37 100644 --- a/game/modules/tome/dialogs/Quit.lua +++ b/game/modules/tome/dialogs/Quit.lua @@ -6,20 +6,21 @@ module(..., package.seeall, class.inherit(engine.Dialog)) function _M:init() engine.Dialog.init(self, "Realy exit ToME?", 300, 100) - self:keyCommands{ - _y = function() + self:keyCommands({ + __DEFAULT = function() + game:unregisterDialog(self) + game.quit_dialog = false + end, + }, { + ACCEPT = function() local save = Savefile.new(game.save_name) save:saveGame(game) save:close() util.showMainMenu() end, - __DEFAULT = function() - game:unregisterDialog(self) - game.quit_dialog = false - end, - } + }) end function _M:drawDialog(s, w, h) - s:drawColorStringCentered(self.font, "Press Y to quit, any other keys to stay", 2, 2, self.iw - 2, self.ih - 2) + s:drawColorStringCentered(self.font, "Press enter to quit, any other keys to stay", 2, 2, self.iw - 2, self.ih - 2) end -- GitLab