diff --git a/src/scrollingmessageframe/DeferredLayout.cpp b/src/scrollingmessageframe/DeferredLayout.cpp index caddf0ed..8bff9cd9 100644 --- a/src/scrollingmessageframe/DeferredLayout.cpp +++ b/src/scrollingmessageframe/DeferredLayout.cpp @@ -182,12 +182,14 @@ void RegisterLuaFunctions() { reinterpret_cast(&WriterLayout)); } +void DropBeforeLayout(void *smf, int) { Drop(smf); } + void PrepareForReload() { g_pending.clear(); } const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; const Game::ReloadAutoRegister _reload{&PrepareForReload}; const Frame::Destruction::AutoSubscribe _destructionSub{&Drop}; -const ScrollingMessageFrame::Layout::AutoSubscribeBefore _layoutSub{&Drop}; +const ScrollingMessageFrame::Layout::AutoSubscribeBefore _layoutSub{&DropBeforeLayout}; } // namespace diff --git a/src/scrollingmessageframe/InsertMode.cpp b/src/scrollingmessageframe/InsertMode.cpp index 89d09109..8f496c22 100644 --- a/src/scrollingmessageframe/InsertMode.cpp +++ b/src/scrollingmessageframe/InsertMode.cpp @@ -39,6 +39,7 @@ #include "Offsets.h" #include "frame/Destruction.h" #include "scrollingmessageframe/DisplayRefreshed.h" +#include "scrollingmessageframe/InsertMode.h" #include #include @@ -92,28 +93,18 @@ void __fastcall AnchorNewLine(void *anchor, void *, int point, void *relative, i flag); } +void *LineOf(void *smf, int index) { + const auto *lines = Game::Read(smf, Offsets::OFF_SMF_LINE_ARRAY); + return Game::Read(lines + index * Offsets::SMF_LINE_STRIDE, Offsets::OFF_SMF_LINE_FONTSTRING); +} + // Anchors the frame's lines again, in order, with the layout's arguments and // steps. void AnchorLines(void *smf) { const int count = Game::Read(smf, Offsets::OFF_SMF_LINE_COUNT); - const auto *lines = Game::Read(smf, Offsets::OFF_SMF_LINE_ARRAY); - void *previous = nullptr; for (int i = 0; i < count; ++i) { - void *line = Game::Read(lines + i * Offsets::SMF_LINE_STRIDE, Offsets::OFF_SMF_LINE_FONTSTRING); - void *anchor = AnchorOf(line); - reinterpret_cast(Offsets::FUN_LAYOUT_CLEAR_ALL_POINTS)(anchor); - if (previous == nullptr) - SetLinePoint(smf, anchor, Offsets::FRAMEPOINT_BOTTOMLEFT, AnchorOf(smf), Offsets::FRAMEPOINT_BOTTOMLEFT, - Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_INSET_LEFT), - Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_INSET_BOTTOM), 1); - else - SetLinePoint(smf, anchor, Offsets::FRAMEPOINT_BOTTOMLEFT, AnchorOf(previous), Offsets::FRAMEPOINT_TOPLEFT, - 0.0f, - reinterpret_cast(Offsets::FUN_PIXEL_ALIGN_Y)( - Game::Read(line, Offsets::OFF_FONTSTRING_SPACING)), - 1); - reinterpret_cast(Offsets::FUN_LAYOUT_RESIZE)(anchor, nullptr, 1); - previous = line; + AnchorLine(smf, i); + reinterpret_cast(Offsets::FUN_LAYOUT_RESIZE)(AnchorOf(LineOf(smf, i)), nullptr, 1); } } @@ -191,4 +182,20 @@ const Frame::Destruction::AutoSubscribe _destructionSub{&OnFrameDestroyed}; } // namespace +void AnchorLine(void *smf, int index) { + void *line = LineOf(smf, index); + void *anchor = AnchorOf(line); + reinterpret_cast(Offsets::FUN_LAYOUT_CLEAR_ALL_POINTS)(anchor); + if (index == 0) + SetLinePoint(smf, anchor, Offsets::FRAMEPOINT_BOTTOMLEFT, AnchorOf(smf), Offsets::FRAMEPOINT_BOTTOMLEFT, + Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_INSET_LEFT), + Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_INSET_BOTTOM), 1); + else + SetLinePoint(smf, anchor, Offsets::FRAMEPOINT_BOTTOMLEFT, AnchorOf(LineOf(smf, index - 1)), + Offsets::FRAMEPOINT_TOPLEFT, 0.0f, + reinterpret_cast(Offsets::FUN_PIXEL_ALIGN_Y)( + Game::Read(line, Offsets::OFF_FONTSTRING_SPACING)), + 1); +} + } // namespace ScrollingMessageFrame::InsertMode diff --git a/src/scrollingmessageframe/InsertMode.h b/src/scrollingmessageframe/InsertMode.h new file mode 100644 index 00000000..eecddfca --- /dev/null +++ b/src/scrollingmessageframe/InsertMode.h @@ -0,0 +1,24 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +namespace ScrollingMessageFrame::InsertMode { + +// Anchors line `index` of the frame again as the line layout anchors it when +// it makes the line, for the frame's insert mode: line 0 on the frame, every +// other line on the line before it. Clears the line's points first; the +// caller lays the line out. +void AnchorLine(void *smf, int index); + +} // namespace ScrollingMessageFrame::InsertMode diff --git a/src/scrollingmessageframe/Layout.cpp b/src/scrollingmessageframe/Layout.cpp index 93a79a8b..1b5b94b1 100644 --- a/src/scrollingmessageframe/Layout.cpp +++ b/src/scrollingmessageframe/Layout.cpp @@ -28,7 +28,7 @@ Layout_t g_origLayout = nullptr; void __fastcall Layout_h(void *smf, void *edx, int newestRecord) { for (const AutoSubscribeBefore *s = g_beforeHead; s != nullptr; s = s->next) - s->fn(smf); + s->fn(smf, newestRecord); g_origLayout(smf, edx, newestRecord); for (const AutoSubscribe *s = g_head; s != nullptr; s = s->next) s->fn(smf); @@ -41,6 +41,6 @@ const Game::HookAutoRegister _hook{Offsets::FUN_SMF_DISPLAY_REFRESH, reinterpret AutoSubscribe::AutoSubscribe(Listener fn) : fn(fn), next(g_head) { g_head = this; } -AutoSubscribeBefore::AutoSubscribeBefore(Listener fn) : fn(fn), next(g_beforeHead) { g_beforeHead = this; } +AutoSubscribeBefore::AutoSubscribeBefore(BeforeListener fn) : fn(fn), next(g_beforeHead) { g_beforeHead = this; } } // namespace ScrollingMessageFrame::Layout diff --git a/src/scrollingmessageframe/Layout.h b/src/scrollingmessageframe/Layout.h index 3555c42c..b4423856 100644 --- a/src/scrollingmessageframe/Layout.h +++ b/src/scrollingmessageframe/Layout.h @@ -33,12 +33,15 @@ struct AutoSubscribe { AutoSubscribe *next; }; -// A listener that runs BEFORE the layout, with the frame. The layout can run -// Lua (the frame's OnMessageScrollChanged), and so a nested layout, before it -// returns; state a layout makes current must be dropped here, not after. +// A listener that runs BEFORE the layout, with the frame and the record the +// layout runs from (the scroll record it is about to store). The layout can +// run Lua (the frame's OnMessageScrollChanged), and so a nested layout, before +// it returns; state a layout makes current must be dropped here, not after. +using BeforeListener = void (*)(void *smf, int newestRecord); + struct AutoSubscribeBefore { - explicit AutoSubscribeBefore(Listener fn); - Listener fn; + explicit AutoSubscribeBefore(BeforeListener fn); + BeforeListener fn; AutoSubscribeBefore *next; }; diff --git a/src/scrollingmessageframe/LineReuse.cpp b/src/scrollingmessageframe/LineReuse.cpp new file mode 100644 index 00000000..f863fe6d --- /dev/null +++ b/src/scrollingmessageframe/LineReuse.cpp @@ -0,0 +1,92 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// The lines a layout keeps on screen keep their font strings. +// +// The line layout (FUN_SMF_DISPLAY_REFRESH) shows record (position - i) in +// line i, bottom up, and sets each line's text through ShowLine (0x00788AF0). +// When the position moves - a message at the bottom, a scroll - every line +// gets the text the line below or above it had: a copy, a freed built string, +// a new measure and a new shaping each. Retail's mixin and 4.3.4 do the same. +// +// Before a layout of a shown frame, the line entries are turned by the +// distance the position moves, so each line that stays on screen is the font +// string that already holds its text. The layout then runs as it is. Its +// SetText (0x00771D80) returns at its compare when the text is the one the +// line holds, so the line keeps its built string and its measured height; +// SetVertexColor skips a repeated colour; and its forced Resize moves the +// built string with the line's rectangle (the rect callback 0x00772D40 +// rebuilds only when the size changes). Only the lines that come round get a +// new text. The hyperlink buttons are rebuilt from the lines after every +// layout, and the inline textures are anchored to their line, so both follow. +// +// The entries move as whole 0x10-byte values, as the engine's own array grow +// (0x00789060) moves them with SMemReAlloc. Turning the array keeps every +// line anchored on the line below it but two: the new line 0, which was on +// the line below it, goes on the frame, and the old line 0, which was on the +// frame, goes on the line below it now. Line 0 is anchored first, so no line +// is ever anchored to one that depends on it. Both are anchored as the layout +// anchors a new line, in the frame's insert mode. +// +// The distance is the layout's record minus the scroll record, the record +// line 0 shows. That holds after a layout of a shown frame, which counts the +// lines it places; a layout of a hidden frame counts none, so a frame whose +// count is 0 is laid out as before. A distance as large as the lines is no +// reuse at all. A wrong distance costs only the reuse: the layout sets every +// text it shows either way. + +#include "Game.h" +#include "Offsets.h" +#include "scrollingmessageframe/InsertMode.h" +#include "scrollingmessageframe/Layout.h" + +#include +#include + +namespace ScrollingMessageFrame::LineReuse { + +namespace { + +void BeforeLayout(void *smf, int newestRecord) { + struct Line { + uint8_t bytes[Offsets::SMF_LINE_STRIDE]; + }; + if (Game::Read(smf, Offsets::OFF_FRAME_VISIBLE) == 0 || + Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_LINES_SHOWN) <= 0 || + Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_COUNT) <= 0) + return; + const int lines = Game::Read(smf, Offsets::OFF_SMF_LINE_COUNT); + const int max = Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_MAX_MESSAGES); + if (lines <= 1 || max <= 0) + return; + // The ring wraps at the limit; the shorter way round is the distance. + int distance = newestRecord - Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_SCROLL_RECORD); + if (2 * distance > max) + distance -= max; + else if (2 * distance < -max) + distance += max; + if (distance == 0 || distance >= lines || distance <= -lines) + return; + // Line i takes the entry line (i - distance) had. + const int turn = (distance + lines) % lines; + auto *entries = Game::Read(smf, Offsets::OFF_SMF_LINE_ARRAY); + std::rotate(entries, entries + (lines - turn), entries + lines); + InsertMode::AnchorLine(smf, 0); + InsertMode::AnchorLine(smf, turn); +} + +const ScrollingMessageFrame::Layout::AutoSubscribeBefore _layoutSub{&BeforeLayout}; + +} // namespace + +} // namespace ScrollingMessageFrame::LineReuse