libigl v2.5.0
Loading...
Searching...
No Matches
ImGuiHelpers.h
Go to the documentation of this file.
1// This file is part of libigl, a simple c++ geometry processing library.
2//
3// Copyright (C) 2018 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
4//
5// This Source Code Form is subject to the terms of the Mozilla Public License
6// v. 2.0. If a copy of the MPL was not distributed with this file, You can
7// obtain one at http://mozilla.org/MPL/2.0/.
8#ifndef IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
9#define IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
10
12#include "ImGuiTraits.h"
13#include <imgui.h>
14#include <vector>
15#include <string>
16#include <algorithm>
17#include <functional>
18#include <cstddef>
20
21// Extend ImGui by populating its namespace directly
22//
23// Code snippets taken from there:
24// https://eliasdaler.github.io/using-imgui-with-sfml-pt2/
25namespace ImGui
26{
27
28static auto vector_getter = [](void* vec, int idx, const char** out_text)
29{
30 auto& vector = *static_cast<std::vector<std::string>*>(vec);
31 if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
32 *out_text = vector.at(idx).c_str();
33 return true;
34};
35
36inline bool Combo(const char* label, int* idx, std::vector<std::string>& values)
37{
38 if (values.empty()) { return false; }
39 return Combo(label, idx, vector_getter,
40 static_cast<void*>(&values), values.size());
41}
42
43inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
44{
45 auto func = [](void* data, int i, const char** out_text) {
46 auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
47 const char *s = getter(i);
48 if (s) { *out_text = s; return true; }
49 else { return false; }
50 };
51 return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
52}
53
54inline bool ListBox(const char* label, int* idx, std::vector<std::string>& values)
55{
56 if (values.empty()) { return false; }
57 return ListBox(label, idx, vector_getter,
58 static_cast<void*>(&values), values.size());
59}
60
61inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL)
62{
63 char buf[1024];
64 std::fill_n(buf, 1024, 0);
65 std::copy_n(str.begin(), std::min(1024, (int) str.size()), buf);
66 if (ImGui::InputText(label, buf, 1024, flags, callback, user_data))
67 {
68 str = std::string(buf);
69 return true;
70 }
71 return false;
72}
73
74// template<typename T>
75// inline bool DragScalar(const char *label, T* value, void* v, float v_speed, const void* v_min = NULL, const void* v_max = NULL, const char* format = NULL, float power = 1.0f)
76// {
77// const char *fmt = format;
78// if (format == nullptr) {
79// fmt = ImGuiDataTypeTraits<T>::format;
80// }
81// return DragScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
82// }
83
84// template<typename T>
85// inline bool InputScalar(const char *label, T* value, T min = 0, T max = 0, const char* format = nulltptr)
86// {
87// const char *fmt = format;
88// if (format == nullptr) {
89// fmt = ImGuiDataTypeTraits<T>::format;
90// }
91// return InputScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
92// }
93
94template<typename T>
95inline bool SliderScalar(const char *label, T* value, T min = 0, T max = 0, const char* format = "")
96{
97 const char *fmt = format;
98 if (format == nullptr) {
100 }
101 return SliderScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
102}
103
104template<typename Getter, typename Setter>
105inline bool Checkbox(const char* label, Getter get, Setter set)
106{
107 bool value = get();
108 bool ret = ImGui::Checkbox(label, &value);
109 set(value);
110 return ret;
111}
112
113} // namespace ImGui
114
115#endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
Definition ImGuiTraits.h:20
Definition ImGuiHelpers.h:26
bool Combo(const char *label, int *idx, std::vector< std::string > &values)
Definition ImGuiHelpers.h:36
bool Checkbox(const char *label, Getter get, Setter set)
Definition ImGuiHelpers.h:105
bool SliderScalar(const char *label, T *value, T min=0, T max=0, const char *format="")
Definition ImGuiHelpers.h:95
bool InputText(const char *label, std::string &str, ImGuiInputTextFlags flags=0, ImGuiInputTextCallback callback=NULL, void *user_data=NULL)
Definition ImGuiHelpers.h:61
bool ListBox(const char *label, int *idx, std::vector< std::string > &values)
Definition ImGuiHelpers.h:54