emilib
imgui_helpers.hpp
1 // By Emil Ernerfeldt 2015-2016
2 // LICENSE:
3 // This software is dual-licensed to the public domain and under the following
4 // license: you are granted a perpetual, irrevocable license to copy, modify,
5 // publish, and distribute this file as you see fit.
6 
7 #pragma once
8 
9 #include <cmath>
10 #include <cstdint>
11 #include <string>
12 #include <vector>
13 
14 #define IM_VEC2_CLASS_EXTRA \
15  friend ImVec2 operator+(const ImVec2& a, const ImVec2& b) { return {a.x + b.x, a.y + b.y}; } \
16  friend ImVec2 operator-(const ImVec2& a, const ImVec2& b) { return {a.x - b.x, a.y - b.y}; } \
17  friend ImVec2 operator*(const ImVec2& v, float s) { return {v.x * s, v.y * s}; } \
18  friend ImVec2 operator*(float s, const ImVec2& v) { return {v.x * s, v.y * s}; } \
19  friend bool operator==(const ImVec2& a, const ImVec2& b) { return a.x == b.x && a.y == b.y; } \
20  void operator*=(float s) { x *= s; y *= s; } \
21  void operator/=(float s) { x /= s; y /= s; } \
22  float norm() { return std::hypot(x, y); }
23 
24 #define IM_VEC4_CLASS_EXTRA \
25  friend bool operator==(const ImVec4& a, const ImVec4& b) { \
26  return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; } \
27 
28 #include <imgui/imgui.h>
29 
30 using GLuint = uint32_t;
31 
32 namespace imgui_helpers {
33 
35 ImVec2 aspect_correct_image_size(const ImVec2& desired_size, const ImVec2& canvas_size, const ImVec2& minimum_size);
36 
37 void gl_texture(GLuint tex_id, const ImVec2& size);
38 
39 } // namespace imgui_helpers
40 
41 // ----------------------------------------------------------------------------
42 
44 namespace ImGuiPP {
45 
46 bool SliderSize(const std::string& label, size_t* v, size_t v_min, size_t v_max, float power = 1.0f);
47 
48 bool InputText(const std::string& label, std::string& text, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);
49 void Text(const std::string& text);
50 void LabelText(const std::string& label, const std::string& text);
51 bool Button(const std::string& text);
52 
53 bool ListBox(const std::string& label, std::string& current_item, const std::vector<std::string>& items, int height_in_items = -1);
54 bool Combo(const std::string& label, std::string& current_item, const std::vector<std::string>& items, int height_in_items = -1);
55 
57 template<typename Enum>
58 bool RadioButtonEnum(const char* label, Enum* v, Enum v_button)
59 {
60  if (ImGui::RadioButton(label, *v == v_button)) {
61  *v = v_button;
62  return true;
63  } else {
64  return false;
65  }
66 }
67 
68 } // namespace ImGuiPP
Definition: imgui_helpers.hpp:32
bool RadioButtonEnum(const char *label, Enum *v, Enum v_button)
Convenience for enums.
Definition: imgui_helpers.hpp:58
Helper C++ bindings for ImGui.
Definition: imgui_helpers.hpp:44