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