1 | -- MultiplayerMenu.lua |
---|
2 | |
---|
3 | local P = createMenuSheet("MultiplayerMenu") |
---|
4 | |
---|
5 | P.levelList = {} |
---|
6 | P.itemList = {} |
---|
7 | P.showAll = false |
---|
8 | |
---|
9 | function P.onLoad() |
---|
10 | P.multiplayerMode = "startClient" |
---|
11 | end |
---|
12 | |
---|
13 | function P.onShow() |
---|
14 | if P.multiplayerMode == "startClient" then |
---|
15 | local window = winMgr:getWindow("orxonox/MultiplayerJoinButton") |
---|
16 | local button = tolua.cast(window,"CEGUI::RadioButton") |
---|
17 | button:setSelected(true) |
---|
18 | P.showServerList() |
---|
19 | end |
---|
20 | if P.multiplayerMode == "startServer" then |
---|
21 | local window = winMgr:getWindow("orxonox/MultiplayerHostButton") |
---|
22 | local button = tolua.cast(window,"CEGUI::RadioButton") |
---|
23 | button:setSelected(true) |
---|
24 | P.showLevelList() |
---|
25 | end |
---|
26 | if P.multiplayerMode == "startDedicated" then |
---|
27 | local window = winMgr:getWindow("orxonox/MultiplayerDedicatedButton") |
---|
28 | local button = tolua.cast(window,"CEGUI::RadioButton") |
---|
29 | button:setSelected(true) |
---|
30 | P.showLevelList() |
---|
31 | end |
---|
32 | end |
---|
33 | |
---|
34 | function P.MultiplayerJoinButton_clicked(e) |
---|
35 | P.multiplayerMode = "startClient" |
---|
36 | P.showServerList() |
---|
37 | end |
---|
38 | |
---|
39 | function P.MultiplayerHostButton_clicked(e) |
---|
40 | P.multiplayerMode = "startServer" |
---|
41 | P.showLevelList() |
---|
42 | end |
---|
43 | |
---|
44 | function P.MultiplayerDedicatedButton_clicked(e) |
---|
45 | P.multiplayerMode = "startDedicated" |
---|
46 | P.showLevelList() |
---|
47 | end |
---|
48 | |
---|
49 | function P.MultiplayerStartButton_clicked(e) |
---|
50 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
51 | if P.multiplayerMode == "startClient" then |
---|
52 | if choice then |
---|
53 | local client = orxonox.Client:getInstance() |
---|
54 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
55 | client:setDestination( P.serverList[index][2], 55556 ) |
---|
56 | else |
---|
57 | return |
---|
58 | end |
---|
59 | else |
---|
60 | if choice then |
---|
61 | orxonox.LevelManager:getInstance():setDefaultLevel(choice:getText() .. ".oxw") |
---|
62 | else |
---|
63 | return |
---|
64 | end |
---|
65 | end |
---|
66 | orxonox.execute(P.multiplayerMode) |
---|
67 | hideAllMenuSheets() |
---|
68 | end |
---|
69 | |
---|
70 | function P.MultiplayerBackButton_clicked(e) |
---|
71 | hideMenuSheet(P.name) |
---|
72 | end |
---|
73 | |
---|
74 | function P.showLevelList() |
---|
75 | P.createLevelList() |
---|
76 | end |
---|
77 | |
---|
78 | function P.createLevelList() |
---|
79 | P.levelList = {} |
---|
80 | P.itemList = {} |
---|
81 | local listbox = CEGUI.toListbox(winMgr:getWindow("orxonox/MultiplayerListbox")) |
---|
82 | listbox:resetList() |
---|
83 | orxonox.GUIManager:setItemTooltipsEnabledHelper(listbox, true) |
---|
84 | local preselect = orxonox.LevelManager:getInstance():getDefaultLevel() |
---|
85 | local size = orxonox.LevelManager:getInstance():getNumberOfLevels() |
---|
86 | local index = 0 |
---|
87 | local level = nil |
---|
88 | while index < size do |
---|
89 | level = orxonox.LevelManager:getInstance():getAvailableLevelListItem(index) |
---|
90 | if level ~= nil then |
---|
91 | if P.showAll or not level:hasTag("test") then |
---|
92 | table.insert(P.levelList, level) |
---|
93 | end |
---|
94 | end |
---|
95 | index = index + 1 |
---|
96 | end |
---|
97 | --TODO: Reintroduce sorting, if needed. |
---|
98 | --table.sort(levelList) |
---|
99 | for k,v in pairs(P.levelList) do |
---|
100 | local item = CEGUI.createListboxTextItem(v:getName()) |
---|
101 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
102 | listbox:addItem(item) |
---|
103 | if v:getXMLFilename() == preselect then |
---|
104 | listbox:setItemSelectState(item, true) |
---|
105 | end |
---|
106 | P.itemList[k] = listbox:getListboxItemFromIndex(k-1) |
---|
107 | --TODO: The description as tooltip would be nice. |
---|
108 | --local lItem = P.itemList[k] |
---|
109 | --lItem:setTooltipText(v:getDescription()) |
---|
110 | orxonox.GUIManager:setTooltipTextHelper(P.itemList[k], v:getDescription()) |
---|
111 | end |
---|
112 | end |
---|
113 | |
---|
114 | function P.showServerList() |
---|
115 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
116 | CEGUI.toListbox(listbox):resetList() |
---|
117 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
118 | discovery:discover() |
---|
119 | P.serverList = {} |
---|
120 | local index = 0 |
---|
121 | local servername = "" |
---|
122 | local serverip = "" |
---|
123 | while true do |
---|
124 | servername = discovery:getServerListItemName(index) |
---|
125 | if servername == "" then |
---|
126 | break |
---|
127 | end |
---|
128 | serverip = discovery:getServerListItemIP(index) |
---|
129 | if serverip == "" then |
---|
130 | break |
---|
131 | end |
---|
132 | table.insert(P.serverList, {servername, serverip}) |
---|
133 | index = index + 1 |
---|
134 | end |
---|
135 | index = 1 |
---|
136 | for k,v in pairs(P.serverList) do |
---|
137 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
138 | item:setID(index) |
---|
139 | index = index + 1 |
---|
140 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
141 | CEGUI.toListbox(listbox):addItem(item) |
---|
142 | end |
---|
143 | end |
---|
144 | |
---|
145 | return P |
---|
146 | |
---|