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