Skip to content

Commit

Permalink
Tweaked CanvasUi.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhoux committed Jan 3, 2024
1 parent c93952a commit 006186b
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions include/cinder/CanvasUi.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ class CanvasUi {
{
if( this != &rhs ) {
mScale = rhs.mScale;
mMouse = rhs.mMouse;
mClick = rhs.mClick;
mMouseDownPos = rhs.mMouseDownPos;
mAnchor = rhs.mAnchor;
mPosition = rhs.mPosition;
mOriginal = rhs.mOriginal;
mModelMatrix = rhs.mModelMatrix;
mIsDirty = rhs.mIsDirty;
mModelCached = rhs.mModelCached;
mMouseWheelMultiplier = rhs.mMouseWheelMultiplier;
mWindow = rhs.mWindow;
mSignalPriority = rhs.mSignalPriority;
Expand Down Expand Up @@ -93,46 +92,46 @@ class CanvasUi {
//! Returns the current position and scale as a transform matrix.
const mat4 &getModelMatrix() const
{
if( mIsDirty ) {
if( !mModelCached ) {
// Update model matrix.
mModelMatrix = translate( vec3( mPosition, 0 ) );
mModelMatrix *= scale( vec3( mScale ) );
mModelMatrix *= translate( vec3( -mAnchor, 0 ) );
mIsInvDirty = true;
mIsDirty = false;
mModelCached = true;
mInvModelCached = false;
}

return mModelMatrix;
}
//! Returns the inverse of the current transform matrix. Can be used to convert coordinates. See also `CanvasUi::toLocal`.
const mat4 &getInverseModelMatrix() const
{
if( mIsInvDirty ) {
mInvModelMatrix = inverse( mModelMatrix );
mIsInvDirty = false;
if( !mModelCached || !mInvModelCached ) {
mInvModelMatrix = inverse( getModelMatrix() );
mInvModelCached = true;
}

return mInvModelMatrix;
}
//! Converts a given point \a pt from world to object space, effectively undoing the canvas transformations.
vec2 toLocal( const vec2 &pt ) const
{
auto &m = getInverseModelMatrix();
const auto &m = getInverseModelMatrix();
return { m * vec4( pt, 0, 1 ) };
}

void reset()
{
mPosition = mAnchor = vec2( 0 );
mScale = mScaleTarget = 1.0f;
mIsDirty = mIsInvDirty = true;
mModelCached = mInvModelCached = false;
}

void resize( const ivec2 &size )
{
mPosition += 0.5f * vec2( size - mWindowSize ) / mScaleTarget;
mAnchor += 0.5f * vec2( size - mWindowSize ) / mScaleTarget;
mIsDirty = mIsInvDirty = true;
mModelCached = mInvModelCached = false;
mWindowSize = size;
}

Expand All @@ -154,7 +153,7 @@ class CanvasUi {

reposition( mousePos );

mClick = mousePos;
mMouseDownPos = mousePos;
mOriginal = mPosition;
}

Expand All @@ -163,9 +162,8 @@ class CanvasUi {
if( !mEnabled )
return;

mMouse = mousePos;
mPosition = mOriginal + mMouse - mClick;
mIsDirty = true;
mPosition = mOriginal + mousePos - mMouseDownPos;
mModelCached = mInvModelCached = false;
}

void mouseWheel( const vec2 &mousePos, float increment )
Expand All @@ -175,10 +173,8 @@ class CanvasUi {

reposition( mousePos );

mMouse = mousePos;
mScaleTarget *= 1.0f + mMouseWheelMultiplier * increment;
mScaleTarget = clamp( mScaleTarget, 0.1f, 100.0f ); // Limit scale to the range [10%...10,000%].
mIsDirty = true;
}

//! Sets the multiplier on mouse wheel zooming. Larger values zoom faster. Negative values invert the direction. Default is \c 0.1.
Expand All @@ -189,9 +185,12 @@ class CanvasUi {
private:
void update()
{
mScale += mMouseWheelMultiplier * ( mScaleTarget - mScale );
mIsDirty = mIsInvDirty = true;
if( !approxEqual( mScaleTarget, mScale ) ) {
mScale += 0.1f * ( mScaleTarget - mScale );
mModelCached = mInvModelCached = false;
}
}

void reposition( const vec2 &mouse )
{
// Convert mouse to object space.
Expand All @@ -205,8 +204,7 @@ class CanvasUi {
std::vector<signals::Connection> mConnections;
app::WindowRef mWindow;
ivec2 mWindowSize;
vec2 mMouse{ 0 };
vec2 mClick{ 0 };
vec2 mMouseDownPos{ 0 };
vec2 mAnchor{ 0 };
vec2 mPosition{ 0 };
vec2 mOriginal{ 0 };
Expand All @@ -216,8 +214,8 @@ class CanvasUi {
int mSignalPriority{ 0 };
mutable mat4 mModelMatrix;
mutable mat4 mInvModelMatrix;
mutable bool mIsDirty{ false };
mutable bool mIsInvDirty{ false };
mutable bool mModelCached{ true };
mutable bool mInvModelCached{ true };
bool mEnabled{ true };
};

Expand Down

0 comments on commit 006186b

Please sign in to comment.