Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/data/gui/scripts/KeyBindMenu.lua @ 7232

Last change on this file since 7232 was 6773, checked in by rgrieder, 15 years ago

Eliminated all unnecessary global Lua variables and replaced them either with a local or a instance variable (P.myVar).

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