From 5f3bed1c583399f815f32d830575acef46e249c8 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Thu, 5 Jun 2025 00:54:14 +0200 Subject: [PATCH] refactor(enum): made SurfaceType an enum class and added support for enum class in autogen --- autogen/src/object/codegen.cpp | 2 +- core/src/enum/surface.h | 18 +++++++++--------- core/src/objects/part.cpp | 16 ++++++++-------- core/src/objects/part.h | 12 ++++++------ core/src/rendering/renderer.cpp | 26 +++++++++++++------------- editor/placedocument.cpp | 2 +- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/autogen/src/object/codegen.cpp b/autogen/src/object/codegen.cpp index 9ea7fc4..17c9529 100644 --- a/autogen/src/object/codegen.cpp +++ b/autogen/src/object/codegen.cpp @@ -146,7 +146,7 @@ static void writePropertyGetHandler(std::ofstream& out, ClassAnalysis state) { } else if (prop.cframeMember == CFrameMember_Rotation) { out << "\n return Variant(" << prop.fieldName << ".ToEulerAnglesXYZ());"; } else if (prop.backingFieldType == "EnumItem") { - out << "\n return Variant(EnumType::" << prop.backingFieldEnum << ".FromValueInternal(" << prop.fieldName << "));"; + out << "\n return Variant(EnumType::" << prop.backingFieldEnum << ".FromValueInternal((int)" << prop.fieldName << "));"; } else { out << "\n return Variant(" << castToVariant(prop.fieldName, prop.backingFieldType) << ");"; } diff --git a/core/src/enum/surface.h b/core/src/enum/surface.h index 505c6e4..3974765 100644 --- a/core/src/enum/surface.h +++ b/core/src/enum/surface.h @@ -12,15 +12,15 @@ enum DEF_ENUM NormalId { Front = 5 }; -enum DEF_ENUM SurfaceType { - SurfaceSmooth = 0, - SurfaceGlue = 1, - SurfaceWeld = 2, - SurfaceStuds = 3, - SurfaceInlets = 4, - SurfaceUniversal = 5, - SurfaceHinge = 6, - SurfaceMotor = 7, +enum class DEF_ENUM SurfaceType { + Smooth = 0, + Glue = 1, + Weld = 2, + Studs = 3, + Inlet = 4, + Universal = 5, + Hinge = 6, + Motor = 7, }; namespace EnumType { diff --git a/core/src/objects/part.cpp b/core/src/objects/part.cpp index 144568f..64a0196 100644 --- a/core/src/objects/part.cpp +++ b/core/src/objects/part.cpp @@ -120,7 +120,7 @@ SurfaceType Part::surfaceFromFace(NormalId face) { case Front: return frontSurface; case Back: return backSurface; } - return SurfaceSmooth; // Unreachable + return SurfaceType::Smooth; // Unreachable } float Part::GetSurfaceParamA(Vector3 face) { @@ -199,14 +199,14 @@ bool Part::checkSurfacesTouching(CFrame surfaceFrame, Vector3 size, Vector3 myFa } std::optional> makeJointFromSurfaces(SurfaceType a, SurfaceType b) { - if (a == SurfaceWeld || b == SurfaceWeld || a == SurfaceGlue || b == SurfaceGlue) return Weld::New(); - if ((a == SurfaceStuds && (b == SurfaceInlets || b == SurfaceUniversal)) - || (a == SurfaceInlets && (b == SurfaceStuds || b == SurfaceUniversal)) - || (a == SurfaceUniversal && (b == SurfaceStuds || b == SurfaceInlets || b == SurfaceUniversal))) + if (a == SurfaceType::Weld || b == SurfaceType::Weld || a == SurfaceType::Glue || b == SurfaceType::Glue) return Weld::New(); + if ((a == SurfaceType::Studs && (b == SurfaceType::Inlet || b == SurfaceType::Universal)) + || (a == SurfaceType::Inlet && (b == SurfaceType::Studs || b == SurfaceType::Universal)) + || (a == SurfaceType::Universal && (b == SurfaceType::Studs || b == SurfaceType::Inlet || b == SurfaceType::Universal))) return Snap::New(); - if (a == SurfaceHinge) + if (a == SurfaceType::Hinge) return Rotate::New(); - if (a == SurfaceMotor) + if (a == SurfaceType::Motor) return RotateV::New(); return std::nullopt; } @@ -251,7 +251,7 @@ void Part::MakeJoints() { SurfaceType otherSurface = surfaceFromFace(faceFromNormal(otherFace)); // If it is a hinge, only attach if actually touching the "hinge" - if ((mySurface == SurfaceHinge || mySurface == SurfaceMotor) && !checkSurfacesTouching(surfaceFrame, Vector3(0.4, 0.4, 0.4), myFace, otherFace, otherPart)) continue; + if ((mySurface == SurfaceType::Hinge || mySurface == SurfaceType::Motor) && !checkSurfacesTouching(surfaceFrame, Vector3(0.4, 0.4, 0.4), myFace, otherFace, otherPart)) continue; // Create contacts // Contact always occurs at the center of Part0's surface (even if that point does not overlap both surfaces) diff --git a/core/src/objects/part.h b/core/src/objects/part.h index 7237bfb..bf70a80 100644 --- a/core/src/objects/part.h +++ b/core/src/objects/part.h @@ -70,12 +70,12 @@ public: DEF_PROP bool locked = false; DEF_PROP_CATEGORY(SURFACE) - DEF_PROP SurfaceType topSurface = SurfaceType::SurfaceStuds; - DEF_PROP SurfaceType bottomSurface = SurfaceType::SurfaceInlets; - DEF_PROP SurfaceType leftSurface = SurfaceType::SurfaceSmooth; - DEF_PROP SurfaceType rightSurface = SurfaceType::SurfaceSmooth; - DEF_PROP SurfaceType frontSurface = SurfaceType::SurfaceSmooth; - DEF_PROP SurfaceType backSurface = SurfaceType::SurfaceSmooth; + DEF_PROP SurfaceType topSurface = SurfaceType::Studs; + DEF_PROP SurfaceType bottomSurface = SurfaceType::Inlet; + DEF_PROP SurfaceType leftSurface = SurfaceType::Smooth; + DEF_PROP SurfaceType rightSurface = SurfaceType::Smooth; + DEF_PROP SurfaceType frontSurface = SurfaceType::Smooth; + DEF_PROP SurfaceType backSurface = SurfaceType::Smooth; DEF_PROP_CATEGORY(SURFACE_INPUT) DEF_PROP float topParamA = -0.5; diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index 0b25c90..811fd97 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -154,12 +154,12 @@ void renderParts() { shader->set("texScale", part->size); shader->set("transparency", part->transparency); - shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", part->rightSurface); - shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", part->topSurface); - shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", part->backSurface); - shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", part->leftSurface); - shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", part->bottomSurface); - shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", part->frontSurface); + shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", (int)part->rightSurface); + shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", (int)part->topSurface); + shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", (int)part->backSurface); + shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", (int)part->leftSurface); + shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", (int)part->bottomSurface); + shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", (int)part->frontSurface); CUBE_MESH->bind(); glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount); @@ -184,12 +184,12 @@ void renderParts() { shader->set("texScale", part->size); shader->set("transparency", part->transparency); - shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", part->rightSurface); - shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", part->topSurface); - shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", part->backSurface); - shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", part->leftSurface); - shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", part->bottomSurface); - shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", part->frontSurface); + shader->set("surfaces[" + std::to_string(NormalId::Right) + "]", (int)part->rightSurface); + shader->set("surfaces[" + std::to_string(NormalId::Top) + "]", (int)part->topSurface); + shader->set("surfaces[" + std::to_string(NormalId::Back) + "]", (int)part->backSurface); + shader->set("surfaces[" + std::to_string(NormalId::Left) + "]", (int)part->leftSurface); + shader->set("surfaces[" + std::to_string(NormalId::Bottom) + "]", (int)part->bottomSurface); + shader->set("surfaces[" + std::to_string(NormalId::Front) + "]", (int)part->frontSurface); CUBE_MESH->bind(); glDrawArrays(GL_TRIANGLES, 0, CUBE_MESH->vertexCount); @@ -233,7 +233,7 @@ void renderSurfaceExtras() { for (int i = 0; i < 6; i++) { NormalId face = (NormalId)i; SurfaceType type = part->GetSurfaceFromFace(face); - if (type <= SurfaceType::SurfaceUniversal) continue; + if (type <= SurfaceType::Universal) continue; Vector3 surfaceCenter = part->cframe * (normalFromFace(face) * part->size / 2.f); diff --git a/editor/placedocument.cpp b/editor/placedocument.cpp index bebb18d..512dc2f 100644 --- a/editor/placedocument.cpp +++ b/editor/placedocument.cpp @@ -131,7 +131,7 @@ void PlaceDocument::init() { // part1->frontSurface = SurfaceWeld; // part0->backSurface = SurfaceHinge; - part0->backSurface = SurfaceMotor; + part0->backSurface = SurfaceType::Motor; // part1->frontSurface = SurfaceHinge; std::shared_ptr