Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged notifications branch back to trunk.

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