From 4ce763c55550e60a84d4f4077c50246d3bfd161e Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Fri, 19 Aug 2016 11:11:21 +0200 Subject: [PATCH] Add `ScaleWidget` based on `GuiBaseWidget` --- src/Application/GuiBaseWidget.h | 22 ++++++++++ src/Application/ScaleWidget.cpp | 74 +++++++++++++++++++++++++++++++++ src/Application/ScaleWidget.h | 37 +++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 src/Application/GuiBaseWidget.h create mode 100644 src/Application/ScaleWidget.cpp create mode 100644 src/Application/ScaleWidget.h diff --git a/src/Application/GuiBaseWidget.h b/src/Application/GuiBaseWidget.h new file mode 100644 index 0000000..0314d5c --- /dev/null +++ b/src/Application/GuiBaseWidget.h @@ -0,0 +1,22 @@ +#pragma once + +#include "ofEvents.h" + +namespace ofx { +namespace piMapper { + +class GuiBaseWidget { + public: + virtual void setup() = 0; + virtual void update() = 0; + virtual void draw() = 0; + + virtual void onMousePressed(ofMouseEventArgs & e) = 0; + virtual void onMouseReleased(ofMouseEventArgs & e) = 0; + virtual void onMouseDragged(ofMouseEventArgs & e) = 0; + + virtual bool inside(float x, float y) = 0; +}; + +} // namespace piMapper +} // namespace ofx \ No newline at end of file diff --git a/src/Application/ScaleWidget.cpp b/src/Application/ScaleWidget.cpp new file mode 100644 index 0000000..e2055ce --- /dev/null +++ b/src/Application/ScaleWidget.cpp @@ -0,0 +1,74 @@ +#include "ScaleWidget.h" + +namespace ofx { +namespace piMapper { + +ScaleWidget::ScaleWidget(){ + _line.addVertex(ofPoint(0, 0)); + _line.addVertex(ofPoint(0, 0)); + + _handle.width = 20; + _handle.height = 20; +} + +void ScaleWidget::setup(){ + +} + +void ScaleWidget::update(){ + +} + +void ScaleWidget::draw(){ + ofPushStyle(); + + ofSetColor(255, 255, 255); + ofNoFill(); + + ofSetLineWidth(1); + _line.draw(); + + ofSetLineWidth(2); + ofDrawRectangle(_handle); + + ofPopStyle(); +} + +void ScaleWidget::onMousePressed(ofMouseEventArgs & e){ + if(_handle.inside(e.x, e.y)){ + std::cout << "Handle clicked" << std::endl; + _dragging = true; + } +} + +void ScaleWidget::onMouseReleased(ofMouseEventArgs & e){ + _dragging = false; +} + +void ScaleWidget::onMouseDragged(ofMouseEventArgs & e){ + if(_dragging){ + _handle.x = e.x; + _handle.y = e.y; + } +} + +bool ScaleWidget::inside(float x, float y){ + ofPoint p = ofPoint(x, y); + return _handle.inside(p); +} + +void ScaleWidget::setRect(ofRectangle rect){ + ofPoint center = rect.getCenter(); + + _line.getVertices()[0].x = center.x; + _line.getVertices()[0].y = center.y; + + _line.getVertices()[1].x = rect.x + rect.width; + _line.getVertices()[1].y = rect.y; + + _handle.x = rect.x + rect.width; + _handle.y = rect.y; +} + +} // namespace piMapper +} // namespace ofx diff --git a/src/Application/ScaleWidget.h b/src/Application/ScaleWidget.h new file mode 100644 index 0000000..4b3210d --- /dev/null +++ b/src/Application/ScaleWidget.h @@ -0,0 +1,37 @@ +#pragma once + +#include "ofRectangle.h" +#include "ofPolyline.h" +#include "GuiBaseWidget.h" +#include "ofGraphics.h" + +namespace ofx { +namespace piMapper { + +class ScaleWidget : GuiBaseWidget { + public: + ScaleWidget(); + + void setup(); + void update(); + void draw(); + + void onMousePressed(ofMouseEventArgs & e); + void onMouseReleased(ofMouseEventArgs & e); + void onMouseDragged(ofMouseEventArgs & e); + + bool inside(float x, float y); + + // This should be the size of the objet's bounding box + void setRect(ofRectangle rect); + + private: + ofRectangle _handle; + + ofPolyline _line; + + bool _dragging; +}; + +} // namespace piMapper +} // namespace ofx \ No newline at end of file