| 1 |
--[[ Copyright (c) 2025 Damian "ShiroAka" |
| 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 |
--! Adviser message history |
| 22 |
class "UIAdviserHistory" (UIResizable) |
| 23 |
|
| 24 |
---@type UIAdviserHistory |
| 25 |
local UIAdviserHistory = _G["UIAdviserHistory"] |
| 26 |
|
| 27 |
|
| 28 |
local col_bg = Colours.AdviserPanelBackground |
| 29 |
local col_highlight = Colours.AdviserPanelHighlight |
| 30 |
local col_shadow = Colours.AdviserPanelShadow |
| 31 |
local col_delete_button = Colours.AdviserPanelDeleteButton |
| 32 |
|
| 33 |
function UIAdviserHistory:UIAdviserHistory(ui) |
| 34 |
local app = ui.app |
| 35 |
|
| 36 |
self.ui = ui |
| 37 |
self.modal_class = "adviser_history" |
| 38 |
|
| 39 |
self.esc_closes = true |
| 40 |
self.resizable = false |
| 41 |
|
| 42 |
self.last_seen_history_top = nil -- Stores the top message in the adviser's 'message_history' list at the time of the last update |
| 43 |
self.last_history_count = nil -- Stores the messages count in the adviser's 'message_history' list at the time of the last update |
| 44 |
self.list_table = {} -- List of controls in this dialog |
| 45 |
|
| 46 |
self.default_button_sound = "selectx.wav" |
| 47 |
|
| 48 |
self.rows_shown = 0 |
| 49 |
|
| 50 |
self.row_height = 40 |
| 51 |
self.header_height = 20 |
| 52 |
self.window_margin = 15 |
| 53 |
self.delete_col_width = 30 |
| 54 |
self.message_col_width = 400 |
| 55 |
|
| 56 |
local total_width = 489 |
| 57 |
local height_divisor = 4 |
| 58 |
|
| 59 |
self.approx_rows_space = math.floor(app.config.height / height_divisor) |
| 60 |
self.num_visible_rows = math.ceil(self.approx_rows_space / self.row_height) |
| 61 |
|
| 62 |
local rows_space = self.num_visible_rows * self.row_height |
| 63 |
local total_height = self.header_height + rows_space + self.window_margin * 2 -- header + values + margin (top and bottom) |
| 64 |
|
| 65 |
self:UIResizable(ui, total_width, total_height, col_bg) |
| 66 |
self:setDefaultPosition(0.05, 0.05) |
| 67 |
self:createControls() |
| 68 |
self:update() |
| 69 |
end |
| 70 |
|
| 71 |
function UIAdviserHistory:deleteButtonClicked(message_index) |
| 72 |
self.ui.adviser:removeMessageFromHistory(message_index) |
| 73 |
end |
| 74 |
|
| 75 |
local deleteAllButtonClicked = --[[persistable:adviser_history_delete_all_button]] function(self) |
| 76 |
self.ui.adviser:deleteAllMessagesFromHistory() |
| 77 |
end |
| 78 |
|
| 79 |
function UIAdviserHistory:createControls() |
| 80 |
local rows = self.num_visible_rows |
| 81 |
|
| 82 |
self.panel_sprites = self.ui.app.gfx:loadSpriteTable("QData", "Req03V", true) |
| 83 |
self.white_font = self.ui.app.gfx:loadFontAndSpriteTable("QData", "Font01V") |
| 84 |
|
| 85 |
if rows ~= self.rows_shown then |
| 86 |
local function delete_factory(num) |
| 87 |
return --[[persistable:adviser_history_delete_button]] function() |
| 88 |
local scrollbar_offset = self.scrollbar.value - 1 |
| 89 |
local message_index = scrollbar_offset + num |
| 90 |
self:deleteButtonClicked(message_index) |
| 91 |
end |
| 92 |
end |
| 93 |
local scrollbarMovedCallback = --[[persistable:adviser_history_scrollbar]] function() |
| 94 |
self:scrollbarMoved() |
| 95 |
end |
| 96 |
|
| 97 |
self.rows_shown = rows |
| 98 |
self.list_table = {} |
| 99 |
local x = self.window_margin |
| 100 |
local y = self.window_margin |
| 101 |
|
| 102 |
-- Draw headers |
| 103 |
local delete_header = self:addBevelPanel(x, y, self.delete_col_width, self.header_height, col_highlight) |
| 104 |
local delete_all_button = delete_header:makeButton(0, 0, self.delete_col_width, self.header_height, nil, deleteAllButtonClicked) |
| 105 |
:setTooltip(_S.tooltip.adviser_history.header.delete_message):setLabel("X", self.white_font, "center") |
| 106 |
x = x + self.delete_col_width |
| 107 |
|
| 108 |
local message_header = self:addBevelPanel(x, y, self.message_col_width, self.header_height, col_highlight) |
| 109 |
:setLabel(_S.adviser_history.message):setTooltip(_S.tooltip.adviser_history.header.message) |
| 110 |
x = x + self.message_col_width |
| 111 |
y = y + self.header_height |
| 112 |
|
| 113 |
-- Draw rows |
| 114 |
for i = 1, rows, 1 do |
| 115 |
x = self.window_margin |
| 116 |
local delete_panel = self:addBevelPanel(x, y, self.delete_col_width, self.row_height, col_delete_button) |
| 117 |
local delete_button = delete_panel:makeButton(0, 0, self.delete_col_width, self.row_height, nil, delete_factory(i)) |
| 118 |
x = x + self.delete_col_width |
| 119 |
|
| 120 |
local message_panel = self:addBevelPanel(x, y, self.message_col_width, self.row_height, col_bg) |
| 121 |
:setTooltip(_S.tooltip.adviser_history.message):setTextWrap(true) |
| 122 |
x = x + self.message_col_width |
| 123 |
y = y + self.row_height |
| 124 |
|
| 125 |
table.insert(self.list_table, { |
| 126 |
delete_header = delete_header, |
| 127 |
delete_all_button = delete_all_button, |
| 128 |
message_header = message_header, |
| 129 |
delete_panel = delete_panel, |
| 130 |
delete_button = delete_button, |
| 131 |
message_panel = message_panel, |
| 132 |
}) |
| 133 |
end |
| 134 |
|
| 135 |
x = x + 10 -- Add 10 pixels from end of the message rows |
| 136 |
y = self.window_margin + self.header_height + 10 |
| 137 |
|
| 138 |
-- Add scrollbar |
| 139 |
self.scrollbar = self:addColourPanel(x, y, 24, self.row_height * rows - 10, |
| 140 |
col_shadow.red, col_shadow.green, col_shadow.blue) |
| 141 |
:makeScrollbar(col_bg, scrollbarMovedCallback, 1, 1, 10, 1) |
| 142 |
|
| 143 |
-- Add close button |
| 144 |
self:addPanel(337, x, 15):makeButton(0, 0, 24, 24, 338, self.close) |
| 145 |
:setTooltip(_S.tooltip.adviser_history.close) |
| 146 |
end |
| 147 |
end |
| 148 |
|
| 149 |
function UIAdviserHistory:update() |
| 150 |
local adviser_messages = self.ui.adviser.message_history |
| 151 |
self.last_seen_history_top = adviser_messages[1] |
| 152 |
self.last_history_count = #adviser_messages |
| 153 |
|
| 154 |
self.scrollbar:setRange(1, math.max(1, #adviser_messages), self.rows_shown, self.scrollbar.value) |
| 155 |
self:scrollbarMoved() |
| 156 |
end |
| 157 |
|
| 158 |
function UIAdviserHistory:scrollbarMoved() |
| 159 |
local scroll_pos = self.scrollbar.value |
| 160 |
local adviser_messages = self.ui.adviser.message_history |
| 161 |
|
| 162 |
for i = 1, self.rows_shown, 1 do |
| 163 |
local message = adviser_messages[i + scroll_pos - 1] |
| 164 |
local row = self.list_table[i] |
| 165 |
if message then |
| 166 |
row.message_panel:setLabel(message, nil, "left"):setColour(col_bg) |
| 167 |
row.delete_button:setLabel("X", self.white_font, "center"):setTooltip(_S.tooltip.adviser_history.delete_message) |
| 168 |
row.delete_button:enable(true) |
| 169 |
else |
| 170 |
row.message_panel:setLabel(""):setColour(col_shadow) |
| 171 |
row.delete_button:setLabel(""):setTooltip(nil) |
| 172 |
row.delete_button:enable(false) |
| 173 |
end |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
function UIAdviserHistory:onTick() |
| 178 |
-- The adviser.message_history has a cap of 20 messages by default |
| 179 |
-- New messages only get added to the top (it's a stack) or, if duplicate, a message |
| 180 |
-- gets moved from the middle to the top. |
| 181 |
|
| 182 |
-- Checking the length of both lists would fail if a message was moved from the middle to the top due to duplication. |
| 183 |
-- So we just check if the top string between lists has changed to determine if we need to update |
| 184 |
local new_history_top = self.ui.adviser.message_history[1] |
| 185 |
local new_history_count = #self.ui.adviser.message_history |
| 186 |
local out_of_date = self.last_seen_history_top ~= new_history_top or self.last_history_count ~= new_history_count |
| 187 |
|
| 188 |
if out_of_date then |
| 189 |
self:update() |
| 190 |
end |
| 191 |
end |
| 192 |
|
| 193 |
function UIAdviserHistory:afterLoad(old, new) |
| 194 |
if old < 249 then |
| 195 |
self:close() |
| 196 |
end |
| 197 |
Window.afterLoad(self, old, new) |
| 198 |
end |