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 | end |
---|
45 | |
---|
46 | function P.InternetButton_clicked(e) |
---|
47 | P.joinMode = 2 |
---|
48 | end |
---|
49 | |
---|
50 | function P.MultiplayerHostButton_clicked(e) |
---|
51 | showMenuSheet("HostMenu", true) |
---|
52 | end |
---|
53 | |
---|
54 | |
---|
55 | function P.MultiplayerJoinButton_clicked(e) |
---|
56 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
57 | |
---|
58 | if P.joinMode == 2 then |
---|
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 | else |
---|
69 | --wait for Sandro's function |
---|
70 | end |
---|
71 | |
---|
72 | end |
---|
73 | |
---|
74 | function P.MultiplayerBackButton_clicked(e) |
---|
75 | hideMenuSheet(P.name) |
---|
76 | end |
---|
77 | |
---|
78 | function P.showServerList() |
---|
79 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
80 | CEGUI.toListbox(listbox):resetList() |
---|
81 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
82 | discovery:discover() |
---|
83 | P.serverList = {} |
---|
84 | local index = 0 |
---|
85 | local servername = "" |
---|
86 | local serverip = "" |
---|
87 | while true do |
---|
88 | servername = discovery:getServerListItemName(index) |
---|
89 | if servername == "" then |
---|
90 | break |
---|
91 | end |
---|
92 | serverip = discovery:getServerListItemIP(index) |
---|
93 | if serverip == "" then |
---|
94 | break |
---|
95 | end |
---|
96 | table.insert(P.serverList, {servername, serverip}) |
---|
97 | index = index + 1 |
---|
98 | end |
---|
99 | index = 1 |
---|
100 | for k,v in pairs(P.serverList) do |
---|
101 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
102 | item:setID(index) |
---|
103 | index = index + 1 |
---|
104 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
105 | CEGUI.toListbox(listbox):addItem(item) |
---|
106 | end |
---|
107 | end |
---|
108 | |
---|
109 | function P.onKeyPressed() |
---|
110 | buttonIteratorHelper(P.buttonList, code, P, 2, 2) |
---|
111 | end |
---|
112 | |
---|
113 | return P |
---|
114 | |
---|