From ee6245a6f99f707190de36b17c11079b618cd812 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 6 Aug 2020 22:05:48 +0300
Subject: [PATCH] Add popup tools

---
 .../gui2/src/modules/camera_tools.hpp         |  2 +
 applications/gui2/src/views/camera.cpp        | 44 ++++++++++++++++++-
 applications/gui2/src/views/camera.hpp        |  3 ++
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/applications/gui2/src/modules/camera_tools.hpp b/applications/gui2/src/modules/camera_tools.hpp
index 78182b770..5616a89aa 100644
--- a/applications/gui2/src/modules/camera_tools.hpp
+++ b/applications/gui2/src/modules/camera_tools.hpp
@@ -19,6 +19,8 @@ enum class CameraTools {
 	MOVE_CURSOR,			// Move 3D Cursor
 	ROTATE_CURSOR,
 	ORIGIN_TO_CURSOR,
+	RESET_ORIGIN,
+	SAVE_CURSOR,
 	ROTATE_X,
 	ROTATE_Y,
 	ROTATE_Z,
diff --git a/applications/gui2/src/views/camera.cpp b/applications/gui2/src/views/camera.cpp
index c7416b7de..a1a3592c9 100644
--- a/applications/gui2/src/views/camera.cpp
+++ b/applications/gui2/src/views/camera.cpp
@@ -397,7 +397,15 @@ ToolPanel::ToolPanel(nanogui::Widget *parent, ftl::gui2::Camera* ctrl, CameraVie
 	_addButton(CameraTools::ZOOM_IN, ENTYPO_ICON_CIRCLE_WITH_PLUS, "Zoom In (+)");
 	_addButton(CameraTools::ZOOM_OUT, ENTYPO_ICON_CIRCLE_WITH_MINUS, "Zoom Out (-)");
 	_addButton(CameraTools::INSPECT_POINT, ENTYPO_ICON_MAGNIFYING_GLASS, "Inspect Point");
-	_addButton(CameraTools::ORIGIN_TO_CURSOR, ENTYPO_ICON_LOCATION, "Origin to 3D Cursor");
+	//_addButton(CameraTools::ORIGIN_TO_CURSOR, ENTYPO_ICON_LOCATION, "Origin to 3D Cursor");
+	auto *cur_but = _addButton({
+		CameraTools::ORIGIN_TO_CURSOR,
+		CameraTools::RESET_ORIGIN,
+		CameraTools::SAVE_CURSOR
+	}, ENTYPO_ICON_LOCATION, "Use Cursor");
+	_addButton(cur_but, CameraTools::ORIGIN_TO_CURSOR, "Origin to Cursor");
+	_addButton(cur_but, CameraTools::RESET_ORIGIN, "Reset Origin");
+	_addButton(cur_but, CameraTools::SAVE_CURSOR, "Save Cursor as Pose");
 	_addButton(CameraTools::OVERLAY, ENTYPO_ICON_LINE_GRAPH, "Show/Hide Overlay");
 	_addButton(CameraTools::CLIPPING, ENTYPO_ICON_SCISSORS, "Enable/Disable Clipping");
 
@@ -424,7 +432,7 @@ ToolPanel::~ToolPanel() {
 void ToolPanel::_addButton(ftl::gui2::CameraTools tool, int icon, const std::string &tooltip) {
 	auto *b = new nanogui::Button(container_, "", icon);
 	b->setTooltip(tooltip);
-	b->setFlags(nanogui::Button::Flags::RadioButton);
+	//b->setFlags(nanogui::Button::Flags::RadioButton);
 	b->setCallback([this, tool]() {
 		active_ = tool;
 		for (auto &f : callbacks_) {
@@ -434,6 +442,38 @@ void ToolPanel::_addButton(ftl::gui2::CameraTools tool, int icon, const std::str
 	buttons_[tool] = b;
 }
 
+void ToolPanel::_addButton(ftl::gui2::PopupButton *parent, ftl::gui2::CameraTools tool, const std::string &label) {
+	auto *b = new nanogui::Button(parent->popup(), label);
+	b->setCallback([this, parent, tool]() {
+		parent->setPushed(false);
+		active_ = tool;
+		for (auto &f : callbacks_) {
+			if (f(tool)) break;
+		}
+	});
+	//buttons_[tool] = b;
+}
+
+ftl::gui2::PopupButton *ToolPanel::_addButton(std::unordered_set<ftl::gui2::CameraTools> tools, int icon, const std::string &tooltip) {
+	auto *b = new ftl::gui2::PopupButton(container_, "", icon);
+	b->setTooltip(tooltip);
+	b->setSide(nanogui::Popup::Side::Left);
+	b->setChevronIcon(0);
+	
+	for (auto t : tools) {
+		buttons_[t] = b;
+	}
+
+	auto *popup = b->popup();
+	popup->setLayout(new nanogui::BoxLayout
+		(nanogui::Orientation::Vertical, nanogui::Alignment::Fill, 10, 6));
+
+	auto theme = dynamic_cast<ftl::gui2::Screen*>(screen())->getTheme("media_small");
+	popup->setTheme(theme);
+
+	return b;
+}
+
 void ToolPanel::setTool(ftl::gui2::CameraTools tool) {
 	active_ = tool;
 	if (buttons_.count(tool)) {
diff --git a/applications/gui2/src/views/camera.hpp b/applications/gui2/src/views/camera.hpp
index 6f12c96bf..da1dc3db7 100644
--- a/applications/gui2/src/views/camera.hpp
+++ b/applications/gui2/src/views/camera.hpp
@@ -7,6 +7,7 @@
 #include "../widgets/window.hpp"
 #include "../widgets/soundctrl.hpp"
 #include "../widgets/imageview.hpp"
+#include "../widgets/popupbutton.hpp"
 #include "../modules/camera_tools.hpp"
 
 namespace ftl {
@@ -47,6 +48,8 @@ private:
 	std::list<std::function<bool(ftl::gui2::CameraTools)>> callbacks_;
 
 	void _addButton(ftl::gui2::CameraTools, int icon, const std::string &tooltip);
+	void _addButton(ftl::gui2::PopupButton *parent, ftl::gui2::CameraTools, const std::string &label);
+	ftl::gui2::PopupButton *_addButton(std::unordered_set<ftl::gui2::CameraTools> tools, int icon, const std::string &tooltip);
 
 public:
 	EIGEN_MAKE_ALIGNED_OPERATOR_NEW
-- 
GitLab