| 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 |
--! The (ideally) helpful advisor who pops up from the bottom dialog during a game. |
| 24 |
class "UIAdviser" (Window) |
| 25 |
|
| 26 |
---@type UIAdviser |
| 27 |
local UIAdviser = _G["UIAdviser"] |
| 28 |
|
| 29 |
function UIAdviser:UIAdviser(ui) |
| 30 |
self:Window() |
| 31 |
|
| 32 |
local app = ui.app |
| 33 |
|
| 34 |
self.esc_closes = false |
| 35 |
self.modal_class = "adviser" |
| 36 |
self.tick_rate = app.world.tick_rate |
| 37 |
self.tick_timer = self.tick_rate -- Initialize tick timer |
| 38 |
self.frame = 1 -- Current frame |
| 39 |
self.number_frames = 4 -- Used for playing animation only once |
| 40 |
self.speech = nil -- Store what adviser is going to say |
| 41 |
self.queued_messages = {} -- There might be many messages in a row |
| 42 |
self.timer = nil -- Timer which hides adviser at the end |
| 43 |
|
| 44 |
self.message_history = {} -- Store past messages to be able to view them later |
| 45 |
self.message_history_limit = 20 -- Limit the number of messages stored |
| 46 |
|
| 47 |
-- There are 5 phases the adviser might be in on a given time point. |
| 48 |
-- Not visible, getting up, talking, idling, getting_down. |
| 49 |
self.phase = 0 |
| 50 |
|
| 51 |
self.ui = ui |
| 52 |
self.width = 80 |
| 53 |
self.height = 74 |
| 54 |
self.x = 378 |
| 55 |
self.y = -16 |
| 56 |
self.balloon_width = 0 |
| 57 |
self.panel_sprites = app.gfx:loadSpriteTable("Data", "Panel02V", true) |
| 58 |
self.black_font = app.gfx:loadFontAndSpriteTable("QData", "Font50V", nil, nil, { apply_ui_scale = true }) |
| 59 |
|
| 60 |
local th = TH.animation() |
| 61 |
self.th = th |
| 62 |
end |
| 63 |
|
| 64 |
function UIAdviser:messageIndexInHistory(message) |
| 65 |
for index, msg in ipairs(self.message_history) do |
| 66 |
if msg == message then |
| 67 |
return index |
| 68 |
end |
| 69 |
end |
| 70 |
return nil |
| 71 |
end |
| 72 |
|
| 73 |
--! Adds a message to the adviser's message history. |
| 74 |
--! If the history exceeds a certain limit, the oldest messages are removed. |
| 75 |
function UIAdviser:addMessageToHistory(speech) |
| 76 |
local message_index_in_history = self:messageIndexInHistory(speech) |
| 77 |
|
| 78 |
if message_index_in_history then |
| 79 |
-- If the message is already in history, remove it first |
| 80 |
table.remove(self.message_history, message_index_in_history) |
| 81 |
end |
| 82 |
|
| 83 |
-- Add the message at the top of the history |
| 84 |
table.insert(self.message_history, 1, speech) |
| 85 |
|
| 86 |
-- If the history exceeds the limit, remove the oldest messages (the last ones in the list) |
| 87 |
while #self.message_history > self.message_history_limit do |
| 88 |
table.remove(self.message_history, #self.message_history) |
| 89 |
end |
| 90 |
end |
| 91 |
|
| 92 |
--! Adds all queued messages to the adviser's message history. |
| 93 |
function UIAdviser:addQueuedMessagesToHistory() |
| 94 |
for _, message in ipairs(self.queued_messages) do |
| 95 |
|
| 96 |
self:addMessageToHistory(message.speech) |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
--! Removes a message from the adviser's message history. |
| 101 |
function UIAdviser:removeMessageFromHistory(index) |
| 102 |
table.remove(self.message_history, index) |
| 103 |
end |
| 104 |
|
| 105 |
--! Deletes all messages from the adviser's message history. |
| 106 |
function UIAdviser:deleteAllMessagesFromHistory() |
| 107 |
self.message_history = {} |
| 108 |
end |
| 109 |
|
| 110 |
-- Shows the adviser by running the "popup" animation. |
| 111 |
-- Then moves on to the next phase automatically. |
| 112 |
function UIAdviser:show() |
| 113 |
self.phase = 1 |
| 114 |
self.th:setAnimation(self.ui.app.world.anims, 438, DrawFlags.Nearest) |
| 115 |
self.frame = 1 |
| 116 |
self.number_frames = 4 |
| 117 |
end |
| 118 |
|
| 119 |
-- Displays the text bubble along with the next message |
| 120 |
-- from the queue. |
| 121 |
function UIAdviser:talk() |
| 122 |
self.phase = 2 |
| 123 |
|
| 124 |
-- Preferably without the Nearest draw flag for a less pixelated look but |
| 125 |
-- without it if direct zoom is enabled and ui_scale is > 1 then there is |
| 126 |
-- artifacting around the mouth of the adviser. |
| 127 |
self.th:setAnimation(self.ui.app.world.anims, 460, DrawFlags.Nearest) |
| 128 |
self.frame = 1 |
| 129 |
self.timer = nil -- Reset the idle timer |
| 130 |
self.number_frames = 45 |
| 131 |
-- Fetch the next message from the queue. |
| 132 |
local best = 1 |
| 133 |
if self.queued_messages[1].priority then |
| 134 |
for i = 1, #self.queued_messages do |
| 135 |
if best ~= i and self.queued_messages[i].priority and |
| 136 |
self.queued_messages[best].priority < self.queued_messages[i].priority then |
| 137 |
best = i |
| 138 |
end |
| 139 |
end |
| 140 |
end |
| 141 |
local speech = self.queued_messages[best].speech |
| 142 |
self.stay_up = self.queued_messages[best].stay_up |
| 143 |
table.remove(self.queued_messages, best) |
| 144 |
self:addMessageToHistory(speech) |
| 145 |
self.speech = speech |
| 146 |
-- Calculate number of lines needed for the text. |
| 147 |
-- Each "/" at end of string indicates a blank line |
| 148 |
local number_lines = 3 |
| 149 |
local speech_trimmed = speech:gsub("/*$", "") |
| 150 |
number_lines = number_lines - (#speech - #speech_trimmed) |
| 151 |
speech = speech_trimmed |
| 152 |
|
| 153 |
-- Calculate balloon width from string length |
| 154 |
self.balloon_width = math.floor(#speech / number_lines) * 7 |
| 155 |
if self.balloon_width >= 420 then -- Balloon too large |
| 156 |
self.balloon_width = 420 |
| 157 |
elseif self.balloon_width <= 40 then -- Balloon too small |
| 158 |
self.balloon_width = 40 |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
-- Makes the adviser idle for a while before disappearing. |
| 163 |
-- This means that the text bubble is removed. |
| 164 |
function UIAdviser:idle() |
| 165 |
self.phase = 3 |
| 166 |
-- Remove the bubble and start a timer for disappearance |
| 167 |
-- unless he should stay up until the next message. |
| 168 |
if not self.stay_up then |
| 169 |
self.speech = nil |
| 170 |
self.timer = 150 |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
-- Hides the adviser by running the appropriate animation. |
| 175 |
function UIAdviser:hide() |
| 176 |
self.timer = nil |
| 177 |
self.phase = 4 |
| 178 |
self.th:setAnimation(self.ui.app.world.anims, 440, DrawFlags.Nearest) |
| 179 |
self.frame = 1 |
| 180 |
self.number_frames = 4 |
| 181 |
end |
| 182 |
|
| 183 |
--! Function checks if the adviser has been asked to say something already queued |
| 184 |
--!param speech (string) Text to check |
| 185 |
--!return (boolean) true if duplicate found |
| 186 |
function UIAdviser:checkForDuplicates(speech) |
| 187 |
local speech_to_check = speech.text -- Humanise for clarity |
| 188 |
for _, text in ipairs(self.queued_messages) do |
| 189 |
local speech_to_compare = text.speech -- Humanise for clarity |
| 190 |
if speech_to_compare == speech_to_check then return true end |
| 191 |
end |
| 192 |
end |
| 193 |
|
| 194 |
-- Makes the adviser say something |
| 195 |
--!param speech The table containing the text he should say and the priority. |
| 196 |
--!param talk_until_next_announce Whether he should stay up |
| 197 |
-- until the next say() call is made. Useful for the tutorial. |
| 198 |
--!param override_current Cancels previous messages (if any) immediately |
| 199 |
-- and shows this new one instead. |
| 200 |
function UIAdviser:say(speech, talk_until_next_announce, override_current) |
| 201 |
assert(type(speech) == "table") |
| 202 |
if not self.ui.app.config.adviser_disabled then |
| 203 |
-- Drop duplicate messages unless overridden |
| 204 |
if not override_current and self:checkForDuplicates(speech) then return end |
| 205 |
-- Queue the new message |
| 206 |
self.queued_messages[#self.queued_messages + 1] = { |
| 207 |
speech = speech.text, |
| 208 |
stay_up = talk_until_next_announce, |
| 209 |
priority = speech.priority |
| 210 |
} |
| 211 |
if self.phase == 0 then |
| 212 |
-- The adviser is not active at all at the moment. |
| 213 |
self:show() |
| 214 |
elseif self.phase == 3 then |
| 215 |
-- He's not talking, so we can show the new message. |
| 216 |
self:talk() |
| 217 |
elseif self.phase == 4 then |
| 218 |
-- He's getting down. Let him do that and then tell him |
| 219 |
-- to go up again. |
| 220 |
self.up_again = true |
| 221 |
elseif override_current then |
| 222 |
-- He was saying/was about to say something else. Discard those messages. |
| 223 |
self.queued_messages[1] = self.queued_messages[#self.queued_messages] |
| 224 |
while #self.queued_messages > 1 do |
| 225 |
table.remove(self.queued_messages) |
| 226 |
end |
| 227 |
-- Now say the new thing instead. |
| 228 |
self:talk() |
| 229 |
end |
| 230 |
-- If none of the above apply the message is now queued and will be shown in |
| 231 |
-- due time. |
| 232 |
end |
| 233 |
end |
| 234 |
|
| 235 |
function UIAdviser:draw(canvas, x, y) |
| 236 |
Window.draw(self, canvas, x, y) |
| 237 |
|
| 238 |
local s = TheApp.config.ui_scale |
| 239 |
x, y = x + self.x * s, y + self.y * s |
| 240 |
|
| 241 |
self.th:setScaleFactor(s) |
| 242 |
self.th:draw(canvas, x + 200 * s, y) |
| 243 |
|
| 244 |
if self.phase == 2 then |
| 245 |
-- Draw balloon only in the "talk" phase. |
| 246 |
local x_left_sprite |
| 247 |
for dx = 0, self.balloon_width * s, 16 * s do |
| 248 |
x_left_sprite = x + 139 * s - dx |
| 249 |
self.panel_sprites:draw(canvas, 38, x_left_sprite, y - 25 * s, { scaleFactor = s }) |
| 250 |
end |
| 251 |
self.panel_sprites:draw(canvas, 37, x_left_sprite - 16 * s, y - 25 * s, { scaleFactor = s }) |
| 252 |
self.panel_sprites:draw(canvas, 39, x + 155 * s, y - 40 * s, { scaleFactor = s }) |
| 253 |
-- Draw text |
| 254 |
self.black_font:drawWrapped(canvas, self.speech, x_left_sprite - 8 * s, y - 20 * s, self.balloon_width * s + 60 * s) |
| 255 |
end |
| 256 |
end |
| 257 |
|
| 258 |
function UIAdviser:onMouseDown(button, x, y) |
| 259 |
-- If the adviser is not up, don't do anything. |
| 260 |
if self.phase == 0 or self.phase == 4 then |
| 261 |
return Window.onMouseDown(self, button, x, y) |
| 262 |
end |
| 263 |
local s = TheApp.config.ui_scale |
| 264 |
|
| 265 |
-- Normal operation outside the adviser bounds |
| 266 |
if x + self.balloon_width * s < 128 * s or x > 200 * s or |
| 267 |
y + self.y * s > 0 or y + self.y * s + 40 * s < 0 then |
| 268 |
if x < self.x * s - 200 * s or y < self.y * s - 40 * s or |
| 269 |
x > self.x * s - 200 * s + self.width * s or y > self.y * s + self.height * s - 40 * s then |
| 270 |
return Window.onMouseDown(self, button, x, y) |
| 271 |
end |
| 272 |
end |
| 273 |
|
| 274 |
-- Dismiss the current message if left click. If right click, |
| 275 |
-- dismiss the whole queue. |
| 276 |
if button == "left" then |
| 277 |
if #self.queued_messages > 0 then |
| 278 |
self:talk() |
| 279 |
else |
| 280 |
self:hide() |
| 281 |
end |
| 282 |
elseif button == "right" then |
| 283 |
self:hide() |
| 284 |
-- Dump dismissed messages to history |
| 285 |
self:addQueuedMessagesToHistory() |
| 286 |
self.queued_messages = {} |
| 287 |
end |
| 288 |
end |
| 289 |
|
| 290 |
function UIAdviser:onTick() |
| 291 |
if self.timer == 0 then |
| 292 |
self:hide() -- Timer ends, so we hide the adviser |
| 293 |
elseif self.timer ~= nil then |
| 294 |
self.timer = self.timer - 1 |
| 295 |
end |
| 296 |
if self.frame < self.number_frames then |
| 297 |
if self.tick_timer == 0 then -- Used for making a smooth animation |
| 298 |
self.tick_timer = self.tick_rate |
| 299 |
-- If no animation set (adviser not being shown already) |
| 300 |
if self.th:getAnimation() ~= 0 then |
| 301 |
self.th:tick() |
| 302 |
self.frame = self.frame + 1 |
| 303 |
end |
| 304 |
else |
| 305 |
self.tick_timer = self.tick_timer - 1 |
| 306 |
end |
| 307 |
elseif self.frame == self.number_frames then |
| 308 |
if self.phase == 1 then |
| 309 |
-- Adviser is now up, let him speak. |
| 310 |
self:talk() |
| 311 |
elseif self.phase == 2 then |
| 312 |
-- Adviser finished to talk so make him idle unless |
| 313 |
-- there's another message waiting. |
| 314 |
if #self.queued_messages > 0 then |
| 315 |
-- Show the next queued message |
| 316 |
self:talk() |
| 317 |
elseif not self.stay_up then |
| 318 |
-- Continue to talk if stay_up is set |
| 319 |
self:idle() |
| 320 |
end |
| 321 |
elseif self.phase == 4 then |
| 322 |
-- The adviser is getting down so we want to hide him, but we have |
| 323 |
-- to wait until the animation ends. |
| 324 |
if self.up_again then |
| 325 |
-- Another message arrived while getting down. |
| 326 |
self:show() |
| 327 |
self.up_again = false |
| 328 |
else |
| 329 |
self.phase = 0 |
| 330 |
self.th:makeInvisible() |
| 331 |
end |
| 332 |
end |
| 333 |
end |
| 334 |
end |
| 335 |
|
| 336 |
function UIAdviser:afterLoad(old, new) |
| 337 |
if old < 47 then |
| 338 |
self.enabled = true |
| 339 |
end |
| 340 |
if old < 236 then |
| 341 |
self.black_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font50V", nil, nil, { apply_ui_scale = true }) |
| 342 |
end |
| 343 |
if old < 246 then |
| 344 |
self.message_history = {} |
| 345 |
self.message_history_limit = 20 |
| 346 |
end |
| 347 |
Window.afterLoad(self, old, new) |
| 348 |
end |