Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/data/gui/scripts/KeyBindMenu.lua @ 6355

Last change on this file since 6355 was 6355, checked in by dafrick, 14 years ago

Made KeyBindMenu less Resolution dependent, because of this the keybind buttons aren't vertically aligned anymore.
Also made scrolling (with the mouse wheel) (to some extent) invariant of the size of the ScrollablePane in the KeyBindManager.

  • Property svn:executable set to *
File size: 9.4 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( group == "MouseAxes" ) then
112        return "Mouse " .. string.sub(name, string.find(name, '.(.*)')+1) .. " " .. string.sub(name, 1, 1) .. "-Axes"
113    end
114    return key
115end
116
117function P.createLine(k)
118    local offset = 0
119    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
120    line:setHeight(CEGUI.UDim(0, lineHeight))
121    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
122
123    local command = winMgr:createWindow("TaharezLook/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
124    command:setText(nameList[k])
125    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
126    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
127    line:addChildWindow(command)
128    offset = offset + commandWidth + spaceWidth
129
130    local plus = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
131    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
132    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
133    plus:setText("add")
134    orxonox.KeyBinderManager:getInstance():subscribeEventHelper(plus, "Clicked", P.filename .. ".KeyBindPlus_clicked")
135    line:addChildWindow(plus)
136    offset = offset + addWidth + spaceWidth
137
138    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
139    for i=0,(numButtons-1) do
140        local button = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
141        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
142        name = P.KeyNameNiceifier(name)
143        button:setText(name)
144        sampleWindow:setText(name)
145        local size = getMinTextSize(sampleWindow)
146        local buttonWidth = size[2]
147        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
148        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
149        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(button, "Clicked", P.filename .. ".KeyBindButton_clicked")
150        --button:subscribeScriptedEvent("EventClicked", P.filename .. ".KeyBindButton_clicked")
151        line:addChildWindow(button)
152        offset = offset + buttonWidth
153
154        local clear = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
155        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
156        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
157        clear:setText("X")
158        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(clear, "Clicked", P.filename .. ".KeyBindClear_clicked")
159        line:addChildWindow(clear)
160        offset = offset + clearWidth + spaceWidth
161    end
162
163    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
164   
165    return line
166end
167
168function P.createLines()
169    local window = winMgr:getWindow("orxonox/KeyBindPane")
170
171    for k,v in pairs(commandList) do
172        local line = P.createLine(k)
173        table.insert(linesList, line)
174        window:addChildWindow(line)
175    end
176   
177    pane = tolua.cast(window, "CEGUI::ScrollablePane")
178    pane:setVerticalStepSize(getScrollingStepSize(window))
179end
180
181function P.KeyBindButton_clicked(e)
182    local we = CEGUI.toWindowEventArgs(e)
183    local name = we.window:getName()
184
185    local match = string.gmatch(name, "%d+")
186    local commandNr = tonumber(match())
187    local buttonNr = tonumber(match())
188
189    local arguments = {}
190    arguments[1] = commandNr
191    arguments[2] = buttonNr
192    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
193end
194
195function P.KeyBindPlus_clicked(e)
196    local we = CEGUI.toWindowEventArgs(e)
197    local name = we.window:getName()
198
199    local match = string.gmatch(name, "%d+")
200    local commandNr = tonumber(match())
201
202    local arguments = {}
203    arguments[1] = commandNr
204    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
205end
206
207function P.KeyBindClear_clicked(e)
208    local we = CEGUI.toWindowEventArgs(e)
209    local name = we.window:getName()
210
211    local match = string.gmatch(name, "%d+")
212    local commandNr = tonumber(match())
213    local buttonNr = tonumber(match())
214 
215    orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
216
217    P.callback()
218end
219
220function P.keybind(arguments)
221    local commandNr = arguments[1]
222    local buttonNr = arguments[2]
223    if buttonNr ~= nil then
224        orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
225    end
226
227    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
228end
229
230function P.callback()
231    local pane = tolua.cast(winMgr:getWindow("orxonox/KeyBindPane"), "CEGUI::ScrollablePane")
232    local position = pane:getVerticalScrollPosition()
233    while table.getn(linesList) ~= 0 do
234        if linesList[1] ~= nil then
235            winMgr:destroyWindow(linesList[1]:getName())
236        end
237        table.remove(linesList, 1)
238    end
239
240    linesList = {}
241
242    P.createLines()
243    if(InfoPopup ~= nil) then
244        InfoPopup.close()
245    end
246    pane:setVerticalScrollPosition( position )
247end
248
249function P.KeyBindBackButton_clicked(e)
250    hideGUI("KeyBindMenu")
251end
252
253return P
Note: See TracBrowser for help on using the repository browser.