Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/data/gui/scripts/KeyBindMenu.lua @ 7925

Last change on this file since 7925 was 7924, checked in by landauf, 13 years ago
  • the "back" button of MultiplayerMenu and SettingsMenu now fills the whole row at the bottom, so it can be accessed from both columns by pressing 'down'
  • added keyboard support for KeyBindMenu, MiscConfigMenu, and MouseControlsMenu
  • Property svn:eol-style set to native
File size: 9.9 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)
102
103    P:initButtons(1, 1)
104    P:setButton(1, 1, {
105            ["button"] = winMgr:getWindow("orxonox/KeyBindBackButton"),
106            ["callback"]  = P.KeyBindBackButton_clicked
107    })
108end
109
110function P.KeyNameNiceifier(key)
111    local name = string.sub(key, string.find(key, '%.(.*)')+1)
112    local group = string.sub(key, string.find(key, '(.*)%.'))
113    group = string.sub(group,1,string.len(group)-1)
114    if( group == "Keys") then
115        return "Key " .. string.sub(name, string.find(name, 'Key(.*)')+3)
116    elseif( group == "MouseButtons") then
117        return "Mouse " .. name
118    elseif( string.find(group, "JoyStickButtons") ~= nil ) then
119        return "Joystick " .. name
120    elseif( string.find(group, "JoyStickAxes") ~= nil ) then
121        return "Joystick Axis " .. string.sub(name, 5, 6) .. string.sub(name, string.find(name, 'Axis%d%d(.*)')+6)
122    elseif( group == "MouseAxes" ) then
123        return "Mouse " .. string.sub(name, string.find(name, '.(.*)')+1) .. " " .. string.sub(name, 1, 1) .. "-Axis"
124    end
125    return key
126end
127
128function P.createLine(k)
129    local offset = 0
130    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
131    line:setHeight(CEGUI.UDim(0, lineHeight))
132    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
133
134    local command = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
135    command:setText(nameList[k])
136    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
137    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
138    line:addChildWindow(command)
139    offset = offset + commandWidth + spaceWidth
140
141    local plus = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
142    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
143    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
144    plus:setText("add")
145    orxonox.GUIManager:subscribeEventHelper(plus, "Clicked", P.name .. ".KeyBindPlus_clicked")
146    line:addChildWindow(plus)
147    offset = offset + addWidth + spaceWidth
148
149    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
150    for i=0,(numButtons-1) do
151        local button = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
152        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
153        name = P.KeyNameNiceifier(name)
154        button:setText(name)
155        sampleWindow:setText(name)
156        local size = getMinTextSize(sampleWindow)
157        local buttonWidth = size[2]
158        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
159        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
160        orxonox.GUIManager:subscribeEventHelper(button, "Clicked", P.name .. ".KeyBindButton_clicked")
161        --button:subscribeScriptedEvent("EventClicked", P.name .. ".KeyBindButton_clicked")
162        line:addChildWindow(button)
163        offset = offset + buttonWidth
164
165        local clear = winMgr:createWindow("MenuWidgets/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
166        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
167        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
168        clear:setText("X")
169        orxonox.GUIManager:subscribeEventHelper(clear, "Clicked", P.name .. ".KeyBindClear_clicked")
170        line:addChildWindow(clear)
171        offset = offset + clearWidth + spaceWidth
172    end
173
174    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
175
176    return line
177end
178
179function P.createLines()
180    local window = winMgr:getWindow("orxonox/KeyBindPane")
181
182    for k,v in pairs(commandList) do
183        local line = P.createLine(k)
184        table.insert(linesList, line)
185        window:addChildWindow(line)
186    end
187
188    local pane = tolua.cast(window, "CEGUI::ScrollablePane")
189    pane:setVerticalStepSize(getScrollingStepSize(window))
190end
191
192function P.KeyBindButton_clicked(e)
193    local we = CEGUI.toWindowEventArgs(e)
194    local name = we.window:getName()
195
196    local match = string.gmatch(name, "%d+")
197    local commandNr = tonumber(match())
198    local buttonNr = tonumber(match())
199
200    local arguments = {}
201    arguments[1] = commandNr
202    arguments[2] = buttonNr
203    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
204end
205
206function P.KeyBindPlus_clicked(e)
207    local we = CEGUI.toWindowEventArgs(e)
208    local name = we.window:getName()
209
210    local match = string.gmatch(name, "%d+")
211    local commandNr = tonumber(match())
212
213    local arguments = {}
214    arguments[1] = commandNr
215    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
216end
217
218function P.KeyBindClear_clicked(e)
219    local we = CEGUI.toWindowEventArgs(e)
220    local name = we.window:getName()
221
222    local match = string.gmatch(name, "%d+")
223    local commandNr = tonumber(match())
224    local buttonNr = tonumber(match())
225
226    local str = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr)
227    orxonox.KeyBinderManager:getInstance():unbind(str)
228
229    P.callback()
230end
231
232function P.keybind(arguments)
233    local commandNr = arguments[1]
234    local buttonNr = arguments[2]
235    if buttonNr ~= nil then
236        local str = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr)
237        orxonox.KeyBinderManager:getInstance():unbind(str)
238    end
239
240    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
241end
242
243function P.callback()
244    local pane = tolua.cast(winMgr:getWindow("orxonox/KeyBindPane"), "CEGUI::ScrollablePane")
245    local position = pane:getVerticalScrollPosition()
246    while table.getn(linesList) ~= 0 do
247        if linesList[1] ~= nil then
248            winMgr:destroyWindow(linesList[1]:getName())
249        end
250        table.remove(linesList, 1)
251    end
252
253    linesList = {}
254
255    P.createLines()
256    if(InfoPopup ~= nil) then
257        InfoPopup.close()
258    end
259    pane:setVerticalScrollPosition( position )
260end
261
262function P.KeyBindBackButton_clicked(e)
263    hideMenuSheet("KeyBindMenu")
264end
265
266return P
Note: See TracBrowser for help on using the repository browser.