Debian Codegraph @

CorsixTH/Lua/dialogs/machine_dialog.lua

raw
1 --[[ Copyright (c) 2009 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 "UIMachine" (Window)
22
23 ---@type UIMachine
24 local UIMachine = _G["UIMachine"]
25
26 function UIMachine:UIMachine(ui, machine, room)
27 self:Window()
28
29 local app = ui.app
30 self.esc_closes = true
31 self.machine = machine
32 self.room = room
33 self.ui = ui
34 self.modal_class = "humanoid_info"
35 self.width = 188
36 self.height = 206
37 self:setDefaultPosition(-20, 30)
38 self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req03V", true)
39 self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
40
41 self:addPanel(333, 0, 0) -- Dialog header
42 self:addPanel(334, 0, 74) -- The next part
43 for y = 131, 180, 7 do
44 self:addPanel(335, 0, y) -- Some background
45 end
46 self:addPanel(336, 0, 182) -- Dialog footer
47
48 -- Call button
49 self:addPanel(339, 20, 127)
50 :makeButton(0, 0, 63, 60, 340, self.callHandyman)
51 :setTooltip(_S.tooltip.machine_window.repair)
52 :setSound("selectx.wav")
53 -- Replace button
54 self:addPanel(341, 92, 127)
55 :makeButton(0, 0, 45, 60, 342, self.replaceMachine)
56 :setTooltip(_S.tooltip.machine_window.replace)
57 :setSound("selectx.wav")
58 -- Close button
59 self:addPanel(337, 146, 18):makeButton(0, 0, 24, 24, 338, self.close)
60 :setTooltip(_S.tooltip.machine_window.close)
61
62 self:makeTooltip(_S.tooltip.machine_window.name, 18, 19, 139, 42)
63 self:makeTooltip(_S.tooltip.machine_window.times_used, 18, 49, 139, 77)
64 self:makeTooltip(_S.tooltip.machine_window.status, 24, 88, 128, 115)
65 end
66
67 function UIMachine:draw(canvas, x, y)
68 Window.draw(self, canvas, x, y)
69
70 local s = TheApp.config.ui_scale
71 x, y = self.x * s + x, self.y * s + y
72 local mach = self.machine
73
74 local font = self.white_font
75 local output
76 if self.room.needs_repair then
77 output = "(" .. mach.object_type.name .. ")"
78 else
79 output = mach.object_type.name
80 end
81 font:draw(canvas, output, x + 27 * s, y + 27 * s) -- Name
82 font:draw(canvas, mach.total_usage, x + 60 * s, y + 59 * s) -- Total number of times used
83
84 local status_bar_width = math.floor((1 - mach.times_used/mach.strength) * 40 * s + 0.5)
85 if status_bar_width ~= 0 then
86 for dx = 0, status_bar_width - 1 do
87 self.panel_sprites:draw(canvas, 352, x + 53 * s + dx, y + 99 * s, { scaleFactor = s })
88 end
89 end
90 end
91
92 function UIMachine:callHandyman()
93 if self.machine.times_used ~= 0 then
94 local taskIndex = self.machine.hospital:getIndexOfTask(self.machine.tile_x, self.machine.tile_y, "repairing")
95 if taskIndex == -1 then
96 local call = self.ui.app.world.dispatcher:callForRepair(self.machine, false, true)
97 self.machine.hospital:addHandymanTask(self.machine, "repairing", 2, self.machine.tile_x, self.machine.tile_y, call)
98 else
99 self.machine.hospital:modifyHandymanTaskPriority(taskIndex, 2, "repairing")
100 end
101 self.machine:setRepairingStatus()
102 end
103 end
104
105 --! Function checks we can afford a new machine
106 --! Then offers player to purchase
107 function UIMachine:replaceMachine()
108 -- Maybe TODO: Prevent purchase of a machine of same strength?
109 local machine = self.machine
110 local hosp = self.ui.hospital
111 local cost = hosp.research.research_progress[machine.object_type].cost
112 if hosp.balance < cost then
113 -- give visual warning that player doesn't have enough $ to buy
114 local advice = _A.warnings.cannot_afford_machine:format(cost, machine.object_type.name)
115 self.ui.adviser:say(advice, false, true)
116 self.ui:playSound("wrong2.wav")
117 return
118 end
119
120 local prompt = _S.confirmation.replace_machine:format(machine.object_type.name, cost)
121 if self.ui.app.config.new_machine_extra_info then
122 local new_strength = hosp.research.research_progress[machine.object_type].start_strength
123 local current_strength = machine.strength
124 prompt = (_S.confirmation.replace_machine_extra_info):format(new_strength, current_strength) .. "//" .. prompt
125 end
126
127 self.ui:addWindow(UIConfirmDialog(self.ui, true, prompt,
128 --[[persistable:replace_machine_confirm_dialog]]function() machine:replaceMachine(cost)
129 end
130 ))
131 end
132
133 function UIMachine:onMouseDown(code, x, y)
134 -- cycle through all machines when you right click on the machine title
135 if code == "right" then
136 local s = TheApp.config.ui_scale
137 local is_hit_namebox = x > self.tooltip_regions[1].x * s and x < self.tooltip_regions[1].r * s
138 and y > self.tooltip_regions[1].y * s and y < self.tooltip_regions[1].b * s
139 if is_hit_namebox then
140 -- move to next machine
141 local ui = self.ui
142 local first_machine, next_machine = nil, self.machine
143 local next_room
144 for _, entity in ipairs(ui.app.world.entities) do
145 -- is a machine and not a slave (e.g. operating_table_b)
146 if class.is(entity, Machine) and not entity.master then
147 next_room = entity:getRoom()
148 if next_room.is_active then
149 if not first_machine then
150 first_machine = entity
151 end
152 if not next_machine then
153 next_machine = entity
154 break
155 elseif entity == next_machine then
156 next_machine = nil
157 end
158 end
159 end
160 end
161 if not next_machine or next_machine == self.machine then
162 next_machine = first_machine
163 end
164 if next_machine and next_machine ~= self.machine then
165 -- center screen on machine
166 local sx, sy = ui.app.map:WorldToScreen(next_machine.tile_x, next_machine.tile_y)
167 local dx, dy = next_machine.th:getPosition()
168 ui:scrollMapTo(sx + dx, sy + dy)
169 -- switch machine window
170 ui:addWindow(UIMachine(ui, next_machine, next_room))
171 ui:playSound("camclick.wav")
172 -- show machine info in dynamic info
173 ui.bottom_panel:setDynamicInfo(next_machine:getDynamicInfo())
174 end
175 end
176 end
177 return Window.onMouseDown(self, code, x, y)
178 end
179
180 function UIMachine:afterLoad(old, new)
181 if old < 236 then
182 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
183 end
184 Window:afterLoad(old, new)
185 end