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