refactor enums: CursorShape, InterpolationType, ParticleSpawnShape

This commit is contained in:
MihailRis 2025-04-13 14:18:50 +03:00
parent 7749675a61
commit 3be8546bf4
9 changed files with 44 additions and 91 deletions

View File

@ -1,3 +1,4 @@
#define VC_ENABLE_REFLECTION
#include "../ContentLoader.hpp"
#include <algorithm>
@ -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);

View File

@ -1,39 +0,0 @@
#include "commons.hpp"
#include <map>
std::optional<CursorShape> CursorShape_from(std::string_view name) {
static std::map<std::string_view, CursorShape> 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<int>(shape)];
}

View File

@ -3,6 +3,8 @@
#include <string>
#include <optional>
#include "util/EnumMetadata.hpp"
enum class DrawPrimitive {
point = 0,
line,
@ -48,8 +50,18 @@ enum class CursorShape {
LAST=NOT_ALLOWED
};
std::optional<CursorShape> 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:

View File

@ -1,3 +1,4 @@
#define VC_ENABLE_REFLECTION
#include "gui_xml.hpp"
#include <stdexcept>
@ -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);
}
}

View File

@ -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) {

View File

@ -5,17 +5,6 @@
#include <stdexcept>
#include <glm/glm.hpp>
std::optional<InterpolationType> 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,

View File

@ -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> 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;

View File

@ -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<int>(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"]) {

View File

@ -5,8 +5,9 @@
#include <vector>
#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