Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6362 was 6362, checked in by scheusso, 15 years ago

joystick names are pretty now

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