Debian Codegraph @

CorsixTH/Lua/dialogs/message.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 --! Small fax notification window which sits on the bottom bar.
22 class "UIMessage" (Window)
23
24 ---@type UIMessage
25 local UIMessage = _G["UIMessage"]
26
27 function UIMessage:UIMessage(ui, x, stop_x, onClose, type, message, owner, timeout, default_choice, callback)
28 self:Window()
29
30 local app = ui.app
31
32 self.esc_closes = false
33 self.on_top = false
34 self.onClose = onClose
35 self.timer = timeout
36 self.default_choice = default_choice
37 self.ui = ui
38 self.message = message
39 self.callback = callback
40 if owner then
41 self.owner = owner
42 if owner.message_callback then
43 owner:message_callback(true) -- There can be only one message per owner, just remove any existing one
44 end
45 assert(owner.message_callback == nil)
46 owner.message_callback = --[[persistable:owner_of_message_callback]] function(_, do_remove)
47 if do_remove then
48 self:removeMessage()
49 else
50 self:openMessage()
51 end
52 end
53 end
54 self.width = 30
55 self.height = 28
56 self.stop_x = stop_x
57 self.stop_y = -24
58 self.x = x
59 self.y = 4
60 self.panel_sprites = app.gfx:loadSpriteTable("Data", "Panel02V", true)
61 self.type = type
62
63 local types = { emergency = 43, epidemy = 45, strike = 47, personality = 49, information = 51, disease = 53, report = 55 }
64 local kind = types[type]
65
66 self.can_dismiss = self.type ~= "strike" and #self.message.choices == 1
67
68 self.button = self:addPanel(kind, 0, 0)
69 :setTooltip(self.can_dismiss and _S.tooltip.message.button_dismiss or _S.tooltip.message.button) -- FIXME: tooltip doesn't work very well here
70 :makeToggleButton(0, 0, 30, 28, kind + 1, self.openMessage, nil, self.dismissMessage)
71 -- The emergency has a rotating siren
72 if kind == 43 then
73 self.rotator = {}
74 for i = 57, 60 do
75 self.rotator[i] = self:addPanel(i, 10, 8)
76 self.rotator[i].visible = false
77 end
78 self.active = 57
79 end
80 end
81
82 function UIMessage:draw(canvas, x, y)
83 if self.on_top then
84 Window.draw(self, canvas, x, y)
85 else
86 canvas:pushClip(0, y + self.stop_y, canvas:getWidth(), self.height, true)
87 Window.draw(self, canvas, x, y)
88 canvas:popClip()
89 end
90 end
91
92 function UIMessage:close(...)
93 assert(self.onClose == nil, "UIMessage closed improperly")
94 return Window.close(self, ...)
95 end
96
97 -- Adjust the toggle state to match if the message is open or not
98 function UIMessage:adjustToggle()
99 if (self.button.toggled and not self.fax) or
100 (not self.button.toggled and self.fax) then
101 self.button:toggle()
102 end
103 end
104
105 --! Displays the fax/strike message to the player when opened from the bottom_panel.
106 function UIMessage:openMessage()
107 if TheApp.world:isUserActionProhibited() then
108 self.ui:playSound("wrong2.wav")
109 self:adjustToggle()
110 return
111 end
112 if self.type == "strike" then -- strikes are special cases, as they are not faxes
113 self.ui:addWindow(UIStaffRise(self.ui, self.owner, self.message))
114 self:removeMessage()
115 else
116 if self.fax then
117 self.fax:close()
118 else
119 self.fax = UIFax(self.ui, self)
120 self.ui:addWindow(self.fax)
121 self.ui:playSound("fax_in.wav")
122 end
123 -- Manual adjustion of toggle state is necessary if owner's message_callback was used
124 self:adjustToggle()
125 end
126 TheApp.video:setBlueFilterActive(false)
127 end
128
129 -- Removes the Message, executing a choice if given, else just deletes it
130 --!param choice_number (number) if given, removes the message by executing this choice.
131 function UIMessage:removeMessage(choice_number)
132 if choice_number then
133 if not self.fax then
134 self.fax = UIFax(self.ui, self) -- NB: just create, don't add to ui
135 end
136 self.fax:choice(choice_number)
137 else
138 if self.fax then
139 self.fax:close()
140 end
141 if self.owner then
142 self.owner.message = nil
143 self.owner.message_callback = nil
144 end
145 if self.onClose then
146 local onClose = self.onClose
147 self.onClose = nil
148 onClose(self)
149 end
150 self:close()
151 end
152 end
153
154 -- Tries to dismiss the message. This is only possible if there is only one choice.
155 function UIMessage:dismissMessage()
156 if self.can_dismiss then
157 self:removeMessage(1)
158 else
159 self.ui:playSound("wrong2.wav")
160 self:adjustToggle()
161 end
162 end
163
164 function UIMessage:setXLimit(stop_x)
165 self.stop_x = stop_x
166 end
167
168 function UIMessage:onTick()
169 if self.on_top == false and self.y == self.stop_y then
170 self.on_top = true
171 end
172 if self.y > self.stop_y then
173 local y = self.y - 8
174 if y > self.stop_y then
175 self.y = y
176 else
177 self.y = self.stop_y
178 end
179 elseif self.x > self.stop_x then
180 local x = self.x - 3
181 if x > self.stop_x then
182 self.x = x
183 else
184 self.x = self.stop_x
185 end
186 end
187 end
188
189 function UIMessage:onWorldTick()
190 if self.timer then
191 self.timer = self.timer - 1
192 if self.timer <= 0 then
193 self.timer = nil
194 self:removeMessage(self.default_choice)
195 end
196 end
197 if self.active then
198 self.rotator[self.active].visible = false
199 if self.active == 60 then
200 self.active = 57
201 else
202 self.active = self.active + 1
203 end
204 self.rotator[self.active].visible = true
205 end
206 end
207
208 function UIMessage:afterLoad(old, new)
209 if old < 21 then
210 -- self.button added; however check for existence first
211 -- since the savegame bump was a couple of revisions later
212 if not self.button then
213 self.button = self.buttons[1]
214 end
215 end
216 Window.afterLoad(self, old, new)
217 end