Skip to content

Commit 625e86b

Browse files
Pirulaxqaisjp
andauthored
Refactor newly added drawing functions to use the new parser (#1481)
Co-authored-by: Qais Patankar <[email protected]>
1 parent 16441fa commit 625e86b

File tree

4 files changed

+68
-92
lines changed

4 files changed

+68
-92
lines changed

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,14 @@ class CStaticFunctionDefinitions
401401
static bool SetCursorAlpha(float fAlpha);
402402

403403
// Drawing funcs
404-
static ID3DXFont* ResolveD3DXFont(eFontType fontType, CClientDxFont* pDxFontElement);
404+
static ID3DXFont* ResolveD3DXFont(eFontType fontType, CClientDxFont* pDxFontElement);
405+
static inline ID3DXFont* ResolveD3DXFont(const std::variant<CClientDxFont*, eFontType>& variant)
406+
{
407+
if (std::holds_alternative<eFontType>(variant))
408+
return g_pCore->GetGraphics()->GetFont(std::get<eFontType>(variant));
409+
410+
return std::get<CClientDxFont*>(variant)->GetD3DXFont();
411+
};
405412

406413
// GUI funcs
407414
static bool GUIGetInputEnabled();

Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.cpp

Lines changed: 20 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13-
#include <lua/CLuaFunctionParser.h>
13+
#include "CLuaDefs.h"
14+
#include "lua/CLuaFunctionParser.h"
15+
1416
#define MIN_CLIENT_REQ_DXSETRENDERTARGET_CALL_RESTRICTIONS "1.3.0-9.04431"
1517
extern bool g_bAllowAspectRatioAdjustment;
1618

@@ -31,8 +33,8 @@ void CLuaDrawingDefs::LoadFunctions()
3133
{"dxDrawMaterialPrimitive", DxDrawMaterialPrimitive},
3234
{"dxDrawMaterialPrimitive3D", DxDrawMaterialPrimitive3D},
3335
{"dxDrawWiredSphere", ArgumentParser<DxDrawWiredSphere>},
34-
{"dxGetTextWidth", DxGetTextWidth},
35-
{"dxGetTextSize", DxGetTextSize},
36+
{"dxGetTextWidth", ArgumentParser<DxGetTextWidth>},
37+
{"dxGetTextSize", ArgumentParser<DxGetTextSize>},
3638
{"dxGetFontHeight", DxGetFontHeight},
3739
{"dxCreateFont", DxCreateFont},
3840
{"dxCreateTexture", DxCreateTexture},
@@ -107,7 +109,7 @@ void CLuaDrawingDefs::AddDxFontClass(lua_State* luaVM)
107109

108110
lua_classfunction(luaVM, "getHeight", OOP_DxGetFontHeight);
109111
lua_classfunction(luaVM, "getTextWidth", OOP_DxGetTextWidth);
110-
lua_classfunction(luaVM, "getTextSize", OOP_DxGetTextSize);
112+
lua_classfunction(luaVM, "getTextSize", ArgumentParser<OOP_DxGetTextSize>);
111113

112114
// lua_classvariable ( luaVM, "height", NULL, "dxGetFontHeight"); // swap arguments, .height[scale] = int(height);
113115

@@ -855,98 +857,27 @@ int CLuaDrawingDefs::OOP_DxGetTextWidth(lua_State* luaVM)
855857
return 1;
856858
}
857859

858-
int CLuaDrawingDefs::DxGetTextSize(lua_State* luaVM)
860+
CVector2D CLuaDrawingDefs::OOP_DxGetTextSize(std::variant<CClientDxFont*, eFontType> variantFont, const std::string text, const std::optional<float> optWidth,
861+
const std::optional<float> optScaleXY, const std::optional<float> optScaleY,
862+
const std::optional<bool> optWordBreak, const std::optional<bool> optColorCoded)
859863
{
860-
// float, float dxGetTextSize ( string text, [float width=0, float scaleXY=1.0, float=scaleY=1.0, mixed font="default",
861-
// bool wordBreak=false, bool colorCoded=false] )
862-
SString strText;
863-
float fWidth;
864-
float fScaleX;
865-
float fScaleY;
866-
eFontType fontType;
867-
CClientDxFont* pDxFontElement;
868-
bool bWordBreak;
869-
bool bColorCoded;
864+
// float dxGetTextHeight ( string text, [float width, float scaleXY=1.0, float=scaleY=1.0, mixed font="default", bool wordBreak=false, bool
865+
// colorCoded=false] )
866+
CGraphicsInterface* const graphics = g_pCore->GetGraphics();
870867

871-
CScriptArgReader argStream(luaVM);
872-
argStream.ReadString(strText);
873-
argStream.ReadNumber(fWidth, 0);
874-
if (argStream.NextIsUserDataOfType<CLuaVector2D>())
875-
{
876-
CVector2D vecScale;
877-
argStream.ReadVector2D(vecScale);
878-
fScaleX = vecScale.fX;
879-
fScaleY = vecScale.fY;
880-
}
881-
else
868+
// resolve scale (use X as Y value, if optScaleY is empty)
869+
CVector2D scale(1.0f, 1.0f);
870+
if (optScaleXY.has_value())
882871
{
883-
argStream.ReadNumber(fScaleX, 1);
884-
if (argStream.NextIsNumber())
885-
argStream.ReadNumber(fScaleY);
886-
else
887-
fScaleY = fScaleX;
872+
scale.fX = optScaleXY.value();
873+
scale.fY = optScaleY.has_value() ? optScaleY.value() : scale.fX;
888874
}
889-
MixedReadDxFontString(argStream, fontType, FONT_DEFAULT, pDxFontElement);
890-
argStream.ReadBool(bWordBreak, false);
891-
argStream.ReadBool(bColorCoded, false);
892-
893-
if (argStream.HasErrors())
894-
return luaL_error(luaVM, argStream.GetFullErrorMessage());
895-
896-
// Get DX font
897-
ID3DXFont* pD3DXFont = CStaticFunctionDefinitions::ResolveD3DXFont(fontType, pDxFontElement);
898875

899876
CVector2D vecSize;
900-
g_pCore->GetGraphics()->GetDXTextSize(vecSize, strText.c_str(), fWidth, fScaleX, fScaleY, pD3DXFont, bWordBreak, bColorCoded);
901-
lua_pushnumber(luaVM, vecSize.fX);
902-
lua_pushnumber(luaVM, vecSize.fY);
903-
return 2;
904-
}
905-
906-
int CLuaDrawingDefs::OOP_DxGetTextSize(lua_State* luaVM)
907-
{
908-
// vector2 DxFont:getTextSize ( string text, [float width=0, float scaleXY=1.0, float=scaleY=1.0,
909-
// bool wordBreak=false, bool colorCoded=false] )
910-
SString strText;
911-
float fWidth;
912-
float fScaleX;
913-
float fScaleY;
914-
CClientDxFont* pDxFontElement;
915-
bool bWordBreak;
916-
bool bColorCoded;
917-
918-
CScriptArgReader argStream(luaVM);
919-
argStream.ReadUserData(pDxFontElement);
920-
argStream.ReadString(strText);
921-
argStream.ReadNumber(fWidth, 0);
922-
if (argStream.NextIsUserDataOfType<CLuaVector2D>())
923-
{
924-
CVector2D vecScale;
925-
argStream.ReadVector2D(vecScale);
926-
fScaleX = vecScale.fX;
927-
fScaleY = vecScale.fY;
928-
}
929-
else
930-
{
931-
argStream.ReadNumber(fScaleX, 1);
932-
if (argStream.NextIsNumber())
933-
argStream.ReadNumber(fScaleY);
934-
else
935-
fScaleY = fScaleX;
936-
}
937-
argStream.ReadBool(bWordBreak, false);
938-
argStream.ReadBool(bColorCoded, false);
877+
graphics->GetDXTextSize(vecSize, text.c_str(), optWidth.value_or(0.0f), scale.fX, scale.fY, CStaticFunctionDefinitions::ResolveD3DXFont(variantFont),
878+
optWordBreak.value_or(false), optColorCoded.value_or(false));
939879

940-
if (argStream.HasErrors())
941-
return luaL_error(luaVM, argStream.GetFullErrorMessage());
942-
943-
// Get DX font
944-
ID3DXFont* pD3DXFont = CStaticFunctionDefinitions::ResolveD3DXFont(FONT_DEFAULT, pDxFontElement);
945-
946-
CVector2D vecSize;
947-
g_pCore->GetGraphics()->GetDXTextSize(vecSize, strText.c_str(), fWidth, fScaleX, fScaleY, pD3DXFont, bWordBreak, bColorCoded);
948-
lua_pushvector(luaVM, vecSize);
949-
return 1;
880+
return vecSize;
950881
}
951882

952883
int CLuaDrawingDefs::DxGetFontHeight(lua_State* luaVM)

Client/mods/deathmatch/logic/luadefs/CLuaDrawingDefs.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
*****************************************************************************/
1111

1212
#pragma once
13-
#include "CLuaDefs.h"
13+
#include <optional>
14+
#include <variant>
15+
#include <utility>
1416
#include <optional>
1517

1618
class CLuaDrawingDefs : public CLuaDefs
@@ -32,7 +34,22 @@ class CLuaDrawingDefs : public CLuaDefs
3234
LUA_DECLARE(DxDrawMaterialPrimitive);
3335
LUA_DECLARE(DxDrawPrimitive3D);
3436
LUA_DECLARE(DxDrawMaterialPrimitive3D);
35-
LUA_DECLARE_OOP(DxGetTextSize);
37+
38+
static CVector2D OOP_DxGetTextSize(
39+
// font can be called with a std::nullopt to grab the FONT_DEFAULT, see DxGetTextSize
40+
std::variant<CClientDxFont*, eFontType> font, const std::string text, const std::optional<float> optWidth, const std::optional<float> optScaleXY,
41+
const std::optional<float> optScaleY, const std::optional<bool> optWordBreak, const std::optional<bool> optColorCoded);
42+
43+
static inline std::tuple<float, float> DxGetTextSize(std::string text, std::optional<float> optWidth, std::optional<float> optScaleXY,
44+
std::optional<float> optScaleY, std::optional<std::variant<CClientDxFont*, eFontType>> optFont,
45+
std::optional<bool> optWordBreak, std::optional<bool> optColorCoded)
46+
{
47+
const auto size = OOP_DxGetTextSize(std::move(optFont.value_or(FONT_DEFAULT)), std::move(text), std::move(optWidth), std::move(optScaleXY),
48+
std::move(optScaleY), std::move(optWordBreak), std::move(optColorCoded));
49+
50+
return {size.fX, size.fY};
51+
};
52+
3653
LUA_DECLARE_OOP(DxGetTextWidth);
3754
LUA_DECLARE_OOP(DxGetFontHeight);
3855
LUA_DECLARE(DxCreateFont);

Shared/mods/deathmatch/logic/lua/LuaBasic.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
T PopPrimitive(L, std::size_t stackIndex)
1717
*/
1818

19+
class CVector2D;
20+
class CVector;
21+
class CVector4D;
1922

2023
namespace lua
2124
{
@@ -92,6 +95,24 @@ namespace lua
9295
return Push(L, nullptr);
9396
}
9497

98+
inline int Push(lua_State* L, const CVector2D& value)
99+
{
100+
lua_pushvector(L, value);
101+
return 1;
102+
}
103+
104+
inline int Push(lua_State* L, const CVector& value)
105+
{
106+
lua_pushvector(L, value);
107+
return 1;
108+
}
109+
110+
inline int Push(lua_State* L, const CVector4D& value)
111+
{
112+
lua_pushvector(L, value);
113+
return 1;
114+
}
115+
95116
template <typename T>
96117
int Push(lua_State* L, const std::vector<T>&& val)
97118
{

0 commit comments

Comments
 (0)