0xc3 hace 3 meses
padre
commit
cb18d9633d

+ 33 - 0
src/saura/cmd/sandbox/folder/folder.cpp

@@ -0,0 +1,33 @@
+#include "folder.hpp"
+
+#include <iostream>
+
+namespace saura {
+Folder *Folder::add(std::string name) {
+  children.push_back(Folder(name));
+  return &children[children.size() - 1];
+}
+
+bool Folder::remove_at(size_t index) {
+  if (index >= children.size()) {
+    return false;
+  }
+
+  children.erase(children.begin() + index);
+  return true;
+}
+
+void Folder::swap_children(size_t i, size_t j) {
+  if (i >= children.size() || j >= children.size())
+    return;
+  std::swap(children[i], children[j]);
+}
+
+void Folder::print(uint32_t indent) {
+  std::string pad(indent * 2, ' ');
+  std::cout << pad << "📂 " << name << '\n';
+  for (auto &child : children) {
+    child.print(indent + 1);
+  }
+}
+} // namespace saura

+ 25 - 0
src/saura/cmd/sandbox/folder/folder.hpp

@@ -0,0 +1,25 @@
+#ifndef SAURA_FOLDER_HPP_
+#define SAURA_FOLDER_HPP_
+
+#include <cstdint>
+#include <string>
+#include <vector>
+
+namespace saura {
+class Folder {
+public:
+  std::string name;
+  std::vector<Folder> children;
+
+  Folder() = default;
+  Folder(std::string name) : name(name) {}
+
+  Folder *add(std::string name);
+  bool remove_at(size_t index);
+  void swap_children(size_t i, size_t j);
+  void print(uint32_t indent = 0);
+};
+
+} // namespace saura
+
+#endif

+ 92 - 11
src/saura/cmd/sandbox/sandbox.cpp

@@ -8,18 +8,26 @@
 #include <vector>
 
 #include "saura/cmd/sandbox/client/client.hpp"
+#include "saura/cmd/sandbox/folder/folder.hpp"
 #include "server/server.hpp"
 
 namespace saura {
 struct SandboxApp : AppBase {
   std::shared_ptr<ServerAPI> server_api_ctx;
   std::vector<ClientInfo> clients;
+  Folder category_root;
 
   void start() override {
     spdlog::info("Sandbox start!");
 
     this->server_api_ctx = std::make_shared<ServerAPI>();
     this->clients = server_api_ctx->fetch_clients(10, 0);
+
+    category_root = Folder("root");
+    category_root.add("A");
+    category_root.add("B");
+    category_root.add("C");
+    category_root.print();
   }
 
   void update(double dt) override {
@@ -33,15 +41,92 @@ struct SandboxApp : AppBase {
     ImGui::Text("%s", this->get_title().c_str());
 
     if (ImGui::BeginTabBar("Tabs")) {
-      // Tab 1
-      if (ImGui::BeginTabItem(u8"Главная")) {
+      if (ImGui::BeginTabItem("Главная")) {
         ImGui::Text("Welcome to Tab 1 content!");
         ImGui::Button("Click me in Tab 1");
         ImGui::EndTabItem();
       }
 
-      // Tab 2
-      if (ImGui::BeginTabItem(u8"Клиенты")) {
+      if (ImGui::BeginTabItem("Категории")) {
+        static bool req_open_ppm = false;
+        static bool is_add_node = false;
+        static int32_t selected_node = -1;
+
+        if (ImGui::TreeNodeEx("Root Node", 0)) {
+          if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
+            req_open_ppm = true;
+            selected_node = -1;
+          }
+
+          for (int i = 0; i < category_root.children.size(); i++) {
+            auto &category = category_root.children[i];
+
+            ImGuiTreeNodeFlags node_flags = ImGuiTreeNodeFlags_OpenOnArrow |
+                                            ImGuiTreeNodeFlags_SpanAvailWidth;
+            bool open = ImGui::TreeNodeEx((void *)(intptr_t)(i), node_flags,
+                                          "%s", category.name.c_str());
+
+            if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
+              spdlog::info("Clieked: {}", i);
+
+              req_open_ppm = true;
+              selected_node = i;
+            }
+
+            if (open) {
+              ImGui::TreePop();
+            }
+          }
+          ImGui::TreePop();
+        }
+
+        if (req_open_ppm) {
+          req_open_ppm = false;
+          ImGui::OpenPopup("category/ppm");
+          ImGui::SetNextWindowPos(
+              {ImGui::GetMousePos().x, ImGui::GetMousePos().y});
+        }
+
+        if (ImGui::BeginPopupModal("category/ppm", nullptr,
+
+                                   ImGuiWindowFlags_NoMove |
+                                       ImGuiWindowFlags_NoSavedSettings |
+                                       ImGuiWindowFlags_AlwaysAutoResize |
+                                       ImGuiWindowFlags_NoDecoration)) {
+          if (!is_add_node) {
+            if (ImGui::Button("Создать", ImVec2(120, 0))) {
+              is_add_node = true;
+            }
+
+            if (selected_node != -1 &&
+                ImGui::Button("Удалить", ImVec2(120, 0))) {
+              category_root.remove_at(selected_node);
+              ImGui::CloseCurrentPopup();
+              selected_node = -1;
+            }
+          } else {
+            static char buf[1024] = {};
+
+            ImGui::InputText("Name", buf, sizeof(buf));
+
+            if (ImGui::Button("Создать", ImVec2(120, 0))) {
+              category_root.add(std::string(buf));
+              is_add_node = false;
+              ImGui::CloseCurrentPopup();
+            }
+          }
+
+          if (ImGui::Button("Отмена", ImVec2(120, 0))) {
+            is_add_node = false;
+            ImGui::CloseCurrentPopup();
+          }
+          ImGui::EndPopup();
+        }
+
+        ImGui::EndTabItem();
+      }
+
+      if (ImGui::BeginTabItem("Клиенты")) {
         ImGuiTableFlags flags =
             ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders |
             ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_ScrollY;
@@ -74,12 +159,9 @@ struct SandboxApp : AppBase {
           ImGui::Separator();
 
           if (ImGui::Button("Close", ImVec2(120, 0))) {
-            ImGui::CloseCurrentPopup(); // Function to close the currently
-                                        // open popup
+            ImGui::CloseCurrentPopup();
           }
-
-          ImGui::EndPopup(); // Must be called if BeginPopupModal returned
-                             // true
+          ImGui::EndPopup();
         }
 
         if (ImGui::BeginTable("MyDataTable", 9, flags)) {
@@ -152,8 +234,7 @@ struct SandboxApp : AppBase {
         ImGui::EndTabItem();
       }
 
-      // Tab 3
-      if (ImGui::BeginTabItem(u8"Настройки")) {
+      if (ImGui::BeginTabItem("Настройки")) {
         ImGui::Text("Hello from the third tab.");
         ImGui::EndTabItem();
       }

+ 1 - 1
src/saura/cmd/sandbox/server/server.cpp

@@ -26,7 +26,7 @@ ServerAPI::~ServerAPI() {
 
 void ServerAPI::run() {
   while (true) {
-    fetch_clients(10, 0);
+    // fetch_clients(10, 0);
 
     std::this_thread::sleep_for(std::chrono::seconds(1));
   }