Visual Computing Library
Loading...
Searching...
No Matches
imgui_helpers.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IMGUI_IMGUI_HELPERS_H
24#define VCL_IMGUI_IMGUI_HELPERS_H
25
26#include <functional>
27#include <imgui.h>
28
29#include <vclib/space/core/color.h>
30
31namespace ImGui {
32
33inline bool Checkbox(
34 const char* label,
35 std::function<bool()> get,
36 std::function<void(bool)> set)
37{
38 bool value = get();
39 const bool ret = ImGui::Checkbox(label, &value);
40 set(value);
41 return ret;
42}
43
44inline bool RadioButton(
45 const char* label,
46 std::function<bool()> get,
47 std::function<void(bool)> set)
48{
49 bool value = get();
50 const bool ret = ImGui::RadioButton(label, value);
51 if (ret) {
52 set(true);
53 }
54 return ret;
55}
56
57inline bool SliderFloat(
58 const char* label,
59 std::function<float()> get,
60 std::function<void(float)> set,
61 float vMin,
62 float vMax)
63{
64 float value = get();
65 const bool ret = ImGui::SliderFloat(label, &value, vMin, vMax);
66 if (ret) {
67 set(value);
68 }
69 return ret;
70}
71
72inline bool ColorEdit4(
73 const char* label,
74 std::function<vcl::Color()> get,
75 std::function<void(vcl::Color)> set,
76 ImGuiColorEditFlags flags)
77{
78 // TODO: use float based color
79 vcl::Color c = get();
80 float color[4] = {c.redF(), c.greenF(), c.blueF(), c.alphaF()};
81 const bool ret = ImGui::ColorEdit4(label, color, flags);
82 if (ret) {
83 c.setRgbF(color[0], color[1], color[2], color[3]);
84 set(c);
85 }
86 return ret;
87}
88
89} // namespace ImGui
90
91#endif // VCL_IMGUI_IMGUI_HELPERS_H
The Color class represents a 32 bit color.
Definition color.h:48
float greenF() const
Returns the float green component of this color [0-1].
Definition color.h:207
void setRgbF(float red, float green, float blue, float alpha=1.0)
Sets the RGB values of this color.
Definition color.h:559
float blueF() const
Returns the float blue component of this color [0-1].
Definition color.h:213
float alphaF() const
Returns the float alpha component of this color [0-1].
Definition color.h:219
float redF() const
Returns the float red component of this color [0-1].
Definition color.h:201