1 | -- MultiplayerMenu.lua |
---|
2 | |
---|
3 | local P = createMenuSheet("MultiplayerMenu") |
---|
4 | |
---|
5 | P.buttonList = {} |
---|
6 | |
---|
7 | function P.onLoad() |
---|
8 | P.multiplayerMode = "startClient" |
---|
9 | |
---|
10 | --button are arranged in a 2x2 matrix, the left lower item is nil |
---|
11 | local item = { |
---|
12 | ["button"] = winMgr:getWindow("orxonox/MultiplayerJoinButton2"), |
---|
13 | ["function"] = P.MultiplayerJoinButton2_clicked |
---|
14 | } |
---|
15 | P.buttonList[1] = item |
---|
16 | |
---|
17 | local item = { |
---|
18 | ["button"] = winMgr:getWindow("orxonox/MultiplayerHostButton2"), |
---|
19 | ["function"] = P.MultiplayerHostButton2_clicked |
---|
20 | } |
---|
21 | P.buttonList[2] = item |
---|
22 | |
---|
23 | local item = { |
---|
24 | ["button"] = winMgr:getWindow("orxonox/MultiplayerBackButton"), |
---|
25 | ["function"] = P.MultiplayerBackButton_clicked |
---|
26 | } |
---|
27 | P.buttonList[4] = item |
---|
28 | |
---|
29 | end |
---|
30 | |
---|
31 | function P.onShow() |
---|
32 | P.showServerList() |
---|
33 | |
---|
34 | --indices to iterate through buttonlist |
---|
35 | P.oldindex = -2 |
---|
36 | P.index = -1 |
---|
37 | end |
---|
38 | |
---|
39 | function P.MultiplayerJoinButton_clicked(e) |
---|
40 | P.multiplayerMode = "startClient" |
---|
41 | P.showServerList() |
---|
42 | end |
---|
43 | |
---|
44 | function P.MultiplayerHostButton_clicked(e) |
---|
45 | P.multiplayerMode = "startServer" |
---|
46 | P.showLevelList() |
---|
47 | end |
---|
48 | |
---|
49 | function P.MultiplayerDedicatedButton_clicked(e) |
---|
50 | P.multiplayerMode = "startDedicated" |
---|
51 | P.showLevelList() |
---|
52 | end |
---|
53 | |
---|
54 | function P.MultiplayerHostButton2_clicked(e) |
---|
55 | showMenuSheet("HostMenu", true) |
---|
56 | end |
---|
57 | |
---|
58 | |
---|
59 | function P.MultiplayerJoinButton2_clicked(e) |
---|
60 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
61 | if choice then |
---|
62 | local client = orxonox.Client:getInstance() |
---|
63 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
64 | client:setDestination( P.serverList[index][2], 55556 ) |
---|
65 | else |
---|
66 | return |
---|
67 | end |
---|
68 | orxonox.execute("startClient") |
---|
69 | hideAllMenuSheets() |
---|
70 | end |
---|
71 | |
---|
72 | function P.MultiplayerBackButton_clicked(e) |
---|
73 | hideMenuSheet(P.name) |
---|
74 | end |
---|
75 | |
---|
76 | function P.showLevelList() |
---|
77 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
78 | CEGUI.toListbox(listbox):resetList() |
---|
79 | local preselect = orxonox.LevelManager:getInstance():getDefaultLevel() |
---|
80 | orxonox.LevelManager:getInstance():compileAvailableLevelList() |
---|
81 | local levelList = {} |
---|
82 | local index = 0 |
---|
83 | local level = "" |
---|
84 | while true do |
---|
85 | level = orxonox.LevelManager:getInstance():getAvailableLevelListItem(index) |
---|
86 | if level == "" then |
---|
87 | break |
---|
88 | end |
---|
89 | table.insert(levelList, level) |
---|
90 | index = index + 1 |
---|
91 | end |
---|
92 | table.sort(levelList) |
---|
93 | index = 1 |
---|
94 | for k,v in pairs(levelList) do |
---|
95 | local item = CEGUI.createListboxTextItem(v) |
---|
96 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
97 | item:setID(index) |
---|
98 | index = index + 1 |
---|
99 | CEGUI.toListbox(listbox):addItem(item) |
---|
100 | if v .. ".oxw" == preselect then |
---|
101 | listbox:setItemSelectState(item, true) |
---|
102 | end |
---|
103 | end |
---|
104 | end |
---|
105 | |
---|
106 | function P.showServerList() |
---|
107 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
108 | CEGUI.toListbox(listbox):resetList() |
---|
109 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
110 | discovery:discover() |
---|
111 | P.serverList = {} |
---|
112 | local index = 0 |
---|
113 | local servername = "" |
---|
114 | local serverip = "" |
---|
115 | while true do |
---|
116 | servername = discovery:getServerListItemName(index) |
---|
117 | if servername == "" then |
---|
118 | break |
---|
119 | end |
---|
120 | serverip = discovery:getServerListItemIP(index) |
---|
121 | if serverip == "" then |
---|
122 | break |
---|
123 | end |
---|
124 | table.insert(P.serverList, {servername, serverip}) |
---|
125 | index = index + 1 |
---|
126 | end |
---|
127 | index = 1 |
---|
128 | for k,v in pairs(P.serverList) do |
---|
129 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
130 | item:setID(index) |
---|
131 | index = index + 1 |
---|
132 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
133 | CEGUI.toListbox(listbox):addItem(item) |
---|
134 | end |
---|
135 | end |
---|
136 | |
---|
137 | function P.onKeyPressed() |
---|
138 | buttonIteratorHelper(P.buttonList, code, P, 2, 2) |
---|
139 | end |
---|
140 | |
---|
141 | return P |
---|
142 | |
---|