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 0 |
---|
9 | P.joinMode = 0 |
---|
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 | end |
---|
41 | |
---|
42 | function P.LanButton_clicked(e) |
---|
43 | P.joinMode = 1 |
---|
44 | P.showServerList() |
---|
45 | end |
---|
46 | |
---|
47 | function P.InternetButton_clicked(e) |
---|
48 | P.joinMode = 2 |
---|
49 | P.showServerList() |
---|
50 | end |
---|
51 | |
---|
52 | function P.MultiplayerHostButton_clicked(e) |
---|
53 | showMenuSheet("HostMenu", true) |
---|
54 | end |
---|
55 | |
---|
56 | |
---|
57 | function P.MultiplayerJoinButton_clicked(e) |
---|
58 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
59 | if choice then |
---|
60 | local client = orxonox.Client:getInstance() |
---|
61 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
62 | client:setDestination( P.serverList[index][2], 55556 ) |
---|
63 | else |
---|
64 | return |
---|
65 | end |
---|
66 | orxonox.execute("startClient") |
---|
67 | hideAllMenuSheets() |
---|
68 | end |
---|
69 | |
---|
70 | function P.MultiplayerBackButton_clicked(e) |
---|
71 | hideMenuSheet(P.name) |
---|
72 | end |
---|
73 | |
---|
74 | function P.showServerList() |
---|
75 | -- LAN Discovery |
---|
76 | if P.joinMode == 1 then |
---|
77 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
78 | CEGUI.toListbox(listbox):resetList() |
---|
79 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
80 | discovery:discover() |
---|
81 | P.serverList = {} |
---|
82 | local index = 0 |
---|
83 | local servername = "" |
---|
84 | local serverip = "" |
---|
85 | while true do |
---|
86 | servername = discovery:getServerListItemName(index) |
---|
87 | if servername == "" then |
---|
88 | break |
---|
89 | end |
---|
90 | serverip = discovery:getServerListItemIP(index) |
---|
91 | if serverip == "" then |
---|
92 | break |
---|
93 | end |
---|
94 | table.insert(P.serverList, {servername, serverip}) |
---|
95 | index = index + 1 |
---|
96 | end |
---|
97 | index = 1 |
---|
98 | for k,v in pairs(P.serverList) do |
---|
99 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
100 | item:setID(index) |
---|
101 | index = index + 1 |
---|
102 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
103 | CEGUI.toListbox(listbox):addItem(item) |
---|
104 | end |
---|
105 | -- WAN Discovery |
---|
106 | elseif P.joinMode == 2 then |
---|
107 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
108 | CEGUI.toListbox(listbox):resetList() |
---|
109 | local discovery = orxonox.WANDiscovery:getInstance() |
---|
110 | cout(0, "discovering.\n" ) |
---|
111 | discovery:discover() |
---|
112 | cout(0, "discovered.\n" ) |
---|
113 | P.serverList = {} |
---|
114 | local index = 0 |
---|
115 | local servername = "" |
---|
116 | local serverip = "" |
---|
117 | while true do |
---|
118 | servername = discovery:getServerListItemName(index) |
---|
119 | if servername == "" then |
---|
120 | break |
---|
121 | end |
---|
122 | serverip = discovery:getServerListItemIP(index) |
---|
123 | if serverip == "" then |
---|
124 | break |
---|
125 | end |
---|
126 | table.insert(P.serverList, {servername, serverip}) |
---|
127 | index = index + 1 |
---|
128 | end |
---|
129 | index = 1 |
---|
130 | for k,v in pairs(P.serverList) do |
---|
131 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
132 | item:setID(index) |
---|
133 | index = index + 1 |
---|
134 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
135 | CEGUI.toListbox(listbox):addItem(item) |
---|
136 | end |
---|
137 | end |
---|
138 | |
---|
139 | end |
---|
140 | |
---|
141 | function P.onKeyPressed() |
---|
142 | buttonIteratorHelper(P.buttonList, code, P, 2, 2) |
---|
143 | end |
---|
144 | |
---|
145 | return P |
---|
146 | |
---|