Elaztek Developer Hub
Blamite Game Engine - blam!  00346.12.11.21.0529.blamite
The core library for the Blamite Game Engine.
blam_ui_editor.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../../debug_ui.h"
4 
6 
8 {
13  {
14  private:
15  int active_widget_index = -1;
16 
17  bool show_add_widget_dialog = false;
18  bool add_widget_to_group = false;
19  BlamWidgetType selected_widget_type = BlamWidgetType::Basic_Rectangle;
20  std::string widget_type_name = "rectangle";
21 
22  BlamUIWidget_Group* ui_root = nullptr;
23 
24  public:
26  {
28  }
29 
31  {
32  ui_root = _ui_root;
33  }
34 
36  {
37  ImGui::OpenPopup("Add Widget");
38  if (ImGui::BeginPopupModal("Add Widget", &show_add_widget_dialog))
39  {
40  if (ImGui::BeginCombo("widget type", widget_type_name.c_str()))
41  {
42  if (ImGui::Selectable("basic_rectangle"))
43  {
44  selected_widget_type = BlamWidgetType::Basic_Rectangle;
45  widget_type_name = "basic_rectangle";
46  }
47 
48  //if (ImGui::Selectable("basic_ellipse"))
49  //{
50  // selected_widget_type = BlamWidgetType::Basic_Ellipse;
51  // widget_type_name = "basic_ellipse";
52  //}
53 
54  if (ImGui::Selectable("group"))
55  {
56  selected_widget_type = BlamWidgetType::Group;
57  widget_type_name = "group";
58  }
59 
61  }
62 
63  if (ImGui::Button("OK"))
64  {
65  if (Blam::UI::GetUIRoot())
66  {
67  BlamUIWidget* new_widget = nullptr;
68 
69  if (selected_widget_type == BlamWidgetType::Basic_Rectangle)
70  {
71  new_widget = new BlamUIWidget_BasicRectangle(ui_root);
72  }
73  else if (selected_widget_type == BlamWidgetType::Group)
74  {
75  new_widget = new BlamUIWidget_Group(ui_root);
76  }
77  else
78  {
79  // unimplemented widget type
80  }
81 
82  if (new_widget)
83  {
84  ui_root->children.push_back(new_widget);
85  }
86  else
87  {
88  // failed to create new ui widget
89  }
90  }
91 
92  show_add_widget_dialog = false;
93  }
94 
96 
97  if (ImGui::Button("Cancel"))
98  {
99  show_add_widget_dialog = false;
100  }
101 
102  ImGui::EndPopup();
103  }
104  }
105 
106  void Draw()
107  {
108  if (!ui_root)
109  {
111  }
112 
113  if (show)
114  {
115  std::string window_title = "";
116 
117  if (ui_root)
118  {
119  window_title = "Blam UI Editor (context: " + ui_root->display_name + ")";
120  }
121  else
122  {
123  window_title = "Blam UI Editor (no root available, root was nullptr)";
124  }
125 
126  if (ImGui::Begin(window_title.c_str(), &show))
127  {
128  if (ui_root)
129  {
130  if (ImGui::Button("add widget"))
131  {
132  show_add_widget_dialog = true;
133  }
134 
135  if (active_widget_index >= 0 && active_widget_index < ui_root->children.size())
136  {
137  ImGui::SameLine();
138 
139  if (ImGui::Button("remove widget"))
140  {
141  BlamUIWidget* widget_to_remove = ui_root->children.at(active_widget_index);
142  ui_root->children.erase(ui_root->children.begin() + active_widget_index);
143  delete widget_to_remove;
144 
145  active_widget_index = -1;
146  }
147 
148  if ((active_widget_index - 1) >= 0)
149  {
150  ImGui::SameLine();
151 
152  if (ImGui::Button("move widget up"))
153  {
154  int new_pos = active_widget_index - 1;
155 
156  BlamUIWidget* widget_to_move = ui_root->children.at(active_widget_index);
157  ui_root->children.erase(ui_root->children.begin() + active_widget_index);
158 
159  ui_root->children.insert(ui_root->children.begin() + new_pos, widget_to_move);
160 
161  active_widget_index--;
162  }
163  }
164 
165  if ((active_widget_index + 1) < ui_root->children.size())
166  {
167  ImGui::SameLine();
168 
169  if (ImGui::Button("move widget down"))
170  {
171  int new_pos = active_widget_index + 1;
172 
173  BlamUIWidget* widget_to_move = ui_root->children.at(active_widget_index);
174  ui_root->children.erase(ui_root->children.begin() + active_widget_index);
175 
176  ui_root->children.insert(ui_root->children.begin() + new_pos, widget_to_move);
177 
178  active_widget_index++;
179  }
180  }
181  }
182 
184 
185  ImGui::Columns(2);
186 
187  ImGui::SetColumnWidth(0, (200.0f * *Blam::Globals::GetGlobalAsFloat("ui_scale_factor")));
188 
190  {
191  for (int i = 0; i < ui_root->children.size(); i++)
192  {
193  BlamUIWidget* widget = ui_root->children.at(i);
194  std::string selectable_item_name = std::to_string(i) + ": " + widget->display_name;
195  bool is_active_item = false;
196 
197  if (i == active_widget_index)
198  {
199  is_active_item = true;
200  }
201 
202  if (ImGui::Selectable(selectable_item_name.c_str(), is_active_item))
203  {
204  active_widget_index = i;
205  }
206 
207  if (ImGui::IsItemHovered())
208  {
210  ImGui::PushTextWrapPos(450.0f);
211  ImGui::Text("widget information");
213 
214  std::string display_name = "display name: " + widget->display_name;
215  std::string description = "description: " + widget->description;
216  std::string index = "index: " + std::to_string(i);
217 
218  ImGui::Text(display_name.c_str());
219  ImGui::Text(description.c_str());
220  ImGui::Text(index.c_str());
221 
224  }
225  }
226 
228  }
229 
231 
232  if (active_widget_index < 0)
233  {
234  ImGui::Text("select a widget to modify");
235  }
236  else if (active_widget_index > ui_root->children.size())
237  {
238  std::string message = "selected index " + std::to_string(active_widget_index) +
239  " was too high (max is " + std::to_string(ui_root->children.size()) + ")";
240 
241  ImGui::Text(message.c_str());
242  }
243  else
244  {
245  BlamUIWidget* widget = ui_root->children.at(active_widget_index);
246 
247  if (ImGui::BeginTabBar("widget_editor_tabs"))
248  {
249  if (ImGui::BeginTabItem("widget information"))
250  {
251  std::string display_name = "display name: " + widget->display_name;
252  std::string description = "description: " + widget->description;
253  std::string index = "index: " + std::to_string(active_widget_index);
254 
255  ImGui::Text(display_name.c_str());
256  ImGui::Text(description.c_str());
257  ImGui::Text(index.c_str());
258 
260  }
261 
262  if (ImGui::BeginTabItem("widget properties"))
263  {
264  widget->ShowImGuiPropertyEditor();
265 
267  }
268 
269 
271  }
272  }
273  }
274  }
275  ImGui::End();
276 
277  if (show_add_widget_dialog)
278  {
280  }
281 
282  if (ui_root)
283  {
284  for (int i = 0; i < ui_root->children.size(); i++)
285  {
286  BlamUIWidget* widget = ui_root->children.at(i);
287 
288  if (widget->GetType() == BlamWidgetType::Group)
289  {
290  BlamUIWidget_Group* group_widget = (BlamUIWidget_Group*)widget;
291  group_widget->ShowImGuiEditorWindow();
292  }
293  }
294  }
295  }
296  }
297  };
298 }
ui.h
BlamUIWidget_Group
Definition: ui.h:92
ImGui::BeginTooltip
IMGUI_API void BeginTooltip()
Definition: imgui.cpp:7362
ImGui::EndPopup
IMGUI_API void EndPopup()
Definition: imgui.cpp:7675
Blam::DebugUI::Windows::BlamUIEditor::ShowAddPrimitivePopup
void ShowAddPrimitivePopup()
Definition: blam_ui_editor.hpp:35
Blam::UI::GetUIRoot
BLAM BlamUIWidget_Group * GetUIRoot()
Definition: ui.cpp:47
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::Basic_Rectangle
@ Basic_Rectangle
ImGui::NextColumn
IMGUI_API void NextColumn()
Definition: imgui_widgets.cpp:7445
BlamUIWidget_Group::ShowImGuiEditorWindow
void ShowImGuiEditorWindow()
Definition: BlamUIWidget_Group.cpp:90
Blam::Globals::GetGlobalAsFloat
BLAM float * GetGlobalAsFloat(std::string name)
Retrieves a global's value as a float.
Definition: globals.cpp:407
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()
Definition: BlamUIWidget.cpp:147
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
Blam::DebugUI::Windows::BlamUIEditor::BlamUIEditor
BlamUIEditor()
Definition: blam_ui_editor.hpp:25
Blam::DebugUI::Windows::BlamUIEditor::BlamUIEditor
BlamUIEditor(BlamUIWidget_Group *_ui_root)
Definition: blam_ui_editor.hpp:30
ImVec2
Definition: imgui.h:179
Blam::DebugUI::Windows::BlamUIEditor::Draw
void Draw()
Draws the contents of the group.
Definition: blam_ui_editor.hpp:106
ImGui::IsItemHovered
IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags=0)
Definition: imgui.cpp:3061
BlamUIWidget::ShowImGuiPropertyEditor
virtual void ShowImGuiPropertyEditor()
Definition: BlamUIWidget.cpp:57
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:5397
BlamUIWidget_Group::children
std::vector< BlamUIWidget * > children
Definition: ui.h:98
BlamUIWidget::display_name
std::string display_name
Definition: ui.h:63
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
Blam::DebugUI::ImGUIDrawingGroup::show
bool show
Controls whether or not the group should be shown. May not be used in all groups.
Definition: debug_ui.h:362
Blam::DebugUI::ImGUIDrawingGroup
Class representing an ImGUI drawing group/draw list item.
Definition: debug_ui.h:359
ImGui::Columns
IMGUI_API void Columns(int count=1, const char *id=NULL, bool border=true)
Definition: imgui_widgets.cpp:7572
BlamUIWidget_BasicRectangle
Definition: ui.h:110
Blam::DebugUI::Windows::BlamUIEditor
Class for the Blam UI Editor debug utility.
Definition: blam_ui_editor.hpp:12
BlamUIWidget
Definition: ui.h:50
ImGui::EndCombo
IMGUI_API void EndCombo()
Definition: imgui_widgets.cpp:1522
ui_root
BlamUIWidget_Group * ui_root
Definition: ui.cpp:6
ImGui::EndTabBar
IMGUI_API void EndTabBar()
Definition: imgui_widgets.cpp:6432
BlamWidgetType::Group
@ Group
BlamUIWidget::description
std::string description
Definition: ui.h:64
ImGui::Separator
IMGUI_API void Separator()
Definition: imgui_widgets.cpp:1284
BlamWidgetType
BlamWidgetType
Definition: ui.h:40
Blam::DebugUI::Windows
Legacy namespace to contain data for the legacy ImGUI console.
Definition: ui.h:14
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