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/MultiplayerJoinButton"), |
---|
13 | ["function"] = P.MultiplayerJoinButton_clicked |
---|
14 | } |
---|
15 | P.buttonList[1] = item |
---|
16 | |
---|
17 | local item = { |
---|
18 | ["button"] = winMgr:getWindow("orxonox/MultiplayerHostButton"), |
---|
19 | ["function"] = P.MultiplayerHostButton_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.MultiplayerHostButton_clicked(e) |
---|
40 | showMenuSheet("HostMenu", true) |
---|
41 | end |
---|
42 | |
---|
43 | |
---|
44 | function P.MultiplayerJoinButton_clicked(e) |
---|
45 | local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem() |
---|
46 | if choice then |
---|
47 | local client = orxonox.Client:getInstance() |
---|
48 | local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID() |
---|
49 | client:setDestination( P.serverList[index][2], 55556 ) |
---|
50 | else |
---|
51 | return |
---|
52 | end |
---|
53 | orxonox.execute("startClient") |
---|
54 | hideAllMenuSheets() |
---|
55 | end |
---|
56 | |
---|
57 | function P.MultiplayerBackButton_clicked(e) |
---|
58 | hideMenuSheet(P.name) |
---|
59 | end |
---|
60 | |
---|
61 | function P.showServerList() |
---|
62 | local listbox = winMgr:getWindow("orxonox/MultiplayerListbox") |
---|
63 | CEGUI.toListbox(listbox):resetList() |
---|
64 | local discovery = orxonox.LANDiscovery:getInstance() |
---|
65 | discovery:discover() |
---|
66 | P.serverList = {} |
---|
67 | local index = 0 |
---|
68 | local servername = "" |
---|
69 | local serverip = "" |
---|
70 | while true do |
---|
71 | servername = discovery:getServerListItemName(index) |
---|
72 | if servername == "" then |
---|
73 | break |
---|
74 | end |
---|
75 | serverip = discovery:getServerListItemIP(index) |
---|
76 | if serverip == "" then |
---|
77 | break |
---|
78 | end |
---|
79 | table.insert(P.serverList, {servername, serverip}) |
---|
80 | index = index + 1 |
---|
81 | end |
---|
82 | index = 1 |
---|
83 | for k,v in pairs(P.serverList) do |
---|
84 | local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] ) |
---|
85 | item:setID(index) |
---|
86 | index = index + 1 |
---|
87 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
88 | CEGUI.toListbox(listbox):addItem(item) |
---|
89 | end |
---|
90 | end |
---|
91 | |
---|
92 | function P.onKeyPressed() |
---|
93 | buttonIteratorHelper(P.buttonList, code, P, 2, 2) |
---|
94 | end |
---|
95 | |
---|
96 | return P |
---|
97 | |
---|