From 1e7e65ef64eb69e12bc1649f872ee3bc18133742 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sun, 21 Aug 2016 22:23:05 +0300 Subject: [PATCH] Got ScaleWidget events captured by Gui --- src/Application/Gui.cpp | 27 ++++++++++++++++++--------- src/Application/Gui.h | 12 +++++------- src/Application/GuiBaseWidget.h | 6 ++++++ src/Application/ScaleWidget.cpp | 21 +++++++++++++++++---- src/Application/ScaleWidget.h | 6 +++++- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/Application/Gui.cpp b/src/Application/Gui.cpp index 61acf30..2296250 100644 --- a/src/Application/Gui.cpp +++ b/src/Application/Gui.cpp @@ -12,6 +12,14 @@ Gui * Gui::instance(){ return _instance; } +Gui::Gui(){ + ofAddListener(_scaleWidget.guiWidgetEvent, this, &Gui::onScaleWidgetEvent); +} + +Gui::~Gui(){ + ofRemoveListener(_scaleWidget.guiWidgetEvent, this, &Gui::onScaleWidgetEvent); +} + void Gui::notifyJointPressed(ofMouseEventArgs & args, int jointIndex){ GuiJointEvent e; e.args = args; @@ -72,18 +80,19 @@ void Gui::onMouseDragged(ofMouseEventArgs & args){ _scaleWidget.onMouseDragged(args); } -void Gui::notifyGuiWidgetEvent(ofMouseEventArgs &args, ofx::piMapper::GuiBaseWidget * widget){ - GuiWidgetEvent e; - e.args = args; - e.widget = widget; - ofNotifyEvent(guiWidgetEvent, e, this); - - cout << "args.Dragged: " << args.Dragged << endl; -} - ScaleWidget & Gui::getScaleWidget(){ return _scaleWidget; } +void Gui::onScaleWidgetEvent(GuiWidgetEvent & event){ + if(event.args.type == event.args.Pressed){ + cout << "ScaleWidget Pressed" << endl; + }else if(event.args.type == event.args.Released){ + cout << "ScaleWidget Released" << endl; + }else if(event.args.type == event.args.Dragged){ + cout << "ScaleWidget Dragged" << endl; + } +} + } // piMapper } // ofx \ No newline at end of file diff --git a/src/Application/Gui.h b/src/Application/Gui.h index a0fc2d7..1ee97f1 100644 --- a/src/Application/Gui.h +++ b/src/Application/Gui.h @@ -36,11 +36,6 @@ struct GuiBackgroundEvent{ ofMouseEventArgs args; }; -struct GuiWidgetEvent{ - ofMouseEventArgs args; - GuiBaseWidget * widget; -}; - class Gui { public: static Gui * instance(); @@ -67,14 +62,17 @@ class Gui { void notifyBackgroundPressed(ofMouseEventArgs & args); ScaleWidget & getScaleWidget(); - ofEvent guiWidgetEvent; - void notifyGuiWidgetEvent(ofMouseEventArgs & args, GuiBaseWidget * widget); void onMousePressed(ofMouseEventArgs & args); void onMouseReleased(ofMouseEventArgs & args); void onMouseDragged(ofMouseEventArgs & args); + void onScaleWidgetEvent(GuiWidgetEvent & event); + private: + Gui(); + ~Gui(); + static Gui * _instance; ScaleWidget _scaleWidget; diff --git a/src/Application/GuiBaseWidget.h b/src/Application/GuiBaseWidget.h index 0314d5c..571d3f9 100644 --- a/src/Application/GuiBaseWidget.h +++ b/src/Application/GuiBaseWidget.h @@ -5,6 +5,10 @@ namespace ofx { namespace piMapper { +struct GuiWidgetEvent{ + ofMouseEventArgs args; +}; + class GuiBaseWidget { public: virtual void setup() = 0; @@ -16,6 +20,8 @@ class GuiBaseWidget { virtual void onMouseDragged(ofMouseEventArgs & e) = 0; virtual bool inside(float x, float y) = 0; + + ofEvent guiWidgetEvent; }; } // namespace piMapper diff --git a/src/Application/ScaleWidget.cpp b/src/Application/ScaleWidget.cpp index 4ef793f..527c2ee 100644 --- a/src/Application/ScaleWidget.cpp +++ b/src/Application/ScaleWidget.cpp @@ -9,6 +9,8 @@ ScaleWidget::ScaleWidget(){ _handle.width = 20; _handle.height = 20; + + _scale = 1.0f; } void ScaleWidget::setup(){ @@ -36,24 +38,35 @@ void ScaleWidget::draw(){ void ScaleWidget::onMousePressed(ofMouseEventArgs & args){ if(_handle.inside(args.x, args.y)){ - std::cout << "ScaleWidget: Handle clicked" << std::endl; _dragging = true; + _originalLine = _line; } + + GuiWidgetEvent e; + e.args = args; + ofNotifyEvent(guiWidgetEvent, e, this); } void ScaleWidget::onMouseReleased(ofMouseEventArgs & args){ - std::cout << "ScaleWidget: Mouse released" << std::endl; _dragging = false; + + GuiWidgetEvent e; + e.args = args; + ofNotifyEvent(guiWidgetEvent, e, this); } void ScaleWidget::onMouseDragged(ofMouseEventArgs & args){ - std::cout << "ScaleWidget: Mouse dragged" << std::endl; if(_dragging){ _handle.x = args.x - (_handle.width / 2.0f); _handle.y = args.y - (_handle.height / 2.0f); + + _line[1].x = args.x; + _line[1].y = args.y; } - //Gui::instance()->notifyGuiWidgetEvent(args, this); + GuiWidgetEvent e; + e.args = args; + ofNotifyEvent(guiWidgetEvent, e, this); } bool ScaleWidget::inside(float x, float y){ diff --git a/src/Application/ScaleWidget.h b/src/Application/ScaleWidget.h index 1fd39a2..51ce803 100644 --- a/src/Application/ScaleWidget.h +++ b/src/Application/ScaleWidget.h @@ -4,7 +4,6 @@ #include "ofPolyline.h" #include "GuiBaseWidget.h" #include "ofGraphics.h" -//#include "Gui.h" namespace ofx { namespace piMapper { @@ -25,11 +24,16 @@ class ScaleWidget : public GuiBaseWidget { // This should be the size of the objet's bounding box void setRect(ofRectangle rect); + + float getScale(); private: ofRectangle _handle; ofPolyline _line; + ofPolyline _originalLine; + + float _scale; bool _dragging; };