Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Adjusted commandButton size to fit the new commands.
Expanded KeyNameNiceifier to MouseAxes-type keys.

  • Property svn:executable set to *
File size: 8.5 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    lineHeight = 30
70    commandWidth = 150
71    buttonWidth = 145
72    clearWidth = 20
73    addWidth = 30
74    spaceWidth = 10
75
76    P.createLines()
77
78    local funct = luaState:createLuaFunctor("KeyBindMenu.callback()")
79    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
80end
81
82function P.KeyNameNiceifier(key)
83    local name = string.sub(key, string.find(key, '%.(.*)')+1)
84    local group = string.sub(key, string.find(key, '(.*)%.'))
85    group = string.sub(group,1,string.len(group)-1)
86    if( group == "Keys") then
87        return "Key " .. string.sub(name, string.find(name, 'Key(.*)')+3)
88    elseif( group == "MouseButtons") then
89        return "Mouse " .. name
90    elseif( group == "MouseAxes" ) then
91        return "Mouse " .. string.sub(name, string.find(name, '.(.*)')+1) .. " " .. string.sub(name, 1, 1) .. "-Axes"
92    end
93    return key
94end
95
96function P.createLine(k)
97    local offset = 0
98    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
99    line:setHeight(CEGUI.UDim(0, lineHeight))
100    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
101
102    local command = winMgr:createWindow("TaharezLook/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
103    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
104    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
105    command:setText(nameList[k])
106    line:addChildWindow(command)
107    offset = offset + commandWidth + spaceWidth
108
109    local plus = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
110    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
111    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
112    plus:setText("add")
113    orxonox.KeyBinderManager:getInstance():subscribeEventHelper(plus, "Clicked", P.filename .. ".KeyBindPlus_clicked")
114    line:addChildWindow(plus)
115    offset = offset + addWidth + spaceWidth
116
117    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
118    for i=0,(numButtons-1) do
119        local button = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
120        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
121        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
122        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
123        name = P.KeyNameNiceifier(name)
124        button:setText(name)
125        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(button, "Clicked", P.filename .. ".KeyBindButton_clicked")
126        --button:subscribeScriptedEvent("EventClicked", P.filename .. ".KeyBindButton_clicked")
127        line:addChildWindow(button)
128        offset = offset + buttonWidth
129
130        local clear = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
131        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
132        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
133        clear:setText("X")
134        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(clear, "Clicked", P.filename .. ".KeyBindClear_clicked")
135        line:addChildWindow(clear)
136        offset = offset + clearWidth + spaceWidth
137    end
138
139    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
140   
141    return line
142end
143
144function P.createLines()
145    local window = winMgr:getWindow("orxonox/KeyBindPane")
146
147    for k,v in pairs(commandList) do
148        local line = P.createLine(k)
149        table.insert(linesList, line)
150        window:addChildWindow(line)
151    end
152end
153
154function P.KeyBindButton_clicked(e)
155    local we = CEGUI.toWindowEventArgs(e)
156    local name = we.window:getName()
157
158    local match = string.gmatch(name, "%d+")
159    local commandNr = tonumber(match())
160    local buttonNr = tonumber(match())
161
162    local arguments = {}
163    arguments[1] = commandNr
164    arguments[2] = buttonNr
165    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
166end
167
168function P.KeyBindPlus_clicked(e)
169    local we = CEGUI.toWindowEventArgs(e)
170    local name = we.window:getName()
171
172    local match = string.gmatch(name, "%d+")
173    local commandNr = tonumber(match())
174
175    local arguments = {}
176    arguments[1] = commandNr
177    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
178end
179
180function P.KeyBindClear_clicked(e)
181    local we = CEGUI.toWindowEventArgs(e)
182    local name = we.window:getName()
183
184    local match = string.gmatch(name, "%d+")
185    local commandNr = tonumber(match())
186    local buttonNr = tonumber(match())
187 
188    orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
189
190    P.callback()
191end
192
193function P.keybind(arguments)
194    local commandNr = arguments[1]
195    local buttonNr = arguments[2]
196    if buttonNr ~= nil then
197        orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
198    end
199
200    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
201end
202
203function P.callback()
204    local pane = tolua.cast(winMgr:getWindow("orxonox/KeyBindPane"), "CEGUI::ScrollablePane")
205    local position = pane:getVerticalScrollPosition()
206    while table.getn(linesList) ~= 0 do
207        if linesList[1] ~= nil then
208            winMgr:destroyWindow(linesList[1]:getName())
209        end
210        table.remove(linesList, 1)
211    end
212
213    linesList = {}
214
215    P.createLines()
216    if(InfoPopup ~= nil) then
217        InfoPopup.close()
218    end
219    pane:setVerticalScrollPosition( position )
220end
221
222function P.KeyBindBackButton_clicked(e)
223    hideGUI("KeyBindMenu")
224end
225
226return P
Note: See TracBrowser for help on using the repository browser.