Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates2/data/gui/scripts/KeyBindMenu.lua @ 6987

Last change on this file since 6987 was 6722, checked in by rgrieder, 15 years ago

Renamed showGUI and hideGUI to showMenuSheet and hideMenuSheet.

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1-- KeyBindMenu.lua
2
3local P = createMenuSheet("KeyBindMenu")
4
5function P.onLoad()
6
7    commandList = {}
8    table.insert(commandList, "fire 0")
9    table.insert(commandList, "fire 1 | unfire")
10    table.insert(commandList, "onpress fire 2")
11    table.insert(commandList, "scale 1 moveFrontBack")
12    table.insert(commandList, "scale -1 moveFrontBack")
13    table.insert(commandList, "boost")
14    table.insert(commandList, "scale 1 moveRightLeft")
15    table.insert(commandList, "scale -1 moveRightLeft")
16    table.insert(commandList, "scale 1 moveUpDown")
17    table.insert(commandList, "scale -1 moveUpDown")
18    table.insert(commandList, "scale -1 rotateRoll")
19    table.insert(commandList, "scale 1 rotateRoll")
20    table.insert(commandList, "scale 1 rotateYaw")
21    table.insert(commandList, "scale -1 rotateYaw")
22    table.insert(commandList, "scale 1 rotatePitch")
23    table.insert(commandList, "scale -1 rotatePitch")
24    table.insert(commandList, "NewHumanController changeMode")
25    table.insert(commandList, "switchCamera")
26    table.insert(commandList, "openConsole")
27    table.insert(commandList, "OverlayGroup toggleVisibility Debug")
28    table.insert(commandList, "OverlayGroup toggleVisibility Stats")
29    table.insert(commandList, "mouseLook")
30    table.insert(commandList, "pause")
31
32    nameList = {}
33    table.insert(nameList, "Primary Fire")
34    table.insert(nameList, "Secondary Fire")
35    table.insert(nameList, "Fire Rocket")
36    table.insert(nameList, "Accelerate")
37    table.insert(nameList, "Break")
38    table.insert(nameList, "Boost")
39    table.insert(nameList, "Move Right")
40    table.insert(nameList, "Move Left")
41    table.insert(nameList, "Move Up")
42    table.insert(nameList, "Move Down")
43    table.insert(nameList, "Roll Right")
44    table.insert(nameList, "Roll Left")
45    table.insert(nameList, "Yaw Left")
46    table.insert(nameList, "Yaw Right")
47    table.insert(nameList, "Pitch Up")
48    table.insert(nameList, "Pitch Down")
49    table.insert(nameList, "Switch Input Mode")
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    --Calculate design parameters:
60    sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/SampleWindow")
61    sampleWindow:setText("SampleText")
62
63    local size = getMinTextSize(sampleWindow)
64    lineHeight = size[1]
65
66    commandWidth = 0
67    for k,v in pairs(commandList) do
68        sampleWindow:setText(nameList[k])
69        size = getMinTextSize(sampleWindow)
70        if size[2] > commandWidth then
71            commandWidth = size[2]
72        end
73    end
74
75    sampleWindow:setText("add")
76    size = getMinTextSize(sampleWindow)
77    addWidth = size[2]
78
79    sampleWindow:setText("X")
80    size = getMinTextSize(sampleWindow)
81    clearWidth = size[2]
82
83    spaceWidth = math.floor(1/14*commandWidth)
84
85    buttonWidth = 145
86
87    P.createLines()
88
89    local funct = luaState:createLuaFunctor("KeyBindMenu.callback()")
90    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
91end
92
93function P.KeyNameNiceifier(key)
94    local name = string.sub(key, string.find(key, '%.(.*)')+1)
95    local group = string.sub(key, string.find(key, '(.*)%.'))
96    group = string.sub(group,1,string.len(group)-1)
97    if( group == "Keys") then
98        return "Key " .. string.sub(name, string.find(name, 'Key(.*)')+3)
99    elseif( group == "MouseButtons") then
100        return "Mouse " .. name
101    elseif( string.find(group, "JoyStickButtons") ~= nil ) then
102        return "Joystick " .. name
103    elseif( string.find(group, "JoyStickAxes") ~= nil ) then
104        return "Joystick Axis" .. string.sub(name, 5, 6) .. string.sub(name, string.find(name, 'Axis%d%d(.*)')+6)
105    elseif( group == "MouseAxes" ) then
106        return "Mouse " .. string.sub(name, string.find(name, '.(.*)')+1) .. " " .. string.sub(name, 1, 1) .. "-Axis"
107    end
108    return key
109end
110
111function P.createLine(k)
112    local offset = 0
113    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
114    line:setHeight(CEGUI.UDim(0, lineHeight))
115    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
116
117    local command = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
118    command:setText(nameList[k])
119    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
120    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
121    line:addChildWindow(command)
122    offset = offset + commandWidth + spaceWidth
123
124    local plus = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
125    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
126    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
127    plus:setText("add")
128    orxonox.GUIManager:subscribeEventHelper(plus, "Clicked", P.name .. ".KeyBindPlus_clicked")
129    line:addChildWindow(plus)
130    offset = offset + addWidth + spaceWidth
131
132    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
133    for i=0,(numButtons-1) do
134        local button = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
135        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
136        name = P.KeyNameNiceifier(name)
137        button:setText(name)
138        sampleWindow:setText(name)
139        local size = getMinTextSize(sampleWindow)
140        local buttonWidth = size[2]
141        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
142        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
143        orxonox.GUIManager:subscribeEventHelper(button, "Clicked", P.name .. ".KeyBindButton_clicked")
144        --button:subscribeScriptedEvent("EventClicked", P.name .. ".KeyBindButton_clicked")
145        line:addChildWindow(button)
146        offset = offset + buttonWidth
147
148        local clear = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
149        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
150        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
151        clear:setText("X")
152        orxonox.GUIManager:subscribeEventHelper(clear, "Clicked", P.name .. ".KeyBindClear_clicked")
153        line:addChildWindow(clear)
154        offset = offset + clearWidth + spaceWidth
155    end
156
157    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
158
159    return line
160end
161
162function P.createLines()
163    local window = winMgr:getWindow("orxonox/KeyBindPane")
164
165    for k,v in pairs(commandList) do
166        local line = P.createLine(k)
167        table.insert(linesList, line)
168        window:addChildWindow(line)
169    end
170
171    pane = tolua.cast(window, "CEGUI::ScrollablePane")
172    pane:setVerticalStepSize(getScrollingStepSize(window))
173end
174
175function P.KeyBindButton_clicked(e)
176    local we = CEGUI.toWindowEventArgs(e)
177    local name = we.window:getName()
178
179    local match = string.gmatch(name, "%d+")
180    local commandNr = tonumber(match())
181    local buttonNr = tonumber(match())
182
183    local arguments = {}
184    arguments[1] = commandNr
185    arguments[2] = buttonNr
186    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
187end
188
189function P.KeyBindPlus_clicked(e)
190    local we = CEGUI.toWindowEventArgs(e)
191    local name = we.window:getName()
192
193    local match = string.gmatch(name, "%d+")
194    local commandNr = tonumber(match())
195
196    local arguments = {}
197    arguments[1] = commandNr
198    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
199end
200
201function P.KeyBindClear_clicked(e)
202    local we = CEGUI.toWindowEventArgs(e)
203    local name = we.window:getName()
204
205    local match = string.gmatch(name, "%d+")
206    local commandNr = tonumber(match())
207    local buttonNr = tonumber(match())
208
209    local str = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr)
210    orxonox.KeyBinderManager:getInstance():unbind(str)
211
212    P.callback()
213end
214
215function P.keybind(arguments)
216    local commandNr = arguments[1]
217    local buttonNr = arguments[2]
218    if buttonNr ~= nil then
219        local str = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr)
220        orxonox.KeyBinderManager:getInstance():unbind(str)
221    end
222
223    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
224end
225
226function P.callback()
227    local pane = tolua.cast(winMgr:getWindow("orxonox/KeyBindPane"), "CEGUI::ScrollablePane")
228    local position = pane:getVerticalScrollPosition()
229    while table.getn(linesList) ~= 0 do
230        if linesList[1] ~= nil then
231            winMgr:destroyWindow(linesList[1]:getName())
232        end
233        table.remove(linesList, 1)
234    end
235
236    linesList = {}
237
238    P.createLines()
239    if(InfoPopup ~= nil) then
240        InfoPopup.close()
241    end
242    pane:setVerticalScrollPosition( position )
243end
244
245function P.KeyBindBackButton_clicked(e)
246    hideMenuSheet("KeyBindMenu")
247end
248
249return P
Note: See TracBrowser for help on using the repository browser.