Euphoria
imgui_extra.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <utility>
5 #include <functional>
6 
7 #include "base/vec2.h"
8 #include "base/vec3.h"
9 
10 #include "imgui/imgui.h"
11 
12 namespace eu
13 {
14  struct Angle;
15  struct Rgb;
16  struct Rgba;
17  struct Rgbai;
18 }
19 
20 namespace eu::render
21 {
22  struct Texture2;
23 }
24 
25 struct ImVec2;
26 struct ImVec4;
27 
28 
29 namespace eu::window
30 {
31  enum class Corner
32  {
33  top_left = 0,
34  top_right = 1,
35  bottom_left = 2,
36  bottom_right = 3,
37  center = 4
38  };
39 
40  ImVec2
41  con(const vec2f &v);
42 
43  vec2f
44  con(const ImVec2 &v);
45 }
46 
47 namespace eu::window::imgui
48 {
49  // knob based on https://github.com/ocornut/imgui/issues/942
50 
51  enum KnobStyle
52  {
53  // interaction style
57 
58  // purely visible stuff
63 
67 
76  };
77 
78  void
79  add_help_marker_for_previous_widget(const char* desc);
80 
81  void
82  add_help_text_for_previous_widget(const char* desc);
83 
84  void
85  imgui_label(const std::string& str);
86 
87  bool
89  (
90  const char* name,
91  Angle* angle,
92  const Angle& min_deg,
93  const Angle& max_deg
94  );
95 
96  bool
98  (
99  const char* name,
100  Angle* angle
101  );
102 
103  bool
104  imgui_toggle_button(const char* label, bool down, const ImVec2& size);
105 
106 
107  ImTextureID
109 
110 
111  void
112  imgui_image(render::Texture2* texture);
113 
114  bool
115  imgui_color_edit(const char* name, Rgb* rgb);
116 
117  bool
118  imgui_color_edit(const char* name, Rgba* rgb);
119 
120  bool
121  imgui_color_edit(const char* name, Rgbai* rgb);
122 
123  bool
125  (
126  Corner corner,
127  const std::string& title,
128  float distance = 10.0f,
129  float distance_y = -1.0f
130  );
131 
133  {
136 
138  void operator=(const VisuallyDisabledWidgets&) = delete;
141  };
142 
143  bool
144  imgui_selectable_or_disabled(bool enabled, const char* label);
145 
146  bool
147  imgui_knob
148  (
149  const char* label,
150  float* p_value,
151  float v_min,
152  float v_max,
154  );
155 
156  bool
157  begin_canvas_widget(const ImVec4& background_color, const char* title);
158 
159  void
161 
162  // wrappers over dear imgui functions to avoid exposing the whole dear imgui header
163  bool
164  begin_combo(const char* label, const char* preview);
165 
166  bool
167  imgui_selectable(const char* label, bool is_selected);
168 
169  void
170  end_combo();
171 
172  template<typename T>
174  (
175  const char* label,
176  T* data,
177  std::vector<std::pair<const char*, T>> values
178  )
179  {
180  const auto found = std::find_if
181  (
182  values.begin(), values.end(),
183  [data](const std::pair<const char*, T>& p)
184  {
185  return p.
186  second == *data;
187  }
188  );
189 
190  const char* preview_value = found != values.end() ? found->first : "";
191 
192  bool was_changed = false;
193 
194  if(begin_combo(label, preview_value))
195  {
196  for(const auto& v: values)
197  {
198  if(imgui_selectable(v.first, v.second == *data))
199  {
200  *data = v.second;
201  was_changed = true;
202  }
203  }
204  end_combo();
205  }
206 
207  return was_changed;
208  }
209 
210  template <typename T, typename F>
211  void
212  imgui_custom_dropdown(const char* name, T* current, T max, F to_string)
213  {
214  if(begin_combo(name, to_string(*current).c_str()))
215  {
216  for(int index = 0; index < static_cast<int>(max); index += 1)
217  {
218  const auto o = static_cast<T>(index);
219  if(imgui_selectable(to_string(o).c_str(), *current == o))
220  {
221  *current = o;
222  }
223  }
224  end_combo();
225  }
226  }
227 }
bool imgui_angle_slider(const char *name, Angle *angle, const Angle &mindeg, const Angle &maxdeg)
Definition: imgui_extra.cc:75
bool imgui_selectable(const char *label, bool is_selected)
Definition: imgui_extra.cc:573
bool imgui_knob(const char *label, float *p_value, float v_min, float v_max, KnobStyle style)
Definition: imgui_extra.cc:358
void add_help_marker_for_previous_widget(const char *desc)
Definition: imgui_extra.cc:43
bool imgui_toggle_button(const char *label, bool down, const ImVec2 &size)
Definition: imgui_extra.cc:129
void imgui_label(const std::string &str)
Definition: imgui_extra.cc:67
void end_canvas_widget()
Definition: imgui_extra.cc:558
bool begin_combo(const char *label, const char *preview)
Definition: imgui_extra.cc:567
bool begin_canvas_widget(const ImVec4 &background_color, const char *title)
Definition: imgui_extra.cc:542
@ knob_style_vis_display_value_on_hover
Definition: imgui_extra.h:66
@ knob_style_vis_draw_background
Definition: imgui_extra.h:62
@ knob_style_vis_value_as_tooltip
Definition: imgui_extra.h:64
@ knob_style_vis_value_instead_of_name
Definition: imgui_extra.h:65
@ knob_style_vis_markers_visible
Definition: imgui_extra.h:59
@ knob_style_vis_off_marker_hidden
Definition: imgui_extra.h:61
@ knob_style_vis_max_and_min_visible
Definition: imgui_extra.h:60
bool begin_fixed_overlay(Corner corner, const std::string &title, float a_distance, float a_distance_y)
Definition: imgui_extra.cc:246
bool imgui_selectable_or_disabled(bool enabled, const char *label)
Definition: imgui_extra.cc:306
void imgui_image(render::Texture2 *texture)
Definition: imgui_extra.cc:188
bool imgui_combo(const char *label, T *data, std::vector< std::pair< const char *, T >> values)
Definition: imgui_extra.h:174
void imgui_custom_dropdown(const char *name, T *current, T max, F to_string)
Definition: imgui_extra.h:212
ImTextureID c_texture_to_imgui(render::Texture2 *texture)
Definition: imgui_extra.cc:172
bool imgui_color_edit(const char *name, Rgb *c)
Definition: imgui_extra.cc:147
void add_help_text_for_previous_widget(const char *desc)
Definition: imgui_extra.cc:52
ImVec2 con(const vec2f &v)
Definition: imgui_extra.cc:27
Definition: assert.h:90
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
Definition: rgb.h:62
Definition: rgb.h:143
Definition: rgb.h:45
Definition: vec2.h:33
VisuallyDisabledWidgets(VisuallyDisabledWidgets &&other)=delete
void operator=(VisuallyDisabledWidgets &&)=delete
VisuallyDisabledWidgets(const VisuallyDisabledWidgets &other)=delete
void operator=(const VisuallyDisabledWidgets &)=delete