Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/data/gui/scripts/KeyBindMenu.lua @ 8534

Last change on this file since 8534 was 7928, checked in by landauf, 14 years ago

more improvements for keyboard control of menus:

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