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 lower items are both the back button |
---|
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, 1, { |
---|
26 | ["button"] = winMgr:getWindow("orxonox/MultiplayerBackButton"), |
---|
27 | ["callback"] = P.MultiplayerBackButton_clicked |
---|
28 | }) |
---|
29 | |
---|
30 | P:setButton(2, 2, P:getButton(2, 1)) |
---|
31 | end |
---|
32 | |
---|
33 | function P.onShow() |
---|
34 | --P.showServerList() |
---|
35 | |
---|
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 |
---|
46 | end |
---|
47 | |
---|
48 | function P.LanButton_clicked(e) |
---|
49 | local we = CEGUI.toWindowEventArgs(e) |
---|
50 | local button = tolua.cast(we.window,"CEGUI::RadioButton") |
---|
51 | P.joinMode = 1 |
---|
52 | if button:isSelected() == true then |
---|
53 | P.showServerList() |
---|
54 | end |
---|
55 | end |
---|
56 | |
---|
57 | function P.InternetButton_clicked(e) |
---|
58 | local we = CEGUI.toWindowEventArgs(e) |
---|
59 | local button = tolua.cast(we.window,"CEGUI::RadioButton") |
---|
60 | P.joinMode = 2 |
---|
61 | if button:isSelected() == true then |
---|
62 | P.showServerList() |
---|
63 | end |
---|
64 | end |
---|
65 | |
---|
66 | function P.MultiplayerHostButton_clicked(e) |
---|
67 | showMenuSheet("HostMenu", true) |
---|
68 | end |
---|
69 | |
---|
70 | |
---|
71 | function P.MultiplayerJoinButton_clicked(e) |
---|
72 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
73 | local destination = nil |
---|
74 | if choice then |
---|
75 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
76 | destination = P.serverList[index][2] |
---|
77 | else |
---|
78 | return |
---|
79 | end |
---|
80 | orxonox.execute("startClient " .. destination) |
---|
81 | hideAllMenuSheets() |
---|
82 | end |
---|
83 | |
---|
84 | function P.MultiplayerBackButton_clicked(e) |
---|
85 | hideMenuSheet(P.name) |
---|
86 | end |
---|
87 | |
---|
88 | function P.showServerList() |
---|
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 | |
---|
153 | end |
---|
154 | |
---|
155 | return P |
---|
156 | |
---|