From 3830667543150228de7d9c48c682aeb9182e3e42 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 1 Feb 2019 21:33:05 -0500 Subject: [PATCH] Add event::DoubleClick. Change ParamWidget reset to double-click. --- include/app/ParamWidget.hpp | 1 + include/app/Switch.hpp | 1 + include/event.hpp | 9 +++++++++ include/widget/Widget.hpp | 1 + src/app/ParamWidget.cpp | 16 ++++++---------- src/app/Switch.cpp | 4 ++++ src/event.cpp | 14 ++++++++++++++ 7 files changed, 36 insertions(+), 10 deletions(-) diff --git a/include/app/ParamWidget.hpp b/include/app/ParamWidget.hpp index b0f64565..43e76a54 100644 --- a/include/app/ParamWidget.hpp +++ b/include/app/ParamWidget.hpp @@ -19,6 +19,7 @@ struct ParamWidget : widget::OpaqueWidget { void step() override; void draw(const widget::DrawContext &ctx) override; void onButton(const event::Button &e) override; + void onDoubleClick(const event::DoubleClick &e) override; void onEnter(const event::Enter &e) override; void onLeave(const event::Leave &e) override; diff --git a/include/app/Switch.hpp b/include/app/Switch.hpp index f25103bb..190d3a82 100644 --- a/include/app/Switch.hpp +++ b/include/app/Switch.hpp @@ -16,6 +16,7 @@ struct Switch : ParamWidget { bool momentaryReleased = false; void step() override; + void onDoubleClick(const event::DoubleClick &e) override; void onDragStart(const event::DragStart &e) override; void onDragEnd(const event::DragEnd &e) override; }; diff --git a/include/event.hpp b/include/event.hpp index aa40bfcf..9e422dfa 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -86,6 +86,12 @@ struct Button : Event, Position { }; +/** Occurs when the left mouse button is pressed the second time within a time and position window. +*/ +struct DoubleClick : Event { +}; + + /** Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget. Recurses until consumed. */ @@ -246,6 +252,9 @@ struct State { widget::Widget *selectedWidget = NULL; /** For middle-click dragging */ widget::Widget *scrollWidget = NULL; + /** For double-clicking */ + double lastClickTime = -INFINITY; + math::Vec lastClickPos; void setHovered(widget::Widget *w); void setDragged(widget::Widget *w); diff --git a/include/widget/Widget.hpp b/include/widget/Widget.hpp index 8b60db38..91bac5de 100644 --- a/include/widget/Widget.hpp +++ b/include/widget/Widget.hpp @@ -122,6 +122,7 @@ struct Widget { */ virtual void onHover(const event::Hover &e) {recursePositionEvent(&Widget::onHover, e);} virtual void onButton(const event::Button &e) {recursePositionEvent(&Widget::onButton, e);} + virtual void onDoubleClick(const event::DoubleClick &e) {} virtual void onHoverKey(const event::HoverKey &e) {recursePositionEvent(&Widget::onHoverKey, e);} virtual void onHoverText(const event::HoverText &e) {recursePositionEvent(&Widget::onHoverText, e);} virtual void onHoverScroll(const event::HoverScroll &e) {recursePositionEvent(&Widget::onHoverScroll, e);} diff --git a/src/app/ParamWidget.cpp b/src/app/ParamWidget.cpp index 6e2cf51c..ade459f4 100644 --- a/src/app/ParamWidget.cpp +++ b/src/app/ParamWidget.cpp @@ -90,7 +90,7 @@ struct ParamResetItem : ui::MenuItem { ParamWidget *paramWidget; ParamResetItem() { text = "Initialize"; - rightText = WINDOW_MOD_SHIFT_NAME "+Click"; + rightText = "Double-click"; } void onAction(const event::Action &e) override { paramWidget->resetAction(); @@ -101,7 +101,7 @@ struct ParamResetItem : ui::MenuItem { struct ParamFineItem : ui::MenuItem { ParamFineItem() { text = "Fine adjust"; - rightText = WINDOW_MOD_CTRL_NAME "+Drag"; + rightText = WINDOW_MOD_CTRL_NAME "+drag"; disabled = true; } }; @@ -149,18 +149,14 @@ void ParamWidget::onButton(const event::Button &e) { e.consume(this); } - // Shift-click to reset - if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_LEFT && (e.mods & WINDOW_MOD_MASK) == GLFW_MOD_SHIFT) { - resetAction(); - // HACK so that dragging won't occur - e.consume(NULL); - return; - } - if (!e.getConsumed()) widget::OpaqueWidget::onButton(e); } +void ParamWidget::onDoubleClick(const event::DoubleClick &e) { + resetAction(); +} + void ParamWidget::onEnter(const event::Enter &e) { if (settings::paramTooltip && !tooltip && paramQuantity) { ParamTooltip *paramTooltip = new ParamTooltip; diff --git a/src/app/Switch.cpp b/src/app/Switch.cpp index 74e7a40b..e33812e4 100644 --- a/src/app/Switch.cpp +++ b/src/app/Switch.cpp @@ -23,6 +23,10 @@ void Switch::step() { ParamWidget::step(); } +void Switch::onDoubleClick(const event::DoubleClick &e) { + // Don't reset parameter on double-click +} + void Switch::onDragStart(const event::DragStart &e) { if (momentary) { if (paramQuantity) { diff --git a/src/event.cpp b/src/event.cpp index e6031770..597bdf7e 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -133,6 +133,20 @@ void State::handleButton(math::Vec pos, int button, int action, int mods) { if (action == GLFW_PRESS) { setSelected(clickedWidget); } + + if (action == GLFW_PRESS) { + const double doubleClickDuration = 0.5; + const float doubleClickDistance = 10; + double clickTime = glfwGetTime(); + if (clickTime - lastClickTime <= doubleClickDuration && pos.minus(lastClickPos).norm() <= doubleClickDistance) { + + // event::DoubleClick + event::DoubleClick eDoubleClick; + clickedWidget->onDoubleClick(eDoubleClick); + } + lastClickTime = clickTime; + lastClickPos = pos; + } } // if (button == GLFW_MOUSE_BUTTON_MIDDLE) {