Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua @ 6339

Last change on this file since 6339 was 6339, checked in by dafrick, 14 years ago

Undid an error I mad while removing empty lines. Quitting from InGameMenu should work now.

  • 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
5
6if _REQUIREDNAME == nil then
7    KeyBindMenu = P
8else
9    _G[_REQUIREDNAME] = P
10end
11
12P.filename = "KeyBindMenu"
13P.layoutString = "KeyBindMenu.layout"
14
15function P:init()
16
17    commandList = {}
18    table.insert(commandList, "fire 0")
19    table.insert(commandList, "fire 1 | unfire")
20    table.insert(commandList, "onpress fire 2")
21    table.insert(commandList, "scale 1 moveFrontBack")
22    table.insert(commandList, "scale -1 moveFrontBack")
23    table.insert(commandList, "boost")
24    table.insert(commandList, "scale 1 moveRightLeft")
25    table.insert(commandList, "scale -1 moveRightLeft")
26    table.insert(commandList, "scale 1 moveUpDown")
27    table.insert(commandList, "scale -1 moveUpDown")
28    table.insert(commandList, "scale 1 rotateRoll")
29    table.insert(commandList, "scale -1 rotateRoll")
30    table.insert(commandList, "switchCamera")
31    table.insert(commandList, "openConsole")
32    table.insert(commandList, "OverlayGroup toggleVisibility Debug")
33    table.insert(commandList, "OverlayGroup toggleVisibility Stats")
34    table.insert(commandList, "mouseLook")
35    table.insert(commandList, "pause")
36
37    nameList = {}
38    table.insert(nameList, "Primary Fire")
39    table.insert(nameList, "Secondary Fire")
40    table.insert(nameList, "Fire Rocket")
41    table.insert(nameList, "Accelerate")
42    table.insert(nameList, "Break")
43    table.insert(nameList, "Boost")
44    table.insert(nameList, "Move Right")
45    table.insert(nameList, "Move Left")
46    table.insert(nameList, "Move Up")
47    table.insert(nameList, "Move Down")
48    table.insert(nameList, "Roll Right")
49    table.insert(nameList, "Roll Left")
50    table.insert(nameList, "Switch Camera")
51    table.insert(nameList, "Open Console")
52    table.insert(nameList, "Show Debug")
53    table.insert(nameList, "Show Stats")
54    table.insert(nameList, "Look Around")
55    table.insert(nameList, "Pause")
56
57    linesList = {}
58
59    lineHeight = 30
60    commandWidth = 125
61    buttonWidth = 145
62    clearWidth = 20
63    addWidth = 30
64    spaceWidth = 10
65
66    P.createLines()
67
68    local funct = luaState:createLuaFunctor("KeyBindMenu.callback()")
69    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
70end
71
72function P.KeyNameNiceifier(key)
73    local name = string.sub(key, string.find(key, '%.(.*)')+1)
74    local group = string.sub(key, string.find(key, '(.*)%.'))
75    group = string.sub(group,1,string.len(group)-1)
76    if( group == "Keys") then
77        return "Key " .. string.sub(name, string.find(name, 'Key(.*)')+3)
78    elseif( group == "MouseButtons") then
79        return "Mouse " .. name
80    end
81    return key
82end
83
84function P.createLine(k)
85    local offset = 0
86    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
87    line:setHeight(CEGUI.UDim(0, lineHeight))
88    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
89
90    local command = winMgr:createWindow("TaharezLook/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
91    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
92    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
93    command:setText(nameList[k])
94    line:addChildWindow(command)
95    offset = offset + commandWidth + spaceWidth
96
97    local plus = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
98    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
99    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
100    plus:setText("add")
101    orxonox.KeyBinderManager:getInstance():subscribeEventHelper(plus, "Clicked", P.filename .. ".KeyBindPlus_clicked")
102    line:addChildWindow(plus)
103    offset = offset + addWidth + spaceWidth
104
105    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
106    for i=0,(numButtons-1) do
107        local button = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
108        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
109        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
110        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
111        name = P.KeyNameNiceifier(name)
112        button:setText(name)
113        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(button, "Clicked", P.filename .. ".KeyBindButton_clicked")
114        --button:subscribeScriptedEvent("EventClicked", P.filename .. ".KeyBindButton_clicked")
115        line:addChildWindow(button)
116        offset = offset + buttonWidth
117
118        local clear = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
119        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
120        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
121        clear:setText("X")
122        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(clear, "Clicked", P.filename .. ".KeyBindClear_clicked")
123        line:addChildWindow(clear)
124        offset = offset + clearWidth + spaceWidth
125    end
126
127    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
128   
129    return line
130end
131
132function P.createLines()
133    local window = winMgr:getWindow("orxonox/KeyBindPane")
134
135    for k,v in pairs(commandList) do
136        local line = P.createLine(k)
137        table.insert(linesList, line)
138        window:addChildWindow(line)
139    end
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
188    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
189end
190
191function P.callback()
192    while table.getn(linesList) ~= 0 do
193        if linesList[1] ~= nil then
194            winMgr:destroyWindow(linesList[1]:getName())
195        end
196        table.remove(linesList, 1)
197    end
198
199    linesList = {}
200
201    P.createLines()
202    if(InfoPopup ~= nil) then
203        InfoPopup.close()
204    end
205end
206
207function P.KeyBindBackButton_clicked(e)
208    hideGUI("KeyBindMenu")
209end
210
211return P
Note: See TracBrowser for help on using the repository browser.