| 1 | -- SingleplayerMenu.lua | 
|---|
| 2 |  | 
|---|
| 3 | local P = createMenuSheet("SingleplayerConfigMenu") | 
|---|
| 4 |  | 
|---|
| 5 | P.commandList = {} | 
|---|
| 6 | P.nameList = {} | 
|---|
| 7 | P.linesList = {} | 
|---|
| 8 |  | 
|---|
| 9 | P.sampleWindow = nil | 
|---|
| 10 |  | 
|---|
| 11 | P.lineHeight = 0 | 
|---|
| 12 | P.commandWidth = 0 | 
|---|
| 13 | P.editboxWidth = 0 | 
|---|
| 14 | P.resetWidth = 0 | 
|---|
| 15 | P.spaceWidth = 0 | 
|---|
| 16 |  | 
|---|
| 17 | function P.onLoad() | 
|---|
| 18 |     P.sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/SingleplayerConfigMenu/MiscConfigPane/SampleWindow") | 
|---|
| 19 |     P.sampleWindow:setText("SampleText") | 
|---|
| 20 |  | 
|---|
| 21 |     P:setButton(1, 1, { | 
|---|
| 22 |             ["button"] = winMgr:getWindow("orxonox/SingleplayerConfigMenu/CancelButton"), | 
|---|
| 23 |             ["callback"]  = P.SingleplayerConfigCancelButton_clicked | 
|---|
| 24 |     }) | 
|---|
| 25 |      | 
|---|
| 26 |     P:setButton(1, 2, { | 
|---|
| 27 |             ["button"] = winMgr:getWindow("orxonox/SingleplayerConfigMenu/OKButton"), | 
|---|
| 28 |             ["callback"]  = P.SingleplayerConfigOKButton_clicked | 
|---|
| 29 |     }) | 
|---|
| 30 | end | 
|---|
| 31 |  | 
|---|
| 32 | function P.loadConfig(level) | 
|---|
| 33 |     P.commandList = {} | 
|---|
| 34 |     table.insert(P.commandList, "Gametype initialStartCountdown_") | 
|---|
| 35 |     table.insert(P.commandList, "Gametype bAutoStart_") | 
|---|
| 36 |     table.insert(P.commandList, "Gametype numberOfBots_") | 
|---|
| 37 |     table.insert(P.commandList, "Dynamicmatch gameTime_") | 
|---|
| 38 |     table.insert(P.commandList, "Dynamicmatch friendlyfire") | 
|---|
| 39 |     table.insert(P.commandList, "Dynamicmatch tutorial") | 
|---|
| 40 |     table.insert(P.commandList, "LastManStanding lives") | 
|---|
| 41 |     table.insert(P.commandList, "LastManStanding respawnDelay") | 
|---|
| 42 |     table.insert(P.commandList, "LastManStanding bNoPunishment") | 
|---|
| 43 |     table.insert(P.commandList, "LastManStanding bHardPunishment") | 
|---|
| 44 |     table.insert(P.commandList, "LastTeamStanding lives") | 
|---|
| 45 |     table.insert(P.commandList, "LastTeamStanding respawnDelay") | 
|---|
| 46 |     table.insert(P.commandList, "LastTeamStanding bNoPunishment") | 
|---|
| 47 |     table.insert(P.commandList, "LastTeamStanding bHardPunishment") | 
|---|
| 48 |     table.insert(P.commandList, "TeamDeathmatch teams_") | 
|---|
| 49 |     table.insert(P.commandList, "UnderAttack gameTime_") | 
|---|
| 50 |  | 
|---|
| 51 |     P.nameList = {} | 
|---|
| 52 |     table.insert(P.nameList, "Start countdown") | 
|---|
| 53 |     table.insert(P.nameList, "Autostart") | 
|---|
| 54 |     table.insert(P.nameList, "Number of Bots") | 
|---|
| 55 |     table.insert(P.nameList, "Dynamicmatch: game time") | 
|---|
| 56 |     table.insert(P.nameList, "Dynamicmatch: friendly fire") | 
|---|
| 57 |     table.insert(P.nameList, "Dynamicmatch: tutorial") | 
|---|
| 58 |     table.insert(P.nameList, "LastManStanding: lives") | 
|---|
| 59 |     table.insert(P.nameList, "LastManStanding: respawn delay") | 
|---|
| 60 |     table.insert(P.nameList, "LastManStanding: no punishment") | 
|---|
| 61 |     table.insert(P.nameList, "LastManStanding: hard punishment") | 
|---|
| 62 |     table.insert(P.nameList, "LastTeamStanding: lives") | 
|---|
| 63 |     table.insert(P.nameList, "LastTeamStanding: respawn delay") | 
|---|
| 64 |     table.insert(P.nameList, "LastTeamStanding: no punishment") | 
|---|
| 65 |     table.insert(P.nameList, "LastTeamStanding: hard punishment") | 
|---|
| 66 |     table.insert(P.nameList, "TeamDeathmatch: Number of teams") | 
|---|
| 67 |     table.insert(P.nameList, "UnderAttack: game time") | 
|---|
| 68 |  | 
|---|
| 69 |     P.linesList = {} | 
|---|
| 70 |      | 
|---|
| 71 |     --Calculate design parameters: | 
|---|
| 72 |     local size = getMinTextSize(P.sampleWindow) | 
|---|
| 73 |     P.lineHeight = size[1] | 
|---|
| 74 |  | 
|---|
| 75 |     P.commandWidth = 0 | 
|---|
| 76 |     for k,v in pairs(P.commandList) do | 
|---|
| 77 |         P.sampleWindow:setText(P.nameList[k]) | 
|---|
| 78 |         size = getMinTextSize(P.sampleWindow) | 
|---|
| 79 |         if size[2] > P.commandWidth then | 
|---|
| 80 |             P.commandWidth = size[2] | 
|---|
| 81 |         end | 
|---|
| 82 |     end | 
|---|
| 83 |  | 
|---|
| 84 |     P.sampleWindow:setText("reset") | 
|---|
| 85 |     size = getMinTextSize(P.sampleWindow) | 
|---|
| 86 |     P.resetWidth = size[2]+20 | 
|---|
| 87 |  | 
|---|
| 88 |     P.spaceWidth = 10 | 
|---|
| 89 |      | 
|---|
| 90 |     local pane = tolua.cast(winMgr:getWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane"), "CEGUI::ScrollablePane") | 
|---|
| 91 |     size = pane:getViewableArea() | 
|---|
| 92 |     P.editboxWidth = size:getWidth() - P.commandWidth - P.resetWidth - 5*P.spaceWidth | 
|---|
| 93 |  | 
|---|
| 94 |     P.createLines() | 
|---|
| 95 | end | 
|---|
| 96 |  | 
|---|
| 97 | function P.createLine(k) | 
|---|
| 98 |     local offset = 0 | 
|---|
| 99 |     -- destroy config line, if it already exists (otherwise would cause an error) | 
|---|
| 100 |     if winMgr:isWindowPresent("orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k) then | 
|---|
| 101 |         winMgr:destroyWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k) | 
|---|
| 102 |     end | 
|---|
| 103 |     -- content window for the entire line | 
|---|
| 104 |     local line = winMgr:createWindow("DefaultWindow", "orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k) | 
|---|
| 105 |     line:setHeight(CEGUI.UDim(0, P.lineHeight)) | 
|---|
| 106 |     line:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, P.lineHeight*(k-1)))) | 
|---|
| 107 |  | 
|---|
| 108 |     -- config name | 
|---|
| 109 |     local command = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k .. "/Command") | 
|---|
| 110 |     command:setText(P.nameList[k]) | 
|---|
| 111 |     command:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.commandWidth), CEGUI.UDim(1, 0))) | 
|---|
| 112 |     command:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0, 0))) | 
|---|
| 113 |     line:addChildWindow(command) | 
|---|
| 114 |     offset = offset + P.commandWidth + P.spaceWidth | 
|---|
| 115 |  | 
|---|
| 116 |     -- config value (editable) | 
|---|
| 117 |     local configvalue = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k .. "/Configvalue") | 
|---|
| 118 |     configvalue:setProperty("ReadOnly", "set:False") | 
|---|
| 119 |     local value = orxonox.CommandExecutor:query("getConfig " .. P.commandList[k]) | 
|---|
| 120 |     configvalue:setText(value) | 
|---|
| 121 |     configvalue:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.editboxWidth), CEGUI.UDim(0.9, 0))) | 
|---|
| 122 |     configvalue:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.05, 0))) | 
|---|
| 123 |     -- enable the reset button if the value changed | 
|---|
| 124 |     orxonox.GUIManager:subscribeEventHelper(configvalue, "TextAccepted", P.name .. ".SingleplayerConfigEditbox_textAccepted") | 
|---|
| 125 |     line:addChildWindow(configvalue) | 
|---|
| 126 |     offset = offset + P.editboxWidth + P.spaceWidth | 
|---|
| 127 |  | 
|---|
| 128 |     -- reset button (only available when value changed) | 
|---|
| 129 |     local reset = winMgr:createWindow("MenuWidgets/Button", "orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k .. "/Reset") | 
|---|
| 130 |     reset:setSize(CEGUI.UVector2(CEGUI.UDim(0, P.resetWidth), CEGUI.UDim(0.9, 0))) | 
|---|
| 131 |     reset:setPosition(CEGUI.UVector2(CEGUI.UDim(0, offset), CEGUI.UDim(0.05, 0))) | 
|---|
| 132 |     reset:setText("reset") | 
|---|
| 133 |     orxonox.GUIManager:subscribeEventHelper(reset, "Clicked", P.name .. ".SingleplayerConfigResetButton_clicked") | 
|---|
| 134 |     line:addChildWindow(reset) | 
|---|
| 135 |     reset:setEnabled(false) | 
|---|
| 136 |     offset = offset + P.resetWidth + P.spaceWidth | 
|---|
| 137 |  | 
|---|
| 138 |     line:setWidth(CEGUI.UDim(0, offset)) | 
|---|
| 139 |  | 
|---|
| 140 |     return line | 
|---|
| 141 | end | 
|---|
| 142 |  | 
|---|
| 143 | function P.createLines() | 
|---|
| 144 |     local window = winMgr:getWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane") | 
|---|
| 145 |  | 
|---|
| 146 |     for k,v in pairs(P.commandList) do | 
|---|
| 147 |         local line = P.createLine(k) | 
|---|
| 148 |         table.insert(P.linesList, line) | 
|---|
| 149 |         window:addChildWindow(line) | 
|---|
| 150 |     end | 
|---|
| 151 |  | 
|---|
| 152 |     local pane = tolua.cast(window, "CEGUI::ScrollablePane") | 
|---|
| 153 |     pane:setVerticalStepSize(getScrollingStepSize(window)) | 
|---|
| 154 | end | 
|---|
| 155 |  | 
|---|
| 156 | function P.SingleplayerConfigOKButton_clicked(e) | 
|---|
| 157 |     for k,v in pairs(P.commandList) do | 
|---|
| 158 |         -- save the changes | 
|---|
| 159 |         local editbox = winMgr:getWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k .. "/Configvalue") | 
|---|
| 160 |         orxonox.CommandExecutor:execute("config " .. P.commandList[k] .. " " .. editbox:getText()) | 
|---|
| 161 |         local resetButton = winMgr:getWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. k .. "/Reset") | 
|---|
| 162 |         resetButton:setEnabled(false) | 
|---|
| 163 |     end | 
|---|
| 164 |      | 
|---|
| 165 |     hideMenuSheet("SingleplayerConfigMenu") | 
|---|
| 166 | end | 
|---|
| 167 |  | 
|---|
| 168 | function P.SingleplayerConfigCancelButton_clicked(e) | 
|---|
| 169 |     hideMenuSheet("SingleplayerConfigMenu") | 
|---|
| 170 | end | 
|---|
| 171 |  | 
|---|
| 172 | function P.SingleplayerConfigEditbox_textAccepted(e) | 
|---|
| 173 |     local we = CEGUI.toWindowEventArgs(e) | 
|---|
| 174 |     local name = we.window:getName() | 
|---|
| 175 |  | 
|---|
| 176 |     local match = string.gmatch(name, "%d+") | 
|---|
| 177 |     local commandNr = tonumber(match()) | 
|---|
| 178 |  | 
|---|
| 179 |     local resetButton = winMgr:getWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. commandNr .. "/Reset") | 
|---|
| 180 |     resetButton:setEnabled(true) | 
|---|
| 181 | end | 
|---|
| 182 |  | 
|---|
| 183 | function P.SingleplayerConfigResetButton_clicked(e) | 
|---|
| 184 |     local we = CEGUI.toWindowEventArgs(e) | 
|---|
| 185 |     local name = we.window:getName() | 
|---|
| 186 |  | 
|---|
| 187 |     local match = string.gmatch(name, "%d+") | 
|---|
| 188 |     local commandNr = tonumber(match()) | 
|---|
| 189 |  | 
|---|
| 190 |     -- reload the old value | 
|---|
| 191 |     local editbox = winMgr:getWindow("orxonox/SingleplayerConfigMenu/MiscConfigPane/ConfigCommand" .. commandNr .. "/Configvalue") | 
|---|
| 192 |     local value = orxonox.CommandExecutor:query("getConfig " .. P.commandList[commandNr]) | 
|---|
| 193 |     editbox:setText(value) | 
|---|
| 194 |      | 
|---|
| 195 |     we.window:setEnabled(false) | 
|---|
| 196 | end | 
|---|
| 197 |  | 
|---|
| 198 | return P | 
|---|