Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menu/data/gui/scripts/MultiplayerMenu.lua @ 7687

Last change on this file since 7687 was 7687, checked in by konrad, 13 years ago

—function to iterate throgh a menusheet by using arrowkeys is added

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1-- MultiplayerMenu.lua
2
3local P = createMenuSheet("MultiplayerMenu")
4
5P.buttonList = {}
6
7function 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
29end
30
31function P.onShow()
32    P.showServerList()
33
34    --indices to iterate through buttonlist
35    P.oldindex = -2
36    P.index = -1
37end
38
39function P.MultiplayerJoinButton_clicked(e)
40    P.multiplayerMode = "startClient"
41    P.showServerList()
42end
43
44function P.MultiplayerHostButton_clicked(e)
45    P.multiplayerMode = "startServer"
46    P.showLevelList()
47end
48
49function P.MultiplayerDedicatedButton_clicked(e)
50    P.multiplayerMode = "startDedicated"
51    P.showLevelList()
52end
53
54function P.MultiplayerHostButton2_clicked(e)
55    showMenuSheet("HostMenu", true)
56end
57
58
59function 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()
70end
71
72function P.MultiplayerBackButton_clicked(e)
73    hideMenuSheet(P.name)
74end
75
76function 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   
106function 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
135end
136
137function P.onKeyPressed() 
138    buttonIteratorHelper(P.buttonList, code, P, 2, 2)
139end
140
141return P
142
Note: See TracBrowser for help on using the repository browser.