Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/gui/scripts/KeyBindMenu.lua @ 11487

Last change on this file since 11487 was 11353, checked in by patricwi, 8 years ago

merged HUD branch to trunk

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