TheAtlasEngine
 
Loading...
Searching...
No Matches
widgets.hpp
1#pragma once
2#include <imgui.h>
3#include <string>
4#include <glm/glm.hpp>
5#include <GLFW/glfw3.h>
6#include <core/filesystem/file_dialog.hpp>
7#include <core/scene/components.hpp>
8
9namespace atlas::ui {
10
29 bool begin_popup_context_window(const char* p_name,
30 ImGuiMouseButton p_mb,
31 bool p_over_items);
32
40 void draw_vec3(const std::string& p_name,
41 glm::vec3& p_value,
42 float p_reset_value = 0.f);
43
51 void draw_vec4(const std::string& p_name,
52 glm::vec4& p_value,
53 float p_reset_value = 0.f);
54
62 void draw_float(const std::string& p_tag,
63 float& p_value,
64 float p_reset_value = 0.f);
65
73 void draw_input_text(std::string& p_dst, std::string& p_src);
74
80 void draw_text(const std::string& p_value);
81
105 template<typename UComponent, typename UFunction>
106 static void draw_panel_component(const std::string& p_name,
107 flecs::entity& p_entity,
108 const UFunction& p_callback) {
109 const ImGuiTreeNodeFlags tree_node_flags =
110 ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed |
111 ImGuiTreeNodeFlags_SpanAvailWidth |
112 ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_FramePadding;
113
114 ImVec2 content_region = ImGui::GetContentRegionAvail();
115 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 });
116
117 float line_height =
118 ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f;
119 ImGui::Separator();
120
121 bool opened = ImGui::TreeNodeEx((void*)typeid(UComponent).hash_code(),
122 tree_node_flags,
123 "%s",
124 p_name.c_str());
125 ImGui::PopStyleVar();
126
127 ImGui::SameLine(content_region.x - line_height * 0.05f);
128
129 if (ImGui::Button("+", ImVec2(line_height, line_height))) {
130 ImGui::OpenPopup("ComponentSettings");
131 }
132
133 bool remove_component = false; // @note for deferring when to
134 // delete component.
135 if (ImGui::BeginPopup("ComponentSettings")) {
136 if (ImGui::MenuItem("Remove Component")) {
137 remove_component = true;
138 }
139
140 ImGui::EndPopup();
141 }
142
143 if (remove_component) {
144 p_entity.remove<UComponent>();
145 }
146
147 if (opened) {
148 p_callback(p_entity.get_mut<UComponent>());
149 ImGui::TreePop();
150 }
151 }
152
162 template<typename T, typename UFunction>
163 static void draw_component(const std::string& p_tag,
164 flecs::entity& p_entity,
165 const UFunction& p_callable) {
166 const ImGuiTreeNodeFlags tree_node_flags =
167 ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed |
168 ImGuiTreeNodeFlags_SpanAvailWidth |
169 ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_FramePadding;
170
171 if (!p_entity.has<T>()) {
172 return;
173 }
174
175 T* component = p_entity.get_mut<T>();
176
177 ImVec2 content_region = ImGui::GetContentRegionAvail();
178 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 });
179
180 float line_height =
181 ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f;
182 ImGui::Separator();
183
184 bool opened = ImGui::TreeNodeEx(
185 (void*)typeid(T).hash_code(), tree_node_flags, "%s", p_tag.c_str());
186 ImGui::PopStyleVar();
187
188 ImGui::SameLine(content_region.x - line_height * 0.05f);
189
190 if (ImGui::Button("+", ImVec2(line_height, line_height))) {
191 ImGui::OpenPopup("ComponentSettings");
192 }
193
194 bool remove_component = false; // @note for deferring when to
195 // delete component.
196 if (ImGui::BeginPopup("ComponentSettings")) {
197 if (ImGui::MenuItem("Remove Component")) {
198 remove_component = true;
199 }
200
201 ImGui::EndPopup();
202 }
203
204 if (opened) {
205 p_callable(component);
206 ImGui::TreePop();
207 }
208
209 if (remove_component and !std::is_same_v<T, transform>) {
210 p_entity.remove<T>();
211 }
212 }
213
220 void dockspace_window(GLFWwindow* p_window);
221
234 void button_open_file_dialog(const std::string& p_name,
235 std::string& p_filepath,
236 const std::string& p_filter = "obj;glftf;fbx");
237};