[6363] | 1 | -- MultiplayerMenu.lua |
---|
| 2 | |
---|
[6746] | 3 | local P = createMenuSheet("MultiplayerMenu") |
---|
[6363] | 4 | |
---|
[7732] | 5 | --joinMode is 1 for choice "LAN" and 2 for "Internet" |
---|
[7801] | 6 | --initial status 1 |
---|
| 7 | P.joinMode = 1 |
---|
[7732] | 8 | |
---|
[6746] | 9 | function P.onLoad() |
---|
[7916] | 10 | P.multiplayerMode = "startClient" |
---|
[7689] | 11 | |
---|
[7924] | 12 | --button are arranged in a 2x2 matrix, the lower items are both the back button |
---|
[7922] | 13 | P:initButtons(2, 2) |
---|
| 14 | |
---|
| 15 | P:setButton(1, 1, { |
---|
[7689] | 16 | ["button"] = winMgr:getWindow("orxonox/MultiplayerJoinButton"), |
---|
[7922] | 17 | ["callback"] = P.MultiplayerJoinButton_clicked |
---|
| 18 | }) |
---|
[7689] | 19 | |
---|
[7922] | 20 | P:setButton(1, 2, { |
---|
[7689] | 21 | ["button"] = winMgr:getWindow("orxonox/MultiplayerHostButton"), |
---|
[7922] | 22 | ["callback"] = P.MultiplayerHostButton_clicked |
---|
| 23 | }) |
---|
[7689] | 24 | |
---|
[7924] | 25 | P:setButton(2, 1, { |
---|
[7689] | 26 | ["button"] = winMgr:getWindow("orxonox/MultiplayerBackButton"), |
---|
[7922] | 27 | ["callback"] = P.MultiplayerBackButton_clicked |
---|
| 28 | }) |
---|
[7924] | 29 | |
---|
| 30 | P:setButton(2, 2, P:getButton(2, 1)) |
---|
[7163] | 31 | end |
---|
| 32 | |
---|
| 33 | function P.onShow() |
---|
[7801] | 34 | --P.showServerList() |
---|
[7163] | 35 | |
---|
[7801] | 36 | if P.joinMode == 1 then |
---|
| 37 | local window = winMgr:getWindow("orxonox/MultiplayerLanButton") |
---|
| 38 | local button = tolua.cast(window,"CEGUI::RadioButton") |
---|
| 39 | button:setSelected(true) |
---|
| 40 | end |
---|
| 41 | if P.joinMode == 2 then |
---|
| 42 | local window = winMgr:getWindow("orxonox/MultiplayerInternetButton") |
---|
| 43 | local button = tolua.cast(window,"CEGUI::RadioButton") |
---|
| 44 | button:setSelected(true) |
---|
| 45 | end |
---|
[7163] | 46 | end |
---|
| 47 | |
---|
[7732] | 48 | function P.LanButton_clicked(e) |
---|
[7801] | 49 | local we = CEGUI.toWindowEventArgs(e) |
---|
| 50 | local button = tolua.cast(we.window,"CEGUI::RadioButton") |
---|
[7732] | 51 | P.joinMode = 1 |
---|
[7801] | 52 | if button:isSelected() == true then |
---|
[7916] | 53 | P.showServerList() |
---|
[7801] | 54 | end |
---|
[7732] | 55 | end |
---|
| 56 | |
---|
| 57 | function P.InternetButton_clicked(e) |
---|
[7801] | 58 | local we = CEGUI.toWindowEventArgs(e) |
---|
| 59 | local button = tolua.cast(we.window,"CEGUI::RadioButton") |
---|
[7732] | 60 | P.joinMode = 2 |
---|
[7801] | 61 | if button:isSelected() == true then |
---|
[7916] | 62 | P.showServerList() |
---|
| 63 | end |
---|
[7732] | 64 | end |
---|
| 65 | |
---|
[7163] | 66 | function P.MultiplayerHostButton_clicked(e) |
---|
[7689] | 67 | showMenuSheet("HostMenu", true) |
---|
[7163] | 68 | end |
---|
| 69 | |
---|
| 70 | |
---|
[7689] | 71 | function P.MultiplayerJoinButton_clicked(e) |
---|
[7876] | 72 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
| 73 | local destination = nil |
---|
[7801] | 74 | if choice then |
---|
| 75 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
[7876] | 76 | destination = P.serverList[index][2] |
---|
[7163] | 77 | else |
---|
[7801] | 78 | return |
---|
[7163] | 79 | end |
---|
[7876] | 80 | orxonox.execute("startClient " .. destination) |
---|
[7801] | 81 | hideAllMenuSheets() |
---|
[7163] | 82 | end |
---|
| 83 | |
---|
| 84 | function P.MultiplayerBackButton_clicked(e) |
---|
| 85 | hideMenuSheet(P.name) |
---|
| 86 | end |
---|
| 87 | |
---|
| 88 | function P.showServerList() |
---|
[7916] | 89 | -- LAN Discovery |
---|
| 90 | if P.joinMode == 1 then |
---|
| 91 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
| 92 | CEGUI.toListbox(listbox):resetList() |
---|
| 93 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
| 94 | discovery:discover() |
---|
| 95 | P.serverList = {} |
---|
| 96 | local index = 0 |
---|
| 97 | local servername = "" |
---|
| 98 | local serverip = "" |
---|
| 99 | while true do |
---|
| 100 | servername = discovery:getServerListItemName(index) |
---|
| 101 | if servername == "" then |
---|
| 102 | break |
---|
| 103 | end |
---|
| 104 | serverip = discovery:getServerListItemIP(index) |
---|
| 105 | if serverip == "" then |
---|
| 106 | break |
---|
| 107 | end |
---|
| 108 | table.insert(P.serverList, {servername, serverip}) |
---|
| 109 | index = index + 1 |
---|
| 110 | end |
---|
| 111 | index = 1 |
---|
| 112 | for k,v in pairs(P.serverList) do |
---|
| 113 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
| 114 | item:setID(index) |
---|
| 115 | index = index + 1 |
---|
| 116 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
| 117 | CEGUI.toListbox(listbox):addItem(item) |
---|
| 118 | end |
---|
| 119 | -- WAN Discovery |
---|
| 120 | elseif P.joinMode == 2 then |
---|
| 121 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
| 122 | CEGUI.toListbox(listbox):resetList() |
---|
| 123 | local discovery = orxonox.WANDiscovery:getInstance() |
---|
| 124 | cout(0, "discovering.\n" ) |
---|
| 125 | discovery:discover() |
---|
| 126 | cout(0, "discovered.\n" ) |
---|
| 127 | P.serverList = {} |
---|
| 128 | local index = 0 |
---|
| 129 | local servername = "" |
---|
| 130 | local serverip = "" |
---|
| 131 | while true do |
---|
| 132 | servername = discovery:getServerListItemName(index) |
---|
| 133 | if servername == "" then |
---|
| 134 | break |
---|
| 135 | end |
---|
| 136 | serverip = discovery:getServerListItemIP(index) |
---|
| 137 | if serverip == "" then |
---|
| 138 | break |
---|
| 139 | end |
---|
| 140 | table.insert(P.serverList, {servername, serverip}) |
---|
| 141 | index = index + 1 |
---|
| 142 | end |
---|
| 143 | index = 1 |
---|
| 144 | for k,v in pairs(P.serverList) do |
---|
| 145 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
| 146 | item:setID(index) |
---|
| 147 | index = index + 1 |
---|
| 148 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
| 149 | CEGUI.toListbox(listbox):addItem(item) |
---|
| 150 | end |
---|
| 151 | end |
---|
| 152 | |
---|
[6363] | 153 | end |
---|
| 154 | |
---|
| 155 | return P |
---|
| 156 | |
---|