1 | -- SingleplayerMenu.lua |
---|
2 | |
---|
3 | local P = createMenuSheet("SingleplayerMenu") |
---|
4 | P.loadAlong = {"ShipSelectionMenu","CampaignMenu"} |
---|
5 | P.levelList = {} |
---|
6 | P.activeTabIndexes = {} |
---|
7 | P.scrollbarWidth = 13 |
---|
8 | selectedlevel = {} -- level for ship selection |
---|
9 | |
---|
10 | function P.onLoad() |
---|
11 | P.createLevelList() |
---|
12 | |
---|
13 | -- create tabs with desired tab as argument (nil for all) |
---|
14 | P.createFilterTab("Gametypes", "gametype") |
---|
15 | P.createFilterTab("Missions", "mission") |
---|
16 | P.createFilterTab("Minigames", "minigame") |
---|
17 | P.createFilterTab("Showcases", "showcase") |
---|
18 | P.createFilterTab("Presentations", "presentation") |
---|
19 | P.createFilterTab("Tests", "test") |
---|
20 | P.createFilterTab("Show All", nil) |
---|
21 | |
---|
22 | |
---|
23 | -- update description and screenshot boxes |
---|
24 | P.SingleplayerSelectionChanged() |
---|
25 | |
---|
26 | --buttons are arranged in a 1x4 matrix |
---|
27 | P:setButton(1, 1, { |
---|
28 | ["button"] = winMgr:getWindow("orxonox/SingleplayerStartButton"), |
---|
29 | ["callback"] = P.SingleplayerStartButton_clicked |
---|
30 | }) |
---|
31 | |
---|
32 | P:setButton(1, 2, { |
---|
33 | ["button"] = winMgr:getWindow("orxonox/SingleplayerConfigButton"), |
---|
34 | ["callback"] = P.SingleplayerConfigButton_clicked |
---|
35 | }) |
---|
36 | |
---|
37 | P:setButton(1, 3, { |
---|
38 | ["button"] = winMgr:getWindow("orxonox/SingleplayerBackButton"), |
---|
39 | ["callback"] = P.SingleplayerBackButton_clicked |
---|
40 | }) |
---|
41 | P:setButton(1, 4,{ |
---|
42 | ["button"] = winMgr:getWindow("orxonox/CampaignButton"), |
---|
43 | ["callback"] = P.CampaignButton_clicked |
---|
44 | }) |
---|
45 | end |
---|
46 | |
---|
47 | function P.createLevelList() |
---|
48 | P.levelList = {} |
---|
49 | local size = orxonox.LevelManager:getInstance():getNumberOfLevels() |
---|
50 | local index = 0 |
---|
51 | local level = nil |
---|
52 | while index < size do |
---|
53 | level = orxonox.LevelManager:getInstance():getAvailableLevelListItem(index) |
---|
54 | if (level ~= nil and level:getXMLFilename() ~= "_temp.oxw") then |
---|
55 | local levelXMLFilename = level:getXMLFilename() |
---|
56 | -- create an imageset for each screenshot |
---|
57 | local imageName = level:getScreenshot() |
---|
58 | if imageName ~= "" then |
---|
59 | CEGUI.ImagesetManager:getSingleton():createFromImageFile(levelXMLFilename..imageName, imageName) |
---|
60 | else |
---|
61 | CEGUI.ImagesetManager:getSingleton():createFromImageFile(levelXMLFilename..imageName, "noscreenshot.png") |
---|
62 | end |
---|
63 | table.insert(P.levelList, level) |
---|
64 | end |
---|
65 | index = index + 1 |
---|
66 | end |
---|
67 | end |
---|
68 | |
---|
69 | function P.createFilterTab(name, tag) |
---|
70 | -- create unique tab window name |
---|
71 | local tabName = "orxonox/SingleplayerLevelTab" |
---|
72 | if tag ~= nil then |
---|
73 | tabName = tabName..tag |
---|
74 | end |
---|
75 | -- create new tab window with desired name |
---|
76 | local listbox = CEGUI.toListbox(winMgr:createWindow("MenuWidgets/Listbox", tabName)) |
---|
77 | listbox:setText(name) |
---|
78 | listbox:setProperty("UnifiedMaxSize", "{{1,0},{1,0}}") |
---|
79 | listbox:setProperty("UnifiedAreaRect", "{{0.05,0},{0.1,0},{0.5,0},{0.675,0}}") |
---|
80 | -- fill listbox with items |
---|
81 | listbox:resetList() |
---|
82 | orxonox.GUIManager:setItemTooltipsEnabledHelper(listbox, true) |
---|
83 | local preselect = orxonox.LevelManager:getInstance():getDefaultLevel() |
---|
84 | local tabIndexes = {} |
---|
85 | for k,v in pairs(P.levelList) do |
---|
86 | -- only add level if it has desired tag |
---|
87 | if tag == nil or v:hasTag(tag) then |
---|
88 | local item = CEGUI.createListboxTextItem(v:getName()) |
---|
89 | item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush") |
---|
90 | listbox:addItem(item) |
---|
91 | table.insert(tabIndexes, k) |
---|
92 | if v:getXMLFilename() == preselect then |
---|
93 | listbox:setItemSelectState(item, true) |
---|
94 | end |
---|
95 | orxonox.GUIManager:setTooltipTextHelper(item, v:getDescription()) |
---|
96 | end |
---|
97 | end |
---|
98 | table.insert(P.activeTabIndexes, tabIndexes) |
---|
99 | -- listen to selection changes |
---|
100 | orxonox.GUIManager:subscribeEventHelper(listbox, "ItemSelectionChanged", P.name..".SingleplayerSelectionChanged") |
---|
101 | local tabControl = winMgr:getWindow("orxonox/SingleplayerTabControl") |
---|
102 | orxonox.GUIManager:subscribeEventHelper(tabControl, "TabSelectionChanged", P.name..".SingleplayerSelectionChanged") |
---|
103 | if listbox:getItemCount() > 0 then |
---|
104 | tabControl:addChildWindow(tabName) |
---|
105 | end |
---|
106 | end |
---|
107 | |
---|
108 | function P.SingleplayerGetSelectedLevel() |
---|
109 | -- choose the active listbox |
---|
110 | local tabControl = CEGUI.toTabControl(winMgr:getWindow("orxonox/SingleplayerTabControl")) |
---|
111 | local listbox = CEGUI.toListbox(tabControl:getTabContentsAtIndex(tabControl:getSelectedTabIndex())) |
---|
112 | local choice = listbox:getFirstSelectedItem() |
---|
113 | if choice ~= nil then |
---|
114 | -- get the right tab and the right index |
---|
115 | local tabIndexes = P.activeTabIndexes[tabControl:getSelectedTabIndex()+1] |
---|
116 | local index = tabIndexes[listbox:getItemIndex(choice)+1] |
---|
117 | return P.levelList[index] |
---|
118 | else |
---|
119 | return nil |
---|
120 | end |
---|
121 | end |
---|
122 | |
---|
123 | function P.SingleplayerSelectionChanged(e) |
---|
124 | local levelImage = winMgr:getWindow("orxonox/SingleplayerLevelImage") |
---|
125 | local levelDescription = winMgr:getWindow("orxonox/SingleplayerLevelDescription") |
---|
126 | local configButton = winMgr:getWindow("orxonox/SingleplayerConfigButton") |
---|
127 | local level = P.SingleplayerGetSelectedLevel() |
---|
128 | if level ~= nil then |
---|
129 | local levelXMLFilename = level:getXMLFilename() |
---|
130 | local imageName = level:getScreenshot() |
---|
131 | -- set the screenshot and the description for the selected level |
---|
132 | levelImage:setProperty("Image", "set:"..levelXMLFilename..imageName.." image:full_image") |
---|
133 | levelDescription:setText(level:getDescription()) |
---|
134 | -- only enable config button for "gametype" levels |
---|
135 | if level:hasTag("gametype") then |
---|
136 | configButton:setProperty("Disabled", "False") |
---|
137 | else |
---|
138 | configButton:setProperty("Disabled", "True") |
---|
139 | end |
---|
140 | else |
---|
141 | -- also take care of "no level selected" |
---|
142 | levelImage:setProperty("Image", "") |
---|
143 | levelDescription:setText("") |
---|
144 | configButton:setProperty("Disabled", "True") |
---|
145 | end |
---|
146 | end |
---|
147 | |
---|
148 | function P.SingleplayerStartButton_clicked(e) |
---|
149 | selectedlevel = P.SingleplayerGetSelectedLevel() |
---|
150 | if selectedlevel ~= nil then |
---|
151 | if selectedlevel:hasTag("shipselection") then |
---|
152 | local shipSelectionMenu = showMenuSheet("ShipSelectionMenu", true) |
---|
153 | shipSelectionMenu:update() |
---|
154 | else |
---|
155 | orxonox.execute("startGame " .. selectedlevel:getXMLFilename()) |
---|
156 | hideAllMenuSheets() |
---|
157 | end |
---|
158 | end |
---|
159 | end |
---|
160 | |
---|
161 | function P.SingleplayerConfigButton_clicked(e) |
---|
162 | local level = P.SingleplayerGetSelectedLevel() |
---|
163 | if level ~= nil then |
---|
164 | local configMenu = showMenuSheet("SingleplayerConfigMenu") |
---|
165 | configMenu:loadConfig(level) |
---|
166 | end |
---|
167 | end |
---|
168 | |
---|
169 | function P.SingleplayerBackButton_clicked(e) |
---|
170 | hideMenuSheet(P.name) |
---|
171 | end |
---|
172 | |
---|
173 | function P.CampaignButton_clicked(e) |
---|
174 | showMenuSheet("CampaignMenu", true) |
---|
175 | end |
---|
176 | |
---|
177 | return P |
---|