Browse Source

added name cache

meemknight 2 years ago
parent
commit
f2bb4f0eb7

+ 15 - 0
Pika/core/sharedRuntime/pikaImgui/pikaImgui.cpp

@@ -149,6 +149,21 @@ bool pika::pikaImgui::redButton(const char *label, const ImVec2 &size_arg)
 	return rez;
 }
 
+bool pika::pikaImgui::BeginChildFrameColoured(ImGuiID id,
+	glm::vec4 color, const ImVec2 &size, ImGuiWindowFlags extra_flags)
+{
+	ImGuiContext &g = *::ImGui::GetCurrentContext();
+	const ImGuiStyle &style = ::ImGui::GetStyle();
+	::ImGui::PushStyleColor(ImGuiCol_ChildBg, {color.x,color.y,color.z,color.w});
+	::ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding);
+	::ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize);
+	::ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding);
+	bool ret = ::ImGui::BeginChild(id, size, true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding | extra_flags);
+	::ImGui::PopStyleVar(3);
+	::ImGui::PopStyleColor();
+	return ret;
+}
+
 void pika::pikaImgui::addErrorSymbol()
 {
 	::ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 0, 0, 255));

+ 7 - 0
Pika/core/sharedRuntime/pikaImgui/pikaImgui.h

@@ -13,6 +13,7 @@
 #include <pikaAllocator/freeListAllocator.h>
 
 #include <pikaContext.h>
+#include <glm/vec4.hpp>
 
 #include <vector>
 #include <string>
@@ -66,8 +67,14 @@ namespace pika
 			}
 		};
 
+
 		bool redButton(const char *label, const ImVec2 &size_arg = {});
 
+		bool BeginChildFrameColoured(ImGuiID id,
+			glm::vec4 color,
+			const ImVec2 &size = {},
+			ImGuiWindowFlags extra_flags = 0);
+
 		void addErrorSymbol();
 		void addWarningSymbol();
 

+ 21 - 3
Pika/pluggins/pluggins/sushiViewer/sushiViewer.cpp

@@ -71,7 +71,16 @@ void SushiViewer::displaySushiUiElementImgui(::sushi::SushiUiElement &e, glm::ve
 	if (ImGui::BeginChildFrame(e.id, {0, 500}, true))
 	{
 		ImGui::PushID(e.id);
-		ImGui::Text("Ui element editor: %s, id: %u", e.name, e.id);
+		ImGui::Text("Ui element editor: %s id: %u", e.name, e.id);
+
+		char data[16] = {};
+		static_assert(sizeof(data) == sizeof(e.name));
+		std::strncpy(data, e.name, sizeof(e.name) - 1);
+		ImGui::InputText("Rename", data, sizeof(e.name));
+		if (std::strcmp(data, e.name))
+		{
+			sushiContext.rename(&e, data);
+		}
 
 		ImGui::Separator();
 		if (pika::pikaImgui::redButton("Delete"))
@@ -90,12 +99,21 @@ void SushiViewer::displaySushiUiElementImgui(::sushi::SushiUiElement &e, glm::ve
 
 void SushiViewer::displaySushiParentElementImgui(::sushi::SushiParent &e, glm::vec4 parent)
 {
-	if (ImGui::BeginChildFrame(2, {0, 700}, true))
+	if (pika::pikaImgui::BeginChildFrameColoured(2, {0.2,0.3,0.7,0.9}, { 0, 700 }, true))
 	{
 		ImGui::PushID(e.id);
 
 		ImGui::Text("Parent editor: %s, id: %u", e.name, e.id);
 
+		char data[16] = {};
+		static_assert(sizeof(data) == sizeof(e.name));
+		std::strncpy(data, e.name, sizeof(e.name) - 1);
+		ImGui::InputText("Rename", data, sizeof(e.name));
+		if (std::strcmp(data, e.name))
+		{
+			sushiContext.rename(&e, data);
+		}
+
 		ImGui::Separator();
 		if (pika::pikaImgui::redButton("Delete"))
 		{
@@ -255,7 +273,7 @@ bool SushiViewer::update(pika::Input input, pika::WindowState windowState, Reque
 	{
 		auto id = toDelete.back();
 		toDelete.pop_back();
-		sushiContext.root.deleteById(id);
+		sushiContext.deleteById(id);
 	}
 
 

+ 1 - 2
Pika/resources/logs.txt

@@ -1,2 +1 @@
-#2023-09-06 23:38:03: Created container: SushiViewer
-#2023-09-06 23:43:11: Destroyed continer: SushiViewer #1
+#2023-09-07 11:27:13: Created container: SushiViewer

+ 82 - 48
Pika/thirdparty/sushi/include/sushi/sushi.h

@@ -2,6 +2,8 @@
 #include <gl2d/gl2d.h>
 #include <sushi/sushiPrimitives.h>
 #include <sushi/sushiInput.h>
+#include <unordered_map>
+#include <map>
 
 namespace sushi
 {
@@ -13,7 +15,7 @@ namespace sushi
 		SushiUiElement(const char *name, int id)
 		{
 			this->id = id;
-			std::strncpy(this->name, name, sizeof(name) - 1);
+			std::strncpy(this->name, name, sizeof(this->name) - 1);
 		};
 
 		char name[16] = {};
@@ -28,57 +30,18 @@ namespace sushi
 
 	};
 
-	struct SushiParent;
-
-	//parent or ui
-	struct SushiElement
-	{
-		SushiElement() {};
-		SushiElement(void *ptr, int type):ptr(ptr), type(type) {};
-		SushiElement(SushiUiElement *ptr):ptr(ptr), type(TypeUiElement) {};
-		SushiElement(SushiParent *ptr):ptr(ptr), type(TypeParent) {};
-
-		void *ptr = 0;
-		int type = 0;
-
-		enum Type
-		{
-			TypeUiElement = 1,
-			TypeParent = 2,
-		};
-
-		SushiUiElement *getUiElement()
-		{
-			if (type == TypeUiElement)
-			{
-				return (SushiUiElement *)ptr;
-			}
-			else { return 0; }
-		}
-
-		SushiParent *getParent()
-		{
-			if (type == TypeParent)
-			{
-				return (SushiParent *)ptr;
-			}
-			else { return 0; }
-		}
-
-	};
-
 	struct SushiParent
 	{
 		SushiParent() {};
 		SushiParent(const char *name, int id)
 		{
 			this->id = id;
-			std::strncpy(this->name, name, sizeof(name) - 1);
+			std::strncpy(this->name, name, sizeof(this->name) - 1);
 		};
 		SushiParent(const char *name, int id, Background b)
 		{
 			this->id = id;
-			std::strncpy(this->name, name, sizeof(name) - 1);
+			std::strncpy(this->name, name, sizeof(this->name) - 1);
 			background = b;
 		};
 
@@ -104,24 +67,82 @@ namespace sushi
 
 		void update(gl2d::Renderer2D &renderer,
 			sushi::SushiInput &input, glm::vec4 parentTransform);
-		
+
 		OutData outData;
 
-		bool deleteById(unsigned int id);
+		bool deleteByIdInternal(unsigned int id);
 
-		void addElement(
+		void addElementInternal(
 			const char *name,
 			Transform &transform,
 			Background &background,
 			unsigned int id);
 
-		void addParent(
+		void addParentInternal(
 			const char *name,
 			Transform &transform,
 			Background &background,
 			unsigned int id);
 	};
 
+	//parent or ui
+	struct SushiElement
+	{
+		SushiElement() {};
+		SushiElement(void *ptr, int type):ptr(ptr), type(type) {};
+		SushiElement(SushiUiElement *ptr):ptr(ptr), type(TypeUiElement) {};
+		SushiElement(SushiParent *ptr):ptr(ptr), type(TypeParent) {};
+
+		void *ptr = 0;
+		int type = 0;
+
+		enum Type
+		{
+			TypeUiElement = 1,
+			TypeParent = 2,
+		};
+
+		SushiUiElement *getUiElement()
+		{
+			if (type == TypeUiElement)
+			{
+				return (SushiUiElement *)ptr;
+			}
+			else { return 0; }
+		}
+
+		SushiParent *getParent()
+		{
+			if (type == TypeParent)
+			{
+				return (SushiParent *)ptr;
+			}
+			else { return 0; }
+		}
+
+		bool isUiElement() { return type == TypeUiElement; }
+		bool isParent() { return type == TypeParent; }
+		bool hasValue() { return (type != 0) && (ptr != nullptr); }
+		std::string getName()
+		{
+			if (hasValue())
+			{
+				if (isParent())
+				{
+					return getParent()->name;
+				}else if (isUiElement())
+				{
+					return getUiElement()->name;
+				}
+			}
+			else
+			{
+				return "";
+			}
+		}
+
+	};
+
 	//this is a sushi context. Holds all the windows and manages stuff
 	struct SushyContext
 	{
@@ -147,10 +168,23 @@ namespace sushi
 			Transform &transform,
 			Background &background);
 
+		bool deleteById(unsigned int id);
+
+		std::unordered_multimap<std::string, SushiElement> cachedData;
+
+		void signalElementToCacheInternl(SushiElement el);
+
+		void signalElementToCacheToRemoveInternal(SushiElement el);
+
+		SushiElement genUniqueElement(std::string name);
+
+		std::pair<std::unordered_multimap<std::string, SushiElement>::iterator,
+			std::unordered_multimap<std::string, SushiElement>::iterator> getElements(std::string name);
+
+		void rename(SushiElement el, char *newName);
 	};
 
-	
-	
+
 
 
 

+ 79 - 6
Pika/thirdparty/sushi/src/sushi.cpp

@@ -1,7 +1,72 @@
 #include <sushi/sushi.h>
+#include <cstring>
 
 namespace sushi
 {
+	void SushyContext::signalElementToCacheInternl(SushiElement el)
+	{
+		if (!el.hasValue())return;
+		cachedData.insert({el.getName(), el});
+	}
+
+	void SushyContext::signalElementToCacheToRemoveInternal(SushiElement el)
+	{
+		if (!el.hasValue())return;
+
+		auto range = cachedData.equal_range(el.getName());
+		for (auto it = range.first; it != range.second; ++it)
+		{
+			if (it->second.ptr == el.ptr)
+			{
+				cachedData.erase(it);
+				break;
+			}
+		}
+	}
+
+	SushiElement SushyContext::genUniqueElement(std::string name)
+	{
+		auto range = cachedData.equal_range(name);
+
+		if (range.first == cachedData.end()) { return {}; }
+
+		auto pos2 = range.first;
+		pos2++;
+		if (pos2 == range.second)
+		{
+			return range.first->second;
+		}
+		else
+		{
+			return {};
+		}
+	}
+
+	std::pair<std::unordered_multimap<std::string, SushiElement>::iterator,
+		std::unordered_multimap<std::string, SushiElement>::iterator> SushyContext::getElements(std::string name)
+	{
+		auto range = cachedData.equal_range(name);
+		return range;
+	}
+
+	void SushyContext::rename(SushiElement el, char *newName)
+	{
+		if (!el.hasValue()) { return; }
+
+		signalElementToCacheToRemoveInternal(el);
+
+		if (el.isParent())
+		{
+			std::strncpy(el.getParent()->name, newName, sizeof(el.getParent()->name) - 1);
+		}
+		else if(el.isUiElement())
+		{
+			std::strncpy(el.getUiElement()->name, newName, sizeof(el.getUiElement()->name) - 1);
+		}
+
+		signalElementToCacheInternl(el);
+		
+	}
 
 	unsigned int SushyContext::addElement(
 		SushiParent &parent,
@@ -9,8 +74,11 @@ namespace sushi
 		Transform &transform,
 		Background &background)
 	{
+
+
+
 		unsigned int id = currentIdCounter++;
-		parent.addElement(name, transform, background, id);
+		parent.addElementInternal(name, transform, background, id);
 		return id;
 	}
 
@@ -21,10 +89,15 @@ namespace sushi
 		Background &background)
 	{
 		unsigned int id = currentIdCounter++;
-		parent.addParent(name, transform, background, id);
+		parent.addParentInternal(name, transform, background, id);
 		return id;
 	}
 
+	bool SushyContext::deleteById(unsigned int id)
+	{
+		return root.deleteByIdInternal(id);
+	}
+
 	void SushyContext::createBasicSchene(int baseId, const char *name)
 	{
 		*this = {};
@@ -174,7 +247,7 @@ namespace sushi
 		background.render(renderer, rectRez);
 	}
 
-	bool SushiParent::deleteById(unsigned int id)
+	bool SushiParent::deleteByIdInternal(unsigned int id)
 	{
 		for (int i = 0; i < orderedElementsIds.size(); i++)
 		{
@@ -201,7 +274,7 @@ namespace sushi
 				return 1;
 			}
 
-			if (parents[i].deleteById(id))
+			if (parents[i].deleteByIdInternal(id))
 			{
 				return 1;
 			};
@@ -210,7 +283,7 @@ namespace sushi
 		return 0;
 	}
 
-	void SushiParent::addElement(
+	void SushiParent::addElementInternal(
 		const char *name,
 		Transform &transform,
 		Background &background,
@@ -226,7 +299,7 @@ namespace sushi
 		orderedElementsIds.push_back(id);
 	}
 
-	void SushiParent::addParent(
+	void SushiParent::addParentInternal(
 		const char *name,
 		Transform &transform,
 		Background &background,