diff --git a/README.md b/README.md index 17d38b7e..64969264 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ reference in **[docs/API.md](docs/API.md)**. | [Expansion](docs/API.md#expansion) | `ClassicExpansionAtLeast`, `ClassicExpansionAtMost`, `GetClassicExpansionLevel` | | [Faction](docs/API.md#faction) | `C_Reputation.GetFactionDataByIndex`, `C_Reputation.GetFactionStandings`, `C_Reputation.GetLastStandingChange`, `C_Reputation.GetWatchedFactionData`, `C_Reputation.SetWatchedFactionByID`, `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`, `frame:SetShown`, `fontstring:GetStringHeight`, `fontstring:GetUnboundedStringWidth`, `fontstring:GetWrappedWidth`, `fontstring:GetNumLines`, `fontstring:GetLineHeight`, `fontstring:IsTruncated`, `fontstring:SetMaxLines`, `fontstring:GetMaxLines`, `fontstring:SetFormattedText`, `texture:SetRotation`, `texture:GetRotation`, `texture:SetVertexOffset`, `texture:GetVertexOffset`, `texture:SetColorTexture`, `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`, `frame:SetResizeBounds`, `frame:HookScript`, `frame:IsEventRegistered`, `frame:GetEffectiveAlpha`, `frame:SetAttribute`, `frame:SetAttributeNoHandler`, `frame:ClearAttribute`, `frame:GetAttribute`, `OnAttributeChanged` (script), `SetModernScriptArgs`, `GetModernScriptArgs`, `PreClick` (script), `PostClick` (script), `GetClickFrame` | +| [Frame](docs/API.md#frame) | `region:SetPoint("point")` (one-arg form), `region:SetSize`, `region:GetSize`, `region:IsMouseOver`, `region:GetRect`, `region:IsDragging`, `GetMouseFoci`, `frame:SetShown`, `fontstring:GetStringHeight`, `fontstring:GetUnboundedStringWidth`, `fontstring:GetWrappedWidth`, `fontstring:GetNumLines`, `fontstring:GetLineHeight`, `fontstring:IsTruncated`, `fontstring:SetMaxLines`, `fontstring:GetMaxLines`, `fontstring:SetFormattedText`, `texture:SetRotation`, `texture:GetRotation`, `texture:SetVertexOffset`, `texture:GetVertexOffset`, `texture:SetColorTexture`, `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`, `frame:SetResizeBounds`, `frame:HookScript`, `frame:IsEventRegistered`, `frame:GetEffectiveAlpha`, `frame:SetAttribute`, `frame:SetAttributeNoHandler`, `frame:ClearAttribute`, `frame:GetAttribute`, `OnAttributeChanged` (script), `SetModernScriptArgs`, `GetModernScriptArgs`, `PreClick` (script), `PostClick` (script), `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/docs/API.md b/docs/API.md index 315b677e..5db8bbf1 100644 --- a/docs/API.md +++ b/docs/API.md @@ -200,6 +200,8 @@ build instructions. - [`editBox:ClearHighlightText()`](#editboxclearhighlighttext) - [`editBox:HasFocus()`](#editboxhasfocus) - [`editBox:HasText()`](#editboxhastext) + - [`editBox:SetHighlightColor(r, g, b [, a])`](#editboxsethighlightcolorr-g-b--a) + - [`editBox:GetHighlightColor()`](#editboxgethighlightcolor) - [`frame:SetResizeBounds(minWidth, minHeight [, maxWidth, maxHeight])`](#framesetresizeboundsminwidth-minheight--maxwidth-maxheight) - [`frame:HookScript(scriptType, handler)`](#framehookscriptscripttype-handler) - [`frame:IsEventRegistered(event)`](#frameiseventregisteredevent) @@ -4682,6 +4684,21 @@ and `ClearFocus` but no way to read the focus state. Returns `true` if the edit box holds any text. +### `editBox:SetHighlightColor(r, g, b [, a])` + +Sets the color of the text-selection highlight. Each value is `0` to `1`. +Alpha is optional and defaults to `1`. The new color shows the next time the +box paints a selection. + +```lua +ChatFrameEditBox:SetHighlightColor(1, 0, 0) -- red selection +``` + +### `editBox:GetHighlightColor()` + +Returns the selection-highlight color as four numbers: `r, g, b, a`, each `0` +to `1`. + ### `frame:SetResizeBounds(minWidth, minHeight [, maxWidth, maxHeight])` The modern rename of vanilla's `SetMinResize` / `SetMaxResize` pair. diff --git a/src/Offsets.h b/src/Offsets.h index 13e23233..e615e013 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -299,7 +299,12 @@ enum Offsets { // the pointer at +0xB8 (both verified from the setters below). We read a // line's text/color and re-apply them one slot down, then set line 0. FUN_FONTSTRING_SET_TEXT = 0x00771D80, // __thiscall(fs, text, flag=0) - FUN_FONTSTRING_SET_COLOR = 0x0077F750, // __thiscall(fs, colorBGRA*) + FUN_FONTSTRING_SET_COLOR = 0x0077F750, // __thiscall(region, colorBGRA*) — generic region color setter (fontstrings + textures) + // Generic region color GETTER — `void __thiscall(region, uint32* outBGRA)`, + // writes the packed {b,g,r,a} (0xFFFFFFFF when the region has no explicit + // color). The read half of Texture:GetVertexColor (FUN_0079aa50); paired + // with FUN_FONTSTRING_SET_COLOR above. Backs EditBox:GetHighlightColor. + FUN_REGION_GET_COLOR = 0x0077F8C0, OFF_FONTSTRING_TEXT = 0xF0, // char* — current text buffer OFF_FONTSTRING_COLOR_PTR = 0xB8, // ptr → 4-byte {b,g,r,a} color storage // A line's FontString is only positioned once shown: set the desired @@ -7609,6 +7614,13 @@ enum Offsets { // backs GetUTF8CursorPosition. Verified from the arrow-key mover // FUN_0077cb20's own `FUN_0077bc80(this, byteOff, cursorByte - byteOff)`. FUN_EDITBOX_COUNT_CHARS = 0x0077bc80, + // Selection-highlight regions — three consecutive CSimpleTexture pointers + // (start-line +0x350, middle-block +0x354, end-line +0x358) the editbox + // shows/positions to paint the selection (FUN_0077d950). Their vertex + // color IS the highlight color — Set/GetHighlightColor read/write it via + // the generic region color accessors. The blinking caret is a SEPARATE + // region at +0x3d4, deliberately left alone. + OFF_EDITBOX_HIGHLIGHT_REGION = 0x350, // --- FontString measure internals (GetStringWidth icon fix + GetStringHeight) --- // CSimpleFontString::GetStringWidthInternal — `float(__fastcall)(fs /*ecx*/)`, diff --git a/src/editbox/Methods.cpp b/src/editbox/Methods.cpp index 0983a7f1..56876ec6 100644 --- a/src/editbox/Methods.cpp +++ b/src/editbox/Methods.cpp @@ -21,6 +21,7 @@ // ClearHighlightText — drop the selection, keep the caret // HasFocus — does this box own keyboard focus // HasText — is the box non-empty +// Set/GetHighlightColor — the text-selection highlight color // // The engine already stores and manipulates all of this; we just expose it. // Every method is registered on the EditBox method registry, so — like any @@ -45,6 +46,20 @@ namespace { using SetCursorByte_t = void(__thiscall *)(void *editbox, int byteOffset); using CollapseSelection_t = void(__thiscall *)(void *editbox); using CountChars_t = int(__thiscall *)(void *editbox, int startByte, int byteCount); +using RegionSetColor_t = void(__thiscall *)(void *region, const uint32_t *colorBGRA); +using RegionGetColor_t = void(__thiscall *)(void *region, uint32_t *outBGRA); + +// A Lua color arg (0..1) clamped and scaled to a 0..255 byte, matching the +// engine's own float->byte color conversion (truncating, like Texture's +// SetVertexColor). `dflt` is used when the arg is absent. +uint32_t ColorByte(void *L, int idx, double dflt) { + double v = Game::Lua::IsNumber(L, idx) ? Game::Lua::ToNumber(L, idx) : dflt; + if (v < 0.0) + v = 0.0; + else if (v > 1.0) + v = 1.0; + return static_cast(v * 255.0); +} int __fastcall Script_SetCursorPosition(void *L) { void *eb = Game::Lua::ResolveObject(L, 1); @@ -133,6 +148,55 @@ int __fastcall Script_HasText(void *L) { return 1; } +// SetHighlightColor(r, g, b [, a]) — the color of the text-selection highlight. +// The engine paints the selection with three Texture regions (start-line, +// middle-block, end-line); their vertex color is the highlight color, so set +// it on all three via the engine's own region color setter. Colors are 0..1; +// alpha defaults to 1. Takes effect on the next selection paint. +int __fastcall Script_SetHighlightColor(void *L) { + void *eb = Game::Lua::ResolveObject(L, 1); + if (eb == nullptr || !Game::Lua::IsNumber(L, 2) || + !Game::Lua::IsNumber(L, 3) || !Game::Lua::IsNumber(L, 4)) { + Game::Lua::Error(L, "Usage: EditBox:SetHighlightColor(r, g, b [, a])"); + return 0; + } + const uint32_t r = ColorByte(L, 2, 1.0); + const uint32_t g = ColorByte(L, 3, 1.0); + const uint32_t b = ColorByte(L, 4, 1.0); + const uint32_t a = ColorByte(L, 5, 1.0); + const uint32_t packed = b | (g << 8) | (r << 16) | (a << 24); // {b,g,r,a} + auto setColor = reinterpret_cast( + Offsets::FUN_FONTSTRING_SET_COLOR); + for (int off = Offsets::OFF_EDITBOX_HIGHLIGHT_REGION; + off <= Offsets::OFF_EDITBOX_HIGHLIGHT_REGION + 8; off += 4) { + void *region = Game::Read(eb, off); + if (region != nullptr) + setColor(region, &packed); + } + return 0; +} + +// GetHighlightColor() -> r, g, b, a (each 0..1). Reads the color off the first +// highlight region; a region with no explicit color reads back white (opaque). +int __fastcall Script_GetHighlightColor(void *L) { + void *eb = Game::Lua::ResolveObject(L, 1); + if (eb == nullptr) { + Game::Lua::Error(L, "Usage: EditBox:GetHighlightColor()"); + return 0; + } + uint32_t packed = 0xFFFFFFFF; + void *region = Game::Read(eb, Offsets::OFF_EDITBOX_HIGHLIGHT_REGION); + if (region != nullptr) + reinterpret_cast(Offsets::FUN_REGION_GET_COLOR)( + region, &packed); + const double inv = 1.0 / 255.0; + Game::Lua::PushNumber(L, ((packed >> 16) & 0xff) * inv); // r + Game::Lua::PushNumber(L, ((packed >> 8) & 0xff) * inv); // g + Game::Lua::PushNumber(L, (packed & 0xff) * inv); // b + Game::Lua::PushNumber(L, ((packed >> 24) & 0xff) * inv); // a + return 4; +} + const Game::Lua::FrameMethodEntry g_methods[] = { {"SetCursorPosition", &Script_SetCursorPosition}, {"GetCursorPosition", &Script_GetCursorPosition}, @@ -140,6 +204,8 @@ const Game::Lua::FrameMethodEntry g_methods[] = { {"ClearHighlightText", &Script_ClearHighlightText}, {"HasFocus", &Script_HasFocus}, {"HasText", &Script_HasText}, + {"SetHighlightColor", &Script_SetHighlightColor}, + {"GetHighlightColor", &Script_GetHighlightColor}, }; void RegisterLuaFunctions() {