Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/main_menu.lua

raw
1 --[[ Copyright (c) 2010-2014 Manuel "Roujin" Wolf, Edvin "Lego3" Linge
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Class for main menu window.
22 class "UIMainMenu" (UIResizable)
23
24 ---@type UIMainMenu
25 local UIMainMenu = _G["UIMainMenu"]
26
27 local col_bg = {
28 red = 154,
29 green = 146,
30 blue = 198,
31 }
32
33 local label_ttf_col = { -- slightly translucent
34 red = 255,
35 green = 255,
36 blue = 255,
37 alpha = 192,
38 }
39
40 local menu_item_height = 40
41
42 function UIMainMenu:UIMainMenu(ui)
43
44 -- First define all menu entries with a label, a callback and a tooltip.
45 -- That way we can call the UIResizable constructor with a good height argument.
46 local menu_items = {
47 {_S.main_menu.new_game, self.buttonNewGame, _S.tooltip.main_menu.new_game},
48 {_S.main_menu.custom_campaign, self.buttonCustomCampaign, _S.tooltip.main_menu.custom_campaign},
49 {_S.main_menu.custom_level, self.buttonCustomGame, _S.tooltip.main_menu.custom_level},
50 {_S.main_menu.continue, self.buttonContinueGame, _S.tooltip.main_menu.continue},
51 {_S.main_menu.load_game, self.buttonLoadGame, _S.tooltip.main_menu.load_game},
52 {_S.main_menu.options, self.buttonOptions, _S.tooltip.main_menu.options},
53 {_S.main_menu.map_edit, self.buttonMapEdit, _S.tooltip.main_menu.map_edit},
54 {_S.main_menu.exit, self.buttonExit, _S.tooltip.main_menu.exit}
55 }
56 self.no_menu_entries = #menu_items
57
58 --! Work out the size of non-false items in array
59 --!param items (list) what is to be checked
60 --!return calculated size
61 local function computeSize(items)
62 local size = 0
63 for _, item in pairs(items) do
64 size = item and (size + 1) or size
65 end
66 return size
67 end
68
69 -- Work out the menu's height, giving extra space for the version information
70 local line_height = 15
71 local top_padding = 20
72 local release_string = TheApp:getReleaseString()
73 local num_lines = computeSize({TheApp:isUpdateCheckDisabledByConfig(), TheApp.config.debug, release_string}) -- see UIMainMenu:draw for how these are used
74 local bottom_text_height = (line_height * num_lines)
75 self.height = top_padding + ((menu_item_height + 10) * #menu_items) + bottom_text_height
76
77 self:UIResizable(ui, 200, self.height, col_bg)
78
79 self.esc_closes = false
80 self.modal_class = "main menu"
81 self.on_top = true
82 self:setDefaultPosition(0.5, 0.25)
83
84 -- The main menu also shows the version number of the player's copy of the game.
85 self.label_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, {ttf_color = label_ttf_col, apply_ui_scale = true})
86 self.release_string = release_string
87
88 -- individual buttons
89 self.default_button_sound = "selectx.wav"
90
91 local next_y = 20
92 for _, item in ipairs(menu_items) do
93 next_y = self:addMenuItem(item[1], item[2], item[3], next_y)
94 end
95 end
96
97 --! Adds a single menu item to the main menu.
98 --!param label (string) The (localized) label to use for the new button.
99 --!param callback (function) Function to call when the user clicks the button.
100 --!param tooltip (string) Text to show when the player hovers over the button.
101 --!param y_pos (integer) Y-position from where to add the menu item.
102 --!return (integer) Y-position below which more items can be added.
103 -- This function has added a menu item between y_pos and the return value.
104 function UIMainMenu:addMenuItem(label, callback, tooltip, y_pos)
105 self:addBevelPanel(20, y_pos, 160, menu_item_height, col_bg)
106 :setLabel(label):makeButton(0, 0, 160, menu_item_height, nil, callback)
107 :setTooltip(tooltip)
108 return y_pos + menu_item_height + 10
109 end
110
111 function UIMainMenu:getSavedWindowPositionName()
112 return "main_menu_group"
113 end
114
115 function UIMainMenu:draw(canvas, x, y)
116 UIResizable.draw(self, canvas, x, y)
117 local s = TheApp.config.ui_scale
118 x, y = self.x * s + x, self.y * s + y
119
120 -- The following strings are drawn in reverse order
121 local ly = y + self.height * s - (15 * s)
122 -- Move the version string up a bit if showing check for updates disabled warning.
123 if TheApp:isUpdateCheckDisabledByConfig() then
124 self.label_font:draw(canvas, _S.main_menu.updates_off, x + 5 * s, ly, 190, 0, "right")
125 ly = ly - (15 * s)
126 end
127 -- Move if also showing the savegame version.
128 if TheApp.config.debug then
129 self.label_font:draw(canvas, _S.main_menu.savegame_version .. TheApp.savegame_version, x + 5 * s, ly, 190, 0, "right")
130 ly = ly - (15 * s)
131 end
132 self.label_font:draw(canvas, _S.main_menu.version .. self.release_string, x + 5 * s, ly, 190, 0, "right")
133 end
134
135 function UIMainMenu:onTick()
136 TheApp:idle()
137 end
138
139 function UIMainMenu:onMouseMove(x, y, dx, dy)
140 TheApp:resetIdle()
141 UIResizable.onMouseMove(self, x, y, dx, dy)
142 end
143
144 function UIMainMenu:buttonNewGame()
145 local window = UINewGame(self.ui)
146 self.ui:addWindow(window)
147 end
148
149 function UIMainMenu:buttonCustomCampaign()
150 if TheApp.using_demo_files then
151 self.ui:addWindow(UIInformation(self.ui, {_S.information.no_custom_game_in_demo}))
152 else
153 local window = UICustomCampaign(self.ui)
154 self.ui:addWindow(window)
155 end
156 end
157
158 function UIMainMenu:buttonCustomGame()
159 if TheApp.using_demo_files then
160 self.ui:addWindow(UIInformation(self.ui, {_S.information.no_custom_game_in_demo}))
161 else
162 local window = UICustomGame(self.ui)
163 self.ui:addWindow(window)
164 end
165 end
166
167 function UIMainMenu:buttonContinueGame()
168 local most_recent_saved_game = FileTreeNode(self.ui.app.savegame_dir):getMostRecentlyModifiedChildFile(".%.sav$")
169 if most_recent_saved_game then
170 local path = most_recent_saved_game.path
171 local app = self.ui.app
172 local status, err = pcall(app.load, app, path)
173 if not status then
174 err = _S.errors.load_prefix .. err
175 print(err)
176 app.ui:addWindow(UIInformation(self.ui, {err}))
177 end
178 else
179 local error = _S.errors.load_prefix .. _S.errors.no_games_to_contine
180 print(error)
181 self.ui.app.ui:addWindow(UIInformation(self.ui, {error}))
182 end
183 end
184
185 function UIMainMenu:buttonLoadGame()
186 local window = UILoadGame(self.ui, "menu")
187 self.ui:addWindow(window)
188 end
189
190 function UIMainMenu:buttonOptions()
191 local window = UIOptions(self.ui, "menu")
192 self.ui:addWindow(window)
193 end
194
195 function UIMainMenu:buttonMapEdit()
196 self.ui.app:mapEdit()
197 end
198
199 function UIMainMenu:buttonExit()
200 self.ui:addWindow(UIConfirmDialog(self.ui, false,
201 _S.tooltip.main_menu.quit,
202 --[[persistable:quit_confirm_dialog]]function()
203 self.ui.app:exit()
204 end
205 ))
206 end