diff --git a/gamedata/scavengers/unitdef_post.lua b/gamedata/scavengers/unitdef_post.lua index 5e2a801fd..555f25417 100644 --- a/gamedata/scavengers/unitdef_post.lua +++ b/gamedata/scavengers/unitdef_post.lua @@ -6,8 +6,8 @@ for name,uDef in pairs(UnitDefs) do end local rana = math.random(3,1000) -local ranb = math.random(2,a) -local ranc = math.random(1,b) +-- local ranb = math.random(2,a) +-- local ranc = math.random(1,b) local maxdamagemult = 1 --0.5 local maxvelocitymult = 1 --0.9 diff --git a/luarules/gadgets.lua b/luarules/gadgets.lua index a85b54b8c..763635041 100644 --- a/luarules/gadgets.lua +++ b/luarules/gadgets.lua @@ -1821,9 +1821,9 @@ end -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -function gadgetHandler:KeyPress(key, mods, isRepeat, label, unicode) +function gadgetHandler:KeyPress(key, mods, isRepeat, label, unicode, scanCode) for _,g in ipairs(self.KeyPressList) do - if (g:KeyPress(key, mods, isRepeat, label, unicode)) then + if (g:KeyPress(key, mods, isRepeat, label, unicode, scanCode)) then return true end end @@ -1831,9 +1831,9 @@ function gadgetHandler:KeyPress(key, mods, isRepeat, label, unicode) end -function gadgetHandler:KeyRelease(key, mods, label, unicode) +function gadgetHandler:KeyRelease(key, mods, label, unicode, scanCode) for _,g in ipairs(self.KeyReleaseList) do - if (g:KeyRelease(key, mods, label, unicode)) then + if (g:KeyRelease(key, mods, label, unicode, scanCode)) then return true end end diff --git a/luaui/actions.lua b/luaui/actions.lua new file mode 100644 index 000000000..82c284829 --- /dev/null +++ b/luaui/actions.lua @@ -0,0 +1,313 @@ +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- +-- file: actions.lua +-- brief: action interface for text commands, and bound commands +-- author: Dave Rodgers +-- +-- Copyright (C) 2007. +-- Licensed under the terms of the GNU GPL, v2 or later. +-- +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +local actionHandler = { + textActions = {}, + keyPressActions = {}, + keyRepeatActions = {}, + keyReleaseActions = {}, + syncActions = {} +} + +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +local function ParseTypes(types, def) + if (type(types) ~= "string") then + types = def + end + local text = (string.find(types, "t") ~= nil) + local keyPress = (string.find(types, "p") ~= nil) + local keyRepeat = (string.find(types, "R") ~= nil) + local keyRelease = (string.find(types, "r") ~= nil) + return text, keyPress, keyRepeat, keyRelease +end + +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- +-- Insertions +-- + +local function InsertCallInfo(callInfoList, widget, func, data) + local layer = widget.whInfo.layer + local index = 1 + for i,ci in ipairs(callInfoList) do + local w = ci[1] + if (w == widget) then + return false -- already in the table + end + if (layer >= w.whInfo.layer) then + index = i + 1 + end + end + table.insert(callInfoList, index, { widget, func, data }) + return true +end + + +function actionHandler:AddAction(widget, cmd, func, data, types) + local function add(actionMap) + local callInfoList = actionMap[cmd] + if (callInfoList == nil) then + callInfoList = {} + actionMap[cmd] = callInfoList + end + return InsertCallInfo(callInfoList, widget, func, data) + end + + -- make sure that this is a fully initialized widget + if (not widget.whInfo) then + error("LuaUI error adding action: please use widget:Initialize()") + end + + -- default to text and keyPress (not repeat or releases) + local text, keyPress, keyRepeat, keyRelease = ParseTypes(types, "tpRr") + + local tSuccess, pSuccess, RSuccess, rSuccess = false, false, false, false + + if (text) then tSuccess = add(self.textActions) end + if (keyPress) then pSuccess = add(self.keyPressActions) end + if (keyRepeat) then RSuccess = add(self.keyRepeatActions) end + if (keyRelease) then rSuccess = add(self.keyReleaseActions) end + + return tSuccess, pSuccess, RSuccess, rSuccess +end + + +local function AddMapAction(map, widget, cmd, func, data) + local callInfoList = map[cmd] + if (callInfoList == nil) then + callInfoList = {} + map[cmd] = callInfoList + end + return InsertCallInfo(callInfoList, widget, func, data) +end + + +function actionHandler:AddSyncAction(widget, cmd, func, data) + return AddMapAction(self.syncActions, widget, cmd, func, data) +end + + + +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- +-- Removals +-- + +local function RemoveCallInfo(callInfoList, widget) + local count = 0 + for i,callInfo in ipairs(callInfoList) do + local w = callInfo[1] + if (w == widget) then + table.remove(callInfoList, i) + count = count + 1 + -- break + end + end + return count +end + + +function actionHandler:RemoveAction(widget, cmd, types) + local function remove(actionMap) + local callInfoList = actionMap[cmd] + if (callInfoList == nil) then + return false + end + local count = RemoveCallInfo(callInfoList, widget) + if (#callInfoList <= 0) then + actionMap[cmd] = nil + end + return (count > 0) + end + + -- default to removing all + local text, keyPress, keyRepeat, keyRelease = ParseTypes(types, "tpRr") + + local tSuccess, pSuccess, RSuccess, rSuccess = false, false, false, false + + if (text) then tSuccess = remove(self.textActions) end + if (keyPress) then pSuccess = remove(self.keyPressActions) end + if (keyRepeat) then RSuccess = remove(self.keyRepeatActions) end + if (keyRelease) then rSuccess = remove(self.keyReleaseActions) end + + return tSuccess, pSuccess, RSuccess, rSuccess +end + + +local function RemoveMapAction(map, widget, cmd) + local callInfoList = map[cmd] + if (callInfoList == nil) then + return false + end + local count = RemoveCallInfo(callInfoList, widget) + if (#callInfoList <= 0) then + map[cmd] = nil + end + return (count > 0) +end + + +function actionHandler:RemoveSyncAction(widget, cmd) + return RemoveMapAction(self.syncActions, widget, cmd) +end + + +function actionHandler:RemoveWidgetActions(widget) + local function clearActionList(actionMap) + for cmd, callInfoList in pairs(actionMap) do + RemoveCallInfo(callInfoList, widget) + end + end + clearActionList(self.textActions) + clearActionList(self.keyPressActions) + clearActionList(self.keyRepeatActions) + clearActionList(self.keyReleaseActions) +end + + +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- +-- Calls +-- + +local function MakeWords(line) + local words = {} + for w in string.gmatch(line, "[^%s]+") do + table.insert(words, w) + end + return words +end + + +local function MakeKeySetString(key, mods, getSymbol) + if key == nil then return "" end + + getSymbol = getSymbol or Spring.GetKeySymbol + local keyset = "" + if (mods.alt) then keyset = keyset .. "A+" end + if (mods.ctrl) then keyset = keyset .. "C+" end + if (mods.meta) then keyset = keyset .. "M+" end + if (mods.shift) then keyset = keyset .. "S+" end + local _, defSym = getSymbol(key) + return (keyset .. defSym) +end + + +local function TryAction(actionMap, cmd, optLine, optWords, isRepeat, release) + local callInfoList = actionMap[cmd] + if (callInfoList == nil) then + return false + end + for i,callInfo in ipairs(callInfoList) do + --local widget = callInfo[1] + local func = callInfo[2] + local data = callInfo[3] + if (func(cmd, optLine, optWords, data, isRepeat, release)) then + return true + end + end + return false +end + + +function actionHandler:KeyAction(press, key, mods, isRepeat, scanCode) + local defBinds + local keyset = MakeKeySetString(key, mods, Spring.GetKeySymbol) + + if scanCode then -- engine supports scancodes + local scanset = MakeKeySetString(scanCode, mods, Spring.GetScanSymbol) + defBinds = Spring.GetKeyBindings(keyset, scanset) + else + defBinds = Spring.GetKeyBindings(keyset) + end + + if (defBinds) then + local actionSet + if (press) then + actionSet = isRepeat and self.keyRepeatActions or self.keyPressActions + else + actionSet = self.keyReleaseActions + end + for _,bAction in ipairs(defBinds) do + local bCmd, bOpts = next(bAction, nil) + local words = MakeWords(bOpts) + if (TryAction(actionSet, bCmd, bOpts, words, isRepeat, not press)) then + return true + end + end + end + return false +end + + +function actionHandler:TextAction(line) + local words = MakeWords(line) + local cmd = words[1] + if (cmd == nil) then + return false + end + -- remove the command from the words list and the raw line + table.remove(words, 1) + local _,_,line = string.find(line, "[^%s]+[%s]+(.*)") + if (line == nil) then + line = "" -- no args + end + return TryAction(self.textActions, cmd, line, words, false, nil) +end + + +function actionHandler:RecvFromSynced(...) + local arg1, arg2 = ... + if (type(arg1) == 'string') then + -- a raw sync msg + local callInfoList = self.syncActions[arg1] + if (callInfoList == nil) then + return false + end + + for i,callInfo in ipairs(callInfoList) do + -- local widget = callInfo[1] + local func = callInfo[2] + if (func(...)) then + return true + end + end + return false + end + + if (type(arg1) == 'number') then + -- a proxied chat msg + if (type(arg2) == 'string') then + return GotChatMsg(arg2, arg1) + end + return false + end + + return false -- unknown type +end + + +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +actionHandler.HaveSyncAction = function() return (next(self.syncActions) ~= nil) end + +return actionHandler + +-------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- diff --git a/luaui/tapmain.lua b/luaui/tapmain.lua index 5a96b7dd4..b6de0e763 100644 --- a/luaui/tapmain.lua +++ b/luaui/tapmain.lua @@ -109,12 +109,12 @@ function DrawScreen(vsx, vsy) return widgetHandler:DrawScreen() end -function KeyPress(key, mods, isRepeat, label, unicode) - return widgetHandler:KeyPress(key, mods, isRepeat, label, unicode) +function KeyPress(key, mods, isRepeat, label, unicode, scanCode) + return widgetHandler:KeyPress(key, mods, isRepeat, label, unicode, scanCode) end -function KeyRelease(key, mods, label, unicode) - return widgetHandler:KeyRelease(key, mods, label, unicode) +function KeyRelease(key, mods, label, unicode, scanCode) + return widgetHandler:KeyRelease(key, mods, label, unicode, scanCode) end function MouseMove(x, y, dx, dy, button) diff --git a/luaui/tapwidgets.lua b/luaui/tapwidgets.lua index b27f33c91..89f3310c1 100644 --- a/luaui/tapwidgets.lua +++ b/luaui/tapwidgets.lua @@ -85,7 +85,7 @@ widgetHandler = { allowUserWidgets = true, - actionHandler = include("actions.lua"), + actionHandler = VFS.Include(LUAUI_DIRNAME .. "actions.lua", nil, VFS.ZIP), WG = {}, -- shared table for widgets @@ -1339,7 +1339,7 @@ end -- Keyboard call-ins -- -function widgetHandler:KeyPress(key, mods, isRepeat, label, unicode) +function widgetHandler:KeyPress(key, mods, isRepeat, label, unicode, scanCode) if (self.tweakMode) then local mo = self.mouseOwner if (mo and mo.TweakKeyPress) then @@ -1348,12 +1348,12 @@ function widgetHandler:KeyPress(key, mods, isRepeat, label, unicode) return true end - if (self.actionHandler:KeyAction(true, key, mods, isRepeat)) then + if (self.actionHandler:KeyAction(true, key, mods, isRepeat, scanCode)) then return true end for _,w in ipairs(self.KeyPressList) do - if (w:KeyPress(key, mods, isRepeat, label, unicode)) then + if (w:KeyPress(key, mods, isRepeat, label, unicode, scanCode)) then return true end end @@ -1361,7 +1361,7 @@ function widgetHandler:KeyPress(key, mods, isRepeat, label, unicode) end -function widgetHandler:KeyRelease(key, mods, label, unicode) +function widgetHandler:KeyRelease(key, mods, label, unicode, scanCode) if (self.tweakMode) then local mo = self.mouseOwner if (mo and mo.TweakKeyRelease) then @@ -1373,12 +1373,12 @@ function widgetHandler:KeyRelease(key, mods, label, unicode) return true end - if (self.actionHandler:KeyAction(false, key, mods, false)) then + if (self.actionHandler:KeyAction(false, key, mods, false, scanCode)) then return true end for _,w in ipairs(self.KeyReleaseList) do - if (w:KeyRelease(key, mods, label, unicode)) then + if (w:KeyRelease(key, mods, label, unicode, scanCode)) then return true end end diff --git a/mocaccino/SDK/LuaUnsyncedRead.lua b/mocaccino/SDK/LuaUnsyncedRead.lua index 5f8db287f..b8b3d57a3 100644 --- a/mocaccino/SDK/LuaUnsyncedRead.lua +++ b/mocaccino/SDK/LuaUnsyncedRead.lua @@ -397,8 +397,14 @@ assert(type(key) == "number","Argument key is of invalid type - expected number" return numberMock end -function Spring.GetKeyBindings ( keyset) +function Spring.GetScanSymbol ( key) +assert(type(key) == "number","Argument key is of invalid type - expected number"); +return numberMock + end + +function Spring.GetKeyBindings ( keyset, scanset) assert(type(keyset) == "string","Argument keyset is of invalid type - expected string"); +assert(type(scanset) == "string" or type(scanset) == "nil","Argument scanset is of invalid type - expected string or nil"); return tableMock end diff --git a/mocaccino/SDK/SpringLuaApi.lua b/mocaccino/SDK/SpringLuaApi.lua index e016afd18..735780697 100644 --- a/mocaccino/SDK/SpringLuaApi.lua +++ b/mocaccino/SDK/SpringLuaApi.lua @@ -2003,8 +2003,9 @@ assert(type(key) == "number","Argument key is of invalid type - expected number" return numberMock end -function Spring.GetKeyBindings ( keyset) +function Spring.GetKeyBindings ( keyset, scanset) assert(type(keyset) == "string","Argument keyset is of invalid type - expected string"); +assert(type(scanset) == "string" or type(scanset) == "nil","Argument scanset is of invalid type - expected string or nil"); return tableMock end