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 2x2 matrix, the left lower item is nil |
---|
13 | P:initButtons(2, 2) |
---|
14 | |
---|
15 | P:setButton(1, 1, { |
---|
16 | ["button"] = winMgr:getWindow("orxonox/MultiplayerJoinButton"), |
---|
17 | ["callback"] = P.MultiplayerJoinButton_clicked |
---|
18 | }) |
---|
19 | |
---|
20 | P:setButton(1, 2, { |
---|
21 | ["button"] = winMgr:getWindow("orxonox/MultiplayerHostButton"), |
---|
22 | ["callback"] = P.MultiplayerHostButton_clicked |
---|
23 | }) |
---|
24 | |
---|
25 | P:setButton(2, 2, { |
---|
26 | ["button"] = winMgr:getWindow("orxonox/MultiplayerBackButton"), |
---|
27 | ["callback"] = P.MultiplayerBackButton_clicked |
---|
28 | }) |
---|
29 | end |
---|
30 | |
---|
31 | function P.onShow() |
---|
32 | --P.showServerList() |
---|
33 | |
---|
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 |
---|
44 | end |
---|
45 | |
---|
46 | function P.LanButton_clicked(e) |
---|
47 | local we = CEGUI.toWindowEventArgs(e) |
---|
48 | local button = tolua.cast(we.window,"CEGUI::RadioButton") |
---|
49 | P.joinMode = 1 |
---|
50 | if button:isSelected() == true then |
---|
51 | P.showServerList() |
---|
52 | end |
---|
53 | end |
---|
54 | |
---|
55 | function P.InternetButton_clicked(e) |
---|
56 | local we = CEGUI.toWindowEventArgs(e) |
---|
57 | local button = tolua.cast(we.window,"CEGUI::RadioButton") |
---|
58 | P.joinMode = 2 |
---|
59 | if button:isSelected() == true then |
---|
60 | P.showServerList() |
---|
61 | end |
---|
62 | end |
---|
63 | |
---|
64 | function P.MultiplayerHostButton_clicked(e) |
---|
65 | showMenuSheet("HostMenu", true) |
---|
66 | end |
---|
67 | |
---|
68 | |
---|
69 | function P.MultiplayerJoinButton_clicked(e) |
---|
70 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
71 | local destination = nil |
---|
72 | if choice then |
---|
73 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
74 | destination = P.serverList[index][2] |
---|
75 | else |
---|
76 | return |
---|
77 | end |
---|
78 | orxonox.execute("startClient " .. destination) |
---|
79 | hideAllMenuSheets() |
---|
80 | end |
---|
81 | |
---|
82 | function P.MultiplayerBackButton_clicked(e) |
---|
83 | hideMenuSheet(P.name) |
---|
84 | end |
---|
85 | |
---|
86 | function P.showServerList() |
---|
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 | |
---|
151 | end |
---|
152 | |
---|
153 | return P |
---|
154 | |
---|