ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
VectorEdit.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 
13 #include "visualization/gui/Util.h"
14 
15 namespace cloudViewer {
16 namespace visualization {
17 namespace gui {
18 
19 namespace {
20 static int g_next_vector_edit_id = 1;
21 }
22 
24  std::string id_;
25  Eigen::Vector3f value_;
26  bool is_unit_vector_ = false;
27  std::function<void(const Eigen::Vector3f&)> on_changed_;
28 };
29 
31  impl_->id_ = "##vectoredit_" + std::to_string(g_next_vector_edit_id++);
32 }
33 
35 
36 Eigen::Vector3f VectorEdit::GetValue() const { return impl_->value_; }
37 
38 void VectorEdit::SetValue(const Eigen::Vector3f& val) {
39  if (impl_->is_unit_vector_) {
40  impl_->value_ = val.normalized();
41  } else {
42  impl_->value_ = val;
43  }
44 }
45 
47  std::function<void(const Eigen::Vector3f&)> on_changed) {
48  impl_->on_changed_ = on_changed;
49 }
50 
52  const Constraints& constraints) const {
53  auto em = std::ceil(ImGui::GetTextLineHeight());
54  auto padding = ImGui::GetStyle().FramePadding;
55  return Size(Widget::DIM_GROW, int(std::ceil(em + 2.0f * padding.y)));
56 }
57 
59  auto& frame = GetFrame();
60  ImGui::SetCursorScreenPos(
61  ImVec2(float(frame.x), float(frame.y) - ImGui::GetScrollY()));
62 
63  ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding,
64  0.0); // macOS doesn't round text editing
65 
66  ImGui::PushStyleColor(
67  ImGuiCol_FrameBg,
68  colorToImgui(context.theme.text_edit_background_color));
69  ImGui::PushStyleColor(
70  ImGuiCol_FrameBgHovered,
71  colorToImgui(context.theme.text_edit_background_color));
72  ImGui::PushStyleColor(
73  ImGuiCol_FrameBgActive,
74  colorToImgui(context.theme.text_edit_background_color));
75 
78  ImGui::PushItemWidth(float(GetFrame().width));
79  if (ImGui::InputFloat3(impl_->id_.c_str(), impl_->value_.data())) {
81  }
82  ImGui::PopItemWidth();
85 
86  ImGui::PopStyleColor(3);
87  ImGui::PopStyleVar();
88 
89  if (ImGui::IsItemDeactivatedAfterEdit()) {
90  if (impl_->on_changed_) {
91  impl_->on_changed_(impl_->value_);
92  }
94  }
95 
96  return result;
97 }
98 
99 } // namespace gui
100 } // namespace visualization
101 } // namespace cloudViewer
Rect frame
int width
core::Tensor result
Definition: VtkUtils.cpp:76
Size CalcPreferredSize(const LayoutContext &context, const Constraints &constraints) const override
Definition: VectorEdit.cpp:51
Widget::DrawResult Draw(const DrawContext &context) override
Definition: VectorEdit.cpp:58
void SetValue(const Eigen::Vector3f &val)
Sets the value of the widget. Does not call onValueChanged.
Definition: VectorEdit.cpp:38
void SetOnValueChanged(std::function< void(const Eigen::Vector3f &)> on_changed)
Definition: VectorEdit.cpp:46
virtual const Rect & GetFrame() const
Returns the frame size in pixels.
Definition: Widget.cpp:51
static constexpr int DIM_GROW
Definition: Widget.h:83
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(const Eigen::Vector3f &)> on_changed_
Definition: VectorEdit.cpp:27