Unravel Engine C++ Reference
Loading...
Searching...
No Matches
inspector_physics_shape.cpp
Go to the documentation of this file.
2#include "imgui/imgui.h"
3#include "inspectors.h"
4
5namespace unravel
6{
7
9 entt::meta_any& var,
10 const meta_any_proxy& var_proxy,
11 const var_info& info,
12 const entt::meta_custom& custom) -> inspect_result
13{
14 auto& data = var.cast<physics_compound_shape&>();
15
16 inspect_result result{};
17
18 auto variant_types = entt::get_attribute_as<std::vector<entt::meta_type>>(var.type().custom(), "variant_types");
19
20 size_t item_current_idx = data.shape.index();
21
22 const auto& combo_preview_value = variant_types[item_current_idx];
23
24 auto name = entt::get_pretty_name(combo_preview_value);
25
26 if(ImGui::BeginCombo("##Type", name.c_str()))
27 {
28 for(int n = 0; n < variant_types.size(); n++)
29 {
30 const bool is_selected = (item_current_idx == n);
31
32 auto name = entt::get_pretty_name(variant_types[n]);
33
34 if(ImGui::Selectable(name.c_str(), is_selected))
35 {
36 item_current_idx = n;
37 result.changed = true;
38 }
39
40 result.edit_finished |= ImGui::IsItemDeactivatedAfterEdit();
41
42
43 ImGui::DrawItemActivityOutline();
44
45
46 if(is_selected)
47 {
48 ImGui::SetItemDefaultFocus();
49 }
50 }
51 ImGui::EndCombo();
52 }
53 result.edit_finished |= ImGui::IsItemDeactivatedAfterEdit();
54
55 ImGui::DrawItemActivityOutline();
56
57 if(result.changed)
58 {
59 const auto type = variant_types[item_current_idx];
60 if(type == entt::resolve<physics_box_shape>())
61 {
62 data.shape = physics_box_shape{};
63 }
64 else if(type == entt::resolve<physics_sphere_shape>())
65 {
66 data.shape = physics_sphere_shape{};
67 }
68 else if(type == entt::resolve<physics_capsule_shape>())
69 {
70 data.shape = physics_capsule_shape{};
71 }
72 else if(type == entt::resolve<physics_cylinder_shape>())
73 {
74 data.shape = physics_cylinder_shape{};
75 }
76 else if(type == entt::resolve<physics_mesh_shape>())
77 {
78 data.shape = physics_mesh_shape{};
79 }
80 }
81
82 std::string prop_name = "shape";
83 std::string prop_pretty_name = "Shape";
84 // Create a proxy for the variant member that can get/set through the parent
85 auto make_shape_proxy = [prop_name, prop_pretty_name, var_proxy](auto&& shape_ref) -> meta_any_proxy
86 {
87 using shape_type = std::decay_t<decltype(shape_ref)>;
88 meta_any_proxy shape_proxy;
89 shape_proxy.impl->parent = var_proxy.impl;
90 shape_proxy.impl->type_name = entt::get_pretty_name(entt::resolve<shape_type>());
91 shape_proxy.impl->name = [&]()
92 {
93 const auto& name = var_proxy.impl->name;
94 if(name.empty())
95 {
96 return prop_pretty_name;
97 }
98 return fmt::format("{}/{}", name, prop_pretty_name);
99 }();
100 shape_proxy.impl->getter = [parent_proxy = var_proxy](entt::meta_any& result)
101 {
102 entt::meta_any parent_var;
103 if(parent_proxy.impl->getter(parent_var) && parent_var)
104 {
105 auto& compound_shape = parent_var.cast<physics_compound_shape&>();
106 if(hpp::holds_alternative<shape_type>(compound_shape.shape))
107 {
108 result = entt::meta_any{hpp::get<shape_type>(compound_shape.shape)};
109 return true;
110 }
111 }
112 return false;
113 };
114 shape_proxy.impl->setter = [parent_proxy = var_proxy](meta_any_proxy& proxy, const entt::meta_any& value, uint64_t execution_count) mutable
115 {
116 entt::meta_any parent_var;
117 if(parent_proxy.impl->getter(parent_var) && parent_var)
118 {
119 auto& compound_shape = parent_var.cast<physics_compound_shape&>();
120 if(value.try_cast<shape_type>())
121 {
122 compound_shape.shape = value.cast<shape_type>();
123 return parent_proxy.impl->setter(parent_proxy, parent_var, execution_count);
124 }
125 }
126 return false;
127 };
128 return shape_proxy;
129 };
130
131 auto& override_ctx = ctx.get_cached<prefab_override_context>();
132 override_ctx.push_segment("shape", "Shape");
133
134 if(hpp::holds_alternative<physics_box_shape>(data.shape))
135 {
136 auto& shape = hpp::get<physics_box_shape>(data.shape);
137 auto shape_proxy = make_shape_proxy(shape);
138 entt::meta_any shape_var;
139 if(shape_proxy.impl->getter(shape_var))
140 {
141 result |= ::unravel::inspect_var(ctx, shape_var, shape_proxy, info, custom);
142 }
143 }
144 else if(hpp::holds_alternative<physics_sphere_shape>(data.shape))
145 {
146 auto& shape = hpp::get<physics_sphere_shape>(data.shape);
147 auto shape_proxy = make_shape_proxy(shape);
148 entt::meta_any shape_var;
149 if(shape_proxy.impl->getter(shape_var))
150 {
151 result |= ::unravel::inspect_var(ctx, shape_var, shape_proxy, info, custom);
152 }
153 }
154 else if(hpp::holds_alternative<physics_capsule_shape>(data.shape))
155 {
156 auto& shape = hpp::get<physics_capsule_shape>(data.shape);
157 auto shape_proxy = make_shape_proxy(shape);
158 entt::meta_any shape_var;
159 if(shape_proxy.impl->getter(shape_var))
160 {
161 result |= ::unravel::inspect_var(ctx, shape_var, shape_proxy, info, custom);
162 }
163 }
164 else if(hpp::holds_alternative<physics_cylinder_shape>(data.shape))
165 {
166 auto& shape = hpp::get<physics_cylinder_shape>(data.shape);
167 auto shape_proxy = make_shape_proxy(shape);
168 entt::meta_any shape_var;
169 if(shape_proxy.impl->getter(shape_var))
170 {
171 result |= ::unravel::inspect_var(ctx, shape_var, shape_proxy, info, custom);
172 }
173 }
174 else if(hpp::holds_alternative<physics_mesh_shape>(data.shape))
175 {
176 auto& shape = hpp::get<physics_mesh_shape>(data.shape);
177 auto shape_proxy = make_shape_proxy(shape);
178 entt::meta_any shape_var;
179 if(shape_proxy.impl->getter(shape_var))
180 {
181 result |= ::unravel::inspect_var(ctx, shape_var, shape_proxy, info, custom);
182 }
183 }
184 else
185 {
186 ImGui::LabelText("Unknown", "%s", "test");
187 }
188
189 return result;
190}
191
192} // namespace unravel
std::string name
Definition hub.cpp:33
texture_job_type type
auto get_pretty_name(const meta_type &t) -> std::string
auto get_attribute_as(const meta_custom &custom, const char *name) -> T
Definition reflection.h:24
auto inspect_var(rtti::context &ctx, entt::meta_any &var, const meta_any_proxy &var_proxy, const var_info &info, const entt::meta_custom &custom) -> inspect_result
Main entry point for inspecting any variable with automatic type resolution.
Result of an inspection operation indicating what changes occurred.
Definition inspector.h:164
auto inspect(rtti::context &ctx, entt::meta_any &var, const meta_any_proxy &var_proxy, const var_info &info, const entt::meta_custom &custom) -> inspect_result override
Safe deferred property access proxy for arbitrary object properties.
Definition inspector.h:216
std::shared_ptr< meta_any_proxy_impl > impl
Definition inspector.h:274
Represents a box shape for physics calculations.
Represents a capsule shape for physics calculations.
Represents a compound shape that can contain multiple types of shapes.
Represents a cylinder shape for physics calculations.
Represents a mesh shape for physics calculations.
Represents a sphere shape for physics calculations.
Global context for tracking prefab override changes during inspection.
Definition inspectors.h:99
void push_segment(const std::string &segment, const std::string &pretty_segment)
Pushes a new path segment onto both contexts and applies override styling.
Metadata about a variable being inspected.
Definition inspector.h:151