Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6539 was 6427, checked in by rgrieder, 15 years ago

Fixed (whyever it works now) bug in KeyBind menu that caused an error when removing a binding.
Also removed a GCC warning.

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