Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands/data/gui/scripts/KeyBindMenu.lua @ 7155

Last change on this file since 7155 was 6324, checked in by dafrick, 15 years ago

Improved KeyBindMenu further. This is the final version for the presentation, unless there are some complaints.

  • Property svn:executable set to *
File size: 7.6 KB
Line 
1-- KeyBindMenu.lua
2
3BasicGUI = require("BasicGUI")
4local P = BasicGUI:new() --inherit everything from the gui package
5if _REQUIREDNAME == nil then
6    KeyBindMenu = P
7else
8    _G[_REQUIREDNAME] = P
9end
10
11P.filename = "KeyBindMenu"
12P.layoutString = "KeyBindMenu.layout"
13
14function P:init()
15    commandList = {}
16    table.insert(commandList, "fire 0")
17    table.insert(commandList, "fire 1 | unfire")
18    table.insert(commandList, "onpress fire 2")
19    table.insert(commandList, "scale 1 moveFrontBack")
20    table.insert(commandList, "scale -1 moveFrontBack")
21    table.insert(commandList, "boost")
22    table.insert(commandList, "scale 1 moveRightLeft")
23    table.insert(commandList, "scale -1 moveRightLeft")
24    table.insert(commandList, "scale 1 moveUpDown")
25    table.insert(commandList, "scale -1 moveUpDown")
26    table.insert(commandList, "scale 1 rotateRoll")
27    table.insert(commandList, "scale -1 rotateRoll")
28    table.insert(commandList, "switchCamera")
29    table.insert(commandList, "openConsole")
30    table.insert(commandList, "OverlayGroup toggleVisibility Debug")
31    table.insert(commandList, "OverlayGroup toggleVisibility Stats")
32    table.insert(commandList, "mouseLook")
33    table.insert(commandList, "pause")
34   
35    nameList = {}
36    table.insert(nameList, "Primary Fire")
37    table.insert(nameList, "Secondary Fire")
38    table.insert(nameList, "Fire Rocket")
39    table.insert(nameList, "Accelerate")
40    table.insert(nameList, "Break")
41    table.insert(nameList, "Boost")
42    table.insert(nameList, "Move Right")
43    table.insert(nameList, "Move Left")
44    table.insert(nameList, "Move Up")
45    table.insert(nameList, "Move Down")
46    table.insert(nameList, "Roll Right")
47    table.insert(nameList, "Roll Left")
48    table.insert(nameList, "Switch Camera")
49    table.insert(nameList, "Open Console")
50    table.insert(nameList, "Show Debug")
51    table.insert(nameList, "Show Stats")
52    table.insert(nameList, "Look Around")
53    table.insert(nameList, "Pause")
54   
55    linesList = {}
56
57    lineHeight = 35
58    commandWidth = 200
59    buttonWidth = 170
60    clearWidth = 20
61    spaceWidth = 10
62   
63    P.createLines()
64   
65    local funct = luaState:createLuaFunctor("KeyBindMenu.callback()")
66    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
67end
68
69
70function P.KeyNameNiceifier(key)
71    local name = string.sub(key, string.find(key, '%.(.*)')+1)
72    local group = string.sub(key, string.find(key, '(.*)%.'))
73    group = string.sub(group,1,string.len(group)-1)
74    if( group == "Keys") then
75        return "Key " .. string.sub(name, string.find(name, 'Key(.*)')+3)
76    elseif( group == "MouseButtons") then
77        return "Mouse " .. name
78    end
79    return key
80end
81
82function P.createLine(k)
83    local offset = 0
84    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
85    line:setHeight(CEGUI.UDim(0, lineHeight))
86    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
87   
88    local command = winMgr:createWindow("TaharezLook/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
89    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(0.9, 0)))
90    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.05, 0)))
91    command:setText(nameList[k])
92    line:addChildWindow(command)
93    offset = offset + commandWidth + spaceWidth
94   
95    local plus = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
96    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
97    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
98    plus:setText("+")
99    orxonox.KeyBinderManager:getInstance():subscribeEventHelper(plus, "Clicked", P.filename .. ".KeyBindPlus_clicked")
100    line:addChildWindow(plus)
101    offset = offset + clearWidth + spaceWidth
102   
103    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
104    for i=0,(numButtons-1) do
105        local button = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
106        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
107        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
108        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
109        name = P.KeyNameNiceifier(name)
110        button:setText(name)
111        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(button, "Clicked", P.filename .. ".KeyBindButton_clicked")
112        --button:subscribeScriptedEvent("EventClicked", P.filename .. ".KeyBindButton_clicked")
113        line:addChildWindow(button)
114        offset = offset + buttonWidth
115       
116        local clear = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
117        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
118        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, (i*(buttonWidth+clearWidth+spaceWidth)+buttonWidth)+commandWidth+clearWidth+2*spaceWidth), CEGUI.UDim(0.15, 0)))
119        clear:setText("X")
120        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(clear, "Clicked", P.filename .. ".KeyBindClear_clicked")
121        line:addChildWindow(clear)
122        offset = offset + clearWidth + spaceWidth
123    end
124   
125    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
126   
127    return line
128end
129
130function P.createLines()
131
132    local window = winMgr:getWindow("orxonox/KeyBindPane")
133
134    for k,v in pairs(commandList) do
135        local line = P.createLine(k)
136        table.insert(linesList, line)
137        window:addChildWindow(line)
138    end
139
140end
141
142function P.KeyBindButton_clicked(e)
143    local we = CEGUI.toWindowEventArgs(e)
144    local name = we.window:getName()
145
146    local match = string.gmatch(name, "%d+")
147    local commandNr = tonumber(match())
148    local buttonNr = tonumber(match())
149   
150    local arguments = {}
151    arguments[1] = commandNr
152    arguments[2] = buttonNr
153    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
154end
155
156function P.KeyBindPlus_clicked(e)
157    local we = CEGUI.toWindowEventArgs(e)
158    local name = we.window:getName()
159   
160    local match = string.gmatch(name, "%d+")
161    local commandNr = tonumber(match())
162   
163    local arguments = {}
164    arguments[1] = commandNr
165    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
166end
167
168function P.KeyBindClear_clicked(e)
169    local we = CEGUI.toWindowEventArgs(e)
170    local name = we.window:getName()
171   
172    local match = string.gmatch(name, "%d+")
173    local commandNr = tonumber(match())
174    local buttonNr = tonumber(match())
175   
176    orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
177   
178    P.callback()
179end
180
181function P.keybind(arguments)
182    local commandNr = arguments[1]
183    local buttonNr = arguments[2]
184    if buttonNr ~= nil then
185        orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
186    end
187    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
188end
189
190function P.callback()
191    while table.getn(linesList) ~= 0 do
192        if linesList[1] ~= nil then
193            winMgr:destroyWindow(linesList[1]:getName())
194        end
195        table.remove(linesList, 1)
196    end
197   
198    linesList = {}
199
200    P.createLines()
201   
202    if(InfoPopup ~= nil) then
203        InfoPopup.close()
204    end
205end
206
207function P.KeyBindBackButton_clicked(e)
208    hideGUI("KeyBindMenu")
209end
210
211return P
212
Note: See TracBrowser for help on using the repository browser.