Debian Codegraph @

CorsixTH/Lua/dialogs/build_room.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
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 local TH = require("TH")
22
23 class "UIBuildRoom" (Window)
24
25 ---@type UIBuildRoom
26 local UIBuildRoom = _G["UIBuildRoom"]
27
28 function UIBuildRoom:UIBuildRoom(ui)
29 self:Window()
30
31 local app = ui.app
32 self.ui = ui
33 self.modal_class = "main"
34 self.esc_closes = true
35 self.width = 297
36 self.height = 294
37 self:setDefaultPosition(0.5, 0.5)
38 local selected_label_color = { red = 40, green = 40, blue = 250 }
39 self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req09V", true)
40 self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
41 self.blue_font = app.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true })
42 self.category_index = 0
43 self.list_hover_index = 0
44 self.hover_sound = nil
45 self.preview_anim = false
46 self.default_button_sound = "selectx.wav"
47
48 local function cat(n)
49 return --[[persistable:build_room_set_category]] function(window)
50 return window:setCategory(n)
51 end
52 end
53 local room_n = 1
54 local function rm()
55 local n = room_n
56 room_n = room_n + 1
57 return --[[persistable:build_room_build_room]] function(window)
58 return window:buildRoom(n)
59 end
60 end
61
62 self:addPanel(210, 0, 0):makeButton(9, 9, 129, 32, 211, cat(1)):setTooltip(_S.tooltip.build_room_window.room_classes.diagnosis)
63 self:addPanel(212, 0, 41):makeButton(9, 0, 129, 31, 213, cat(2)):setTooltip(_S.tooltip.build_room_window.room_classes.treatment)
64 -- Clinics should really be at y=73, but TH skips a pixel here
65 -- so that the left and right columns are the same height
66 self:addPanel(214, 0, 72):makeButton(9, 0, 129, 32, 215, cat(3)):setTooltip(_S.tooltip.build_room_window.room_classes.clinic)
67 self:addPanel(216, 0, 104):makeButton(9, 0, 129, 32, 217, cat(4)):setTooltip(_S.tooltip.build_room_window.room_classes.facilities)
68 self:addPanel(218, 0, 146) -- Grid top
69 for y = 179, 249, 10 do
70 self:addPanel(219, 0, y) -- Grid body
71 end
72 self:addPanel(220, 0, 259) -- Grid bottom
73 self:addPanel(221, 146, 0) -- List top
74 for y = 34, 205, 19 do
75 self:addPanel(222, 146, y):makeButton(12, 0, 126, 19, 223, rm()) -- List body
76 .enabled = false
77 end
78
79 -- The close button has no sprite for when pressed, so it has to be custom drawn
80 local build_room_dialog_close = TheApp.gfx:loadSpriteTable("Bitmap", "aux_ui", true)
81 self:addPanel(224, 146, 224):makeButton(8, 34, 134, 27, 224, self.close):setTooltip(_S.tooltip.build_room_window.close)
82 .panel_for_sprite.custom_draw = --[[persistable:build_room_draw_close_button]] function(panel, canvas, x, y)
83 local s = TheApp.config.ui_scale
84 x = x + panel.x * s
85 y = y + panel.y * s
86 panel.window.panel_sprites:draw(canvas, panel.sprite_index, x, y, { scaleFactor = s })
87 local btn = panel.window.active_button
88 if btn and btn.panel_for_sprite == panel and btn.active then
89 build_room_dialog_close:draw(canvas, 1, x + 8 * s, y + 34 * s, { scaleFactor = s })
90 end
91 end
92
93 self.list_title = _S.build_room_window.pick_department
94 self.cost_box = _S.build_room_window.cost .. "0"
95 self.list = {}
96 self.category_titles = {
97 _S.room_classes.diagnosis,
98 _S.room_classes.treatment,
99 _S.room_classes.clinics,
100 _S.room_classes.facilities
101 }
102
103 self:updateBuildableRooms()
104
105 self:makeTooltip(_S.tooltip.build_room_window.cost, 160, 228, 282, 242)
106 end
107
108 --! Checks what rooms are now available to build
109 function UIBuildRoom:updateBuildableRooms()
110 local app = self.ui.app
111 self.category_rooms = {}
112 for i, category in ipairs({"diagnosis", "treatment", "clinics", "facilities"}) do
113 local rooms = {}
114 self.category_rooms[i] = rooms
115 for _, room_disc in pairs(self.ui.hospital.room_discoveries) do
116 local room = room_disc.room
117 -- NB: Unimplemented rooms are hidden unless in debug mode
118 if (app.config.debug or room.class) and room.categories[category] and
119 room_disc.is_discovered then
120 rooms[#rooms + 1] = room
121 end
122 end
123 table.sort(rooms, function(r1, r2) return r1.categories[category] < r2.categories[category] end)
124 end
125 end
126
127 local cat_label_y = {21, 53, 84, 116}
128
129 function UIBuildRoom:draw(canvas, x, y)
130 Window.draw(self, canvas, x, y)
131
132 local s = TheApp.config.ui_scale
133 x, y = self.x * s + x, self.y * s + y
134 self.white_font:draw(canvas, self.list_title, x + 163 * s, y + 18 * s)
135 for i = 1, 4 do
136 (i == self.category_index and self.blue_font or self.white_font)
137 :draw(canvas, self.category_titles[i], x + 19 * s, y + cat_label_y[i] * s)
138 end
139
140 for i, room in ipairs(self.list) do
141 (i == self.list_hover_index and self.blue_font or self.white_font)
142 :draw(canvas, room.name, x + 163 * s, y + 21 * s + i * 19 * s)
143 end
144
145 self.white_font:draw(canvas, self.cost_box, x + 163 * s, y + 232 * s)
146
147 if self.preview_anim then
148 self.preview_anim:setScaleFactor(s)
149 self.preview_anim:draw(canvas, x + 70 * s, y + 200 * s)
150 end
151 end
152
153 function UIBuildRoom:setCategory(index)
154 if index == 1 then
155 self.ui:tutorialStep(3, 2, 3)
156 else
157 self.ui:tutorialStep(3, 3, 2)
158 end
159 self.category_index = index
160 self:updateBuildableRooms()
161 self.list_title = _S.build_room_window.pick_room_type
162 self.list = self.category_rooms[index]
163
164 local last = #self.list + 5
165 for i = 5, 14 do
166 self.buttons[i].enabled = i < last
167 if i < last then
168 self.buttons[i]:setTooltip(self.list[i - 4].tooltip)
169 else
170 self.buttons[i]:setTooltip()
171 end
172 end
173 end
174
175 function UIBuildRoom:buildRoom(index)
176 local hosp = self.ui.hospital
177 local world = self.ui.app.world
178
179 if index == 1 then self.ui:tutorialStep(3, 3, 4) end
180
181 local cost = world.free_build_mode and 0 or
182 hosp.research.research_progress[self.list[index]].build_cost
183 if world.free_build_mode or hosp.balance >= cost then
184 -- Close any full screen window currently open.
185 local fullscreen = self.ui:getWindow(UIFullscreen)
186 if fullscreen then
187 fullscreen:close()
188 end
189 local edit_dlg = UIEditRoom(self.ui, self.list[index])
190 self.ui:addWindow(edit_dlg)
191 else
192 -- give visual warning that player doesn't have enough $ to build
193 self.ui.adviser:say(_A.warnings.money_very_low_take_loan, false, true)
194 self.ui:playSound("Wrong2.wav")
195 end
196 end
197
198 function UIBuildRoom:onMouseMove(x, y, dx, dy)
199 local repaint = Window.onMouseMove(self, x, y, dx, dy)
200
201 local s = TheApp.config.ui_scale
202 local hover_idx = 0
203 if 156 * s <= x and x < 287 * s and 31 * s <= y and y < 226 * s then
204 for i = 5, 14 do
205 local btn = self.buttons[i]
206 if btn.enabled and btn.x * s <= x and x < btn.r * s and btn.y * s <= y and y < btn.b * s then
207 hover_idx = i - 4
208 break
209 end
210 end
211 end
212
213 if hover_idx ~= self.list_hover_index then
214 if self.hover_sound then
215 self.ui:stopSound(self.hover_sound)
216 end
217 self.hover_sound = self.ui:playSound("HLight5.wav")
218 if hover_idx == 0 then
219 self.cost_box = _S.build_room_window.cost .. "0"
220 self.preview_anim = false
221 else
222 local hosp = self.ui.hospital
223 local world = self.ui.app.world
224 local cost = world.free_build_mode and 0 or
225 hosp.research.research_progress[self.list[hover_idx]].build_cost
226 self.cost_box = _S.build_room_window.cost .. cost
227 self.preview_anim = TH.animation()
228 self.preview_anim:setAnimation(self.ui.app.anims, self.list[hover_idx].build_preview_animation)
229 end
230 self.list_hover_index = hover_idx
231 repaint = true
232 end
233
234 return repaint
235 end
236
237 function UIBuildRoom:close()
238 self.ui:tutorialStep(3, {2, 3}, 1)
239 return Window.close(self)
240 end
241
242 function UIBuildRoom:afterLoad(old, new)
243 Window.afterLoad(self, old, new)
244
245 if old < 236 then
246 local selected_label_color = { red = 40, green = 40, blue = 250 }
247 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
248 self.blue_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true })
249 end
250 end