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