// 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 . // Retail's scroll offset on ScrollingMessageFrame: `SetScrollOffset(offset)`, // `GetScrollOffset()`, `GetMaxScrollRange()` and `ScrollByAmount(amount)`. // // Retail's offset (Blizzard_SharedXML/ScrollingMessageFrame.lua) counts the // lines the frame is scrolled up from the newest message, 0 at the bottom, up // to GetMaxScrollRange() = max(GetNumMessages() - 1, 0), where the oldest // message is the bottom line - the place 1.12's own ScrollToTop goes. 1.12 // keeps the same offset: ScrollToOffset and ScrollOffset (Offsets.h) are its // two directions, and GetScrollOffset is the engine's GetCurrentScroll under // retail's name. Retail clamps the offset to its range; the engine wraps one // outside it, so the setters clamp first. SetScrollOffset to the offset the // frame has does nothing, as on retail. // // Retail's ScrollUp and ScrollDown are ScrollByAmount(1) and (-1), so // ScrollByAmount steps the engine's own ScrollUp / ScrollDown, the loop its // PageUp runs. The engine's ScrollUp stops once the oldest message is on // screen, short of the range's end, where that message sits alone on the // bottom line under empty space; moving the offset straight there scrolls // chat up into that space (pfUI's chat wheel did). The clamped target still // bounds the steps, since a hidden frame never lays out and its ScrollUp never // finds the top. Every step re-arms the shown lines' fade, and an amount that // takes no step re-arms through ScrollToOffset: retail's ResetAllFadeTimes. #include "Game.h" #include "Offsets.h" #include "scrollingmessageframe/DisplayRefreshed.h" namespace ScrollingMessageFrame::Scroll { namespace { using ScrollToOffset_t = void(__fastcall *)(void *smf, void *edx, int offset); using SmfInt_t = int(__fastcall *)(void *smf); int ScrollOffset(void *smf) { return reinterpret_cast(Offsets::FUN_SCROLLINGMESSAGEFRAME_SCROLL_OFFSET)(smf); } // One step of the engine's ScrollUp / ScrollDown; false at their end. bool ScrollUp(void *smf) { return reinterpret_cast(Offsets::FUN_SCROLLINGMESSAGEFRAME_SCROLL_UP)(smf) != 0; } bool ScrollDown(void *smf) { return reinterpret_cast(Offsets::FUN_SCROLLINGMESSAGEFRAME_SCROLL_DOWN)(smf) != 0; } int MaxScrollRange(void *smf) { const int count = Game::Read(smf, Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_COUNT); return count > 1 ? count - 1 : 0; } // `offset` clamped to 0..MaxScrollRange (NaN to 0). int Clamped(void *smf, double offset) { const int max = MaxScrollRange(smf); return offset >= 0 ? (offset < max ? static_cast(offset) : max) : 0; } void ScrollTo(void *smf, int offset) { reinterpret_cast(Offsets::FUN_SCROLLINGMESSAGEFRAME_SCROLL_TO_OFFSET)(smf, nullptr, offset); } void *ResolveSmf(void *L) { return Game::Lua::ResolveTypedObject(L, 1, Offsets::VAR_SCROLLINGMESSAGEFRAME_LUA_TYPE_ID); } int __fastcall Script_SetScrollOffset(void *L) { void *frame = ResolveSmf(L); if (frame == nullptr) return 0; if (!Game::Lua::IsNumber(L, 2)) { Game::Lua::Error(L, "Usage: %s:SetScrollOffset(offset)", Game::Lua::ObjectDisplayName(frame)); return 0; } const int offset = Clamped(frame, Game::Lua::ToNumber(L, 2)); if (offset != ScrollOffset(frame)) ScrollTo(frame, offset); return 0; } int __fastcall Script_GetMaxScrollRange(void *L) { void *frame = ResolveSmf(L); if (frame == nullptr) return 0; Game::Lua::PushNumber(L, MaxScrollRange(frame)); return 1; } int __fastcall Script_ScrollByAmount(void *L) { void *frame = ResolveSmf(L); if (frame == nullptr) return 0; if (!Game::Lua::IsNumber(L, 2)) { Game::Lua::Error(L, "Usage: %s:ScrollByAmount(amount)", Game::Lua::ObjectDisplayName(frame)); return 0; } const int offset = ScrollOffset(frame); int steps = Clamped(frame, offset + Game::Lua::ToNumber(L, 2)) - offset; if (steps == 0) ScrollTo(frame, offset); while (steps > 0 && ScrollUp(frame)) --steps; while (steps < 0 && ScrollDown(frame)) ++steps; DisplayRefreshed::MarkDirty(frame); return 0; } const Game::Lua::FrameMethodEntry kMethods[] = { {"SetScrollOffset", &Script_SetScrollOffset}, {"GetScrollOffset", reinterpret_cast(Offsets::FUN_SCRIPT_SCROLLINGMESSAGEFRAME_GET_CURRENT_SCROLL)}, {"GetMaxScrollRange", &Script_GetMaxScrollRange}, {"ScrollByAmount", &Script_ScrollByAmount}, }; const Game::Doc::Field kOffsetArgs[] = { Game::Doc::Req("offset", "number", "Lines from the newest message; clamped to 0..GetMaxScrollRange()."), }; const Game::Doc::Field kOffsetRets[] = { Game::Doc::Req("offset", "number", "Lines from the newest message; 0 when not scrolled."), }; const Game::Doc::Field kRangeRets[] = { Game::Doc::Req("maxScrollRange", "number"), }; const Game::Doc::Field kAmountArgs[] = { Game::Doc::Req("amount", "number", "Positive scrolls toward older messages."), }; const Game::Doc::Function kSetScrollOffset{"Scrolls the frame to a number of lines from the newest message.", kOffsetArgs, {}}; const Game::Doc::Function kGetScrollOffset{"Returns how many lines the frame is scrolled from the newest message.", {}, kOffsetRets}; const Game::Doc::Function kGetMaxScrollRange{"Returns the largest scroll offset: one less than the message count.", {}, kRangeRets}; const Game::Doc::Function kScrollByAmount{ "Scrolls the frame by a number of lines, stopping where ScrollUp and ScrollDown stop, and shows the faded lines " "again.", kAmountArgs, {}}; const Game::Doc::Method kDocs[] = { {"SetScrollOffset", &kSetScrollOffset}, {"GetScrollOffset", &kGetScrollOffset}, {"GetMaxScrollRange", &kGetMaxScrollRange}, {"ScrollByAmount", &kScrollByAmount}, }; void RegisterLuaFunctions() { Game::Lua::RegisterFrameMethods(reinterpret_cast(Offsets::VAR_SCROLLINGMESSAGEFRAME_METHOD_REGISTRY), kMethods, 4, kDocs, 4); } const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; } // namespace } // namespace ScrollingMessageFrame::Scroll