Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/data/gui/scripts/MultiplayerMenu.lua @ 7922

Last change on this file since 7922 was 7922, checked in by landauf, 14 years ago

implemented new keyboard control of menu buttons with these new features:

  • more intuitive placement of buttons in table (row/column format instead of linear index)
  • no need to overwrite onShow() and onKeyPressed() functions, no need for P.buttonList
  • highlights the selected button in a different layout than mouse hovering
  • remembers the selection while moving through the menu hierarchy, but resets it if the menu is closed
  • allows preselected button (for example "Yes" in decision popup)
  • when opening a menu, the first selected button is not always the upper left, but instead depends on the pressed key (e.g. the 'up' key selects the button at the bottom, while the 'down' key selects the button at the top. once a button is selected, the keys behave as usual)

+ fixed wrong callback function in ingame menu

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1-- MultiplayerMenu.lua
2
3local P = createMenuSheet("MultiplayerMenu")
4
5--joinMode is 1 for choice "LAN" and 2 for "Internet"
6--initial status 1
7P.joinMode = 1
8
9function P.onLoad()
10    P.multiplayerMode = "startClient"
11
12    --button are arranged in a 2x2 matrix, the left lower item is nil
13    P:initButtons(2, 2)
14
15    P:setButton(1, 1, {
16            ["button"] = winMgr:getWindow("orxonox/MultiplayerJoinButton"),
17            ["callback"]  = P.MultiplayerJoinButton_clicked
18    })
19
20    P:setButton(1, 2, {
21            ["button"] = winMgr:getWindow("orxonox/MultiplayerHostButton"),
22            ["callback"]  = P.MultiplayerHostButton_clicked
23    })
24
25    P:setButton(2, 2, {
26            ["button"] = winMgr:getWindow("orxonox/MultiplayerBackButton"),
27            ["callback"]  = P.MultiplayerBackButton_clicked
28    })
29end
30
31function P.onShow()
32    --P.showServerList()
33
34    if P.joinMode == 1 then
35        local window = winMgr:getWindow("orxonox/MultiplayerLanButton")
36        local button = tolua.cast(window,"CEGUI::RadioButton")
37        button:setSelected(true)
38    end
39    if P.joinMode == 2 then
40        local window = winMgr:getWindow("orxonox/MultiplayerInternetButton")
41        local button = tolua.cast(window,"CEGUI::RadioButton")
42        button:setSelected(true)
43    end
44end
45
46function P.LanButton_clicked(e)
47    local we = CEGUI.toWindowEventArgs(e)
48    local button = tolua.cast(we.window,"CEGUI::RadioButton")
49    P.joinMode = 1
50    if button:isSelected() == true then
51        P.showServerList()
52    end
53end
54
55function P.InternetButton_clicked(e)
56    local we = CEGUI.toWindowEventArgs(e)
57    local button = tolua.cast(we.window,"CEGUI::RadioButton")
58    P.joinMode = 2
59    if button:isSelected() == true then
60        P.showServerList()
61    end
62end
63
64function P.MultiplayerHostButton_clicked(e)
65    showMenuSheet("HostMenu", true)
66end
67
68
69function P.MultiplayerJoinButton_clicked(e)
70    local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem()
71    local destination = nil
72    if choice then
73        local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID()
74        destination = P.serverList[index][2]
75    else
76        return
77    end
78    orxonox.execute("startClient " .. destination)
79    hideAllMenuSheets()
80end
81
82function P.MultiplayerBackButton_clicked(e)
83    hideMenuSheet(P.name)
84end
85
86function P.showServerList()
87    -- LAN Discovery
88    if P.joinMode == 1 then
89        local listbox = winMgr:getWindow("orxonox/MultiplayerListbox")
90        CEGUI.toListbox(listbox):resetList()
91        local discovery = orxonox.LANDiscovery:getInstance()
92        discovery:discover()
93        P.serverList = {}
94        local index = 0
95        local servername = ""
96        local serverip = ""
97        while true do
98            servername = discovery:getServerListItemName(index)
99            if servername == "" then
100                break
101            end
102            serverip = discovery:getServerListItemIP(index)
103            if serverip == "" then
104                break
105            end
106            table.insert(P.serverList, {servername, serverip})
107            index = index + 1
108        end
109        index = 1
110        for k,v in pairs(P.serverList) do
111            local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] )
112            item:setID(index)
113            index = index + 1
114            item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush")
115            CEGUI.toListbox(listbox):addItem(item)
116        end
117    -- WAN Discovery
118    elseif P.joinMode == 2 then
119        local listbox = winMgr:getWindow("orxonox/MultiplayerListbox")
120        CEGUI.toListbox(listbox):resetList()
121        local discovery = orxonox.WANDiscovery:getInstance()
122        cout(0, "discovering.\n" )
123        discovery:discover()
124        cout(0, "discovered.\n" )
125        P.serverList = {}
126        local index = 0
127        local servername = ""
128        local serverip = ""
129        while true do
130            servername = discovery:getServerListItemName(index)
131            if servername == "" then
132                break
133            end
134            serverip = discovery:getServerListItemIP(index)
135            if serverip == "" then
136                break
137            end
138            table.insert(P.serverList, {servername, serverip})
139            index = index + 1
140        end
141        index = 1
142        for k,v in pairs(P.serverList) do
143            local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] )
144            item:setID(index)
145            index = index + 1
146            item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush")
147            CEGUI.toListbox(listbox):addItem(item)
148        end
149    end
150
151end
152
153return P
154
Note: See TracBrowser for help on using the repository browser.