Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spacerace/data/gui/scripts/KeyBindMenu.lua @ 8487

Last change on this file since 8487 was 8079, checked in by landauf, 14 years ago

merged usability branch back to trunk

incomplete summary of the changes in this branch:

  • enhanced keyboard navigation in GUIs
  • implemented new graphics menu and changeable window size at runtime
  • added developer mode
  • HUD shows if game is paused, game pauses if ingame menu is opened
  • removed a few obsolete commands and hid some that are more for internal use
  • numpad works in console and gui
  • faster loading of level info
  • enhanced usage of compositors (Shader class)
  • improved camera handling, configurable FOV and aspect ratio
  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
1-- KeyBindMenu.lua
2
3local P = createMenuSheet("KeyBindMenu")
4P.loadAlong = { "InfoPopup" }
5
6function P.onLoad()
7
8    commandList = {}
9    table.insert(commandList, "fire 0")
10    table.insert(commandList, "fire 1 | unfire")
11    table.insert(commandList, "onpress fire 2")
12    table.insert(commandList, "onpress fire 3")
13    table.insert(commandList, "scale 1 moveFrontBack")
14    table.insert(commandList, "scale -1 moveFrontBack")
15    table.insert(commandList, "boost")
16    table.insert(commandList, "scale 1 moveRightLeft")
17    table.insert(commandList, "scale -1 moveRightLeft")
18    table.insert(commandList, "scale 1 moveUpDown")
19    table.insert(commandList, "scale -1 moveUpDown")
20    table.insert(commandList, "scale -1 rotateRoll")
21    table.insert(commandList, "scale 1 rotateRoll")
22    table.insert(commandList, "scale 1 rotateYaw")
23    table.insert(commandList, "scale -1 rotateYaw")
24    table.insert(commandList, "scale 1 rotatePitch")
25    table.insert(commandList, "scale -1 rotatePitch")
26    table.insert(commandList, "NewHumanController changeMode")
27    table.insert(commandList, "switchCamera")
28    table.insert(commandList, "InGameConsole openConsole")
29    table.insert(commandList, "OverlayGroup toggleVisibility Debug")
30    table.insert(commandList, "OverlayGroup toggleVisibility Stats")
31    table.insert(commandList, "OrxonoxOverlay toggleVisibility QuestGUI")
32    table.insert(commandList, "OrxonoxOverlay toggleVisibility PickupInventory")
33    table.insert(commandList, "startchat")
34    table.insert(commandList, "startchat_small")
35    table.insert(commandList, "mouseLook")
36    table.insert(commandList, "pause")
37    table.insert(commandList, "printScreen")
38    if orxonox.GUIManager:inDevMode() then
39        table.insert(commandList, "printScreenHD 3")
40    end
41
42    nameList = {}
43    table.insert(nameList, "Primary Fire")
44    table.insert(nameList, "Secondary Fire")
45    table.insert(nameList, "Fire Rocket")
46    table.insert(nameList, "Fire Alternative Rocket")
47    table.insert(nameList, "Accelerate")
48    table.insert(nameList, "Break")
49    table.insert(nameList, "Boost")
50    table.insert(nameList, "Move Right")
51    table.insert(nameList, "Move Left")
52    table.insert(nameList, "Move Up")
53    table.insert(nameList, "Move Down")
54    table.insert(nameList, "Roll Right")
55    table.insert(nameList, "Roll Left")
56    table.insert(nameList, "Yaw Left")
57    table.insert(nameList, "Yaw Right")
58    table.insert(nameList, "Pitch Up")
59    table.insert(nameList, "Pitch Down")
60    table.insert(nameList, "Switch Input Mode")
61    table.insert(nameList, "Switch Camera")
62    table.insert(nameList, "Open Console")
63    table.insert(nameList, "Show Debug")
64    table.insert(nameList, "Show Stats")
65    table.insert(nameList, "Show Quests")
66    table.insert(nameList, "Show Pickups")
67    table.insert(nameList, "Show Chat")
68    table.insert(nameList, "Show small Chat")
69    table.insert(nameList, "Look Around")
70    table.insert(nameList, "Pause")
71    table.insert(nameList, "Screenshot")
72    if orxonox.GUIManager:inDevMode() then
73        table.insert(nameList, "HD screenshot")
74    end
75
76    linesList = {}
77
78    --Calculate design parameters:
79    sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/SampleWindow")
80    sampleWindow:setText("SampleText")
81
82    local size = getMinTextSize(sampleWindow)
83    lineHeight = size[1]
84
85    commandWidth = 0
86    for k,v in pairs(commandList) do
87        sampleWindow:setText(nameList[k])
88        size = getMinTextSize(sampleWindow)
89        if size[2] > commandWidth then
90            commandWidth = size[2]
91        end
92    end
93
94    sampleWindow:setText("add")
95    size = getMinTextSize(sampleWindow)
96    addWidth = size[2]
97
98    sampleWindow:setText("X")
99    size = getMinTextSize(sampleWindow)
100    clearWidth = size[2]
101
102    spaceWidth = math.floor(1/14*commandWidth)
103
104    buttonWidth = 145
105
106    P.createLines()
107
108    local funct = luaState:createLuaFunctor("KeyBindMenu.callback()")
109    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
110
111    P:setButton(1, 1, {
112            ["button"] = winMgr:getWindow("orxonox/KeyBindBackButton"),
113            ["callback"]  = P.KeyBindBackButton_clicked
114    })
115end
116
117function P.KeyNameNiceifier(key)
118    local name = string.sub(key, string.find(key, '%.(.*)')+1)
119    local group = string.sub(key, string.find(key, '(.*)%.'))
120    group = string.sub(group,1,string.len(group)-1)
121    if( group == "Keys") then
122        return "Key " .. string.sub(name, string.find(name, 'Key(.*)')+3)
123    elseif( group == "MouseButtons") then
124        return "Mouse " .. name
125    elseif( string.find(group, "JoyStickButtons") ~= nil ) then
126        return "Joystick " .. name
127    elseif( string.find(group, "JoyStickAxes") ~= nil ) then
128        return "Joystick Axis " .. string.sub(name, 5, 6) .. string.sub(name, string.find(name, 'Axis%d%d(.*)')+6)
129    elseif( group == "MouseAxes" ) then
130        return "Mouse " .. string.sub(name, string.find(name, '.(.*)')+1) .. " " .. string.sub(name, 1, 1) .. "-Axis"
131    end
132    return key
133end
134
135function P.createLine(k)
136    local offset = 0
137    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
138    line:setHeight(CEGUI.UDim(0, lineHeight))
139    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
140
141    local command = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
142    command:setText(nameList[k])
143    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
144    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
145    line:addChildWindow(command)
146    offset = offset + commandWidth + spaceWidth
147
148    local plus = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
149    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
150    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
151    plus:setText("add")
152    orxonox.GUIManager:subscribeEventHelper(plus, "Clicked", P.name .. ".KeyBindPlus_clicked")
153    line:addChildWindow(plus)
154    offset = offset + addWidth + spaceWidth
155
156    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
157    for i=0,(numButtons-1) do
158        local button = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
159        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
160        name = P.KeyNameNiceifier(name)
161        button:setText(name)
162        sampleWindow:setText(name)
163        local size = getMinTextSize(sampleWindow)
164        local buttonWidth = size[2]
165        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
166        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
167        orxonox.GUIManager:subscribeEventHelper(button, "Clicked", P.name .. ".KeyBindButton_clicked")
168        --button:subscribeScriptedEvent("EventClicked", P.name .. ".KeyBindButton_clicked")
169        line:addChildWindow(button)
170        offset = offset + buttonWidth
171
172        local clear = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
173        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
174        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
175        clear:setText("X")
176        orxonox.GUIManager:subscribeEventHelper(clear, "Clicked", P.name .. ".KeyBindClear_clicked")
177        line:addChildWindow(clear)
178        offset = offset + clearWidth + spaceWidth
179    end
180
181    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
182
183    return line
184end
185
186function P.createLines()
187    local window = winMgr:getWindow("orxonox/KeyBindPane")
188
189    for k,v in pairs(commandList) do
190        local line = P.createLine(k)
191        table.insert(linesList, line)
192        window:addChildWindow(line)
193    end
194
195    local pane = tolua.cast(window, "CEGUI::ScrollablePane")
196    pane:setVerticalStepSize(getScrollingStepSize(window))
197end
198
199function P.KeyBindButton_clicked(e)
200    local we = CEGUI.toWindowEventArgs(e)
201    local name = we.window:getName()
202
203    local match = string.gmatch(name, "%d+")
204    local commandNr = tonumber(match())
205    local buttonNr = tonumber(match())
206
207    local arguments = {}
208    arguments[1] = commandNr
209    arguments[2] = buttonNr
210    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
211end
212
213function P.KeyBindPlus_clicked(e)
214    local we = CEGUI.toWindowEventArgs(e)
215    local name = we.window:getName()
216
217    local match = string.gmatch(name, "%d+")
218    local commandNr = tonumber(match())
219
220    local arguments = {}
221    arguments[1] = commandNr
222    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
223end
224
225function P.KeyBindClear_clicked(e)
226    local we = CEGUI.toWindowEventArgs(e)
227    local name = we.window:getName()
228
229    local match = string.gmatch(name, "%d+")
230    local commandNr = tonumber(match())
231    local buttonNr = tonumber(match())
232
233    local str = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr)
234    orxonox.KeyBinderManager:getInstance():unbind(str)
235
236    P.callback()
237end
238
239function P.keybind(arguments)
240    local commandNr = arguments[1]
241    local buttonNr = arguments[2]
242    if buttonNr ~= nil then
243        local str = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr)
244        orxonox.KeyBinderManager:getInstance():unbind(str)
245    end
246
247    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
248end
249
250function P.callback()
251    local pane = tolua.cast(winMgr:getWindow("orxonox/KeyBindPane"), "CEGUI::ScrollablePane")
252    local position = pane:getVerticalScrollPosition()
253    while table.getn(linesList) ~= 0 do
254        if linesList[1] ~= nil then
255            winMgr:destroyWindow(linesList[1]:getName())
256        end
257        table.remove(linesList, 1)
258    end
259
260    linesList = {}
261
262    P.createLines()
263    if(InfoPopup ~= nil) then
264        InfoPopup.close()
265    end
266    pane:setVerticalScrollPosition( position )
267end
268
269function P.KeyBindBackButton_clicked(e)
270    hideMenuSheet("KeyBindMenu")
271end
272
273return P
Note: See TracBrowser for help on using the repository browser.