Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/data/gui/scripts/KeyBindMenu.lua @ 7091

Last change on this file since 7091 was 6773, checked in by rgrieder, 15 years ago

Eliminated all unnecessary global Lua variables and replaced them either with a local or a instance variable (P.myVar).

  • Property svn:eol-style set to native
File size: 9.4 KB
Line 
1-- KeyBindMenu.lua
2
3local P = createMenuSheet("KeyBindMenu")
4P.loadAlong = { "InfoPopup" }
5
6function P.onLoad()
7
8    P.commandList = {}
9    table.insert(P.commandList, "fire 0")
10    table.insert(P.commandList, "fire 1 | unfire")
11    table.insert(P.commandList, "onpress fire 2")
12    table.insert(P.commandList, "scale 1 moveFrontBack")
13    table.insert(P.commandList, "scale -1 moveFrontBack")
14    table.insert(P.commandList, "boost")
15    table.insert(P.commandList, "scale 1 moveRightLeft")
16    table.insert(P.commandList, "scale -1 moveRightLeft")
17    table.insert(P.commandList, "scale 1 moveUpDown")
18    table.insert(P.commandList, "scale -1 moveUpDown")
19    table.insert(P.commandList, "scale -1 rotateRoll")
20    table.insert(P.commandList, "scale 1 rotateRoll")
21    table.insert(P.commandList, "scale 1 rotateYaw")
22    table.insert(P.commandList, "scale -1 rotateYaw")
23    table.insert(P.commandList, "scale 1 rotatePitch")
24    table.insert(P.commandList, "scale -1 rotatePitch")
25    table.insert(P.commandList, "NewHumanController changeMode")
26    table.insert(P.commandList, "switchCamera")
27    table.insert(P.commandList, "openConsole")
28    table.insert(P.commandList, "OverlayGroup toggleVisibility Debug")
29    table.insert(P.commandList, "OverlayGroup toggleVisibility Stats")
30    table.insert(P.commandList, "mouseLook")
31    table.insert(P.commandList, "pause")
32
33    P.nameList = {}
34    table.insert(P.nameList, "Primary Fire")
35    table.insert(P.nameList, "Secondary Fire")
36    table.insert(P.nameList, "Fire Rocket")
37    table.insert(P.nameList, "Accelerate")
38    table.insert(P.nameList, "Break")
39    table.insert(P.nameList, "Boost")
40    table.insert(P.nameList, "Move Right")
41    table.insert(P.nameList, "Move Left")
42    table.insert(P.nameList, "Move Up")
43    table.insert(P.nameList, "Move Down")
44    table.insert(P.nameList, "Roll Right")
45    table.insert(P.nameList, "Roll Left")
46    table.insert(P.nameList, "Yaw Left")
47    table.insert(P.nameList, "Yaw Right")
48    table.insert(P.nameList, "Pitch Up")
49    table.insert(P.nameList, "Pitch Down")
50    table.insert(P.nameList, "Switch Input Mode")
51    table.insert(P.nameList, "Switch Camera")
52    table.insert(P.nameList, "Open Console")
53    table.insert(P.nameList, "Show Debug")
54    table.insert(P.nameList, "Show Stats")
55    table.insert(P.nameList, "Look Around")
56    table.insert(P.nameList, "Pause")
57
58    P.linesList = {}
59
60    --Calculate design parameters:
61    P.sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/SampleWindow")
62    P.sampleWindow:setText("SampleText")
63
64    local size = getMinTextSize(P.sampleWindow)
65    P.lineHeight = size[1]
66
67    P.commandWidth = 0
68    for k,v in pairs(P.commandList) do
69        P.sampleWindow:setText(P.nameList[k])
70        size = getMinTextSize(P.sampleWindow)
71        if size[2] > P.commandWidth then
72            P.commandWidth = size[2]
73        end
74    end
75
76    P.sampleWindow:setText("add")
77    size = getMinTextSize(P.sampleWindow)
78    P.addWidth = size[2]
79
80    P.sampleWindow:setText("X")
81    size = getMinTextSize(P.sampleWindow)
82    P.clearWidth = size[2]
83
84    P.spaceWidth = math.floor(1/14*P.commandWidth)
85
86    P.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, P.lineHeight))
116    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, P.lineHeight*(k-1))))
117
118    local command = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
119    command:setText(P.nameList[k])
120    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.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 + P.commandWidth + P.spaceWidth
124
125    local plus = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
126    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.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 + P.addWidth + P.spaceWidth
132
133    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(P.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(P.commandList[k],i)
137        name = P.KeyNameNiceifier(name)
138        button:setText(name)
139        P.sampleWindow:setText(name)
140        local size = getMinTextSize(P.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, P.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 + P.clearWidth + P.spaceWidth
156    end
157
158    line:setWidth(CEGUI.UDim(0, offset+P.clearWidth))
159
160    return line
161end
162
163function P.createLines()
164    local window = winMgr:getWindow("orxonox/KeyBindPane")
165
166    for k,v in pairs(P.commandList) do
167        local line = P.createLine(k)
168        table.insert(P.linesList, line)
169        window:addChildWindow(line)
170    end
171
172    local 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(P.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(P.commandList[commandNr], buttonNr)
221        orxonox.KeyBinderManager:getInstance():unbind(str)
222    end
223
224    orxonox.KeyBinderManager:getInstance():keybind(P.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(P.linesList) ~= 0 do
231        if P.linesList[1] ~= nil then
232            winMgr:destroyWindow(P.linesList[1]:getName())
233        end
234        table.remove(P.linesList, 1)
235    end
236
237    P.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.