Skip to content

Commit

Permalink
Add editing custom tags of objects.
Browse files Browse the repository at this point in the history
This also shows the internal tags.
  • Loading branch information
enginmanap committed Jan 13, 2025
1 parent d4fa493 commit 40a6fa6
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/GameObjects/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <string>
#include <list>
#include <Utils/HardCodedTags.h>

#include "API/LimonAPI.h"
#include "Editor/ImGuiRequest.h"
Expand Down Expand Up @@ -59,10 +60,28 @@ class GameObject {
return false;
}

/**
*
* @return all tags, including hardcoded ones
*/
const std::list<HashUtil::HashedString>& getTags() const {
return tags;
}

/**
* This method is here only for Editor. Don't use in game code, or refactor to remove rehashing of tags
* @return list of tags, filtered by HardCodedTags list.
*/
std::list<HashUtil::HashedString> getTagsCustomOnly() const {
std::list<HashUtil::HashedString> filteredTags;
for (const auto& currentTag: tags) {
if (std::find(HardCodedTags::ALL_TAGS.begin(), HardCodedTags::ALL_TAGS.end(), currentTag.text) == HardCodedTags::ALL_TAGS.end()) {
filteredTags.emplace_back(currentTag.text);
}
}
return filteredTags;
}

virtual void removeTag(const std::string& text) {
HashUtil::HashedString tag(text);
bool found = false;
Expand Down
33 changes: 33 additions & 0 deletions src/GameObjects/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ bool Model::fillObjects(tinyxml2::XMLDocument &document, tinyxml2::XMLElement *o
}
}

std::list<HashUtil::HashedString> customTags = getTagsCustomOnly();
if (!customTags.empty()) {
tinyxml2::XMLElement *customTagsNode = document.NewElement("CustomTags");
tinyxml2::XMLElement *customTagsCountNode = document.NewElement("Count");
customTagsCountNode->SetText(std::to_string(customTags.size()).c_str());
customTagsNode->InsertEndChild(customTagsCountNode);
objectElement->InsertEndChild(customTagsNode);
for (auto& customTag:customTags) {
tinyxml2::XMLElement *customTagNode = document.NewElement("CustomTag");
customTagNode->SetText(customTag.text.c_str());
customTagsNode->InsertEndChild(customTagNode);
}
}

modelAsset->serializeCustomizations();
return true;
}
Expand All @@ -326,6 +340,25 @@ ImGuiResult Model::addImGuiEditorElements(const ImGuiRequest &request) {
}

ImGui::NewLine();
static char tempTagsBuffer[512] = {0};
std::string joinedTags = StringUtils::join(this->getTagsCustomOnly(), ",");
strncpy(tempTagsBuffer, joinedTags.c_str(), std::min(joinedTags.length(), sizeof(tempTagsBuffer) - 1));
tempTagsBuffer[std::min(joinedTags.length(), sizeof(tempTagsBuffer) - 1)] = 0;
std::vector<std::string> internalTags;
for (auto currentTag : this->getTags()) {
if (std::find(HardCodedTags::ALL_TAGS.begin(), HardCodedTags::ALL_TAGS.end(), currentTag.text) != HardCodedTags::ALL_TAGS.end()) {
internalTags.emplace_back(currentTag.text);
}
}
char internalTagsBuffer[512] = {0};
std::string internalTagsJoined = StringUtils::join(internalTags, ",");
strncpy(internalTagsBuffer, internalTagsJoined.c_str(), std::min(internalTagsJoined.length(), sizeof(internalTagsBuffer) - 1));
tempTagsBuffer[std::min(internalTagsJoined.length(), sizeof(internalTagsBuffer) - 1)] = 0;

ImGui::InputText("Automatic Tags##ForModelObject", internalTagsBuffer, sizeof(internalTagsBuffer), ImGuiInputTextFlags_ReadOnly);
ImGui::InputText("Custom Tags##ForModelObject",tempTagsBuffer, sizeof(tempTagsBuffer), ImGuiInputTextFlags_CharsNoBlank);
joinedTags = tempTagsBuffer;
this->setTagsCustomOnly(StringUtils::split(joinedTags, ","));
if (isAnimated()) {
if (ImGui::CollapsingHeader("Model animation properties")) {
if (ImGui::BeginCombo("Animation Name", animationName.c_str())) {
Expand Down
15 changes: 15 additions & 0 deletions src/GameObjects/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ class Model : public PhysicalRenderable, public GameObject {
GameObject::removeTag(text);
}

private:
/**
* This method is causing rehashing of all new tags. Don't use in game mode, only intended for Editor
* @param tagList custom tags to be set.
*/
void setTagsCustomOnly(const std::vector<std::string> & tagList) {
std::list<HashUtil::HashedString> currentTags = this->getTagsCustomOnly();
for (auto current_tag: currentTags) {
this->removeTag(current_tag.text);
}
for (auto it = tagList.begin(); it != tagList.end(); ++it) {
this->addTag(*it);
}
this->dirtyForFrustum = true;
}

};

Expand Down
21 changes: 20 additions & 1 deletion src/Utils/HardCodedTag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,23 @@ const std::string HardCodedTags::CAMERA_LIGHT_DIRECTIONAL = "directional_camer
const std::string HardCodedTags::CAMERA_LIGHT_POINT = "point_camera";
const std::string HardCodedTags::CAMERA_PLAYER = "player_camera";

const std::string HardCodedTags::PICKED_OBJECT = "picked_object";
const std::string HardCodedTags::PICKED_OBJECT = "picked_object";

const std::vector<std::string> HardCodedTags::ALL_TAGS = {
HardCodedTags::OBJECT_MODEL_STATIC ,
HardCodedTags::OBJECT_MODEL_PHYSICAL ,

HardCodedTags::OBJECT_MODEL_BASIC ,
HardCodedTags::OBJECT_MODEL_ANIMATED ,
HardCodedTags::OBJECT_MODEL_TRANSPARENT ,

HardCodedTags::OBJECT_PLAYER_BASIC ,
HardCodedTags::OBJECT_PLAYER_ANIMATED ,
HardCodedTags::OBJECT_PLAYER_TRANSPARENT ,

HardCodedTags::CAMERA_LIGHT_DIRECTIONAL ,
HardCodedTags::CAMERA_LIGHT_POINT ,
HardCodedTags::CAMERA_PLAYER ,

HardCodedTags::PICKED_OBJECT
};
2 changes: 2 additions & 0 deletions src/Utils/HardCodedTags.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class HardCodedTags {
static const std::string CAMERA_PLAYER;

static const std::string PICKED_OBJECT;

static const std::vector<std::string> ALL_TAGS;//all tags
};

#endif //LIMONENGINE_HARDCODEDTAGS_H
16 changes: 16 additions & 0 deletions src/Utils/StringUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <string>
#include <vector>
#include <sstream>
#include <list>
#include "HashUtil.h"

class StringUtils {
public:
Expand Down Expand Up @@ -49,6 +51,20 @@ class StringUtils {
}
return joinedStream.str();
}


std::string static join(const std::list<HashUtil::HashedString>& source, const std::string& delimiter)
{
std::ostringstream joinedStream;
if (source.empty()) {
return "";
}
for (auto it = source.begin(); it != std::prev(source.end()); ++it) {
joinedStream << it->text << delimiter;
}
joinedStream << source.back().text;
return joinedStream.str();
}
};


Expand Down
14 changes: 13 additions & 1 deletion src/WorldLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ WorldLoader::loadObject( std::shared_ptr<AssetManager> assetManager, tinyxml2::X
}
//Now Load material changes
std::vector<std::pair<std::string, std::shared_ptr<Material>>> custumizedMeshMaterialList;
tinyxml2::XMLElement* meshMaterialListNode = objectNode->FirstChildElement("MeshMaterialList");
tinyxml2::XMLElement* meshMaterialListNode = objectNode->FirstChildElement("MeshMaterialList");
if (meshMaterialListNode != nullptr) {
tinyxml2::XMLElement *meshMaterialNode = meshMaterialListNode->FirstChildElement("MeshMaterial");
while (meshMaterialNode != nullptr) {
Expand All @@ -614,6 +614,18 @@ WorldLoader::loadObject( std::shared_ptr<AssetManager> assetManager, tinyxml2::X
loadedObjectInformation->model->loadOverriddenMeshMaterial(custumizedMeshMaterialList);
}

tinyxml2::XMLElement* customTagsNode = objectNode->FirstChildElement("CustomTags");
if (customTagsNode != nullptr) {
tinyxml2::XMLElement *customTagNode = customTagsNode->FirstChildElement("CustomTag");
while (customTagNode != nullptr) {
if (customTagNode->GetText() != nullptr) {
loadedObjectInformation->model->addTag(customTagNode->GetText());
}
customTagNode = customTagNode->NextSiblingElement("CustomTag");
}
}


loadedObjects.push_back(std::move(loadedObjectInformation));


Expand Down

0 comments on commit 40a6fa6

Please sign in to comment.