@@ -26,7 +26,7 @@ void edit_material(NotClosed<Window> target, SceneResources const& resources, Li
2626 make_id_slot (lit.emissive , " Emissive" , name.c_str (), {true });
2727}
2828
29- void edit_material (SceneResourcesMut resources, UnlitMaterial& unlit) {
29+ void edit_material (SceneResources const & resources, UnlitMaterial& unlit) {
3030 auto name = FixedString<128 >{};
3131 if (unlit.texture ) { name = FixedString<128 >{" {} ({})" , resources.textures [*unlit.texture ].name (), *unlit.texture }; }
3232 make_id_slot (unlit.texture , " Texture" , name.c_str (), {true });
@@ -57,12 +57,13 @@ void ResourceInspector::view(StaticMesh const& mesh, Id<StaticMesh> id) const {
5757}
5858
5959void ResourceInspector::edit (Material& out_material, Id<Material> id) const {
60- auto const name = FixedString<128 >{" {} ({})" , out_material.name , id};
60+ auto const name = FixedString<128 >{" {} ({})" , out_material.name () , id};
6161 auto tn = TreeNode{name.c_str ()};
6262 drag_payload (id, name.c_str ());
6363 if (tn) {
64- if (auto * lit = dynamic_cast <LitMaterial*>(&out_material)) { return edit_material (m_target, m_resources, *lit); }
65- if (auto * unlit = dynamic_cast <UnlitMaterial*>(&out_material)) { return edit_material (m_resources, *unlit); }
64+ auto & mat = out_material.base ();
65+ if (auto * lit = dynamic_cast <LitMaterial*>(&mat)) { return edit_material (m_target, m_resources, *lit); }
66+ if (auto * unlit = dynamic_cast <UnlitMaterial*>(&mat)) { return edit_material (m_resources, *unlit); }
6667 }
6768}
6869
@@ -78,7 +79,7 @@ void ResourceInspector::edit(Mesh& out_mesh, Id<Mesh> id) const {
7879 make_id_slot (primitive.static_mesh , " Static Mesh" , name.c_str ());
7980
8081 name = {};
81- if (primitive.material ) { name = FixedString<128 >{" {} ({})" , m_resources.materials [*primitive.material ]-> name , *primitive.material }; }
82+ if (primitive.material ) { name = FixedString<128 >{" {} ({})" , m_resources.materials [*primitive.material ]. name () , *primitive.material }; }
8283 make_id_slot (primitive.material , " Material" , name.c_str (), {true });
8384
8485 if (small_button_red (" x###remove_primitive" )) { to_erase = index; }
@@ -95,46 +96,54 @@ void ResourceInspector::edit(Mesh& out_mesh, Id<Mesh> id) const {
9596 }
9697}
9798
98- void SceneInspector::resources (std::string& out_name_buf ) const {
99+ void SceneInspector::resources (Data& out_data ) const {
99100 auto const ri = ResourceInspector{m_target, m_scene};
100101 static constexpr auto flags_v = ImGuiTreeNodeFlags_Framed;
101102 bool add_material{}, add_mesh{};
102103 if (auto tn = TreeNode (" Textures" , flags_v)) {
103- for (auto [texture, id] : enumerate(m_resources.textures )) { ri.view (texture, id); }
104+ for (auto [texture, id] : enumerate(m_resources.textures . view () )) { ri.view (texture, id); }
104105 }
105106 if (auto tn = TreeNode (" Static Meshes" , flags_v)) {
106- for (auto [static_mesh, id] : enumerate(m_resources.static_meshes )) { ri.view (static_mesh, id); }
107+ for (auto [static_mesh, id] : enumerate(m_resources.static_meshes . view () )) { ri.view (static_mesh, id); }
107108 }
108109 if (auto tn = TreeNode (" Materials" , flags_v)) {
109- for (auto [material, id] : enumerate(m_resources.materials )) { ri.edit (* material, id); }
110+ for (auto [material, id] : enumerate(m_resources.materials . view ())) { ri.edit (material, id); }
110111 if (ImGui::Button (" Add..." )) { add_material = true ; }
111112 }
112113 if (auto tn = TreeNode{" Meshes" , flags_v}) {
113- for (auto [mesh, id] : enumerate(m_resources.meshes )) { ri.edit (mesh, id); }
114+ for (auto [mesh, id] : enumerate(m_resources.meshes . view () )) { ri.edit (mesh, id); }
114115 if (ImGui::Button (" Add..." )) { add_mesh = true ; }
115116 }
116117
117118 if (add_material) { Popup::open (" Add Material" ); }
119+ if (out_data.input_buffer .empty ()) { out_data.input_buffer .resize (128 , ' \0 ' ); }
120+
121+ if (auto popup = Popup{" Add Mesh" }) {
122+ ImGui::InputText (" Name" , out_data.input_buffer .data (), out_data.input_buffer .size ());
123+ if (ImGui::Button (" Add" ) && *out_data.input_buffer .c_str ()) {
124+ m_scene.add (Mesh{.name = out_data.input_buffer .c_str ()});
125+ Popup::close_current ();
126+ }
127+ }
128+
118129 if (add_mesh) { Popup::open (" Add Mesh" ); }
119- if (out_name_buf.empty ()) { out_name_buf.resize (128 , ' \0 ' ); }
120- auto open_popup = [&out_name_buf](char const * id, auto func) {
121- if (auto popup = Popup{id}) {
122- ImGui::InputText (" Name" , out_name_buf.data (), out_name_buf.size ());
123- if (ImGui::Button (id)) {
124- auto str = out_name_buf.c_str ();
125- if (!*str) { return ; }
126- func (str);
127- Popup::close_current ();
130+ if (auto popup = Popup{" Add Material" }) {
131+ ImGui::InputText (" Name" , out_data.input_buffer .data (), out_data.input_buffer .size ());
132+ static constexpr char const * mat_types_v[] = {" Lit" , " Unlit" };
133+ static constexpr auto mat_types_size_v{static_cast <int >(std::size (mat_types_v))};
134+ char const * mat_type = out_data.material_type >= 0 && out_data.material_type < mat_types_size_v ? mat_types_v[out_data.material_type ] : " Invalid" ;
135+ ImGui::SliderInt (" Type" , &out_data.material_type , 0 , mat_types_size_v - 1 , mat_type);
136+ if (ImGui::Button (" Add" ) && *out_data.input_buffer .c_str ()) {
137+ auto mat = std::unique_ptr<MaterialBase>{};
138+ switch (out_data.material_type ) {
139+ case 0 : mat = std::make_unique<LitMaterial>(); break ;
140+ case 1 : mat = std::make_unique<UnlitMaterial>(); break ;
128141 }
142+ mat->name = out_data.input_buffer .c_str ();
143+ m_scene.add (Material{std::move (mat)});
144+ Popup::close_current ();
129145 }
130- };
131- auto push_material = [this ](std::string name) {
132- auto mat = std::make_unique<LitMaterial>();
133- mat->name = std::move (name);
134- m_scene.add (std::move (mat));
135- };
136- open_popup (" Add Material" , [&push_material](char const * name) { push_material (name); });
137- open_popup (" Add Mesh" , [this ](char const * name) { m_scene.add (Mesh{.name = name}); });
146+ }
138147}
139148
140149void SceneInspector::camera () const {
@@ -218,7 +227,7 @@ void SceneInspector::mesh(Node& out_node) const {
218227 auto * mesh_id = out_node.find <Id<Mesh>>();
219228 if (auto tn = TreeNode{" Mesh" , ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed}) {
220229 auto name = FixedString<128 >{};
221- if (mesh_id) { name = FixedString<128 >{" {} ({})" , m_scene .find (*mesh_id)->name , *mesh_id}; }
230+ if (mesh_id) { name = FixedString<128 >{" {} ({})" , m_resources. meshes .find (*mesh_id)->name , *mesh_id}; }
222231 make_id_slot<Mesh>(out_node, " Mesh" , name.c_str ());
223232 }
224233}
0 commit comments