From 3be8546bf4b3ada1694a7e21ca79c59941c7d0e6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 13 Apr 2025 14:18:50 +0300 Subject: [PATCH] refactor enums: CursorShape, InterpolationType, ParticleSpawnShape --- src/content/loading/GeneratorLoader.cpp | 9 ++---- src/graphics/core/commons.cpp | 39 ------------------------- src/graphics/core/commons.hpp | 16 ++++++++-- src/graphics/ui/gui_xml.cpp | 6 ++-- src/logic/scripting/lua/libs/libgui.cpp | 9 +++--- src/maths/Heightmap.cpp | 11 ------- src/maths/Heightmap.hpp | 7 ++++- src/presets/ParticlesPreset.cpp | 26 +++-------------- src/presets/ParticlesPreset.hpp | 12 +++++--- 9 files changed, 44 insertions(+), 91 deletions(-) delete mode 100644 src/graphics/core/commons.cpp diff --git a/src/content/loading/GeneratorLoader.cpp b/src/content/loading/GeneratorLoader.cpp index a845bc21..2f8f40d5 100644 --- a/src/content/loading/GeneratorLoader.cpp +++ b/src/content/loading/GeneratorLoader.cpp @@ -1,3 +1,4 @@ +#define VC_ENABLE_REFLECTION #include "../ContentLoader.hpp" #include @@ -210,13 +211,9 @@ void ContentLoader::loadGenerator( map.at("heights-bpd").get(def.heightsBPD); std::string interpName; map.at("heights-interpolation").get(interpName); - if (auto interp = InterpolationType_from(interpName)) { - def.heightsInterpolation = *interp; - } + InterpolationTypeMeta.getItem(interpName, def.heightsInterpolation); map.at("biomes-interpolation").get(interpName); - if (auto interp = InterpolationType_from(interpName)) { - def.biomesInterpolation = *interp; - } + InterpolationTypeMeta.getItem(interpName, def.biomesInterpolation); map.at("sea-level").get(def.seaLevel); map.at("wide-structs-chunks-radius").get(def.wideStructsChunksRadius); diff --git a/src/graphics/core/commons.cpp b/src/graphics/core/commons.cpp deleted file mode 100644 index 98cc5c13..00000000 --- a/src/graphics/core/commons.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "commons.hpp" - -#include - -std::optional CursorShape_from(std::string_view name) { - static std::map shapes = { - {"arrow", CursorShape::ARROW}, - {"text", CursorShape::TEXT}, - {"crosshair", CursorShape::CROSSHAIR}, - {"pointer", CursorShape::POINTER}, - {"ew-resize", CursorShape::EW_RESIZE}, - {"ns-resize", CursorShape::NS_RESIZE}, - {"nwse-resize", CursorShape::NWSE_RESIZE}, - {"nesw-resize", CursorShape::NESW_RESIZE}, - {"all-resize", CursorShape::ALL_RESIZE}, - {"not-allowed", CursorShape::NOT_ALLOWED} - }; - const auto& found = shapes.find(name); - if (found == shapes.end()) { - return std::nullopt; - } - return found->second; -} - -std::string to_string(CursorShape shape) { - static std::string names[] = { - "arrow", - "text", - "crosshair", - "pointer", - "ew-resize", - "ns-resize", - "nwse-resize", - "nesw-resize", - "all-resize", - "not-allowed" - }; - return names[static_cast(shape)]; -} diff --git a/src/graphics/core/commons.hpp b/src/graphics/core/commons.hpp index 80819206..7c26b75b 100644 --- a/src/graphics/core/commons.hpp +++ b/src/graphics/core/commons.hpp @@ -3,6 +3,8 @@ #include #include +#include "util/EnumMetadata.hpp" + enum class DrawPrimitive { point = 0, line, @@ -48,8 +50,18 @@ enum class CursorShape { LAST=NOT_ALLOWED }; -std::optional CursorShape_from(std::string_view name); -std::string to_string(CursorShape shape); +VC_ENUM_METADATA(CursorShape) + {"arrow", CursorShape::ARROW}, + {"text", CursorShape::TEXT}, + {"crosshair", CursorShape::CROSSHAIR}, + {"pointer", CursorShape::POINTER}, + {"ew-resize", CursorShape::EW_RESIZE}, + {"ns-resize", CursorShape::NS_RESIZE}, + {"nwse-resize", CursorShape::NWSE_RESIZE}, + {"nesw-resize", CursorShape::NESW_RESIZE}, + {"all-resize", CursorShape::ALL_RESIZE}, + {"not-allowed", CursorShape::NOT_ALLOWED}, +VC_ENUM_END class Flushable { public: diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 61719f29..31c7bd56 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -1,3 +1,4 @@ +#define VC_ENABLE_REFLECTION #include "gui_xml.hpp" #include @@ -168,8 +169,9 @@ static void read_uinode( node.setTooltipDelay(element.attr("tooltip-delay").asFloat()); } if (element.has("cursor")) { - if (auto cursor = CursorShape_from(element.attr("cursor").getText())) { - node.setCursor(*cursor); + CursorShape cursor; + if (CursorShapeMeta.getItem(element.attr("cursor").getText(), cursor)) { + node.setCursor(cursor); } } diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index 4a5f4ec9..2df7f322 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -1,3 +1,4 @@ +#define VC_ENABLE_REFLECTION #include "libgui.hpp" #include "assets/Assets.hpp" #include "engine/Engine.hpp" @@ -422,7 +423,7 @@ static int p_get_line_pos(UINode*, lua::State* L) { } static int p_get_cursor(UINode* node, lua::State* L) { - return lua::pushstring(L, to_string(node->getCursor())); + return lua::pushlstring(L, CursorShapeMeta.getName(node->getCursor())); } static int p_get_scroll(UINode* node, lua::State* L) { @@ -660,9 +661,9 @@ static void p_set_focused( } static void p_set_cursor(UINode* node, lua::State* L, int idx) { - if (auto cursor = CursorShape_from(lua::require_string(L, idx))) { - node->setCursor(*cursor); - } + auto cursor = CursorShape::ARROW; // reset to default + CursorShapeMeta.getItem(lua::require_string(L, idx), cursor); + node->setCursor(cursor); } static int p_set_scroll(UINode* node, lua::State* L, int idx) { diff --git a/src/maths/Heightmap.cpp b/src/maths/Heightmap.cpp index ab6ad153..99c27098 100644 --- a/src/maths/Heightmap.cpp +++ b/src/maths/Heightmap.cpp @@ -5,17 +5,6 @@ #include #include -std::optional InterpolationType_from(std::string_view str) { - if (str == "nearest") { - return InterpolationType::NEAREST; - } else if (str == "linear") { - return InterpolationType::LINEAR; - } else if (str == "cubic") { - return InterpolationType::CUBIC; - } - return std::nullopt; -} - static inline float sample_at( const float* buffer, uint width, diff --git a/src/maths/Heightmap.hpp b/src/maths/Heightmap.hpp index a5bd4f4d..1c4c7ff0 100644 --- a/src/maths/Heightmap.hpp +++ b/src/maths/Heightmap.hpp @@ -6,6 +6,7 @@ #include "typedefs.hpp" #include "maths/Heightmap.hpp" +#include "util/EnumMetadata.hpp" enum class InterpolationType { NEAREST, @@ -13,7 +14,11 @@ enum class InterpolationType { CUBIC, }; -std::optional InterpolationType_from(std::string_view str); +VC_ENUM_METADATA(InterpolationType) + {"nearest", InterpolationType::NEAREST}, + {"linear", InterpolationType::LINEAR}, + {"cubic", InterpolationType::CUBIC}, +VC_ENUM_END class Heightmap { uint width, height; diff --git a/src/presets/ParticlesPreset.cpp b/src/presets/ParticlesPreset.cpp index 8a007076..df129f1f 100644 --- a/src/presets/ParticlesPreset.cpp +++ b/src/presets/ParticlesPreset.cpp @@ -1,26 +1,8 @@ +#define VC_ENABLE_REFLECTION + #include "ParticlesPreset.hpp" - #include "data/dv_util.hpp" -std::string to_string(ParticleSpawnShape shape) { - static std::string names[] = { - "ball", - "sphere", - "box" - }; - return names[static_cast(shape)]; -} - -ParticleSpawnShape ParticleSpawnShape_from(std::string_view s) { - if (s == "ball") { - return ParticleSpawnShape::BALL; - } else if (s == "sphere") { - return ParticleSpawnShape::SPHERE; - } else { - return ParticleSpawnShape::BOX; - } -} - dv::value ParticlesPreset::serialize() const { auto root = dv::object(); if (frames.empty()) { @@ -47,7 +29,7 @@ dv::value ParticlesPreset::serialize() const { root["min_angular_vel"] = minAngularVelocity; root["max_angular_vel"] = maxAngularVelocity; root["spawn_spread"] = dv::to_value(size); - root["spawn_shape"] = to_string(spawnShape); + root["spawn_shape"] = ParticleSpawnShapeMeta.getName(spawnShape); root["random_sub_uv"] = randomSubUV; return root; } @@ -82,7 +64,7 @@ void ParticlesPreset::deserialize(const dv::value& src) { dv::get_vec(src["explosion"], explosion); } if (src.has("spawn_shape")) { - spawnShape = ParticleSpawnShape_from(src["spawn_shape"].asString()); + ParticleSpawnShapeMeta.getItem(src["spawn_shape"].asString(), spawnShape); } if (src.has("frames")) { for (const auto& frame : src["frames"]) { diff --git a/src/presets/ParticlesPreset.hpp b/src/presets/ParticlesPreset.hpp index 4b271849..dd5a66d5 100644 --- a/src/presets/ParticlesPreset.hpp +++ b/src/presets/ParticlesPreset.hpp @@ -5,8 +5,9 @@ #include #include "interfaces/Serializable.hpp" +#include "util/EnumMetadata.hpp" -enum ParticleSpawnShape { +enum class ParticleSpawnShape { /// @brief Coordinates are regulary distributed within /// the volume of a ball. BALL = 0, @@ -18,8 +19,11 @@ enum ParticleSpawnShape { BOX }; -std::string to_string(ParticleSpawnShape shape); -ParticleSpawnShape ParticleSpawnShape_from(std::string_view s); +VC_ENUM_METADATA(ParticleSpawnShape) + {"ball", ParticleSpawnShape::BALL}, + {"sphere", ParticleSpawnShape::SPHERE}, + {"box", ParticleSpawnShape::BOX}, +VC_ENUM_END struct ParticlesPreset : public Serializable { /// @brief Collision detection @@ -53,7 +57,7 @@ struct ParticlesPreset : public Serializable { /// @brief Maximum angular velocity float maxAngularVelocity = 0.0f; /// @brief Spawn spread shape - ParticleSpawnShape spawnShape = BALL; + ParticleSpawnShape spawnShape = ParticleSpawnShape::BALL; /// @brief Spawn spread glm::vec3 spawnSpread {}; /// @brief Texture name