feat(ui): add CSimpleFontString_SetFont script method

This commit is contained in:
VDm 2025-08-16 01:10:03 +04:00
parent f3cfac1764
commit 976fe64b66
3 changed files with 57 additions and 4 deletions

View File

@ -6,7 +6,7 @@
#include "util/Unimplemented.hpp"
#include "util/StringTo.hpp"
#include "gx/Coordinate.hpp"
#include <cstdint>
int32_t CSimpleFontString_IsObjectType(lua_State* L) {
auto type = CSimpleFontString::GetObjectType();
@ -222,15 +222,49 @@ int32_t CSimpleFontString_GetFont(lua_State* L) {
}
int32_t CSimpleFontString_SetFont(lua_State* L) {
WHOA_UNIMPLEMENTED(0);
auto type = CSimpleFontString::GetObjectType();
auto string = static_cast<CSimpleFontString*>(FrameScript_GetObjectThis(L, type));
if (!lua_isstring(L, 2) || !lua_isnumber(L, 3)) {
return luaL_error(L, "Usage: %s:SetFont(\"font\", fontHeight [, flags])", string->GetDisplayName());
}
auto fontName = lua_tostring(L, 2);
auto fontHeight = lua_tonumber(L, 3);
fontHeight /= CoordinateGetAspectCompensation() * 1024.0;
fontHeight = NDCToDDCWidth(fontHeight);
if (fontHeight <= 0.00000011920929) {
return luaL_error(L, "ERROR: %s:SetFont(): invalid fontHeight: %f, height must be > 0", string->GetDisplayName(), fontHeight);
}
if (!*fontName) {
return 0;
}
uint32_t fontFlags = 0;
if (lua_isstring(L, 4)) {
fontFlags = StringToFontFlags(lua_tostring(L, 4));
}
if (string->SetFont(fontName, fontHeight, fontFlags, false)) {
// TODO: Set some object flag
lua_pushnumber(L, 1.0);
} else {
lua_pushnil(L);
}
return 1;
}
int32_t CSimpleFontString_GetText(lua_State* L) {
WHOA_UNIMPLEMENTED(0);
auto type = CSimpleFontString::GetObjectType();
auto string = static_cast<CSimpleFontString*>(FrameScript_GetObjectThis(L, type));
lua_pushstring(L, string->m_text);
return 1;
}
int32_t CSimpleFontString_GetFieldSize(lua_State* L) {
WHOA_UNIMPLEMENTED(0);
lua_pushnumber(L, 8191.0);
return 1;
}
int32_t CSimpleFontString_SetText(lua_State* L) {

View File

@ -199,3 +199,20 @@ bool StringToAnchorPoint(const char* string, int32_t& point) {
return false;
}
uint32_t StringToFontFlags(const char* string) {
static std::pair<uint32_t, const char*> table[3] = {
{ 1, "OUTLINE" },
{ 4, "THICKOUTLINE" },
{ 2, "MONOCHROME" }
};
uint32_t result = 0;
for (size_t i = 0; i < 3; ++i) {
if (!SStrCmpI(string, table[i].second, STORM_MAX_STR)) {
result |= table[i].first;
}
}
return result;
}

View File

@ -21,4 +21,6 @@ bool StringToOrientation(const char* string, uint32_t& orientation);
bool StringToAnchorPoint(const char* string, int32_t& point);
uint32_t StringToFontFlags(const char* string);
#endif