ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Slider.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <imgui.h>
11 
12 #include <algorithm>
13 #include <cmath>
14 
16 
17 namespace cloudViewer {
18 namespace visualization {
19 namespace gui {
20 
21 namespace {
22 static int g_next_slider_id = 1;
23 }
24 
25 struct Slider::Impl {
27  std::string id_;
28  // A double has 53-bits of integer value, which should be enough for
29  // anything we want a slider for. A slider isn't really useful for
30  // a range of 2^53 anyway.
31  double value_ = 0.0;
32  double min_value_ = -1e35;
33  double max_value_ = 1e35;
34  std::function<void(double)> on_value_changed_;
35 };
36 
37 Slider::Slider(Type type) : impl_(new Slider::Impl()) {
38  impl_->id_ = "##slider_" + std::to_string(g_next_slider_id++);
39  impl_->type_ = type;
40 }
41 
43 
44 int Slider::GetIntValue() const { return int(impl_->value_); }
45 
46 double Slider::GetDoubleValue() const { return impl_->value_; }
47 
48 void Slider::SetValue(double val) {
49  impl_->value_ =
50  std::max(impl_->min_value_, std::min(impl_->max_value_, val));
51  if (impl_->type_ == INT) {
52  impl_->value_ = std::round(impl_->value_);
53  }
54 }
55 
56 double Slider::GetMinimumValue() const { return impl_->min_value_; }
57 
58 double Slider::GetMaximumValue() const { return impl_->max_value_; }
59 
60 void Slider::SetLimits(double min_value, double max_value) {
61  impl_->min_value_ = min_value;
62  impl_->max_value_ = max_value;
63  if (impl_->type_ == INT) {
64  impl_->min_value_ = std::round(impl_->min_value_);
65  impl_->max_value_ = std::round(impl_->max_value_);
66  }
67  SetValue(impl_->value_); // make sure value is within new limits
68 }
69 
70 void Slider::SetOnValueChanged(std::function<void(double)> on_value_changed) {
71  impl_->on_value_changed_ = on_value_changed;
72 }
73 
75  const Constraints& constraints) const {
76  auto line_height = ImGui::GetTextLineHeight();
77  auto height = line_height + 2.0 * ImGui::GetStyle().FramePadding.y;
78 
79  return Size(Widget::DIM_GROW, int(std::ceil(height)));
80 }
81 
83  auto& frame = GetFrame();
84  ImGui::SetCursorScreenPos(
85  ImVec2(float(frame.x), float(frame.y) - ImGui::GetScrollY()));
86 
87  auto new_value = impl_->value_;
89  ImGui::PushItemWidth(float(GetFrame().width));
90  if (impl_->type_ == INT) {
91  int i_new_value = int(new_value);
92  ImGui::SliderInt(impl_->id_.c_str(), &i_new_value,
93  int(impl_->min_value_), int(impl_->max_value_));
94  new_value = double(i_new_value);
95  } else {
96  float f_new_value = float(new_value);
97  ImGui::SliderFloat(impl_->id_.c_str(), &f_new_value,
98  float(impl_->min_value_), float(impl_->max_value_));
99  new_value = double(f_new_value);
100  }
101  ImGui::PopItemWidth();
104 
105  if (impl_->value_ != new_value) {
106  impl_->value_ = new_value;
107  if (impl_->on_value_changed_) {
108  impl_->on_value_changed_(new_value);
109  }
111  }
113 }
114 
115 } // namespace gui
116 } // namespace visualization
117 } // namespace cloudViewer
Rect frame
int width
int height
char type
DrawResult Draw(const DrawContext &context) override
Definition: Slider.cpp:82
Size CalcPreferredSize(const LayoutContext &theme, const Constraints &constraints) const override
Definition: Slider.cpp:74
int GetIntValue() const
Returns the value of the control as an integer.
Definition: Slider.cpp:44
void SetLimits(double min_value, double max_value)
Definition: Slider.cpp:60
double GetDoubleValue() const
Returns the value of the control as a double.
Definition: Slider.cpp:46
void SetOnValueChanged(std::function< void(double)> on_value_changed)
Sets a function to call when the value changes because of user action.
Definition: Slider.cpp:70
virtual const Rect & GetFrame() const
Returns the frame size in pixels.
Definition: Widget.cpp:51
static constexpr int DIM_GROW
Definition: Widget.h:83
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
ImGuiContext * context
Definition: Window.cpp:76
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20
std::function< void(double)> on_value_changed_
Definition: Slider.cpp:34