| 1 |
--[[ Copyright (c) 2026 Fraggenard |
| 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 |
--! UI display area for announcer subtitles |
| 22 |
class "Subtitles" (Window) |
| 23 |
|
| 24 |
---@type Subtitles |
| 25 |
local Subtitles = _G["Subtitles"] |
| 26 |
|
| 27 |
--! Subtitles constructor |
| 28 |
function Subtitles:Subtitles() |
| 29 |
self:Window() |
| 30 |
self.x = 0 |
| 31 |
self.y = 0 |
| 32 |
self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 33 |
|
| 34 |
self.queue = SubtitleQueue() |
| 35 |
end |
| 36 |
|
| 37 |
--! Pushes an announcer audio file's associated subtitle text to the queue for display |
| 38 |
--!param name (string) Filename of the announcer sound to display |
| 39 |
function Subtitles:queueSubtitle(name) |
| 40 |
if _S.subtitles ~= nil then |
| 41 |
local subtitleString = _S.subtitles[string.gsub(string.lower(name), ".wav", "")] |
| 42 |
if subtitleString ~= nil then |
| 43 |
--Second field of subtitle object is the subtitle's display lifetime, measured in ticks |
| 44 |
local duration = 280 -- ~5 seconds in ticks |
| 45 |
self.queue:push({subtitleString, duration}) |
| 46 |
end |
| 47 |
end |
| 48 |
end |
| 49 |
|
| 50 |
function Subtitles:draw(canvas, x, y) |
| 51 |
Window.draw(self, canvas, x, y) |
| 52 |
local s = TheApp.config.ui_scale |
| 53 |
|
| 54 |
if not self.queue:isEmpty() then |
| 55 |
if TheApp.config.enable_announcer_subtitles then |
| 56 |
local displayIndex = 1 |
| 57 |
for _, subtitle in ipairs(self.queue.subtitles) do |
| 58 |
self.white_font:draw(canvas, subtitle[1], 5, (5 + 16 * s) * displayIndex, 0, 16) |
| 59 |
displayIndex = displayIndex + 1 |
| 60 |
end |
| 61 |
end |
| 62 |
end |
| 63 |
end |
| 64 |
|
| 65 |
--! Subtitle area's tick handler |
| 66 |
-- Removes a subtitle from the queue if it has reached its tick lifetime |
| 67 |
function Subtitles:onTick() |
| 68 |
if not self.queue:isEmpty() then |
| 69 |
for _, subtitle in ipairs(self.queue.subtitles) do |
| 70 |
local subtitleLifetime = subtitle[2] |
| 71 |
if subtitleLifetime <= 1 then |
| 72 |
self.queue:pop() |
| 73 |
else |
| 74 |
subtitle[2] = subtitleLifetime - 1 |
| 75 |
end |
| 76 |
end |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
function Subtitles:afterLoad(old, new) |
| 81 |
self.queue = SubtitleQueue() |
| 82 |
end |
| 83 |
|
| 84 |
--! First in, first out queue for concurrently displayed subtitles |
| 85 |
class "SubtitleQueue" |
| 86 |
|
| 87 |
---@type SubtitleQueue |
| 88 |
local SubtitleQueue = _G["SubtitleQueue"] |
| 89 |
|
| 90 |
--! SubtitleQueue constructor |
| 91 |
function SubtitleQueue:SubtitleQueue() |
| 92 |
self.count = 0 |
| 93 |
self.subtitles = {} |
| 94 |
end |
| 95 |
|
| 96 |
--! Pushes subtitle text to the queue for display |
| 97 |
--!param subtitle (string) Subtitle text to display |
| 98 |
function SubtitleQueue:push(subtitle) |
| 99 |
table.insert(self.subtitles, subtitle) |
| 100 |
self.count = self.count + 1 |
| 101 |
end |
| 102 |
|
| 103 |
--! Removes subtitle text at the top of the queue |
| 104 |
function SubtitleQueue:pop() |
| 105 |
if self.subtitles[1] ~= nil then |
| 106 |
table.remove(self.subtitles, 1) |
| 107 |
self.count = self.count - 1 |
| 108 |
end |
| 109 |
end |
| 110 |
|
| 111 |
--! Returns true if the queue is empty, false otherwise |
| 112 |
function SubtitleQueue:isEmpty() |
| 113 |
return self.count == 0 |
| 114 |
end |