From 5c3b61265a791ad9591bbc8f6fe4b8af319703b7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 Mar 2024 16:59:34 +0300 Subject: [PATCH] blocks and items 'caption' property --- res/content/base/blocks/grass.json | 1 + res/content/base/blocks/lightbulb.json | 1 + src/content/ContentLoader.cpp | 5 +++++ src/items/ItemDef.cpp | 2 ++ src/items/ItemDef.h | 4 ++++ src/util/stringutil.cpp | 20 ++++++++++++++++++++ src/util/stringutil.h | 6 ++++++ src/voxel_engine.cpp | 2 +- src/voxels/Block.cpp | 2 ++ src/voxels/Block.h | 2 ++ 10 files changed, 44 insertions(+), 1 deletion(-) diff --git a/res/content/base/blocks/grass.json b/res/content/base/blocks/grass.json index 8d43b1a7..59388ee5 100644 --- a/res/content/base/blocks/grass.json +++ b/res/content/base/blocks/grass.json @@ -1,4 +1,5 @@ { + "caption": "tall grass", "texture": "grass", "material": "base:grass", "draw-group": 1, diff --git a/res/content/base/blocks/lightbulb.json b/res/content/base/blocks/lightbulb.json index cf603c2b..f8aa71b3 100644 --- a/res/content/base/blocks/lightbulb.json +++ b/res/content/base/blocks/lightbulb.json @@ -1,4 +1,5 @@ { + "caption": "light bulb", "texture": "lightbulb", "emission": [15, 14, 13], "model": "aabb", diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 2ca5998e..35ce691e 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -107,6 +107,8 @@ void ContentLoader::fixPackIndices() { void ContentLoader::loadBlock(Block& def, std::string name, fs::path file) { auto root = files::read_json(file); + root->str("caption", def.caption); + // block texturing if (root->has("texture")) { std::string texture; @@ -253,6 +255,8 @@ void ContentLoader::loadCustomBlockModel(Block& def, dynamic::Map* primitives) { void ContentLoader::loadItem(ItemDef& def, std::string name, fs::path file) { auto root = files::read_json(file); + root->str("caption", def.caption); + std::string iconTypeStr = ""; root->str("icon-type", iconTypeStr); if (iconTypeStr == "none") { @@ -346,6 +350,7 @@ void ContentLoader::load(ContentBuilder& builder) { if (!def.hidden) { auto& item = builder.createItem(full+BLOCK_ITEM_SUFFIX); item.generated = true; + item.caption = def.caption; item.iconType = item_icon_type::block; item.icon = full; item.placingBlock = full; diff --git a/src/items/ItemDef.cpp b/src/items/ItemDef.cpp index c5bef078..43f9c1bf 100644 --- a/src/items/ItemDef.cpp +++ b/src/items/ItemDef.cpp @@ -1,4 +1,6 @@ #include "ItemDef.h" +#include "../util/stringutil.h" ItemDef::ItemDef(std::string name) : name(name) { + caption = util::id_to_caption(name); } diff --git a/src/items/ItemDef.h b/src/items/ItemDef.h index 64b7690f..a536cf91 100644 --- a/src/items/ItemDef.h +++ b/src/items/ItemDef.h @@ -21,8 +21,12 @@ enum class item_icon_type { class ItemDef { public: + /// @brief Item string id (with prefix included) std::string const name; + /// @brief Item name will shown in inventory + std::string caption; + itemcount_t stackSize = 64; bool generated = false; uint8_t emission[4] {0, 0, 0, 0}; diff --git a/src/util/stringutil.cpp b/src/util/stringutil.cpp index 39afa87e..3eb3d225 100644 --- a/src/util/stringutil.cpp +++ b/src/util/stringutil.cpp @@ -335,6 +335,26 @@ std::wstring util::pascal_case(const std::wstring& str) { return result; } +std::string util::id_to_caption(const std::string& id) { + std::string result = id; + + size_t index = result.find(':'); + if (index < result.length()-1) { + result = result.substr(index+1); + } else { + return ""; + } + size_t offset = 0; + for (; offset < result.length() && result[offset] == '_'; offset++) {} + + for (; offset < result.length(); offset++) { + if (result[offset] == '_') { + result[offset] = ' '; + } + } + return result; +} + /// @brief Split string by delimiter /// @param str source string /// @param delimiter split delimiter size diff --git a/src/util/stringutil.h b/src/util/stringutil.h index 3ab83092..ad260de5 100644 --- a/src/util/stringutil.h +++ b/src/util/stringutil.h @@ -39,6 +39,12 @@ namespace util { extern std::wstring capitalized(const std::wstring& str); extern std::wstring pascal_case(const std::wstring& str); + /// @brief Convert `any_prefix:some_data_id` to `some data id`. Leaves + /// '_' characters at end of the id. + /// @param id source id + /// @return resulting caption or empty string if there's nothing but prefix + extern std::string id_to_caption(const std::string& id); + extern std::vector split(const std::string& str, char delimiter); extern std::vector split(const std::wstring& str, char delimiter); } diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index ec9e470c..1552dd0c 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -7,11 +7,11 @@ #include "core_defs.h" #include "engine.h" -#include "util/platform.h" #include "coders/toml.h" #include "files/files.h" #include "files/settings_io.h" #include "files/engine_paths.h" +#include "util/platform.h" #include "util/command_line.h" #define SETTINGS_FILE "settings.toml" diff --git a/src/voxels/Block.cpp b/src/voxels/Block.cpp index dd4ff3bf..bf9a3d20 100644 --- a/src/voxels/Block.cpp +++ b/src/voxels/Block.cpp @@ -1,6 +1,7 @@ #include "Block.h" #include "../core_defs.h" +#include "../util/stringutil.h" CoordSystem::CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ) : axisX(axisX), axisY(axisY), axisZ(axisZ) @@ -42,6 +43,7 @@ Block::Block(std::string name) textureFaces {TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,TEXTURE_NOTFOUND, TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,TEXTURE_NOTFOUND} { rotations = BlockRotProfile::PIPE; + caption = util::id_to_caption(name); } Block::Block(std::string name, std::string texture) : name(name), diff --git a/src/voxels/Block.h b/src/voxels/Block.h index a3ee44d3..40efab20 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -93,6 +93,8 @@ class Block { public: /// @brief Block string id (with prefix included) std::string const name; + + std::string caption; /// @brief Textures set applied to block sides std::string textureFaces[6]; // -x,x, -y,y, -z,z