1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Implement _tojson meta-method for base types.

This commit is contained in:
Sandu Liviu Catalin 2022-09-18 15:02:39 +03:00
parent 5f20ffc4de
commit a788e059a5
21 changed files with 205 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -300,6 +301,21 @@ String AABB::ToString() const
return fmt::format("{},{},{},{},{},{}", min.x, min.y, min.z, max.x, max.y, max.z); return fmt::format("{},{},{},{},{},{}", min.x, min.y, min.z, max.x, max.y, max.z);
} }
// ------------------------------------------------------------------------------------------------
void AABB::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{min:{{x:{},y:{},z:{}}},max:{{x:{},y:{},z:{}}},",
min.x, min.y, min.z, max.x, max.y, max.z);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{min:[{},{},{}],max:[{},{},{}]}},",
min.x, min.y, min.z, max.x, max.y, max.z);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void AABB::SetStr(SQChar delim, StackStrF & values) void AABB::SetStr(SQChar delim, StackStrF & values)
{ {
@ -793,6 +809,7 @@ void Register_AABB(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &AABB::ToString) .Func(_SC("_tostring"), &AABB::ToString)
.Func(_SC("_tojson"), &AABB::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)

View File

@ -304,6 +304,11 @@ struct AABB
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter. * Set the values extracted from the specified string using the specified delimiter.
*/ */

View File

@ -3,6 +3,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -360,6 +361,19 @@ String Circle::ToString() const
return fmt::format("{},{},{}", pos.x, pos.y, rad); return fmt::format("{},{},{}", pos.x, pos.y, rad);
} }
// ------------------------------------------------------------------------------------------------
void Circle::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},r:{}}},", pos.x, pos.y, rad);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", pos.x, pos.y, rad);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Circle::SetRadius(Value nr) void Circle::SetRadius(Value nr)
{ {
@ -552,6 +566,7 @@ void Register_Circle(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Circle::ToString) .Func(_SC("_tostring"), &Circle::ToString)
.Func(_SC("_toJSON"), &Circle::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)

View File

@ -341,6 +341,11 @@ struct Circle
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set the specified radius. * Set the specified radius.
*/ */

View File

@ -4,6 +4,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -481,6 +482,19 @@ String Color3::ToString() const
return fmt::format("{},{},{}", r, g, b); return fmt::format("{},{},{}", r, g, b);
} }
// ------------------------------------------------------------------------------------------------
void Color3::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{r:{},g:{},b:{}}},", r, g, b);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", r, g, b);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Color3::SetScalar(Value ns) void Color3::SetScalar(Value ns)
{ {
@ -741,6 +755,7 @@ void Register_Color3(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Color3::ToString) .Func(_SC("_tostring"), &Color3::ToString)
.Func(_SC("_tojson"), &Color3::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)

View File

@ -405,6 +405,11 @@ struct Color3
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */

View File

@ -4,6 +4,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -506,6 +507,19 @@ String Color4::ToString() const
return fmt::format("{},{},{},{}", r, g, b, a); return fmt::format("{},{},{},{}", r, g, b, a);
} }
// ------------------------------------------------------------------------------------------------
void Color4::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{r:{},g:{},b:{},a:{}}},", r, g, b, a);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", r, g, b, a);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Color4::SetScalar(Value ns) void Color4::SetScalar(Value ns)
{ {
@ -781,6 +795,7 @@ void Register_Color4(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Color4::ToString) .Func(_SC("_tostring"), &Color4::ToString)
.Func(_SC("_tojson"), &Color4::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)

View File

@ -405,6 +405,11 @@ struct Color4
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */

View File

@ -5,6 +5,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -349,6 +350,19 @@ String Quaternion::ToString() const
return fmt::format("{},{},{},{}", x, y, z, w); return fmt::format("{},{},{},{}", x, y, z, w);
} }
// ------------------------------------------------------------------------------------------------
void Quaternion::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},w:{}}},", x, y, z, w);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", x, y, z, w);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Quaternion::SetScalar(Value ns) void Quaternion::SetScalar(Value ns)
{ {
@ -775,6 +789,7 @@ void Register_Quaternion(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Quaternion::ToString) .Func(_SC("_tostring"), &Quaternion::ToString)
.Func(_SC("_tojson"), &Quaternion::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)

View File

@ -296,6 +296,11 @@ struct Quaternion
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */

View File

@ -6,6 +6,9 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------
struct CtxJSON;
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Helper constants used by the bas types. * Helper constants used by the bas types.
*/ */

View File

@ -3,6 +3,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -360,6 +361,19 @@ String Sphere::ToString() const
return fmt::format("{},{},{},{}", pos.x, pos.y, pos.z, rad); return fmt::format("{},{},{},{}", pos.x, pos.y, pos.z, rad);
} }
// ------------------------------------------------------------------------------------------------
void Sphere::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},r:{}}},", pos.x, pos.y, pos.z, rad);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", pos.x, pos.y, pos.z, rad);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Sphere::SetRadius(Value nr) void Sphere::SetRadius(Value nr)
{ {
@ -527,6 +541,7 @@ void Register_Sphere(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Sphere::ToString) .Func(_SC("_tostring"), &Sphere::ToString)
.Func(_SC("_tojson"), &Sphere::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)

View File

@ -341,6 +341,11 @@ struct Sphere
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set the specified radius. * Set the specified radius.
*/ */

View File

@ -4,6 +4,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -295,6 +296,19 @@ String Vector2::ToString() const
return fmt::format("{},{}", x, y); return fmt::format("{},{}", x, y);
} }
// ------------------------------------------------------------------------------------------------
void Vector2::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{}}},", x, y);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{}],", x, y);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Vector2::SetScalar(Value ns) void Vector2::SetScalar(Value ns)
{ {
@ -428,6 +442,7 @@ void Register_Vector2(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector2::ToString) .Func(_SC("_tostring"), &Vector2::ToString)
.Func(_SC("_tojson"), &Vector2::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)

View File

@ -285,6 +285,11 @@ struct Vector2
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */

View File

@ -4,6 +4,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -441,6 +442,19 @@ String Vector2i::ToString() const
return fmt::format("{},{}", x, y); return fmt::format("{},{}", x, y);
} }
// ------------------------------------------------------------------------------------------------
void Vector2i::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{}}},", x, y);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{}],", x, y);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Vector2i::SetScalar(Value ns) void Vector2i::SetScalar(Value ns)
{ {
@ -574,6 +588,7 @@ void Register_Vector2i(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector2i::ToString) .Func(_SC("_tostring"), &Vector2i::ToString)
.Func(_SC("_tojson"), &Vector2i::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)

View File

@ -390,6 +390,11 @@ struct Vector2i
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */

View File

@ -5,6 +5,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -331,6 +332,19 @@ String Vector3::ToString() const
return fmt::format("{},{},{}", x, y, z); return fmt::format("{},{},{}", x, y, z);
} }
// ------------------------------------------------------------------------------------------------
void Vector3::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{}}},", x, y, z);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", x, y, z);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Vector3::SetScalar(Value ns) void Vector3::SetScalar(Value ns)
{ {
@ -696,6 +710,7 @@ void Register_Vector3(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector3::ToString) .Func(_SC("_tostring"), &Vector3::ToString)
.Func(_SC("_tojson"), &Vector3::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)

View File

@ -297,6 +297,11 @@ struct Vector3
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */

View File

@ -5,6 +5,7 @@
#include "Base/DynArg.hpp" #include "Base/DynArg.hpp"
#include "Core/Buffer.hpp" #include "Core/Buffer.hpp"
#include "Core/Utility.hpp" #include "Core/Utility.hpp"
#include "Library/JSON.hpp"
#include "Library/Numeric/Random.hpp" #include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -344,6 +345,19 @@ String Vector4::ToString() const
return fmt::format("{},{},{},{}", x, y, z, w); return fmt::format("{},{},{},{}", x, y, z, w);
} }
// ------------------------------------------------------------------------------------------------
void Vector4::ToJSON(CtxJSON & ctx) const
{
if (ctx.mObjectOverArray)
{
fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},w:{}}},", x, y, z, w);
}
else
{
fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", x, y, z, w);
}
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Vector4::SetScalar(Value ns) void Vector4::SetScalar(Value ns)
{ {
@ -526,6 +540,7 @@ void Register_Vector4(HSQUIRRELVM vm)
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn) .SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector4::ToString) .Func(_SC("_tostring"), &Vector4::ToString)
.Func(_SC("_tojson"), &Vector4::ToJSON)
// Meta-methods // Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >) .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)

View File

@ -295,6 +295,11 @@ struct Vector4
*/ */
SQMOD_NODISCARD String ToString() const; SQMOD_NODISCARD String ToString() const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a JSON string.
*/
void ToJSON(CtxJSON & ctx) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value. * Set all components to the specified scalar value.
*/ */