Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestate/data/gui/scripts/KeyBindMenu.lua @ 6569

Last change on this file since 6569 was 6564, checked in by rgrieder, 15 years ago

Changed use of CEGUI widgets: Instead of addressing a type with "TaharezLook/Button" I changed it to "MenuWidgets/MyWidget".
That allows to define multiple skins for the menu and simply load the one we like.
Furthermore the idea is to use "HUDWidgets/MyWidget" for HUD elements. But I haven't added that.

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