diff --git a/README.md b/README.md index fe3f0039..5b31f625 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ reference in **[docs/API.md](docs/API.md)**. | [Expansion](docs/API.md#expansion) | `ClassicExpansionAtLeast`, `ClassicExpansionAtMost`, `GetClassicExpansionLevel` | | [Faction](docs/API.md#faction) | `C_Reputation.GetFactionDataByID`, `C_Reputation.GetFactionDataByIndex`, `C_Reputation.GetFactionStandings`, `C_Reputation.GetLastStandingChange`, `C_Reputation.GetWatchedFactionData`, `C_Reputation.IsFactionActive`, `C_Reputation.IsFactionActiveByID`, `C_Reputation.IsFactionInactive`, `C_Reputation.SetFactionActive`, `C_Reputation.SetFactionActiveByID`, `C_Reputation.SetFactionInactive`, `C_Reputation.SetFactionInactiveByID`, `C_Reputation.SetSelectedFaction`, `C_Reputation.SetSelectedFactionByID`, `C_Reputation.SetWatchedFactionByID`, `C_Reputation.ToggleFactionAtWar`, `C_Reputation.ToggleFactionAtWarByID`, `GetFactionIDByIndex`, `GetFactionInfoByID`, `GetFactionParentID` | | [Focus](docs/API.md#focus) | `ClearFocus`, `FocusUnit` | -| [Frame](docs/API.md#frame) | `region:SetPoint("point")` (one-arg form), `region:SetSize`, `region:GetSize`, `region:IsMouseOver`, `region:GetRect`, `region:IsDragging`, `GetMouseFoci`, `CreateFrame` template lists and `id`, `CreateFrame("Cooldown")`, `cooldown:SetCooldown`, `cooldown:SetReverse`, `cooldown:GetReverse`, `cooldown:SetDrawEdge`, `cooldown:GetDrawEdge`, `frame:SetShown`, `frame:SetClampRectInsets`, `frame:GetClampRectInsets`, `smf:SetHyperlinksEnabled`, `smf:GetHyperlinksEnabled`, `editBox:SetCountInvisibleLetters`, `editBox:IsCountInvisibleLetters`, `fontstring:GetStringHeight`, `fontstring:GetUnboundedStringWidth`, `fontstring:GetWrappedWidth`, `fontstring:GetNumLines`, `fontstring:GetLineHeight`, `fontstring:IsTruncated`, `fontstring:SetMaxLines`, `fontstring:GetMaxLines`, `fontstring:SetWordWrap`, `fontstring:CanWordWrap`, `fontstring:GetFieldSize`, `SetIndentedWordWrap`, `GetIndentedWordWrap` (font strings, fonts, edit boxes, message frames, SimpleHTML), `fontstring:SetFormattedText`, `button:SetFormattedText`, `texture:SetRotation`, `texture:GetRotation`, `texture:SetVertexOffset`, `texture:GetVertexOffset`, `texture:SetColorTexture`, `texture:SetAtlas`, `texture:GetAtlas`, `texture:ResetTexCoord`, `texture:SetSpriteSheetCell`, `texture:SetDesaturation`, `texture:GetDesaturation`, `texture:SetMask`, `frame:CreateMaskTexture`, `texture:AddMaskTexture`, `texture:RemoveMaskTexture`, `texture:GetNumMaskTextures`, `texture:GetMaskTexture`, `fontstring:SetRotation`, `fontstring:GetRotation`, `editBox:SetCursorPosition`, `editBox:GetCursorPosition`, `editBox:GetUTF8CursorPosition`, `editBox:ClearHighlightText`, `editBox:HasFocus`, `editBox:HasText`, `editBox:SetHighlightColor`, `editBox:GetHighlightColor`, `editBox:ClearHistory`, `editBox:Enable`, `editBox:Disable`, `editBox:IsEnabled`, `slider:Enable`, `slider:Disable`, `slider:IsEnabled`, `editBox:IsInIMECompositionMode`, `OnEnable` (script), `OnDisable` (script), `OnCharComposition` (script), `OnTextChanged` `userInput`, `frame:SetResizeBounds`, `frame:HookScript`, `region:IsProtected`, `region:CanChangeProtectedState`, `frame:CanChangeAttribute`, `frame:AllowAttributeChanges`, `frame:SetPropagateKeyboardInput`, `frame:GetPropagateKeyboardInput`, `frame:SetDontSavePosition`, `frame:GetDontSavePosition`, `frame:GetBoundsRect`, `button:SetNormalFontObject`, `button:GetNormalFontObject`, `button:SetMotionScriptsWhileDisabled`, `button:GetMotionScriptsWhileDisabled`, `button:SetEnabled`, `frame:IsEventRegistered`, `frame:RegisterUnitEvent`, `frame:GetEffectiveAlpha`, `frame:SetAttribute`, `frame:SetAttributeNoHandler`, `frame:ClearAttribute`, `frame:GetAttribute`, `OnAttributeChanged` (script), `SetModernScriptArgs`, `GetModernScriptArgs`, `PreClick` (script), `PostClick` (script), `button:RegisterForClicks("AnyUp"/"AnyDown")`, `button:RegisterForMouse`, `GetClickFrame` | +| [Frame](docs/API.md#frame) | `region:SetPoint("point")` (one-arg form), `region:SetSize`, `region:GetSize`, `region:IsMouseOver`, `region:GetRect`, `region:IsDragging`, `GetMouseFoci`, `CreateFrame` template lists and `id`, `CreateFrame("Cooldown")`, `cooldown:SetCooldown`, `cooldown:SetReverse`, `cooldown:GetReverse`, `cooldown:SetDrawEdge`, `cooldown:GetDrawEdge`, `frame:SetShown`, `frame:SetClampRectInsets`, `frame:GetClampRectInsets`, `smf:SetHyperlinksEnabled`, `smf:GetHyperlinksEnabled`, `editBox:SetCountInvisibleLetters`, `editBox:IsCountInvisibleLetters`, `fontstring:GetStringHeight`, `fontstring:GetUnboundedStringWidth`, `fontstring:GetWrappedWidth`, `fontstring:GetNumLines`, `fontstring:GetLineHeight`, `fontstring:IsTruncated`, `fontstring:SetMaxLines`, `fontstring:GetMaxLines`, `fontstring:SetWordWrap`, `fontstring:CanWordWrap`, `fontstring:GetFieldSize`, `SetIndentedWordWrap`, `GetIndentedWordWrap` (font strings, fonts, edit boxes, message frames, SimpleHTML), `fontstring:SetFormattedText`, `button:SetFormattedText`, `texture:SetRotation`, `texture:GetRotation`, `texture:SetVertexOffset`, `texture:GetVertexOffset`, `texture:SetColorTexture`, `texture:SetAtlas`, `texture:GetAtlas`, `texture:ResetTexCoord`, `texture:SetSpriteSheetCell`, `texture:SetDesaturation`, `texture:GetDesaturation`, `texture:SetMask`, `frame:CreateMaskTexture`, `texture:AddMaskTexture`, `texture:RemoveMaskTexture`, `texture:GetNumMaskTextures`, `texture:GetMaskTexture`, `fontstring:SetRotation`, `fontstring:GetRotation`, `editBox:SetCursorPosition`, `editBox:GetCursorPosition`, `editBox:GetUTF8CursorPosition`, `editBox:ClearHighlightText`, `editBox:HasFocus`, `editBox:HasText`, `editBox:SetHighlightColor`, `editBox:GetHighlightColor`, `editBox:ClearHistory`, `editBox:Enable`, `editBox:Disable`, `editBox:IsEnabled`, `slider:Enable`, `slider:Disable`, `slider:IsEnabled`, `editBox:IsInIMECompositionMode`, `OnEnable` (script), `OnDisable` (script), `OnMinMaxChanged` (script), `OnCharComposition` (script), `OnTextChanged` `userInput`, `frame:SetResizeBounds`, `frame:HookScript`, `region:IsProtected`, `region:CanChangeProtectedState`, `frame:CanChangeAttribute`, `frame:AllowAttributeChanges`, `frame:SetPropagateKeyboardInput`, `frame:GetPropagateKeyboardInput`, `frame:SetDontSavePosition`, `frame:GetDontSavePosition`, `frame:GetBoundsRect`, `button:SetNormalFontObject`, `button:GetNormalFontObject`, `button:SetMotionScriptsWhileDisabled`, `button:GetMotionScriptsWhileDisabled`, `button:SetEnabled`, `frame:IsEventRegistered`, `frame:RegisterUnitEvent`, `frame:GetEffectiveAlpha`, `frame:SetAttribute`, `frame:SetAttributeNoHandler`, `frame:ClearAttribute`, `frame:GetAttribute`, `OnAttributeChanged` (script), `SetModernScriptArgs`, `GetModernScriptArgs`, `PreClick` (script), `PostClick` (script), `button:RegisterForClicks("AnyUp"/"AnyDown")`, `button:RegisterForMouse`, `GetClickFrame` | | [FriendList](docs/API.md#friendlist) | `C_FriendList.GetFriendInfo`, `C_FriendList.GetFriendInfoByIndex`, `C_FriendList.GetNumFriends`, `C_FriendList.GetNumOnlineFriends`, `C_FriendList.GetNumWhoResults`, `C_FriendList.GetWhoInfo`, `C_FriendList.IsFriend`, `C_FriendList.IsIgnored`, `C_FriendList.IsIgnoredByGuid`, `C_FriendList.IsWhoQueryPending`, `C_FriendList.SendWhoQueryByName`, `C_FriendList.SetFriendNotes`, `C_FriendList.SetFriendNotesByIndex` | | [GameObject](docs/API.md#gameobject) | `C_GameObjectInfo.GetGameObjectInfoByID`, `C_GameObjectInfo.RequestLoadGameObjectByID`, `ClosestGameObjectPosition` | | [Glue](docs/API.md#glue) | `C_Glue.IsOnGlueScreen` | diff --git a/TODO.md b/TODO.md index 4fdbf29d..198648b2 100644 --- a/TODO.md +++ b/TODO.md @@ -4079,7 +4079,7 @@ Each entry still needs the usual check of how the engine does it before building - **DONE** (`src/slider/Enable.cpp`). **Slider** `Enable` / `Disable` / `IsEnabled`: frame flag `0x400`, read by the thumb-drag path (`CSimpleSlider.cpp:317`); `Disable` runs OnDisable unless loading (we have OnEnable/OnDisable in `src/frame/EnableScripts.cpp`). -- **OnMinMaxChanged** (Slider, StatusBar): `SetMinMaxValues` runs it with +- **DONE** (`src/frame/MinMaxChanged.cpp`). **OnMinMaxChanged** (Slider, StatusBar): `SetMinMaxValues` runs it with `(min, max)` when the range changes (`CSimpleStatusBar.cpp:39/141`). 1.12 has no such script. - **StatusBar** `SetReverseFill` / `GetReverseFill`, `SetRotatesTexture` / diff --git a/docs/API.md b/docs/API.md index 3e6aa47d..67b98063 100644 --- a/docs/API.md +++ b/docs/API.md @@ -295,6 +295,7 @@ build instructions. - [`editBox:Enable()` / `editBox:Disable()` / `editBox:IsEnabled()`](#editboxenable--editboxdisable--editboxisenabled) - [`slider:Enable()` / `slider:Disable()` / `slider:IsEnabled()`](#sliderenable--sliderdisable--sliderisenabled) - [`OnEnable` and `OnDisable` (scripts)](#onenable-and-ondisable-scripts) + - [`OnMinMaxChanged(self, min, max)` (script)](#onminmaxchangedself-min-max-script) - [`editBox:IsInIMECompositionMode()`](#editboxisinimecompositionmode) - [`OnCharComposition(self, text)` (script)](#oncharcompositionself-text-script) - [`OnTextChanged(self, userInput)` (script)](#ontextchangedself-userinput-script) @@ -6754,6 +6755,24 @@ button:SetScript("OnDisable", function(self) self:SetAlpha(0.5) end) button:SetScript("OnEnable", function(self) self:SetAlpha(1) end) ``` +### `OnMinMaxChanged(self, min, max)` (script) + +A slider or a status bar runs this script when `SetMinMaxValues` changes +its range. `min` and `max` are the new range. Set it with `SetScript`, +`HookScript`, or in XML. + +- A call that does not change the range does not run the script. +- The script runs before the frame applies its value to the new range, so + `OnValueChanged` runs after it. +- A status bar whose `min` is more than its `max` gets `max` for both. +- The range in the frame's XML also runs the script when the frame loads. + +```lua +bar:SetScript("OnMinMaxChanged", function(self, min, max) + self.text:SetText(min .. " - " .. max) +end) +``` + ### `editBox:IsInIMECompositionMode()` Returns `1` while an input method (for example, one for Chinese, Japanese, diff --git a/src/Offsets.h b/src/Offsets.h index b89f670d..28b5fb04 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -3140,6 +3140,37 @@ enum Offsets { // `ret 4`: drag arming, then the OnMouseDown script with the button name. FUN_FRAME_ON_MOUSE_DOWN = 0x0076BF70, + // StatusBar's (Script StatusBar:SetMinMaxValues reads it at 0x0078F4B6). + VAR_STATUSBAR_LUA_TYPE_ID = 0x00CF4E5C, + // The value block a Slider (vtable 0x0081CF60) and a StatusBar (0x0081CA68) + // share in shape: flags, then min, then the slider's span (max - min) or + // the bar's max, then the value. Read and written by both classes' + // SetMinMaxValues / SetValue below. + OFF_VALUE_FLAGS = 0x314, + OFF_VALUE_MIN = 0x318, + OFF_SLIDER_SPAN = 0x31C, + OFF_STATUSBAR_MAX = 0x31C, + OFF_VALUE_VALUE = 0x320, + VALUE_FLAG_RANGE_SET = 0x2, + VALUE_FLAG_VALUE_SET = 0x4, + // CSimpleSlider::SetMinMaxValues — `void __thiscall(slider, float min, + // float max)`, `ret 8`: stores min and max - min, ORs 0x3 into the flags, + // then with VALUE_FLAG_VALUE_SET calls FUN_SLIDER_SET_VALUE on the value. + // Called by Slider:SetMinMaxValues (0x00790874), LoadXML and SetValueStep. + FUN_SLIDER_SET_MIN_MAX_VALUES = 0x007898F0, + // CSimpleSlider::SetValue — `void __thiscall(slider, float)`, `ret 4`. + FUN_SLIDER_SET_VALUE = 0x00789930, + // CSimpleStatusBar::SetMinMaxValues — `void __thiscall(bar, float min, + // float max)`, `ret 8`: stores min (max when min > max) and max, ORs 0x3 + // into the flags, then with VALUE_FLAG_VALUE_SET clears it and calls the + // bar's SetValue (vtable OFF_VMT_STATUSBAR_SET_VALUE) on the value. Called + // by StatusBar:SetMinMaxValues (0x0078F584), LoadXML and the unit-frame / + // nameplate health bars. + FUN_STATUSBAR_SET_MIN_MAX_VALUES = 0x00783270, + // CSimpleStatusBar::SetValue's vtable slot (0x007832D0), `void + // __thiscall(bar, float)`, as FUN_STATUSBAR_SET_MIN_MAX_VALUES calls it. + OFF_VMT_STATUSBAR_SET_VALUE = 0x90, + // CFrameScriptObject vtable slot 4 — the `IsA(typeId)` predicate every // frame class implements, and the gate every engine method applies to its // `self` before touching it. `char __thiscall(void *self, int typeId)`. diff --git a/src/frame/ExtraScripts.cpp b/src/frame/ExtraScripts.cpp index 7225374c..88cf1688 100644 --- a/src/frame/ExtraScripts.cpp +++ b/src/frame/ExtraScripts.cpp @@ -36,8 +36,8 @@ int __fastcall Resolver_h(void *frame, void *edx, const char *name) { for (const Script *s = g_head; s != nullptr; s = s->next) { if (!Ascii::EqualCI(name, s->nameLower)) continue; - if (!Game::Lua::IsA(frame, s->typeIdVar)) - return 0; + if (!Game::Lua::IsA(frame, s->typeIdVar)) // another class may have the name + continue; Script::Cell &cell = s->cells[frame]; return reinterpret_cast(cell.v); } diff --git a/src/frame/MinMaxChanged.cpp b/src/frame/MinMaxChanged.cpp new file mode 100644 index 00000000..44461345 --- /dev/null +++ b/src/frame/MinMaxChanged.cpp @@ -0,0 +1,122 @@ +// 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 OnMinMaxChanged script of Slider and StatusBar. +// +// 4.3.4's SetMinMaxValues (CSimpleSlider.cpp:326, CSimpleStatusBar.cpp:141) +// returns when a range is set and the new one equals it; otherwise it stores +// the range, runs OnMinMaxChanged with (min, max), and then applies the +// current value again. The slider's test allows 2^-22 on the minimum and the +// span; the bar's is exact, after the bar takes max as the minimum when +// min > max. 1.12's SetMinMaxValues is the same store and re-apply with no +// test and no script. +// +// The co-hooks run the store with VALUE_FLAG_VALUE_SET held clear, which +// skips only the re-apply at its tail, then put the flag back, run the +// script, and do the re-apply as 1.12 does: the slider calls SetValue, the +// bar clears the flag and calls its SetValue slot. A call that leaves the +// range as it is goes straight to the original. Each class's XML range goes +// through the same setter after its , as in 4.3.4, so the script +// runs on load too. + +#include "Game.h" +#include "Offsets.h" +#include "frame/ExtraScripts.h" +#include "frame/ScriptArgs.h" + +#include +#include + +namespace Frame::MinMaxChanged { + +namespace { + +const Frame::ExtraScripts::Script g_onSliderMinMaxChanged{"onminmaxchanged", Offsets::VAR_SLIDER_LUA_TYPE_ID}; +const Frame::ExtraScripts::Script g_onStatusBarMinMaxChanged{"onminmaxchanged", + Offsets::VAR_STATUSBAR_LUA_TYPE_ID}; + +using SetMinMax_t = void(__fastcall *)(void *frame, void *edx, float min, float max); +using SetValue_t = void(__fastcall *)(void *frame, void *edx, float value); + +SetMinMax_t g_origSlider = nullptr; +SetMinMax_t g_origStatusBar = nullptr; + +uint32_t &Flags(void *frame) { return *reinterpret_cast(static_cast(frame) + Offsets::OFF_VALUE_FLAGS); } + +constexpr uint32_t kValueSet = Offsets::VALUE_FLAG_VALUE_SET; + +// Stores the range through the original without its value re-apply, then +// runs the script with (min, max). Returns whether a value was set before. +bool StoreAndRun(SetMinMax_t orig, const Frame::ExtraScripts::Script &script, void *frame, void *edx, float min, + float max, float shownMin, float shownMax) { + const bool hadValue = (Flags(frame) & kValueSet) != 0; + Flags(frame) &= ~kValueSet; + orig(frame, edx, min, max); + if (hadValue) + Flags(frame) |= kValueSet; + + const uint32_t *slot = script.Slot(frame); + void *L = slot != nullptr && slot[0] != 0 ? Game::Lua::State() : nullptr; + if (L != nullptr) { + const int top = Game::Lua::GetTop(L); + Game::Lua::PushNumber(L, shownMin); + Game::Lua::PushNumber(L, shownMax); + Frame::ScriptArgs::RunSlotWithValues(L, slot, frame, top + 1, 2); + Game::Lua::SetTop(L, top); + } + return hadValue; +} + +void __fastcall SliderSetMinMax_h(void *slider, void *edx, float min, float max) { + constexpr float kEpsilon = 0.00000023841858f; + const float span = max - min; + if ((Flags(slider) & Offsets::VALUE_FLAG_RANGE_SET) != 0 && + std::fabs(span - Game::Read(slider, Offsets::OFF_SLIDER_SPAN)) < kEpsilon && + std::fabs(Game::Read(slider, Offsets::OFF_VALUE_MIN) - min) < kEpsilon) { + g_origSlider(slider, edx, min, max); + return; + } + StoreAndRun(g_origSlider, g_onSliderMinMaxChanged, slider, edx, min, max, min, min + span); + if ((Flags(slider) & kValueSet) != 0) // set before, or by the script + reinterpret_cast(Offsets::FUN_SLIDER_SET_VALUE)( + slider, edx, Game::Read(slider, Offsets::OFF_VALUE_VALUE)); +} + +void __fastcall StatusBarSetMinMax_h(void *bar, void *edx, float min, float max) { + const float storedMin = min > max ? max : min; + if ((Flags(bar) & Offsets::VALUE_FLAG_RANGE_SET) != 0 && + Game::Read(bar, Offsets::OFF_VALUE_MIN) == storedMin && + Game::Read(bar, Offsets::OFF_STATUSBAR_MAX) == max) { + g_origStatusBar(bar, edx, min, max); + return; + } + const bool hadValue = StoreAndRun(g_origStatusBar, g_onStatusBarMinMaxChanged, bar, edx, min, max, storedMin, max); + if (hadValue || (Flags(bar) & kValueSet) != 0) { // set before, or by the script + if (hadValue) + Flags(bar) &= ~kValueSet; // 1.12's tail: the re-apply always runs OnValueChanged + const auto setValue = reinterpret_cast( + Game::Read(*static_cast(bar), Offsets::OFF_VMT_STATUSBAR_SET_VALUE)); + setValue(bar, edx, Game::Read(bar, Offsets::OFF_VALUE_VALUE)); + } +} + +const Game::HookAutoRegister _sliderHook{Offsets::FUN_SLIDER_SET_MIN_MAX_VALUES, + reinterpret_cast(&SliderSetMinMax_h), + reinterpret_cast(&g_origSlider)}; +const Game::HookAutoRegister _statusBarHook{Offsets::FUN_STATUSBAR_SET_MIN_MAX_VALUES, + reinterpret_cast(&StatusBarSetMinMax_h), + reinterpret_cast(&g_origStatusBar)}; + +} // namespace + +} // namespace Frame::MinMaxChanged