Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/folder_settings.lua

raw
1 --[[ Copyright (c) 2013 Mark (Mark L) Lawlor
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 --! Customise window used in the main menu and ingame.
22 class "UIFolder" (UIResizable)
23
24 ---@type UIFolder
25 local UIFolder = _G["UIFolder"]
26
27 local col = {
28 bg = Colours.PanelDefault,
29 setting = Colours.Setting,
30 title = Colours.Title,
31 caption = Colours.Caption,
32 button = Colours.PanelDefault
33 }
34
35 --- Calculates the Y position for the dialog box in the option menu
36 -- and increments along the current position for the next element
37 -- @return The Y position to place the element at
38 function UIFolder:_getOptionYPos()
39 -- Offset from top of options box
40 local STARTING_Y_POS = 50
41 -- Y Height is 20 for panel size + 5 for spacing
42 local Y_HEIGHT = 25
43
44 -- Multiply by the index so that index = 1 is at STARTING_Y_POS
45 local calculated_pos = STARTING_Y_POS + Y_HEIGHT * (self._current_option_index - 1)
46 self._current_option_index = self._current_option_index + 1
47 return calculated_pos
48 end
49
50 function UIFolder:UIFolder(ui, mode)
51 self:UIResizable(ui, 420, 240, col.bg)
52
53 local app = ui.app
54 self.mode = mode
55 self.modal_class = mode == "menu" and "main menu" or "options" or "folders"
56 self.on_top = mode == "menu"
57 self.esc_closes = true
58 self.resizable = false
59 self:setDefaultPosition(0.5, 0.25)
60 self.default_button_sound = "selectx.wav"
61 self.app = app
62 self._current_option_index = 1
63
64 -- Window parts definition
65 -- Title
66 self:addBevelPanel(110, 10, 200, 20, col.title):setLabel(_S.folders_window.caption)
67 .lowered = true
68
69 -- Location of original game
70 local built_in = app.gfx:loadMenuFont()
71
72 local y = self:_getOptionYPos()
73 self:addBevelPanel(20, y, 130, 20, col.caption, col.bg, col.bg)
74 :setLabel(_S.folders_window.data_label)
75 :setTooltip(_S.tooltip.folders_window.data_location)
76 .lowered = true
77 self:addBevelPanel(160, y, 240, 20, col.setting)
78 :setLabel(app.config.theme_hospital_install, built_in)
79 :setAutoClip(true)
80 :makeButton(0, 0, 240, 20, nil, self.buttonBrowseForTHInstall)
81 :setTooltip(_S.tooltip.folders_window.browse_data:format(app.config.theme_hospital_install))
82
83 -- Location of font file
84 y = self:_getOptionYPos()
85 local font_location_label = app.config.unicode_font and app.config.unicode_font or _S.tooltip.folders_window.no_font_specified
86 local tooltip_font = app.config.unicode_font and _S.tooltip.folders_window.browse_font:format(app.config.unicode_font) or _S.tooltip.folders_window.no_font_specified
87
88 self:addBevelPanel(20, y, 130, 20, col.caption, col.bg, col.bg)
89 :setLabel(_S.folders_window.font_label)
90 :setTooltip(_S.tooltip.folders_window.font_location)
91 .lowered = true
92 self.fonts_panel = self:addBevelPanel(160, y, 240, 20, col.setting)
93 :setLabel(font_location_label, built_in)
94 :setAutoClip(true)
95 :makeButton(0, 0, 240, 20, nil, self.buttonBrowseForFont)
96 :setTooltip(tooltip_font)
97
98 -- Location saves alternative
99 y = self:_getOptionYPos()
100 local default_savegame_dir = app:getDefaultSavegameDir()
101 local saves_location = app.config.savegames and app.config.savegames or default_savegame_dir
102 local tooltip_saves = _S.tooltip.folders_window.browse_saves:format(saves_location)
103
104 self:addBevelPanel(20, y, 130, 20, col.caption, col.bg, col.bg)
105 :setLabel(_S.folders_window.savegames_label):setTooltip(_S.tooltip.folders_window.savegames_location)
106 .lowered = true
107 self.saves_panel = self:addBevelPanel(160, y, 220, 20, col.setting)
108 :setLabel(saves_location , built_in)
109 :setAutoClip(true)
110 :makeButton(0, 0, 220, 20, nil, self.buttonBrowseForSavegames)
111 :setTooltip(tooltip_saves)
112 self:addBevelPanel(380, 100, 20, 20, col.button)
113 :setLabel("X")
114 :makeButton(0, 0, 20, 20, nil, self.resetSavegameDir)
115 :setTooltip(_S.tooltip.folders_window.reset_to_default:format(default_savegame_dir))
116
117 -- location for screenshots
118 y = self:_getOptionYPos()
119 local default_screenshots_dir = app:getDefaultScreenshotsDir()
120 local screenshots_location = app.config.screenshots and app.config.screenshots or default_screenshots_dir
121 local tooltip_screenshots = _S.tooltip.folders_window.browse_screenshots:format(screenshots_location)
122
123 self:addBevelPanel(20, y, 130, 20, col.caption, col.bg, col.bg)
124 :setLabel(_S.folders_window.screenshots_label):setTooltip(_S.tooltip.folders_window.screenshots_location)
125 .lowered = true
126 self.screenshots_panel = self:addBevelPanel(160, y, 220, 20, col.setting)
127 :setLabel(screenshots_location, built_in)
128 :setAutoClip(true)
129 :makeButton(0, 0, 220, 20, nil, self.buttonBrowseForScreenshots)
130 :setTooltip(tooltip_screenshots)
131 self:addBevelPanel(380, y, 20, 20, col.button)
132 :setLabel("X")
133 :makeButton(0, 0, 20, 20, nil, self.resetScreenshotDir)
134 :setTooltip(_S.tooltip.folders_window.reset_to_default:format(default_screenshots_dir))
135
136 -- location for music files
137 y = self:_getOptionYPos()
138 local music_location_label = app.config.audio_music and app.config.audio_music or _S.tooltip.folders_window.not_specified
139 local tooltip_music = app.config.audio_music and _S.tooltip.folders_window.browse_music:format(app.config.audio_music) or _S.tooltip.folders_window.not_specified
140
141 self:addBevelPanel(20, y, 130, 20, col.caption, col.bg, col.bg)
142 :setLabel(_S.folders_window.music_label):setTooltip(_S.tooltip.folders_window.music_location)
143 .lowered = true
144 self.music_panel = self:addBevelPanel(160, y, 220, 20, col.setting)
145 :setLabel(music_location_label, built_in)
146 :setAutoClip(true)
147 :makeButton(0, 0, 220, 20, nil, self.buttonBrowseForAudio_music)
148 :setTooltip(tooltip_music)
149 self:addBevelPanel(380, y, 20, 20, col.button)
150 :setLabel("X")
151 :makeButton(0, 0, 20, 20, nil, self.resetMusicDir)
152 :setTooltip(_S.tooltip.folders_window.clear_directory)
153
154 -- "Back" button
155 -- Give some extra padding to adjust for symmetry
156 y = self:_getOptionYPos() + 15
157 self:addBevelPanel(50, y, 320, 40, col.button)
158 :setLabel(_S.folders_window.back)
159 :makeButton(0, 0, 320, 40, nil, self.buttonBack)
160 :setTooltip(_S.tooltip.folders_window.back)
161 self.built_in_font = built_in
162 end
163
164 function UIFolder:resetSavegameDir()
165 local app = TheApp
166 local default_savegame_dir = app:getDefaultSavegameDir()
167 local tooltip_saves = _S.tooltip.folders_window.browse_saves:format(default_savegame_dir)
168 app.config.savegames = nil
169 app:saveConfig()
170 app:initSavegameDir()
171 self.saves_panel:setLabel(default_savegame_dir, self.built_in_font)
172 self.saves_panel:setTooltip(tooltip_saves)
173 end
174
175 function UIFolder:resetScreenshotDir()
176 local app = TheApp
177 local default_screenshots_dir = app:getDefaultScreenshotsDir()
178 local tooltip_screenshots = _S.tooltip.folders_window.browse_screenshots:format(default_screenshots_dir)
179 app.config.screenshots = nil
180 app:saveConfig()
181 app:initScreenshotsDir()
182 self.screenshots_panel:setLabel(default_screenshots_dir, self.built_in_font)
183 self.screenshots_panel:setTooltip(tooltip_screenshots)
184 end
185
186 function UIFolder:resetMusicDir()
187 local app = TheApp
188 local label_music = _S.tooltip.folders_window.not_specified
189 local tooltip_music = _S.tooltip.folders_window.not_specified
190 app.config.audio_music = nil
191 app:saveConfig()
192 app.audio:init()
193 self.music_panel:setLabel(label_music, self.built_in_font)
194 self.music_panel:setTooltip(tooltip_music)
195 end
196
197 function UIFolder:buttonBrowseForFont()
198 local browser = UIChooseFont(self.ui, self.mode)
199 self.ui:addWindow(browser)
200 end
201
202 function UIFolder:buttonBrowseForSavegames()
203 local app = TheApp
204 local old_path = app.config.savegames
205 local function callback(path)
206 if old_path ~= path then
207 app.config.savegames = path
208 app:saveConfig()
209 app:initSavegameDir()
210 self.saves_panel:setLabel(app.config.savegames, self.built_in_font)
211 end
212 end
213 local browser = UIDirectoryBrowser(self.ui, self.mode, _S.folders_window.savegames_location, "DirTreeNode", callback)
214 self.ui:addWindow(browser)
215 end
216
217 function UIFolder:buttonBrowseForTHInstall()
218 local function callback(path)
219 local app = TheApp
220 app.config.theme_hospital_install = path
221 app:saveConfig()
222 app:reset()
223 end
224 local browser = UIDirectoryBrowser(self.ui, self.mode, _S.folders_window.new_th_location, "InstallDirTreeNode", callback)
225 self.ui:addWindow(browser)
226 end
227
228 function UIFolder:buttonBrowseForScreenshots()
229 local app = TheApp
230 local old_path = app.config.screenshots
231 local function callback(path)
232 if old_path ~= path then
233 app.config.screenshots = path
234 app:saveConfig()
235 app:initScreenshotsDir()
236 self.screenshots_panel:setLabel(app.config.screenshots, self.built_in_font)
237 end
238 end
239 local browser = UIDirectoryBrowser(self.ui, self.mode, _S.folders_window.screenshots_location, "DirTreeNode", callback)
240 self.ui:addWindow(browser)
241 end
242
243 function UIFolder:buttonBrowseForAudio_music()
244 local function callback(path)
245 local app = TheApp
246 app.config.audio_music = path
247 app:saveConfig()
248 app.audio:init()
249 self.music_panel:setLabel(app.config.audio_music, self.built_in_font)
250 end
251 local browser = UIDirectoryBrowser(self.ui, self.mode, _S.folders_window.music_location, "DirTreeNode", callback)
252 self.ui:addWindow(browser)
253 end
254
255 function UIFolder:buttonBack()
256 self:close()
257 local window = UIOptions(self.ui, "menu")
258 self.ui:addWindow(window)
259 end
260
261 function UIFolder:close()
262 UIResizable.close(self)
263 if self.mode == "menu" then
264 self.ui:addWindow(UIMainMenu(self.ui))
265 end
266 end