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