Elaztek Developer Hub
Blamite Game Engine - blam!  00406.12.10.23.1457.blamite
The core library for the Blamite Game Engine.
blam_ui_editor.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
7 
12 {
13 private:
14  int active_widget_index = -1;
15 
16  bool show_add_widget_dialog = false;
17  bool add_widget_to_group = false;
18  BlamWidgetType selected_widget_type = BlamWidgetType::Basic_Rectangle;
19  std::string widget_type_name = "rectangle";
20 
21  BlamUIWidget_Group* ui_root = nullptr;
22 
23 public:
25  {
27  }
28 
30  {
31  ui_root = _ui_root;
32  }
33 
35  {
36  ImGui::OpenPopup("Add Widget");
37  if (ImGui::BeginPopupModal("Add Widget", &show_add_widget_dialog))
38  {
39  if (ImGui::BeginCombo("widget type", widget_type_name.c_str()))
40  {
41  if (ImGui::Selectable("basic_rectangle"))
42  {
43  selected_widget_type = BlamWidgetType::Basic_Rectangle;
44  widget_type_name = "basic_rectangle";
45  }
46 
47  //if (ImGui::Selectable("basic_ellipse"))
48  //{
49  // selected_widget_type = BlamWidgetType::Basic_Ellipse;
50  // widget_type_name = "basic_ellipse";
51  //}
52 
53  if (ImGui::Selectable("group"))
54  {
55  selected_widget_type = BlamWidgetType::Group;
56  widget_type_name = "group";
57  }
58 
59  if (ImGui::Selectable("text"))
60  {
61  selected_widget_type = BlamWidgetType::Text;
62  widget_type_name = "text";
63  }
64 
65  if (ImGui::Selectable("ellipse"))
66  {
67  selected_widget_type = BlamWidgetType::Basic_Ellipse;
68  widget_type_name = "ellipse";
69  }
70 
71  if (ImGui::Selectable("triangle"))
72  {
73  selected_widget_type = BlamWidgetType::Basic_Triangle;
74  widget_type_name = "triangle";
75  }
76 
77  if (ImGui::Selectable("line"))
78  {
79  selected_widget_type = BlamWidgetType::Basic_Line;
80  widget_type_name = "line";
81  }
82 
83  if (ImGui::Selectable("vlayout"))
84  {
85  selected_widget_type = BlamWidgetType::Group_VLayout;
86  widget_type_name = "vlayout";
87  }
88 
89  if (ImGui::Selectable("hlayout"))
90  {
91  selected_widget_type = BlamWidgetType::Group_HLayout;
92  widget_type_name = "hlayout";
93  }
94 
96  }
97 
98  if (ImGui::Button("OK"))
99  {
100  if (Blam::UI::GetUIRoot())
101  {
102  BlamUIWidget* new_widget = nullptr;
103 
104  if (selected_widget_type == BlamWidgetType::Basic_Rectangle)
105  {
106  new_widget = new BlamUIWidget_BasicRectangle(ui_root);
107  }
108  else if (selected_widget_type == BlamWidgetType::Group)
109  {
110  new_widget = new BlamUIWidget_Group(ui_root);
111  }
112  else if (selected_widget_type == BlamWidgetType::Text)
113  {
114  new_widget = new BlamUIWidget_Text(ui_root);
115  }
116  else if (selected_widget_type == BlamWidgetType::Basic_Ellipse)
117  {
118  new_widget = new BlamUIWidget_BasicEllipse(ui_root);
119  }
120  else if (selected_widget_type == BlamWidgetType::Basic_Triangle)
121  {
122  new_widget = new BlamUIWidget_BasicTriangle(ui_root);
123  }
124  else if (selected_widget_type == BlamWidgetType::Basic_Line)
125  {
126  new_widget = new BlamUIWidget_BasicLine(ui_root);
127  }
128  else if (selected_widget_type == BlamWidgetType::Group_VLayout)
129  {
130  new_widget = new BlamUIWidget_VLayoutGroup(ui_root);
131  }
132  else if (selected_widget_type == BlamWidgetType::Group_HLayout)
133  {
134 
135  }
136  else
137  {
138  // unimplemented widget type
139  }
140 
141  if (new_widget)
142  {
143  ui_root->children.push_back(new_widget);
144  }
145  else
146  {
147  // failed to create new ui widget
148  }
149  }
150 
151  show_add_widget_dialog = false;
152  }
153 
154  ImGui::SameLine();
155 
156  if (ImGui::Button("Cancel"))
157  {
158  show_add_widget_dialog = false;
159  }
160 
161  ImGui::EndPopup();
162  }
163  }
164 
165  void Draw()
166  {
167  if (!ui_root)
168  {
170  }
171 
172  if (show)
173  {
174  std::string window_title = "";
175 
176  if (ui_root)
177  {
178  window_title = "Blam UI Editor (context: " + ui_root->display_name + ")";
179  }
180  else
181  {
182  window_title = "Blam UI Editor (no root available, root was nullptr)";
183  }
184 
185  if (ImGui::Begin(window_title.c_str(), &show))
186  {
187  if (ui_root)
188  {
189  if (ImGui::Button("add widget"))
190  {
191  show_add_widget_dialog = true;
192  }
193 
194  if (active_widget_index >= 0 && active_widget_index < ui_root->children.size())
195  {
196  ImGui::SameLine();
197 
198  if (ImGui::Button("remove widget"))
199  {
200  BlamUIWidget* widget_to_remove = ui_root->children.at(active_widget_index);
201  ui_root->children.erase(ui_root->children.begin() + active_widget_index);
202  delete widget_to_remove;
203 
204  active_widget_index = -1;
205  }
206 
207  if ((active_widget_index - 1) >= 0)
208  {
209  ImGui::SameLine();
210 
211  if (ImGui::Button("move widget up"))
212  {
213  int new_pos = active_widget_index - 1;
214 
215  BlamUIWidget* widget_to_move = ui_root->children.at(active_widget_index);
216  ui_root->children.erase(ui_root->children.begin() + active_widget_index);
217 
218  ui_root->children.insert(ui_root->children.begin() + new_pos, widget_to_move);
219 
220  active_widget_index--;
221  }
222  }
223 
224  if ((active_widget_index + 1) < ui_root->children.size())
225  {
226  ImGui::SameLine();
227 
228  if (ImGui::Button("move widget down"))
229  {
230  int new_pos = active_widget_index + 1;
231 
232  BlamUIWidget* widget_to_move = ui_root->children.at(active_widget_index);
233  ui_root->children.erase(ui_root->children.begin() + active_widget_index);
234 
235  ui_root->children.insert(ui_root->children.begin() + new_pos, widget_to_move);
236 
237  active_widget_index++;
238  }
239  }
240  }
241 
243 
244  ImGui::Columns(2);
245 
246  ImGui::SetColumnWidth(0, (200.0f * *Blam::Globals::GetGlobalAsFloat("ui_scale_factor")));
247 
249  {
250  for (int i = 0; i < ui_root->children.size(); i++)
251  {
252  BlamUIWidget* widget = ui_root->children.at(i);
253  std::string selectable_item_name = std::to_string(i) + ": " + widget->display_name;
254  bool is_active_item = false;
255 
256  if (i == active_widget_index)
257  {
258  is_active_item = true;
259  }
260 
261  if (ImGui::Selectable(selectable_item_name.c_str(), is_active_item))
262  {
263  active_widget_index = i;
264  }
265 
266  if (ImGui::IsItemHovered())
267  {
269  ImGui::PushTextWrapPos(450.0f);
270  ImGui::Text("widget information");
272 
273  std::string display_name = "display name: " + widget->display_name;
274  std::string description = "description: " + widget->description;
275  std::string index = "index: " + std::to_string(i);
276 
277  ImGui::Text(display_name.c_str());
278  ImGui::Text(description.c_str());
279  ImGui::Text(index.c_str());
280 
283  }
284  }
285 
287  }
288 
290 
291  if (active_widget_index < 0)
292  {
293  ImGui::Text("select a widget to modify");
294  }
295  else if (active_widget_index > ui_root->children.size())
296  {
297  std::string message = "selected index " + std::to_string(active_widget_index) +
298  " was too high (max is " + std::to_string(ui_root->children.size()) + ")";
299 
300  ImGui::Text(message.c_str());
301  }
302  else
303  {
304  BlamUIWidget* widget = ui_root->children.at(active_widget_index);
305 
306  if (ImGui::BeginTabBar("widget_editor_tabs"))
307  {
308  if (ImGui::BeginTabItem("widget information"))
309  {
310  std::string display_name = "display name: " + widget->display_name;
311  std::string description = "description: " + widget->description;
312  std::string index = "index: " + std::to_string(active_widget_index);
313 
314  ImGui::Text(display_name.c_str());
315  ImGui::Text(description.c_str());
316  ImGui::Text(index.c_str());
317 
319  }
320 
321  if (ImGui::BeginTabItem("widget properties"))
322  {
323  widget->ShowImGuiPropertyEditor();
324 
326  }
327 
328 
330  }
331  }
332  }
333  }
334  ImGui::End();
335 
336  if (show_add_widget_dialog)
337  {
339  }
340 
341  if (ui_root)
342  {
343  for (int i = 0; i < ui_root->children.size(); i++)
344  {
345  BlamUIWidget* widget = ui_root->children.at(i);
346 
347  if (widget->GetType() == BlamWidgetType::Group || widget->GetType() == BlamWidgetType::Group_VLayout
348  || widget->GetType() == BlamWidgetType::Group_HLayout)
349  {
350  BlamUIWidget_Group* group_widget = (BlamUIWidget_Group*)widget;
351  group_widget->ShowImGuiEditorWindow();
352  }
353  }
354  }
355  }
356  }
357 };
ui.h
BlamUIWidget_BasicTriangle
Class representing a basic triangle widget.
Definition: ui.h:457
BlamUIWidget_Group
Class representing a Group widget.
Definition: ui.h:263
ImGui::BeginTooltip
IMGUI_API void BeginTooltip()
Definition: imgui.cpp:7362
ImGui::EndPopup
IMGUI_API void EndPopup()
Definition: imgui.cpp:7675
Blam::UI::GetUIRoot
BLAM BlamUIWidget_Group * GetUIRoot()
Retrieves the root UI group widget.
Definition: ui.cpp:64
BlamImGuiWindow_BlamUIEditor::Draw
void Draw()
Draws the contents of the group.
Definition: blam_ui_editor.hpp:165
BlamImGuiWindow::show
bool show
Controls whether or not the group should be shown. May not be used in all groups.
Definition: imgui.h:34
ImGui::BeginPopupModal
IMGUI_API bool BeginPopupModal(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:7647
ImGui::ListBoxFooter
IMGUI_API void ListBoxFooter()
Definition: imgui_widgets.cpp:5668
BlamWidgetType::Text
@ Text
A text object, used to show text on-screen.
BlamWidgetType::Basic_Rectangle
@ Basic_Rectangle
A basic filled or outline rectangle.
BlamWidgetType::Basic_Ellipse
@ Basic_Ellipse
A basic filled or outline ellipse.
ImGui::NextColumn
IMGUI_API void NextColumn()
Definition: imgui_widgets.cpp:7445
BlamUIWidget_Group::ShowImGuiEditorWindow
void ShowImGuiEditorWindow()
Displays the separate ImGUI editor window for this group.
Definition: group.cpp:185
BlamWidgetType::Group_VLayout
@ Group_VLayout
A group widget which displays all items in a vertical list.
Blam::Globals::GetGlobalAsFloat
BLAM float * GetGlobalAsFloat(std::string name)
Retrieves a global's value as a float.
Definition: globals.cpp:403
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:6016
ImGui::SetColumnWidth
IMGUI_API void SetColumnWidth(int column_index, float width)
Definition: imgui_widgets.cpp:7297
BlamUIWidget::GetType
BlamWidgetType GetType()
Retrieves the type of this widget.
Definition: BlamUIWidget.cpp:207
BlamImGuiWindow_BlamUIEditor::BlamImGuiWindow_BlamUIEditor
BlamImGuiWindow_BlamUIEditor(BlamUIWidget_Group *_ui_root)
Definition: blam_ui_editor.hpp:29
ImGui::EndTabItem
IMGUI_API void EndTabItem()
Definition: imgui_widgets.cpp:6851
ImGui::SameLine
IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f)
Definition: imgui.cpp:7147
BlamImGuiWindow
Class representing an ImGUI window.
Definition: imgui.h:31
ImVec2
Definition: imgui.h:179
ImGui::IsItemHovered
IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags=0)
Definition: imgui.cpp:3061
BlamUIWidget::ShowImGuiPropertyEditor
virtual void ShowImGuiPropertyEditor()
Displays an ImGUI-based property editor.
Definition: BlamUIWidget.cpp:57
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:5397
BlamImGuiWindow_BlamUIEditor
Class for the Blam UI Editor debug utility.
Definition: blam_ui_editor.hpp:11
BlamUIWidget_Group::children
std::vector< BlamUIWidget * > children
The list of child widgets within this group.
Definition: ui.h:270
BlamUIWidget::display_name
std::string display_name
The display name of the widget.
Definition: ui.h:118
ImGui::OpenPopup
IMGUI_API void OpenPopup(const char *str_id)
Definition: imgui.cpp:7453
ImGui::GetColumnWidth
IMGUI_API float GetColumnWidth(int column_index=-1)
Definition: imgui_widgets.cpp:7262
ImGui::BeginTabBar
IMGUI_API bool BeginTabBar(const char *str_id, ImGuiTabBarFlags flags=0)
Definition: imgui_widgets.cpp:6366
ImGui::Text
IMGUI_API void Text(const char *fmt,...) IM_FMTARGS(1)
Definition: imgui_widgets.cpp:238
ImGui::PopTextWrapPos
IMGUI_API void PopTextWrapPos()
Definition: imgui.cpp:6313
ImGui::EndTooltip
IMGUI_API void EndTooltip()
Definition: imgui.cpp:7402
ImGui::BeginCombo
IMGUI_API bool BeginCombo(const char *label, const char *preview_value, ImGuiComboFlags flags=0)
Definition: imgui_widgets.cpp:1416
ImGui::Selectable
IMGUI_API bool Selectable(const char *label, bool selected=false, ImGuiSelectableFlags flags=0, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui_widgets.cpp:5469
globals.h
ImGui::Columns
IMGUI_API void Columns(int count=1, const char *id=NULL, bool border=true)
Definition: imgui_widgets.cpp:7572
BlamWidgetType::Basic_Triangle
@ Basic_Triangle
A basic filled or outline triangle.
BlamUIWidget_BasicRectangle
Class representing a basic rectangle widget.
Definition: ui.h:402
BlamWidgetType::Group_HLayout
@ Group_HLayout
A group widget which displays all items in a horizontal list.
BlamUIWidget
Base class for a UI widget.
Definition: ui.h:87
ImGui::EndCombo
IMGUI_API void EndCombo()
Definition: imgui_widgets.cpp:1522
ui_root
BlamUIWidget_Group * ui_root
The UI root widget.
Definition: ui.cpp:13
ImGui::EndTabBar
IMGUI_API void EndTabBar()
Definition: imgui_widgets.cpp:6432
BlamWidgetType::Group
@ Group
A group widget, which can be used to contain any number of other widgets.
BlamUIWidget::description
std::string description
An optional description of the widget.
Definition: ui.h:119
BlamUIWidget_VLayoutGroup
Class representing a Vertical Layout Group widget.
Definition: ui.h:313
BlamWidgetType::Basic_Line
@ Basic_Line
A basic line.
BlamImGuiWindow_BlamUIEditor::BlamImGuiWindow_BlamUIEditor
BlamImGuiWindow_BlamUIEditor()
Definition: blam_ui_editor.hpp:24
BlamUIWidget_BasicEllipse
Class representing a basic ellipse widget.
Definition: ui.h:419
imgui.h
BlamUIWidget_BasicLine
Class representing a basic line widget.
Definition: ui.h:438
ImGui::Separator
IMGUI_API void Separator()
Definition: imgui_widgets.cpp:1284
BlamWidgetType
BlamWidgetType
Enumerator listing possible UI widget types.
Definition: ui.h:60
ImGui::PushTextWrapPos
IMGUI_API void PushTextWrapPos(float wrap_local_pos_x=0.0f)
Definition: imgui.cpp:6306
ImGui::BeginTabItem
IMGUI_API bool BeginTabItem(const char *label, bool *p_open=NULL, ImGuiTabItemFlags flags=0)
Definition: imgui_widgets.cpp:6829
ImGui::ListBoxHeader
IMGUI_API bool ListBoxHeader(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui_widgets.cpp:5614
ImGui::Button
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui_widgets.cpp:644
BlamUIWidget_Text
Class representing a Text widget.
Definition: ui.h:483
BlamImGuiWindow_BlamUIEditor::ShowAddPrimitivePopup
void ShowAddPrimitivePopup()
Definition: blam_ui_editor.hpp:34