Files
ClassicAPI/src/frame/KeyboardPropagation.cpp
T
Brues 5b7fe94d4d feat(xml): alpha, scale, desaturated, indented and other XML attributes
Later clients read these attributes in their XML loaders; 1.12's do not.
Each is now read after the engine has loaded the node (Xml::FrameLoad for
frames, Xml::TypeLoad for the rest) and goes through the setter the
matching Lua method uses. Each applies only when present and non-empty,
so an element's own value overrides its template's.

Frames:
- scale: Frame:SetScale's path (0x00775140). A negative scale is refused,
  anything else goes to SetFrameScale (FUN_FRAME_SET_SCALE 0x0076AC10,
  __thiscall(frame, float, force), ret 8) without force. A Model keeps
  `scale` as its model scale, which its own loader reads.
- dontSavePosition, propagateKeyboardInput: the existing frame flags,
  through FUN_FRAME_SET_FRAME_FLAG.

Textures and font strings:
- alpha: parsed as the frame loader parses its own `alpha` (SStrToFloat,
  clamped to 0..1, truncated to a byte), then set as Texture:SetAlpha and
  FontString:SetAlpha set it (0x0079B580, 0x0079CB70): the region color
  from FUN_REGION_GET_COLOR with its alpha byte replaced, through
  FUN_FONTSTRING_SET_COLOR. It applies after the node's <Color>.
- desaturated (textures): Texture::Desaturation's full amount, the same
  as SetDesaturated. It applies whenever present; an empty value is false.
- indented (font strings): the style bit SetIndentedWordWrap sets.

Status bars:
- reverseFill, rotatesTexture, fillStyle: StatusBar::Fill's modes, read
  after the status bar loader has set the BarTexture. An unknown
  fillStyle name changes nothing.

Tested in game with an XML test addon: every attribute set its value, and
parentKey, wordwrap and countInvisibleLetters still load.
2026-11-01 16:45:11 -06:00

152 lines
6.6 KiB
C++

// 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 <https://www.gnu.org/licenses/>.
// `frame:SetPropagateKeyboardInput([propagate])` and
// `frame:GetPropagateKeyboardInput()`.
//
// The top hands a key down to the frames in its key lists, top stratum first,
// and stops at the first frame whose key-down slot answers nonzero; that frame
// then owns the key. A frame with an OnKeyDown (or OnKeyUp) script always
// answers 1, so a frame that only watches keys swallows every one of them,
// movement and bindings included. The same goes for OnChar and the character
// walk.
//
// 4.3.4 lets the frame pass the key on: SetPropagateKeyboardInput sets frame
// flag 0x200000, and the slots end with `return !(flags & 0x200000) ||
// IsProtected()` - the script still runs, and the slot answers "not taken",
// so the walk goes on to the frames below and the world frame's bindings. The
// frames that do it are the base CSimpleFrame's key-down, key-up and char
// slots and CSimpleEditBox's key-down and key-up (its char slot does not).
// Nothing is protected here, so the flag alone decides.
//
// 1.12's slots are the same shape, so each hook runs the engine's slot and
// clears its "taken" answer for a frame with the flag. Only key-down and char
// are hooked: the top's key-up handler (0x00765FD0) discards the slot's
// answer, so 4.3.4's key-up return has nothing to change here. As in 4.3.4,
// a frame that passes a key down on is not recorded as the key's owner, so
// its key up goes to the frame that took the key.
//
// XML sets the flag with the frame attribute `propagateKeyboardInput`.
#include "Game.h"
#include "Offsets.h"
#include "xml/FrameLoad.h"
#include <cstdint>
namespace Frame::KeyboardPropagation {
namespace {
using KeySlot_t = int(__fastcall *)(void *frame, void *edx, void *event);
using SetFrameFlag_t = void(__fastcall *)(void *frame, void *edx, uint32_t mask, int on);
KeySlot_t g_origFrameOnChar = nullptr;
KeySlot_t g_origFrameOnKeyDown = nullptr;
KeySlot_t g_origEditBoxOnKeyDown = nullptr;
bool Propagates(void *frame) {
return (Game::Read<uint32_t>(frame, Offsets::OFF_FRAME_FLAGS) & Offsets::FRAME_FLAG_PROPAGATE_KEYBOARD) != 0;
}
int __fastcall FrameOnChar_h(void *frame, void *edx, void *event) {
const int taken = g_origFrameOnChar(frame, edx, event);
return taken != 0 && Propagates(frame) ? 0 : taken;
}
int __fastcall FrameOnKeyDown_h(void *frame, void *edx, void *event) {
const int taken = g_origFrameOnKeyDown(frame, edx, event);
return taken != 0 && Propagates(frame) ? 0 : taken;
}
int __fastcall EditBoxOnKeyDown_h(void *frame, void *edx, void *event) {
const int taken = g_origEditBoxOnKeyDown(frame, edx, event);
return taken != 0 && Propagates(frame) ? 0 : taken;
}
const Game::HookAutoRegister _frameOnCharHook{Offsets::FUN_FRAME_ON_CHAR, reinterpret_cast<void *>(&FrameOnChar_h),
reinterpret_cast<void **>(&g_origFrameOnChar)};
const Game::HookAutoRegister _frameOnKeyDownHook{Offsets::FUN_FRAME_ON_KEY_DOWN,
reinterpret_cast<void *>(&FrameOnKeyDown_h),
reinterpret_cast<void **>(&g_origFrameOnKeyDown)};
const Game::HookAutoRegister _editBoxOnKeyDownHook{Offsets::FUN_EDITBOX_ON_KEY_DOWN,
reinterpret_cast<void *>(&EditBoxOnKeyDown_h),
reinterpret_cast<void **>(&g_origEditBoxOnKeyDown)};
// 4.3.4 reads the flag with StringToBOOL(L, 2, 0): no argument clears it.
int __fastcall Script_SetPropagateKeyboardInput(void *L) {
void *frame = Game::Lua::ResolveFrame(L);
if (frame == nullptr)
return 0;
const bool on = Game::Lua::ToFlag(L, 2);
reinterpret_cast<SetFrameFlag_t>(Offsets::FUN_FRAME_SET_FRAME_FLAG)(
frame, nullptr, Offsets::FRAME_FLAG_PROPAGATE_KEYBOARD, on);
return 0;
}
// 4.3.4's frame loader sets the flag only when the node has the attribute, so
// a template's value stays unless the element names its own.
void OnFrameNode(void *object, const void *node, void * /*status*/) {
const char *value = Xml::Attr(node, "propagateKeyboardInput");
if (value == nullptr || *value == '\0')
return;
reinterpret_cast<SetFrameFlag_t>(Offsets::FUN_FRAME_SET_FRAME_FLAG)(
object, nullptr, Offsets::FRAME_FLAG_PROPAGATE_KEYBOARD, Game::StringToFlag(value));
}
const Xml::FrameLoad::AutoSubscribe _xmlSub{&OnFrameNode};
int __fastcall Script_GetPropagateKeyboardInput(void *L) {
void *frame = Game::Lua::ResolveFrame(L);
if (frame == nullptr)
return 0;
Game::Lua::PushBool(L, Propagates(frame));
return 1;
}
const Game::Lua::FrameMethodEntry g_methods[] = {
{"SetPropagateKeyboardInput", &Script_SetPropagateKeyboardInput},
{"GetPropagateKeyboardInput", &Script_GetPropagateKeyboardInput},
};
const Game::Doc::Field kSetArgs[] = {
Game::Doc::Opt("propagate", "boolean", "false",
"When true, the frame's OnKeyDown and OnChar scripts run and the key goes on to the frames "
"below and to the key bindings."),
};
const Game::Doc::Field kGetRets[] = {
Game::Doc::Req("propagate", "boolean"),
};
const Game::Doc::Function kSetPropagateKeyboardInput{
"Sets whether the frame passes the keys it gets on to the frames below it.", kSetArgs, {}};
const Game::Doc::Function kGetPropagateKeyboardInput{
"Returns whether the frame passes the keys it gets on to the frames below it.", {}, kGetRets};
const Game::Doc::Method g_methodDocs[] = {
{"SetPropagateKeyboardInput", &kSetPropagateKeyboardInput},
{"GetPropagateKeyboardInput", &kGetPropagateKeyboardInput},
};
void RegisterLuaFunctions() {
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_FRAME_METHOD_REGISTRY), g_methods,
static_cast<int>(sizeof(g_methods) / sizeof(g_methods[0])), g_methodDocs,
static_cast<int>(sizeof(g_methodDocs) / sizeof(g_methodDocs[0])));
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace Frame::KeyboardPropagation