[6363] | 1 | -- MultiplayerMenu.lua |
---|
| 2 | |
---|
[6746] | 3 | local P = createMenuSheet("MultiplayerMenu") |
---|
[6363] | 4 | |
---|
[7663] | 5 | P.buttonList = {} |
---|
| 6 | |
---|
[6746] | 7 | function P.onLoad() |
---|
[7663] | 8 | P.multiplayerMode = "startClient" |
---|
| 9 | |
---|
[7687] | 10 | --button are arranged in a 2x2 matrix, the left lower item is nil |
---|
[7663] | 11 | local item = { |
---|
| 12 | ["button"] = winMgr:getWindow("orxonox/MultiplayerJoinButton2"), |
---|
[7687] | 13 | ["function"] = P.MultiplayerJoinButton2_clicked |
---|
[7663] | 14 | } |
---|
| 15 | P.buttonList[1] = item |
---|
| 16 | |
---|
| 17 | local item = { |
---|
| 18 | ["button"] = winMgr:getWindow("orxonox/MultiplayerHostButton2"), |
---|
[7687] | 19 | ["function"] = P.MultiplayerHostButton2_clicked |
---|
[7663] | 20 | } |
---|
| 21 | P.buttonList[2] = item |
---|
| 22 | |
---|
| 23 | local item = { |
---|
| 24 | ["button"] = winMgr:getWindow("orxonox/MultiplayerBackButton"), |
---|
| 25 | ["function"] = P.MultiplayerBackButton_clicked |
---|
| 26 | } |
---|
| 27 | P.buttonList[4] = item |
---|
| 28 | |
---|
[7163] | 29 | end |
---|
| 30 | |
---|
| 31 | function P.onShow() |
---|
[7587] | 32 | P.showServerList() |
---|
[7687] | 33 | |
---|
| 34 | --indices to iterate through buttonlist |
---|
[7663] | 35 | P.oldindex = -2 |
---|
| 36 | P.index = -1 |
---|
[7163] | 37 | end |
---|
| 38 | |
---|
| 39 | function P.MultiplayerJoinButton_clicked(e) |
---|
| 40 | P.multiplayerMode = "startClient" |
---|
| 41 | P.showServerList() |
---|
| 42 | end |
---|
| 43 | |
---|
| 44 | function P.MultiplayerHostButton_clicked(e) |
---|
| 45 | P.multiplayerMode = "startServer" |
---|
| 46 | P.showLevelList() |
---|
| 47 | end |
---|
| 48 | |
---|
| 49 | function P.MultiplayerDedicatedButton_clicked(e) |
---|
| 50 | P.multiplayerMode = "startDedicated" |
---|
| 51 | P.showLevelList() |
---|
| 52 | end |
---|
| 53 | |
---|
[7587] | 54 | function P.MultiplayerHostButton2_clicked(e) |
---|
| 55 | showMenuSheet("HostMenu", true) |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | function P.MultiplayerJoinButton2_clicked(e) |
---|
[7163] | 60 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
[7587] | 61 | if choice then |
---|
| 62 | local client = orxonox.Client:getInstance() |
---|
| 63 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
| 64 | client:setDestination( P.serverList[index][2], 55556 ) |
---|
[7163] | 65 | else |
---|
[7587] | 66 | return |
---|
[7163] | 67 | end |
---|
[7587] | 68 | orxonox.execute("startClient") |
---|
[7163] | 69 | hideAllMenuSheets() |
---|
| 70 | end |
---|
| 71 | |
---|
| 72 | function P.MultiplayerBackButton_clicked(e) |
---|
| 73 | hideMenuSheet(P.name) |
---|
| 74 | end |
---|
| 75 | |
---|
| 76 | function P.showLevelList() |
---|
| 77 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
| 78 | CEGUI.toListbox(listbox):resetList() |
---|
| 79 | local preselect = orxonox.LevelManager:getInstance():getDefaultLevel() |
---|
[6363] | 80 | orxonox.LevelManager:getInstance():compileAvailableLevelList() |
---|
| 81 | local levelList = {} |
---|
| 82 | local index = 0 |
---|
| 83 | local level = "" |
---|
| 84 | while true do |
---|
| 85 | level = orxonox.LevelManager:getInstance():getAvailableLevelListItem(index) |
---|
| 86 | if level == "" then |
---|
| 87 | break |
---|
| 88 | end |
---|
| 89 | table.insert(levelList, level) |
---|
| 90 | index = index + 1 |
---|
| 91 | end |
---|
| 92 | table.sort(levelList) |
---|
[7163] | 93 | index = 1 |
---|
[6363] | 94 | for k,v in pairs(levelList) do |
---|
[7163] | 95 | local item = CEGUI.createListboxTextItem(v) |
---|
[6746] | 96 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
[7163] | 97 | item:setID(index) |
---|
| 98 | index = index + 1 |
---|
[6363] | 99 | CEGUI.toListbox(listbox):addItem(item) |
---|
| 100 | if v .. ".oxw" == preselect then |
---|
| 101 | listbox:setItemSelectState(item, true) |
---|
| 102 | end |
---|
| 103 | end |
---|
| 104 | end |
---|
[7163] | 105 | |
---|
| 106 | function P.showServerList() |
---|
| 107 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
| 108 | CEGUI.toListbox(listbox):resetList() |
---|
| 109 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
| 110 | discovery:discover() |
---|
| 111 | P.serverList = {} |
---|
| 112 | local index = 0 |
---|
| 113 | local servername = "" |
---|
| 114 | local serverip = "" |
---|
| 115 | while true do |
---|
| 116 | servername = discovery:getServerListItemName(index) |
---|
| 117 | if servername == "" then |
---|
| 118 | break |
---|
| 119 | end |
---|
| 120 | serverip = discovery:getServerListItemIP(index) |
---|
| 121 | if serverip == "" then |
---|
| 122 | break |
---|
| 123 | end |
---|
| 124 | table.insert(P.serverList, {servername, serverip}) |
---|
| 125 | index = index + 1 |
---|
[6363] | 126 | end |
---|
[7163] | 127 | index = 1 |
---|
| 128 | for k,v in pairs(P.serverList) do |
---|
| 129 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
| 130 | item:setID(index) |
---|
| 131 | index = index + 1 |
---|
| 132 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
| 133 | CEGUI.toListbox(listbox):addItem(item) |
---|
[6363] | 134 | end |
---|
| 135 | end |
---|
| 136 | |
---|
[7663] | 137 | function P.onKeyPressed() |
---|
| 138 | buttonIteratorHelper(P.buttonList, code, P, 2, 2) |
---|
| 139 | end |
---|
| 140 | |
---|
[6363] | 141 | return P |
---|
| 142 | |
---|