Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6361 was 6355, checked in by dafrick, 15 years ago

Made KeyBindMenu less Resolution dependent, because of this the keybind buttons aren't vertically aligned anymore.
Also made scrolling (with the mouse wheel) (to some extent) invariant of the size of the ScrollablePane in the KeyBindManager.

  • Property svn:executable set to *
File size: 9.4 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
[6352]111    elseif( group == "MouseAxes" ) then
112        return "Mouse " .. string.sub(name, string.find(name, '.(.*)')+1) .. " " .. string.sub(name, 1, 1) .. "-Axes"
[6334]113    end
[6335]114    return key
[6334]115end
116
117function P.createLine(k)
118    local offset = 0
119    local line = winMgr:createWindow("DefaultWindow", "orxonox/KeyBindPane/Binding" .. k)
120    line:setHeight(CEGUI.UDim(0, lineHeight))
[6335]121    line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, lineHeight*(k-1))))
[6334]122
[6335]123    local command = winMgr:createWindow("TaharezLook/StaticText", "orxonox/KeyBindPane/Binding" .. k .. "/Command")
[6355]124    command:setText(nameList[k])
[6334]125    command:setSize(CEGUI.UVector2(CEGUI.UDim(0, commandWidth), CEGUI.UDim(1, 0)))
[6335]126    command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0)))
[6334]127    line:addChildWindow(command)
128    offset = offset + commandWidth + spaceWidth
[6335]129
[6334]130    local plus = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Plus")
131    plus:setSize(CEGUI.UVector2(CEGUI.UDim(0, addWidth), CEGUI.UDim(0.7, 0)))
132    plus:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
133    plus:setText("add")
134    orxonox.KeyBinderManager:getInstance():subscribeEventHelper(plus, "Clicked", P.filename .. ".KeyBindPlus_clicked")
135    line:addChildWindow(plus)
[6335]136    offset = offset + addWidth + spaceWidth
[6334]137
138    local numButtons = orxonox.KeyBinderManager:getInstance():getCurrent():getNumberOfBindings(commandList[k]);
139    for i=0,(numButtons-1) do
[6335]140        local button = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Button" .. i)
[6334]141        local name = orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[k],i)
142        name = P.KeyNameNiceifier(name)
[6335]143        button:setText(name)
[6355]144        sampleWindow:setText(name)
145        local size = getMinTextSize(sampleWindow)
146        local buttonWidth = size[2]
147        button:setSize(CEGUI.UVector2(CEGUI.UDim(0, buttonWidth), CEGUI.UDim(0.7, 0)))
148        button:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
[6335]149        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(button, "Clicked", P.filename .. ".KeyBindButton_clicked")
150        --button:subscribeScriptedEvent("EventClicked", P.filename .. ".KeyBindButton_clicked")
151        line:addChildWindow(button)
152        offset = offset + buttonWidth
[6334]153
154        local clear = winMgr:createWindow("TaharezLook/TabButton", "orxonox/KeyBindPane/Binding" .. k .. "/Clear" .. i)
155        clear:setSize(CEGUI.UVector2(CEGUI.UDim(0, clearWidth), CEGUI.UDim(0.7, 0)))
[6339]156        clear:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.15, 0)))
[6334]157        clear:setText("X")
158        orxonox.KeyBinderManager:getInstance():subscribeEventHelper(clear, "Clicked", P.filename .. ".KeyBindClear_clicked")
159        line:addChildWindow(clear)
160        offset = offset + clearWidth + spaceWidth
[6335]161    end
[6334]162
[6335]163    line:setWidth(CEGUI.UDim(0, offset+clearWidth))
[6334]164   
[6335]165    return line
[6334]166end
167
168function P.createLines()
169    local window = winMgr:getWindow("orxonox/KeyBindPane")
170
[6335]171    for k,v in pairs(commandList) do
[6334]172        local line = P.createLine(k)
[6335]173        table.insert(linesList, line)
[6334]174        window:addChildWindow(line)
175    end
[6355]176   
177    pane = tolua.cast(window, "CEGUI::ScrollablePane")
178    pane:setVerticalStepSize(getScrollingStepSize(window))
[6334]179end
180
181function P.KeyBindButton_clicked(e)
182    local we = CEGUI.toWindowEventArgs(e)
183    local name = we.window:getName()
184
[6335]185    local match = string.gmatch(name, "%d+")
[6334]186    local commandNr = tonumber(match())
[6335]187    local buttonNr = tonumber(match())
[6334]188
189    local arguments = {}
190    arguments[1] = commandNr
191    arguments[2] = buttonNr
[6335]192    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
193end
[6334]194
[6335]195function P.KeyBindPlus_clicked(e)
[6334]196    local we = CEGUI.toWindowEventArgs(e)
197    local name = we.window:getName()
[6335]198
[6334]199    local match = string.gmatch(name, "%d+")
200    local commandNr = tonumber(match())
[6335]201
[6334]202    local arguments = {}
203    arguments[1] = commandNr
[6335]204    openInfoPopup("Press any button/key or move a mouse/joystick axis.", KeyBindMenu.keybind, false, arguments)
205end
206
[6334]207function P.KeyBindClear_clicked(e)
208    local we = CEGUI.toWindowEventArgs(e)
209    local name = we.window:getName()
[6335]210
[6334]211    local match = string.gmatch(name, "%d+")
212    local commandNr = tonumber(match())
213    local buttonNr = tonumber(match())
214 
215    orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
[6335]216
[6334]217    P.callback()
218end
219
220function P.keybind(arguments)
221    local commandNr = arguments[1]
222    local buttonNr = arguments[2]
223    if buttonNr ~= nil then
224        orxonox.KeyBinderManager:getInstance():unbind(orxonox.KeyBinderManager:getInstance():getCurrent():getBinding(commandList[commandNr], buttonNr))
225    end
226
227    orxonox.KeyBinderManager:getInstance():keybind(commandList[commandNr])
228end
229
230function P.callback()
[6351]231    local pane = tolua.cast(winMgr:getWindow("orxonox/KeyBindPane"), "CEGUI::ScrollablePane")
232    local position = pane:getVerticalScrollPosition()
[6335]233    while table.getn(linesList) ~= 0 do
234        if linesList[1] ~= nil then
[6334]235            winMgr:destroyWindow(linesList[1]:getName())
236        end
237        table.remove(linesList, 1)
238    end
239
[6335]240    linesList = {}
241
[6334]242    P.createLines()
243    if(InfoPopup ~= nil) then
244        InfoPopup.close()
[6335]245    end
[6351]246    pane:setVerticalScrollPosition( position )
[6335]247end
[6334]248
[6335]249function P.KeyBindBackButton_clicked(e)
250    hideGUI("KeyBindMenu")
251end
[6334]252
[6335]253return P
Note: See TracBrowser for help on using the repository browser.