Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/gui/scripts/KeyBindMenu.lua @ 6748

Last change on this file since 6748 was 6748, checked in by rgrieder, 14 years ago

A GUISheet can assign its field "loadAlong" any strings designating other sheets that have to be loaded as well.
Implemented this for all menus sheets as I see fit (load the whole MainMenu, but don't load MainMenu or Settings for the InGameMenu).
This is just a measure to avoid lags when clicking through the menus.

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