-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUiHelper.h
198 lines (161 loc) · 6.05 KB
/
UiHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once
#include "MathGens.h"
#include "GlitterEnums.h"
#include "CommandManager.h"
#include "PropertyCommands.h"
#include "ImGui/imgui.h"
#include "ImGui/imgui_stdlib.h"
#include "UI.h"
namespace Glitter
{
namespace Editor
{
static void beginPropertyColumn()
{
ImGui::Columns(2, 0, false);
ImGui::SetColumnWidth(0, ImGui::GetWindowSize().x / 3.0f);
}
static void endPropertyColumn()
{
ImGui::Columns(1);
}
static void propertyLabel(const char* lbl)
{
ImGui::Text(lbl);
ImGui::NextColumn();
ImGui::SetNextItemWidth(-1);
}
template <typename T>
static void addTextProperty(const char* label, std::string val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(std::string)> func)
{
static std::string old;
propertyLabel(label);
if (ImGui::InputText(std::string("##").append(label).c_str(), &val))
func(obj.get(), val);
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, val, func));
}
template <typename T>
static void addIntProperty(const char* label, int val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(int)> func)
{
static int old;
propertyLabel(label);
if (ImGui::InputInt(std::string("##").append(label).c_str(), &val, 1, 10))
func(obj.get(), val);
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, val, func));
}
template <typename T>
static void addUIntProperty(const char* label, unsigned int val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(unsigned int)> func)
{
static unsigned int old;
int num = val;
propertyLabel(label);
if (ImGui::InputInt(std::string("##").append(label).c_str(), &num, 1, 10))
{
if (num < 0)
num = 0;
func(obj.get(), num);
}
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != num)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, (unsigned int)num, func));
}
template <typename T>
static void addFloatProperty(const char* label, float val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(float)> func)
{
static float old;
propertyLabel(label);
if (ImGui::InputFloat(std::string("##").append(label).c_str(), &val, 1.0f, 5.0f, "%g"))
func(obj.get(), val);
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, val, func));
}
template <typename T>
static void addVector2Property(const char* label, Glitter::Vector2 val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(Glitter::Vector2)> func)
{
float v2[2] = { val.x, val.y };
static Glitter::Vector2 old;
propertyLabel(label);
if (ImGui::DragFloat2(std::string("##").append(label).c_str(), v2, 0.1f, 0.0f, 0.0f, "%g"))
func(obj.get(), Glitter::Vector2(v2[0], v2[1]));
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, Glitter::Vector2(v2[0], v2[1]), func));
}
template <typename T>
static void addVector3Property(const char* label, Glitter::Vector3 val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(Glitter::Vector3)> func)
{
float v3[3] = { val.x, val.y, val.z };
static Glitter::Vector3 old;
propertyLabel(label);
if (ImGui::DragFloat3(std::string("##").append(label).c_str(), v3, 0.1f, 0.0f, 0.0f, "%g"))
func(obj.get(), Glitter::Vector3(v3[0], v3[1], v3[2]));
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, Glitter::Vector3(v3[0], v3[1], v3[2]), func));
}
template <typename T>
static void addColorProperty(const char* label, Glitter::Color val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(Glitter::Color)> func)
{
float v4[4] = { val.r, val.g, val.b, val.a };
static Glitter::Color old;
propertyLabel(label);
ImGuiColorEditFlags flags = ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_Float | ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_DisplayRGB;
flags |= ImGuiColorEditFlags_PickerHueWheel;
if (ImGui::ColorEdit4(std::string("##").append(label).c_str(), v4, flags))
func(obj.get(), Glitter::Color(v4[0], v4[1], v4[2], v4[3]));
ImGui::NextColumn();
if (ImGui::IsItemActivated())
old = val;
if (ImGui::IsItemDeactivated() && old != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, old, Glitter::Color(v4[0], v4[1], v4[2], v4[3]), func));
}
template <typename T, typename S>
static void addComboBoxProperty(const char* label, const std::string* items, const size_t count, S val, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(S)> func)
{
int selectedIndex = size_t(val);
std::string comboLabel = Glitter::glitterEnumToString(items, count, (size_t)val);
propertyLabel(label);
if (ImGui::BeginCombo(std::string("##").append(label).c_str(), comboLabel.c_str()))
{
for (int n = 0; n < count; ++n)
{
const bool selected = selectedIndex == n;
if (ImGui::Selectable(items[n].c_str(), selected))
{
selectedIndex = n;
if ((S)(selectedIndex) != val)
CommandManager::pushNew(new ChangePropertyCommand(label, obj, val, S(selectedIndex), func));
}
if (selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::NextColumn();
}
template <typename T>
static void addFlagsProperty(const char* label, unsigned int flags, unsigned int flagVal, std::shared_ptr<T>& obj, std::_Mem_fn<void (T::*)(unsigned int)> func)
{
unsigned int f = flags;
if (ImGui::CheckboxFlags(label, &f, flagVal))
CommandManager::pushNew(new ChangePropertyCommand(label, obj, flags, f, func));
}
}
}