Cogs.Core
VariableInspector.cpp
1#include "Inspectors.h"
2
3#include "Context.h"
4#include "Renderer/IRenderer.h"
5#include "InspectorGuiHelper.h"
6#include "Services/Variables.h"
7#include "Utilities/Parsing.h"
8
9#include "Foundation/Reflection/TypeDatabase.h"
10
11#include "imgui.h"
12
13#include <algorithm>
14
15using namespace Cogs::Reflection;
16
17namespace Cogs
18{
19 namespace Core
20 {
22 {
23 Context * context;
24 Variable * variable;
25 };
26
27 int inputCallback(ImGuiInputTextCallbackData* /*data*/)
28 {
29 return 0;
30 }
31 }
32}
33
34void Cogs::Core::variableInspector(Context * context, bool * show)
35{
36 if (*show) {
37 ImGui::SetNextWindowSize(ImVec2(600, 1000), ImGuiCond_Once);
38
39 guiBegin("Variables", show);
40
41 auto & variables = context->variables->getVariables();
42
43 std::unordered_map<std::string, std::vector<Variable *>> groupedVars;
44 std::vector<std::string> groups;
45
46 for (auto & p : variables) {
47 auto splits = split(p.second.getName(), ".");
48
49 if (splits.size() > 1) {
50 std::string name(splits[0]);
51 auto & group = groupedVars[name];
52
53 if (group.empty()) {
54 groups.emplace_back(name);
55 }
56
57 group.push_back(&p.second);
58 } else if (splits.size() > 0) {
59 auto & group = groupedVars["global"];
60
61 group.push_back(&p.second);
62 }
63 }
64
65 std::sort(groups.begin(), groups.end());
66
67 for (auto & g : groups) {
68 auto gs = groupedVars[g];
69 if (gs.size()) {
70 if (ImGui::CollapsingHeader(g.c_str())) {
71 ScopedIndent si;
72
73 // Sort by name.
74 std::sort(gs.begin(), gs.end(), [](const Variable* a, const Variable* b) { return a->getName() < b->getName(); });
75 for (auto & pp : gs) {
76 auto & variable = *pp;
77
78 auto pName = variable.getName().data();
79 std::string buffer(variable.getValue());
80 buffer.resize(128);
81
82 InputUserData userData = {
83 context,
84 &variable,
85 };
86
87 auto type = variable.getType();
88
89 switch (type) {
90 case ParsedDataType::Bool:
91 {
92 bool value = variable.getBool();
93 if (ImGui::Checkbox(pName, &value)) {
94 variable.setBool(value);
95 }
96 break;
97 }
98 case ParsedDataType::Float:
99 {
100 float value = variable.getFloat();
101 if (ImGui::DragFloat(pName, &value)) {
102 variable.setFloat(value);
103 }
104 break;
105 }
106 case ParsedDataType::Double:
107 {
108 float value = (float)variable.getDouble();
109 if (ImGui::DragFloat(pName, &value)) {
110 variable.setDouble(value);
111 }
112 break;
113 }
114 case ParsedDataType::Int:
115 {
116 int value = variable.getInt();
117 if (ImGui::DragInt(pName, &value)) {
118 variable.setInt(value);
119 }
120 break;
121 }
122 default:
123 if (ImGui::InputText(pName, buffer.data(), buffer.size(), ImGuiInputTextFlags_EnterReturnsTrue, inputCallback, &userData)) {
124
125 ImguiRenderer* guiRenderer = context->renderer->getGuiRenderer();
126 if (guiRenderer) {
127 guiRenderer->addTextToGlyphBuilder(buffer.data());
128 }
129
130 variable.setValue(buffer.c_str());
131 }
132 break;
133 }
134 }
135 }
136 }
137
138
139 }
140
141 guiEnd();
142 }
143}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Contains reflection support.
Definition: Component.h:11
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Runtime control variable.
Definition: Variables.h:27