Line data Source code
1 : #include "inspector_panel.hpp"
2 :
3 : #include <imgui_stdlib.h>
4 :
5 : namespace editor {
6 :
7 : #define CHECK_ID_IS_COMPONENT(id, component_type) \
8 : id == selected_entity->world().component<component_type>().id()
9 :
10 : void draw_component(const editor::Context& ctx,
11 : wren::scene::components::MeshRenderer& mesh_renderer);
12 : void draw_component(wren::scene::components::Transform& transform);
13 : void draw_component(const std::string_view& tag, wren::math::Vec3f& vec);
14 :
15 0 : void render_inspector_panel(
16 : const editor::Context& ctx,
17 : const std::shared_ptr<wren::scene::Scene>& scene,
18 : const std::optional<flecs::entity>& selected_entity) {
19 0 : ImGui::Begin("Inspector");
20 :
21 0 : if (!selected_entity.has_value()) {
22 0 : ImGui::End();
23 0 : return;
24 : }
25 :
26 0 : wren::scene::Entity entity{selected_entity.value(), scene};
27 :
28 : // Edit name
29 0 : auto name = std::string(selected_entity->name());
30 0 : ImGui::InputText("Name", &name);
31 0 : selected_entity->set_name(name.c_str());
32 :
33 : // Iterate all components
34 0 : selected_entity->each([selected_entity, ctx](flecs::id id) {
35 0 : ImGui::Separator();
36 :
37 0 : if (CHECK_ID_IS_COMPONENT(id, wren::scene::components::Transform)) {
38 0 : draw_component(
39 0 : *selected_entity->get_mut<wren::scene::components::Transform>());
40 0 : } else if (CHECK_ID_IS_COMPONENT(id,
41 : wren::scene::components::MeshRenderer)) {
42 0 : draw_component(
43 0 : ctx,
44 0 : *selected_entity->get_mut<wren::scene::components::MeshRenderer>());
45 0 : } else {
46 0 : ImGui::Text("Can't inspect component type: %s", id.str().c_str());
47 : }
48 0 : });
49 :
50 0 : if (ImGui::BeginPopupContextItem("add_component")) {
51 0 : if (ImGui::Button("Mesh Renderer")) {
52 0 : selected_entity->add<wren::scene::components::MeshRenderer>();
53 0 : }
54 :
55 0 : ImGui::EndPopup();
56 0 : }
57 0 : if (ImGui::Button("Add component")) {
58 0 : ImGui::OpenPopup("add_component");
59 0 : }
60 :
61 0 : ImGui::End();
62 0 : }
63 :
64 0 : void draw_component(const editor::Context& ctx,
65 : wren::scene::components::MeshRenderer& mesh_renderer) {
66 0 : ImGui::Text("Mesh Renderer");
67 :
68 0 : auto mesh_filename = mesh_renderer.mesh_file().filename().string();
69 :
70 0 : ImGui::InputText("file", &mesh_filename);
71 :
72 0 : if (ImGui::BeginDragDropTarget()) {
73 0 : const auto* payload = ImGui::AcceptDragDropPayload("FILESYSTEM_BROWSER");
74 0 : if (payload != nullptr) {
75 0 : std::string new_file(static_cast<const char*>(payload->Data),
76 0 : payload->DataSize);
77 0 : mesh_renderer.update_mesh(ctx.project_path, new_file);
78 0 : }
79 :
80 0 : ImGui::EndDragDropTarget();
81 0 : }
82 0 : }
83 :
84 0 : void draw_component(wren::scene::components::Transform& transform) {
85 0 : draw_component("pos", transform.position);
86 0 : draw_component("rot", transform.rotation);
87 : // draw_component(transform.scale);
88 0 : }
89 :
90 0 : void draw_component(const std::string_view& tag, wren::math::Vec3f& vec) {
91 0 : ImGui::LabelText("##name", "%s", tag.data());
92 0 : ImGui::PushItemWidth(80);
93 0 : ImGui::Text("X");
94 0 : ImGui::SameLine();
95 0 : float x = vec.x();
96 0 : if (ImGui::DragFloat(("##x" + std::string(tag.data())).c_str(), &x, 0.1F))
97 0 : vec.x(x);
98 :
99 0 : ImGui::SameLine();
100 0 : ImGui::Text("Y");
101 0 : ImGui::SameLine();
102 0 : float y = vec.y();
103 0 : if (ImGui::DragFloat(("##y" + std::string(tag.data())).c_str(), &y, 0.1F))
104 0 : vec.y(y);
105 :
106 0 : ImGui::SameLine();
107 0 : ImGui::Text("Z");
108 0 : ImGui::SameLine();
109 0 : float z = vec.z();
110 0 : if (ImGui::DragFloat(("##z" + std::string(tag.data())).c_str(), &z, 0.1F))
111 0 : vec.z(z);
112 :
113 0 : ImGui::PopItemWidth();
114 0 : }
115 :
116 : } // namespace editor
|