Nebula
Loading...
Searching...
No Matches
componentinspection.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13//------------------------------------------------------------------------------
14#include "util/delegate.h"
15#include "util/stringatom.h"
16#include "game/entity.h"
17#include "game/componentid.h"
22#include "util/color.h"
23#include "imgui.h"
24
25namespace Game
26{
27
29{
30public:
31 using DrawFunc = void(*)(Game::ComponentId, void*, bool*);
32
34 static void Destroy();
35
36 static void Register(ComponentId component, DrawFunc);
37
38 static void DrawInspector(ComponentId component, void* data, bool* commit);
39
40 // set to true if you want full information to be displayed in the inspector
41 bool debug = false;
42
43private:
46
48
50};
51
52template<typename TYPE>
53inline void
54ComponentDrawFuncT(ComponentId component, void* data, bool* commit);
55
56template<typename TYPE, std::size_t i = 0>
57inline void
58InspectorDrawField(ComponentId component, void* data, bool* commit)
59{
60 if constexpr (i < TYPE::Traits::num_fields)
61 {
62 if (TYPE::Traits::field_hide_in_inspector[i] && !ComponentInspection::Instance()->debug)
63 {
64 // Just move to the next field
65 InspectorDrawField<TYPE, i + 1>(component, data, commit);
66 }
67 else
68 {
69 using field_tuple = typename TYPE::Traits::field_types;
70 using field_type = typename std::tuple_element<i, field_tuple>::type;
71 Util::String fieldName = TYPE::Traits::field_names[i];
72 fieldName.CamelCaseToWords();
73 fieldName.Capitalize();
74 ImGui::TableSetColumnIndex(0);
75 ImGui::AlignTextToFramePadding();
76 ImGui::Text(fieldName.AsCharPtr());
77 if (TYPE::Traits::field_descriptions[i] != nullptr)
78 {
79 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
80 {
81 if (ImGui::BeginTooltip())
82 {
83 ImGui::TextDisabled(TYPE::Traits::field_typenames[i]);
84 ImGui::Text(TYPE::Traits::field_descriptions[i]);
85 ImGui::EndTooltip();
86 }
87 }
88 }
89 ImGui::TableSetColumnIndex(1);
90 ComponentDrawFuncT<field_type>(component, (byte*)data + TYPE::Traits::field_byte_offsets[i], commit);
91
92 if constexpr (i < TYPE::Traits::num_fields - 1)
93 {
94 ImGui::TableNextRow();
95 InspectorDrawField<TYPE, i + 1>(component, data, commit);
96 }
97 }
98 }
99}
100
101//------------------------------------------------------------------------------
104template<typename TYPE>
105inline void
106ComponentDrawFuncT(ComponentId component, void* data, bool* commit)
107{
108 if constexpr (requires { &TYPE::Traits::num_fields; })
109 {
110 if constexpr (TYPE::Traits::num_fields > 0 && !std::is_enum<TYPE>())
111 {
112 InspectorDrawField<TYPE>(component, data, commit);
113 }
114 }
115 return;
116}
117
118template<> void ComponentDrawFuncT<Game::Entity>(ComponentId, void*, bool*);
119template<> void ComponentDrawFuncT<bool>(ComponentId, void*, bool*);
120template<> void ComponentDrawFuncT<int>(ComponentId, void*, bool*);
121template<> void ComponentDrawFuncT<int64>(ComponentId, void*, bool*);
122template<> void ComponentDrawFuncT<uint>(ComponentId, void*, bool*);
123template<> void ComponentDrawFuncT<uint64>(ComponentId, void*, bool*);
124template<> void ComponentDrawFuncT<float>(ComponentId, void*, bool*);
125template<> void ComponentDrawFuncT<Util::StringAtom>(ComponentId, void*, bool*);
126template<> void ComponentDrawFuncT<Math::mat4>(ComponentId, void*, bool*);
127template<> void ComponentDrawFuncT<Math::vec3>(ComponentId, void*, bool*);
128template<> void ComponentDrawFuncT<Math::vec4>(ComponentId, void*, bool*);
129template<> void ComponentDrawFuncT<Math::quat>(ComponentId, void*, bool*);
130template<> void ComponentDrawFuncT<Game::Position>(ComponentId, void*, bool*);
131template<> void ComponentDrawFuncT<Game::Orientation>(ComponentId, void*, bool*);
132template<> void ComponentDrawFuncT<Game::Scale>(ComponentId, void*, bool*);
133template<> void ComponentDrawFuncT<Util::Color>(ComponentId, void*, bool*);
134
135} // namespace Game
Component inspection functions.
Definition componentinspection.h:29
Util::Array< DrawFunc > inspectors
Definition componentinspection.h:49
ComponentInspection()
Definition componentinspection.cc:88
static ComponentInspection * Singleton
Definition componentinspection.h:47
static void Register(ComponentId component, DrawFunc)
Definition componentinspection.cc:55
bool debug
Definition componentinspection.h:41
void(*)(Game::ComponentId, void *, bool *) DrawFunc
Definition componentinspection.h:31
static void DrawInspector(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:74
static void Destroy()
This static method is used to destroy the registry object and should be called right before the main ...
Definition componentinspection.cc:42
~ComponentInspection()
Definition componentinspection.cc:96
static ComponentInspection * Instance()
The registry's constructor is called by the Instance() method, and nobody else.
Definition componentinspection.cc:25
Nebula's dynamic array class.
Definition array.h:60
Game::EditorState.
Definition graphicsmanager.h:60
MemDb::AttributeId ComponentId
Definition componentid.h:15
void ComponentDrawFuncT< Game::Entity >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:106
void ComponentDrawFuncT< Math::mat4 >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:230
void ComponentDrawFuncT(ComponentId component, void *data, bool *commit)
Definition componentinspection.h:106
void ComponentDrawFuncT< bool >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:122
void ComponentDrawFuncT< Game::Position >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:288
void ComponentDrawFuncT< Util::StringAtom >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:200
void ComponentDrawFuncT< uint64 >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:174
void ComponentDrawFuncT< int64 >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:148
void ComponentDrawFuncT< int >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:135
void ComponentDrawFuncT< Math::vec4 >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:262
void ComponentDrawFuncT< Game::Orientation >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:305
void ComponentDrawFuncT< float >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:187
void InspectorDrawField(ComponentId component, void *data, bool *commit)
Definition componentinspection.h:58
void ComponentDrawFuncT< Math::quat >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:275
void ComponentDrawFuncT< Util::Color >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:357
void ComponentDrawFuncT< uint >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:161
void ComponentDrawFuncT< Game::Scale >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:322
void ComponentDrawFuncT< Math::vec3 >(ComponentId component, void *data, bool *commit)
Definition componentinspection.cc:249
Definition attributeid.h:19
Nebula's universal string class.
Definition string.h:50
void Capitalize()
convert first char of string to upper case
Definition string.cc:941
const char * AsCharPtr() const
return contents as character pointer
Definition string.h:542
void CamelCaseToWords()
insert spaces before each capital letter in the string.
Definition string.cc:951