LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Table.h
1#pragma once
2
3#include "UICore.h"
4#include "Property.h"
5
6#include "LkEngine/Editor/EditorGlobals.h" /* For UI::Debug namespace, should be changed in the future. */
7
8namespace LkEngine::UI {
9
10 FORCEINLINE void BeginPropertyGrid(const char* Label = nullptr,
11 const ImVec2& Size = ImVec2(0.0f, 0.0f),
12 ImGuiTableFlags Flags = ImGuiTableFlags_SizingStretchProp,
13 const bool UseHeaderLabels = false)
14 {
15 UI::PushID();
16
17 UIContext.bInGrid = true;
18
19 if (UI::Debug::GridBorders & EBorder::Horizontal) Flags |= ImGuiTableFlags_BordersH;
20 if (UI::Debug::GridBorders & EBorder::Vertical) Flags |= ImGuiTableFlags_BordersV;
21 if (UI::Debug::GridBorders & EBorder::All) Flags |= ImGuiTableFlags_Borders;
22
23 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8.0f, 8.0f));
24 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(4.0f, 4.0f));
25 if (!ImGui::BeginTable(Label ? Label : GenerateID(), 2, Flags, Size))
26 {
27 return;
28 }
29
30 static constexpr ImGuiTableColumnFlags ColumnFlags = ImGuiTableColumnFlags_None;
31 for (uint32_t ColumnIdx = 0; ColumnIdx < 2; ColumnIdx++)
32 {
33 if (UseHeaderLabels)
34 {
35 ImGui::TableSetupColumn(LK_FMT_LIB::format("Column-{}", ColumnIdx).c_str(), ColumnFlags);
36 }
37 else
38 {
39 ImGui::TableSetupColumn(LK_FMT_LIB::format("##Column-{}", ColumnIdx).c_str(), ColumnFlags);
40 }
41 }
42
43 if (UseHeaderLabels)
44 {
45 ImGui::TableHeadersRow();
46 }
47 ImGui::TableNextRow();
48 }
49
50 FORCEINLINE void EndPropertyGrid()
51 {
52 ImGui::EndTable();
53 ImGui::PopStyleVar(2); /* ItemSpacing, FramePadding */
54
55 UIContext.bInGrid = false;
56
57 UI::PopID();
58 }
59
60 FORCEINLINE bool PropertyGridHeader(const std::string& Name, bool OpenByDefault = true)
61 {
62 ImGuiTreeNodeFlags TreeNodeFlags = ImGuiTreeNodeFlags_Framed
63 | ImGuiTreeNodeFlags_SpanAvailWidth
64 | ImGuiTreeNodeFlags_AllowItemOverlap
65 | ImGuiTreeNodeFlags_FramePadding;
66
67 if (OpenByDefault)
68 {
69 TreeNodeFlags |= ImGuiTreeNodeFlags_DefaultOpen;
70 }
71
72 static constexpr float FramePaddingX = 6.0f;
73 static constexpr float FramePaddingY = 6.0f; // affects height of the header
74
75 UI::FScopedStyle HeaderRounding(ImGuiStyleVar_FrameRounding, 0.0f);
76 UI::FScopedStyle HeaderPaddingAndHeight(ImGuiStyleVar_FramePadding, ImVec2(FramePaddingX, FramePaddingY));
77
78 ImGui::PushID(Name.c_str());
79 UI::Font::Push("Bold");
80 const bool IsOpen = ImGui::TreeNodeEx("##DummyID", TreeNodeFlags, Name.c_str());
81 UI::Font::Pop();
82 ImGui::PopID();
83
84 return IsOpen;
85 }
86
87 FORCEINLINE void EndPropertyGridHeader()
88 {
89 ImGui::TreePop();
90 }
91
92 template<typename T>
93 static void Table(const char* TableName,
94 const char** Columns,
95 const uint32_t ColumnCount,
96 const ImVec2& Size,
97 T RenderFunc)
98 {
99 if ((Size.x <= 0.0f) || (Size.y <= 0.0f))
100 {
101 return;
102 }
103
104 static constexpr float EdgeOffset = 4.0f;
105
106 FScopedStyle CellPadding(ImGuiStyleVar_CellPadding, ImVec2(4.0f, 0.0f));
107 const ImColor BgColor = ImColor(RGBA32::Background);
108 const ImColor ColRowAlt = ColorWithMultipliedValue(BgColor, 1.29f);
109
110 FScopedColor RowColor(ImGuiCol_TableRowBg, BgColor);
111 FScopedColor RowAltColor(ImGuiCol_TableRowBgAlt, ColRowAlt);
112 FScopedColor TableColor(ImGuiCol_ChildBg, BgColor);
113
114 static constexpr ImGuiTableFlags TableFlags = ImGuiTableFlags_NoPadInnerX
115 | ImGuiTableFlags_Resizable
116 | ImGuiTableFlags_Reorderable
117 | ImGuiTableFlags_ScrollY
118 | ImGuiTableFlags_RowBg;
119
120 if (!ImGui::BeginTable(TableName, ColumnCount, TableFlags, Size))
121 {
122 return;
123 }
124
125 /* Cache the cursor position on the X-axis. */
126 const float CursorX = ImGui::GetCursorScreenPos().x;
127
128 for (uint32_t Idx = 0; Idx < ColumnCount; Idx++)
129 {
130 ImGui::TableSetupColumn(Columns[Idx]);
131 }
132
133 /* Setup table headers. */
134 {
135 const ImColor ActiveColor = ColorWithMultipliedValue(BgColor, 1.35f);
136 FScopedColorStack HeaderCol(ImGuiCol_HeaderHovered, ActiveColor,
137 ImGuiCol_HeaderActive, ActiveColor);
138
139 ImGui::TableSetupScrollFreeze(ImGui::TableGetColumnCount(), 1);
140 ImGui::TableNextRow(ImGuiTableRowFlags_Headers, 24.0f);
141
142 for (uint32_t Idx = 0; Idx < ColumnCount; Idx++)
143 {
144 ImGui::TableSetColumnIndex(Idx);
145 const char* ColumnName = ImGui::TableGetColumnName(Idx);
146 ImGui::PushID(ColumnName);
147
152 //UI::ShiftCursor(EdgeOffset * 3.0f, EdgeOffset * 2.0f);
153 UI::ShiftCursor(EdgeOffset * 3.0f, EdgeOffset * 1.0f);
154 ImGui::TableHeader(ColumnName);
155 UI::ShiftCursor(-EdgeOffset * 3.0f, -EdgeOffset * 1.0f);
156
157 ImGui::PopID();
158 }
159
160 ImGui::SetCursorScreenPos(ImVec2(CursorX, ImGui::GetCursorScreenPos().y));
161 }
162
163 RenderFunc();
164
165 ImGui::EndTable();
166 }
167
168 template<typename T, std::size_t N>
169 static void Table(const char* TableName,
170 const std::array<const char*, N>& Columns,
171 const ImVec2& Size,
172 T RenderFunc)
173 {
174 if ((Size.x <= 0.0f) || (Size.y <= 0.0f))
175 {
176 return;
177 }
178
179 static constexpr float EdgeOffset = 4.0f;
180
181 FScopedStyle CellPadding(ImGuiStyleVar_CellPadding, ImVec2(4.0f, 0.0f));
182 const ImColor BgColor = ImColor(RGBA32::Background);
183 const ImColor ColRowAlt = ColorWithMultipliedValue(BgColor, 1.29f);
184
185 FScopedColor TableColor(ImGuiCol_ChildBg, BgColor);
186 FScopedColor RowColor(ImGuiCol_TableRowBg, BgColor);
187 FScopedColor RowAltColor(ImGuiCol_TableRowBgAlt, ColRowAlt);
188
189 static constexpr ImGuiTableFlags TableFlags = ImGuiTableFlags_NoPadInnerX
190 | ImGuiTableFlags_Resizable
191 | ImGuiTableFlags_Reorderable
192 | ImGuiTableFlags_ScrollY
193 | ImGuiTableFlags_RowBg;
194
195 if (!ImGui::BeginTable(TableName, N, TableFlags, Size))
196 {
197 return;
198 }
199
200 /* Cache the cursor position on the X-axis. */
201 const float CursorX = ImGui::GetCursorScreenPos().x;
202
203 for (uint32_t Idx = 0; Idx < N; Idx++)
204 {
205 ImGui::TableSetupColumn(Columns[Idx]);
206 }
207
208 /* Setup table headers. */
209 {
210 const ImColor ActiveColor = ColorWithMultipliedValue(BgColor, 1.35f);
211 FScopedColorStack HeaderCol(
212 ImGuiCol_HeaderHovered, ActiveColor,
213 ImGuiCol_HeaderActive, ActiveColor
214 );
215
216 ImGui::TableSetupScrollFreeze(ImGui::TableGetColumnCount(), 1);
217 ImGui::TableNextRow(ImGuiTableRowFlags_Headers, 24.0f);
218
219 for (uint32_t Idx = 0; Idx < N; Idx++)
220 {
221 ImGui::TableSetColumnIndex(Idx);
222 const char* ColumnName = ImGui::TableGetColumnName(Idx);
223 ImGui::PushID(ColumnName);
224
229 //UI::ShiftCursor(EdgeOffset * 3.0f, EdgeOffset * 2.0f);
230 UI::ShiftCursor(EdgeOffset * 3.0f, EdgeOffset * 1.0f);
231 ImGui::TableHeader(ColumnName);
232 UI::ShiftCursor(-EdgeOffset * 3.0f, -EdgeOffset * 1.0f);
233
234 ImGui::PopID();
235 }
236
237 ImGui::SetCursorScreenPos(ImVec2(CursorX, ImGui::GetCursorScreenPos().y));
238 }
239
240 RenderFunc();
241
242 ImGui::EndTable();
243 }
244
245
246 bool TableRowClickable(const char* RowID, const float RowHeight);
247
248}
Core UI.