blocks and items 'caption' property

This commit is contained in:
MihailRis 2024-03-12 16:59:34 +03:00
parent c9da6b9a0c
commit 5c3b61265a
10 changed files with 44 additions and 1 deletions

View File

@ -1,4 +1,5 @@
{
"caption": "tall grass",
"texture": "grass",
"material": "base:grass",
"draw-group": 1,

View File

@ -1,4 +1,5 @@
{
"caption": "light bulb",
"texture": "lightbulb",
"emission": [15, 14, 13],
"model": "aabb",

View File

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

View File

@ -1,4 +1,6 @@
#include "ItemDef.h"
#include "../util/stringutil.h"
ItemDef::ItemDef(std::string name) : name(name) {
caption = util::id_to_caption(name);
}

View File

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

View File

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

View File

@ -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<std::string> split(const std::string& str, char delimiter);
extern std::vector<std::wstring> split(const std::wstring& str, char delimiter);
}

View File

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

View File

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

View File

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