diff --git a/src/gameui/CGTooltip.cpp b/src/gameui/CGTooltip.cpp index f298f8d..ec5ef3a 100644 --- a/src/gameui/CGTooltip.cpp +++ b/src/gameui/CGTooltip.cpp @@ -34,6 +34,32 @@ CGTooltip::CGTooltip(CSimpleFrame* parent) : CSimpleFrame(parent) { } +void CGTooltip::ClearTooltip() { +} + +void CGTooltip::ResetPosition(int32_t a1) { +} + +void CGTooltip::SetOwner(CSimpleFrame* owner, TOOLTIP_ANCHORPOINT anchorpoint, float xoffset, float yoffset) { + this->ClearTooltip(); + this->SetFrameAlpha(255); + // TODO: this->unk77 = 0; + + if (this->m_owner != owner + || this->m_anchorPoint != anchorpoint + || this->m_offsetX != xoffset + || this->m_offsetY != yoffset) { + this->m_offsetX = xoffset; + this->m_offsetY = yoffset; + this->m_owner = owner; + this->m_anchorPoint = owner ? anchorpoint : ANCHOR_NONE; + this->ResetPosition(1); + } +} + +void CGTooltip::AddFontStrings(CSimpleFontString* leftstring, CSimpleFontString* rightstring) { +} + bool CGTooltip::IsA(int32_t type) { return type == CGTooltip::s_objectType || type == CSimpleFrame::s_objectType diff --git a/src/gameui/CGTooltip.hpp b/src/gameui/CGTooltip.hpp index dd64fca..baa38ac 100644 --- a/src/gameui/CGTooltip.hpp +++ b/src/gameui/CGTooltip.hpp @@ -4,6 +4,23 @@ #include "ui/CSimpleFrame.hpp" #include "ui/CSimpleTop.hpp" +class CSimpleFontString; + +enum TOOLTIP_ANCHORPOINT { + ANCHOR_LEFT = 0, + ANCHOR_RIGHT, + ANCHOR_BOTTOMLEFT, + ANCHOR_BOTTOM, + ANCHOR_BOTTOMRIGHT, + ANCHOR_TOPLEFT, + ANCHOR_TOP, + ANCHOR_TOPRIGHT, + ANCHOR_CURSOR, + ANCHOR_NONE, + ANCHOR_PRESERVE, + ANCHOR_CURSOR_RIGHT, +}; + class CGTooltip : public CSimpleFrame { public: // Static variables @@ -18,6 +35,10 @@ class CGTooltip : public CSimpleFrame { // Member functions CGTooltip(CSimpleFrame* parent); + void ClearTooltip(); + void ResetPosition(int32_t a1); + void SetOwner(CSimpleFrame* owner, TOOLTIP_ANCHORPOINT anchorpoint, float xoffset, float yoffset); + void AddFontStrings(CSimpleFontString* leftstring, CSimpleFontString* rightstring); // Virtual member functions virtual bool IsA(int32_t type); @@ -25,6 +46,13 @@ class CGTooltip : public CSimpleFrame { virtual ScriptIx* GetScriptByName(const char* name, ScriptData& data); // Member variables + CSimpleFrame* m_owner = nullptr; + TOOLTIP_ANCHORPOINT m_anchorPoint = ANCHOR_NONE; + float m_padding = 0.0f; + float m_minWidth = 0.0f; + uint32_t m_minWidthForced = 0; + float m_offsetX = 0.0f; + float m_offsetY = 0.0f; ScriptIx m_onTooltipSetDefaultAnchor; ScriptIx m_onTooltipCleared; ScriptIx m_onTooltipAddMoney; diff --git a/src/gameui/CGTooltipScript.cpp b/src/gameui/CGTooltipScript.cpp index a26a188..5353b56 100644 --- a/src/gameui/CGTooltipScript.cpp +++ b/src/gameui/CGTooltipScript.cpp @@ -1,26 +1,81 @@ #include "gameui/CGTooltipScript.hpp" #include "gameui/CGTooltip.hpp" +#include "ui/CSimpleFontString.hpp" +#include "gx/Coordinate.hpp" #include "util/Lua.hpp" #include "util/Unimplemented.hpp" +#include "util/StringTo.hpp" static int32_t Script_AddFontStrings(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CGTooltip::GetObjectType(); + auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); + + CSimpleFontString* arguments[2] = {}; + + for (int32_t i = 2; i < 4; ++i) { + if (lua_type(L, i) != LUA_TTABLE) { + return luaL_error(L, "Usage: %s:AddFontStrings(leftstring, rightstring)", tooltip->GetDisplayName()); + } + + lua_rawgeti(L, i, 0); + auto fontString = static_cast(lua_touserdata(L, -1)); + lua_settop(L, -2); + + if (!fontString) { + return luaL_error(L, "%s:AddFontStrings(): Couldn't find 'this' in fontstring", tooltip->GetDisplayName()); + } + + if (!fontString->IsA(CSimpleFontString::GetObjectType())) { + return luaL_error(L, "%s:AddFontStrings(): Wrong object type, expected fontstring", tooltip->GetDisplayName()); + } + + arguments[i - 2] = fontString; + } + + tooltip->AddFontStrings(arguments[0], arguments[1]); + return 0; } static int32_t Script_SetMinimumWidth(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CGTooltip::GetObjectType(); + auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); + + if (!lua_isnumber(L, 2)) { + return luaL_error(L, "Usage: %s:SetMinimumWidth(width [,force])", tooltip->GetDisplayName()); + } + + tooltip->m_minWidth = lua_tonumber(L, 2); + tooltip->m_minWidthForced = StringToBOOL(L, 3, 0); + return 0; } static int32_t Script_GetMinimumWidth(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CGTooltip::GetObjectType(); + auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); + lua_pushnumber(L, tooltip->m_minWidth); + if (tooltip->m_minWidthForced) { + lua_pushnumber(L, 1.0); + } else { + lua_pushnil(L); + } + return 2; } static int32_t Script_SetPadding(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CGTooltip::GetObjectType(); + auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); + + float value = lua_tonumber(L, 2); + value /= CoordinateGetAspectCompensation() * 1024.0f; + tooltip->m_padding = NDCToDDCWidth(value); + return 0; } static int32_t Script_GetPadding(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CGTooltip::GetObjectType(); + auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); + lua_pushnumber(L, tooltip->m_padding); + return 1; } static int32_t Script_IsOwned(lua_State* L) { @@ -32,7 +87,7 @@ static int32_t Script_IsOwned(lua_State* L) { } lua_rawgeti(L, 2, 0); - auto frame = static_cast(lua_touserdata(L, -1)); + auto frame = static_cast(lua_touserdata(L, -1)); lua_settop(L, -2); if (!frame) { @@ -43,7 +98,7 @@ static int32_t Script_IsOwned(lua_State* L) { return luaL_error(L, "%s:IsOwned(): Wrong object type, expected frame", tooltip->GetDisplayName()); } - if (tooltip->m_parent == frame) { + if (tooltip->m_owner == frame) { lua_pushnumber(L, 1.0); } else { lua_pushnil(L); @@ -55,13 +110,64 @@ static int32_t Script_GetOwner(lua_State* L) { auto type = CGTooltip::GetObjectType(); auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); - // TODO - lua_pushnil(L); + if (tooltip->m_owner) { + if (!tooltip->m_owner->lua_registered) { + tooltip->m_owner->RegisterScriptObject(nullptr); + } + lua_rawgeti(L, LUA_REGISTRYINDEX, tooltip->m_owner->lua_objectRef); + } else { + lua_pushnil(L); + } + return 1; } static int32_t Script_SetOwner(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + auto type = CGTooltip::GetObjectType(); + auto tooltip = static_cast(FrameScript_GetObjectThis(L, type)); + + tooltip->Hide(); + + if (lua_type(L, 2) != LUA_TTABLE) { + return luaL_error(L, "Usage: %s:SetOwner(frame)", tooltip->GetDisplayName()); + } + + lua_rawgeti(L, 2, 0); + auto frame = static_cast(lua_touserdata(L, -1)); + lua_settop(L, -2); + + if (!frame) { + return luaL_error(L, "%s:SetOwner(): Couldn't find 'this' in frame object", tooltip->GetDisplayName()); + } + + if (!frame->IsA(CSimpleFrame::GetObjectType())) { + return luaL_error(L, "%s:SetOwner(): Wrong object type, expected frame", tooltip->GetDisplayName()); + } + + if (frame == tooltip) { + return luaL_error(L, "%s:SetOwner(): Can't set owner to self", tooltip->GetDisplayName()); + } + + int32_t anchorPoint = 0; + if (lua_isstring(L, 3)) { + StringToAnchorPoint(lua_tolstring(L, 3, nullptr), anchorPoint); + } + + float xoffset = 0.0f; + if (lua_isnumber(L, 4)) { + float value = lua_tonumber(L, 4); + value /= CoordinateGetAspectCompensation() * 1024.0f; + xoffset = NDCToDDCWidth(value); + } + + float yoffset = 0.0f; + if (lua_isnumber(L, 5)) { + float value = lua_tonumber(L, 5); + value /= CoordinateGetAspectCompensation() * 1024.0f; + yoffset = NDCToDDCWidth(value); + } + + tooltip->SetOwner(frame, static_cast(anchorPoint), xoffset, yoffset); } static int32_t Script_GetAnchorType(lua_State* L) { diff --git a/src/util/StringTo.cpp b/src/util/StringTo.cpp index 49506e6..edcd744 100644 --- a/src/util/StringTo.cpp +++ b/src/util/StringTo.cpp @@ -1,6 +1,7 @@ #include "util/StringTo.hpp" #include "util/Lua.hpp" #include +#include uint64_t StringToClickAction(const char* actionStr) { if (!actionStr || !*actionStr) { @@ -170,3 +171,31 @@ bool StringToOrientation(const char* string, uint32_t& orientation) { } return false; } + +bool StringToAnchorPoint(const char* string, int32_t& point) { + static std::pair table[12] = { + { 0, "ANCHOR_LEFT" }, + { 1, "ANCHOR_RIGHT" }, + { 2, "ANCHOR_BOTTOMLEFT" }, + { 3, "ANCHOR_BOTTOM" }, + { 4, "ANCHOR_BOTTOMRIGHT" }, + { 5, "ANCHOR_TOPLEFT" }, + { 6, "ANCHOR_TOP" }, + { 7, "ANCHOR_TOPRIGHT" }, + { 8, "ANCHOR_CURSOR" }, + { 9, "ANCHOR_NONE" }, + { 10, "ANCHOR_PRESERVE" }, + { 11, "ANCHOR_CURSOR_RIGHT" }, + }; + + point = 0; + + for (size_t i = 0; i < 12; ++i) { + if (!SStrCmpI(string, table[i].second, STORM_MAX_STR)) { + point = table[i].first; + return true; + } + } + + return false; +} diff --git a/src/util/StringTo.hpp b/src/util/StringTo.hpp index 1fb435f..4b698ae 100644 --- a/src/util/StringTo.hpp +++ b/src/util/StringTo.hpp @@ -19,4 +19,6 @@ int32_t StringToJustify(const char*, uint32_t&); bool StringToOrientation(const char* string, uint32_t& orientation); +bool StringToAnchorPoint(const char* string, int32_t& point); + #endif