Skip to content

Instantly share code, notes, and snippets.

@paulhoux
Created February 11, 2017 13:46
Show Gist options
  • Save paulhoux/e91c1dcbc6b727d4b6c13f560186006f to your computer and use it in GitHub Desktop.
Save paulhoux/e91c1dcbc6b727d4b6c13f560186006f to your computer and use it in GitHub Desktop.
CanvasUi: a simple 2D modifier tool to drag and zoom your work area. Made for Cinder.
/*
Copyright (c) 2016, Paul Houx - All rights reserved.
This code is intended for use with the Cinder C++ library: http://libcinder.org
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "cinder/app/MouseEvent.h"
#include "cinder/app/Window.h"
namespace tools {
//! Enables user interaction with a 2D canvas via the mouse.
class CanvasUi {
public:
CanvasUi( const ci::app::WindowRef &window = nullptr, int signalPriority = 0 )
: mIsDirty( false )
, mIsInvDirty( false )
, mEnabled( true )
, mAnchor( 0 )
, mPosition( 0 )
, mScale( 1 )
, mMouseWheelMultiplier( 0.1f )
{
connect( window, signalPriority );
}
~CanvasUi() { disconnect(); }
CanvasUi &operator=( const CanvasUi &rhs )
{
mScale = rhs.mScale;
mMouse = rhs.mMouse;
mClick = rhs.mClick;
mAnchor = rhs.mAnchor;
mPosition = rhs.mPosition;
mOriginal = rhs.mOriginal;
mModelMatrix = rhs.mModelMatrix;
mIsDirty = rhs.mIsDirty;
mMouseWheelMultiplier = rhs.mMouseWheelMultiplier;
mWindow = rhs.mWindow;
mSignalPriority = rhs.mSignalPriority;
mEnabled = rhs.mEnabled;
connect( mWindow, mSignalPriority );
return *this;
}
//! Connects to mouseDown, mouseDrag and mouseWheel signals of \a window, with optional priority \a signalPriority.
void connect( const ci::app::WindowRef &window, int signalPriority = 0 )
{
if( window == mWindow && signalPriority == mSignalPriority )
return;
disconnect();
mWindow = window;
mSignalPriority = signalPriority;
if( window ) {
mMouseConnections.push_back( window->getSignalMouseDown().connect( signalPriority, [this]( ci::app::MouseEvent &event ) { mouseDown( event ); } ) );
mMouseConnections.push_back( window->getSignalMouseDrag().connect( signalPriority, [this]( ci::app::MouseEvent &event ) { mouseDrag( event ); } ) );
mMouseConnections.push_back( window->getSignalMouseWheel().connect( signalPriority, [this]( ci::app::MouseEvent &event ) { mouseWheel( event ); } ) );
}
}
//! Disconnects all signal handlers.
void disconnect()
{
for( auto &conn : mMouseConnections )
conn.disconnect();
mMouseConnections.clear();
mWindow.reset();
}
//! Returns whether the CanvasUi is connected to mouse and window signal handlers.
bool isConnected() const { return mWindow != nullptr; }
//! Sets whether the CanvasUi will modify its transform matrix either through its Window signals or through the various mouse*() member functions.
void enable( bool enable = true ) { mEnabled = enable; }
//! Prevents the CanvasUi from modifying its transform matrix either through its Window signals or through the various mouse*() member functions.
void disable() { mEnabled = false; }
//! Returns whether the CanvasUi will modify its transform matrix either through its Window signals or through the various mouse*() member functions.
bool isEnabled() const { return mEnabled; }
//! Returns the current position and scale as a transform matrix.
const ci::mat4 &getModelMatrix() const
{
if( mIsDirty ) {
// Update model matrix.
mModelMatrix = glm::translate( ci::vec3( mPosition, 0 ) );
mModelMatrix *= glm::scale( ci::vec3( mScale ) );
mModelMatrix *= glm::translate( ci::vec3( -mAnchor, 0 ) );
mIsInvDirty = true;
mIsDirty = false;
}
return mModelMatrix;
}
//! Returns the inverse of the current transform matrix. Can be used to convert coordinates. See also `CanvasUi::toLocal`.
const ci::mat4 &getInverseModelMatrix() const
{
if( mIsInvDirty ) {
mInvModelMatrix = glm::inverse( mModelMatrix );
mIsInvDirty = false;
}
return mInvModelMatrix;
}
//! Converts a given point \a pt from world to object space, effectively undoing the canvas transformations.
ci::vec2 toLocal( const ci::vec2 &pt )
{
auto &m = getInverseModelMatrix();
return ci::vec2( m * ci::vec4( pt, 0, 1 ) );
}
void mouseDown( ci::app::MouseEvent &event ) { mouseDown( event.getPos() ); }
void mouseDrag( ci::app::MouseEvent &event ) { mouseDrag( event.getPos() ); }
void mouseWheel( ci::app::MouseEvent &event ) { mouseWheel( event.getPos(), event.getWheelIncrement() ); }
void mouseDown( const ci::vec2 &mousePos )
{
if( !mEnabled )
return;
reposition( mousePos );
mClick = mousePos;
mOriginal = mPosition;
}
void mouseDrag( const ci::vec2 &mousePos )
{
if( !mEnabled )
return;
mMouse = mousePos;
mPosition = mOriginal + mMouse - mClick;
mIsDirty = true;
}
void mouseWheel( const ci::vec2 &mousePos, float increment )
{
if( !mEnabled )
return;
reposition( mousePos );
mMouse = mousePos;
mScale *= 1.0f + mMouseWheelMultiplier * increment;
mIsDirty = true;
}
//! Sets the multiplier on mouse wheel zooming. Larger values zoom faster. Negative values invert the direction. Default is \c 0.1.
void setMouseWheelMultiplier( float multiplier ) { mMouseWheelMultiplier = multiplier; }
//! Returns the multiplier on mouse wheel zooming. Default is \c 0.1.
float getMouseWheelMultiplier() const { return mMouseWheelMultiplier; }
private:
void reposition( const ci::vec2 &mouse )
{
// Convert mouse to object space.
ci::vec2 anchor = ci::vec2( getInverseModelMatrix() * ci::vec4( mouse, 0, 1 ) );
// Calculate new position, anchor and scale.
mPosition += ci::vec2( mModelMatrix * ci::vec4( anchor - mAnchor, 0, 0 ) );
mAnchor = anchor;
}
private:
float mScale;
float mMouseWheelMultiplier;
ci::vec2 mMouse, mClick;
ci::vec2 mAnchor, mPosition, mOriginal;
ci::app::WindowRef mWindow;
int mSignalPriority;
std::vector<ci::signals::Connection> mMouseConnections;
mutable ci::mat4 mModelMatrix, mInvModelMatrix;
mutable bool mIsDirty, mIsInvDirty;
bool mEnabled;
};
} // namespace tools
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment