ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Checkbox.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 <cmath>
13 #include <string>
14 
16 #include "visualization/gui/Util.h"
17 
18 namespace cloudViewer {
19 namespace visualization {
20 namespace gui {
21 
22 namespace {
23 static int g_next_checkbox_id = 1;
24 }
25 
27  std::string name_;
28  std::string id_;
29  bool is_checked_ = false;
30  std::function<void(bool)> on_checked_;
31 };
32 
33 Checkbox::Checkbox(const char* name) : impl_(new Checkbox::Impl()) {
34  impl_->name_ = name;
35  impl_->id_ =
36  impl_->name_ + "##checkbox_" + std::to_string(g_next_checkbox_id++);
37 }
38 
40 
41 bool Checkbox::IsChecked() const { return impl_->is_checked_; }
42 
43 void Checkbox::SetChecked(bool checked) { impl_->is_checked_ = checked; }
44 
45 void Checkbox::SetOnChecked(std::function<void(bool)> on_checked) {
46  impl_->on_checked_ = on_checked;
47 }
48 
50  const Constraints& constraints) const {
51  auto em = ImGui::GetTextLineHeight();
52  auto padding = ImGui::GetStyle().FramePadding;
53  auto text_size = ImGui::GetFont()->CalcTextSizeA(
54  float(context.theme.font_size), 10000, 10000, impl_->name_.c_str());
55  int height = int(std::ceil(em + 2.0f * padding.y));
56  auto checkbox_width = height + padding.x;
57  return Size(int(checkbox_width + std::ceil(text_size.x + 2.0f * padding.x)),
58  height);
59 }
60 
62  auto& frame = GetFrame();
63  ImGui::SetCursorScreenPos(
64  ImVec2(float(frame.x), float(frame.y) - ImGui::GetScrollY()));
66 
67  // ImGUI doesn't offer styling specific to checkboxes other than the
68  // color of the checkmark, so we need to adjust the colors ourselves.
69  if (impl_->is_checked_) {
70  ImGui::PushStyleColor(
71  ImGuiCol_FrameBg,
72  colorToImgui(context.theme.checkbox_background_on_color));
73  ImGui::PushStyleColor(
74  ImGuiCol_FrameBgHovered,
75  colorToImgui(context.theme.checkbox_background_hover_on_color));
76  } else {
77  ImGui::PushStyleColor(
78  ImGuiCol_FrameBg,
79  colorToImgui(context.theme.checkbox_background_off_color));
80  ImGui::PushStyleColor(
81  ImGuiCol_FrameBgHovered,
83  context.theme.checkbox_background_hover_off_color));
84  }
85 
87  ImGui::PushItemWidth(float(GetFrame().width));
88  if (ImGui::Checkbox(impl_->id_.c_str(), &impl_->is_checked_)) {
89  if (impl_->on_checked_) {
90  impl_->on_checked_(impl_->is_checked_);
91  }
93  }
94  ImGui::PopItemWidth();
97 
98  ImGui::PopStyleColor(2);
99 
100  return result;
101 }
102 
103 } // namespace gui
104 } // namespace visualization
105 } // namespace cloudViewer
Rect frame
int width
std::string name
int height
core::Tensor result
Definition: VtkUtils.cpp:76
DrawResult Draw(const DrawContext &context) override
Definition: Checkbox.cpp:61
Size CalcPreferredSize(const LayoutContext &context, const Constraints &constraints) const override
Definition: Checkbox.cpp:49
void SetOnChecked(std::function< void(bool)> on_checked)
Definition: Checkbox.cpp:45
virtual const Rect & GetFrame() const
Returns the frame size in pixels.
Definition: Widget.cpp:51
ImGuiContext * context
Definition: Window.cpp:76
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
ImVec4 colorToImgui(const Color &color)
Definition: Util.cpp:20
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20
std::function< void(bool)> on_checked_
Definition: Checkbox.cpp:30