From e3205fb690b51c9b1175650a47d7ed6c4b9ec93b Mon Sep 17 00:00:00 2001 From: Victor Cebollada Date: Wed, 1 Jun 2016 14:21:17 +0100 Subject: [PATCH 1/1] Vertical scrolling for text-editor. * Text can be vertically scrolled by panning on the text, panning the handles, keyboard's cursor keys, and adding or removing text. * Smooth handle panning feature implemented. Allows the handle to follow the finger instead jumping to the next cursor position. * Cursor position issues fixed when there are lines with different heights. * TODO: Fix the highlight box visibility and the text's option popup. Change-Id: If00fbfd4f9e0cd065a6d2b6c908b9a56f0b3a480 Signed-off-by: Victor Cebollada --- .../controls/text-controls/text-editor-impl.cpp | 22 +- .../controls/text-controls/text-field-impl.cpp | 10 + .../internal/text/cursor-helper-functions.cpp | 2 +- .../internal/text/decorator/text-decorator.cpp | 253 ++++++++++---- .../internal/text/decorator/text-decorator.h | 42 ++- .../internal/text/text-controller-impl.cpp | 389 +++++++++++++++------ dali-toolkit/internal/text/text-controller-impl.h | 10 +- dali-toolkit/internal/text/text-controller.cpp | 71 +++- dali-toolkit/internal/text/text-controller.h | 42 +++ 9 files changed, 642 insertions(+), 199 deletions(-) diff --git a/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp b/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp index 4763884..8f7916c 100644 --- a/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp +++ b/dali-toolkit/internal/controls/text-controls/text-editor-impl.cpp @@ -52,10 +52,11 @@ namespace // unnamed namespace { #if defined(DEBUG_ENABLED) - Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS"); +Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS"); #endif - const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND; +const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND; +const float DEFAULT_SCROLL_SPEED = 1200.f; ///< The default scroll speed for the text editor in pixels/second. } // unnamed namespace namespace @@ -928,10 +929,20 @@ void TextEditor::OnInitialize() mController->GetLayoutEngine().SetLayout( LayoutEngine::MULTI_LINE_BOX ); + // Enables the text input. mController->EnableTextInput( mDecorator ); + // Enables the vertical scrolling after the text input has been enabled. + mController->SetVerticalScrollEnabled( true ); + + // Disables the horizontal scrolling. + mController->SetHorizontalScrollEnabled( false ); + mController->SetMaximumNumberOfCharacters( std::numeric_limits::max() ); + // Enable the smooth handle panning. + mController->SetSmoothHandlePanEnabled( true ); + // Forward input events to controller EnableGestureDetection( static_cast( Gesture::Tap | Gesture::Pan | Gesture::LongPress ) ); GetTapGestureDetector().SetMaximumTapsRequired( 2 ); @@ -948,8 +959,11 @@ void TextEditor::OnInitialize() mDecorator->SetBoundingBox( Rect( 0.0f, 0.0f, stageSize.width, stageSize.height ) ); } - // Flip vertically the 'left' selection handle - mDecorator->FlipHandleVertically( LEFT_SELECTION_HANDLE, true ); + // Whether to flip the selection handles as soon as they cross. + mDecorator->FlipSelectionHandlesOnCrossEnabled( true ); + + // Set the default scroll speed. + mDecorator->SetScrollSpeed( DEFAULT_SCROLL_SPEED ); // Fill-parent area by default self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); diff --git a/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp b/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp index 69776fa..32fe824 100644 --- a/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp +++ b/dali-toolkit/internal/controls/text-controls/text-field-impl.cpp @@ -1114,8 +1114,18 @@ void TextField::OnInitialize() mController->GetLayoutEngine().SetLayout( LayoutEngine::SINGLE_LINE_BOX ); + // Enables the text input. mController->EnableTextInput( mDecorator ); + // Enables the horizontal scrolling after the text input has been enabled. + mController->SetHorizontalScrollEnabled( true ); + + // Disables the vertical scrolling. + mController->SetVerticalScrollEnabled( false ); + + // Disable the smooth handle panning. + mController->SetSmoothHandlePanEnabled( false ); + // Forward input events to controller EnableGestureDetection( static_cast( Gesture::Tap | Gesture::Pan | Gesture::LongPress ) ); GetTapGestureDetector().SetMaximumTapsRequired( 2 ); diff --git a/dali-toolkit/internal/text/cursor-helper-functions.cpp b/dali-toolkit/internal/text/cursor-helper-functions.cpp index 2b5e328..10c1e22 100644 --- a/dali-toolkit/internal/text/cursor-helper-functions.cpp +++ b/dali-toolkit/internal/text/cursor-helper-functions.cpp @@ -558,7 +558,7 @@ void GetCursorPosition( VisualModelPtr visualModel, ( isFirstPositionOfLine && !isRightToLeftParagraph ) ); cursorInfo.secondaryPosition.x = -glyphMetrics.xBearing + secondaryPosition.x + ( addGlyphAdvance ? glyphMetrics.advance : 0.f ); - cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight - line.descender - ( glyphMetrics.fontHeight - glyphMetrics.ascender ); + cursorInfo.secondaryPosition.y = cursorInfo.lineOffset + cursorInfo.lineHeight - cursorInfo.secondaryCursorHeight; // Transform the cursor info from line's coords to text's coords. cursorInfo.secondaryPosition.x += line.alignmentOffset; diff --git a/dali-toolkit/internal/text/decorator/text-decorator.cpp b/dali-toolkit/internal/text/decorator/text-decorator.cpp index cc76691..0b3ff48 100644 --- a/dali-toolkit/internal/text/decorator/text-decorator.cpp +++ b/dali-toolkit/internal/text/decorator/text-decorator.cpp @@ -93,17 +93,17 @@ const Dali::Vector4 LIGHT_BLUE( 0.75f, 0.96f, 1.f, 1.f ); // The text highlight const Dali::Vector4 HANDLE_COLOR( 0.0f, (183.0f / 255.0f), (229.0f / 255.0f), 1.0f ); -const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval -const float TO_MILLISECONDS = 1000.f; -const float TO_SECONDS = 1.f / TO_MILLISECONDS; +const unsigned int CURSOR_BLINK_INTERVAL = 500u; ///< Cursor blink interval in milliseconds. +const float TO_MILLISECONDS = 1000.f; ///< Converts from seconds to milliseconds. +const float TO_SECONDS = 1.f / TO_MILLISECONDS; ///< Converts from milliseconds to seconds. -const unsigned int SCROLL_TICK_INTERVAL = 50u; +const unsigned int SCROLL_TICK_INTERVAL = 50u; ///< Scroll interval in milliseconds. +const float SCROLL_THRESHOLD = 10.f; ///< Threshold in pixels close to the edges of the decorator boundaries from where the scroll timer starts to emit signals. +const float SCROLL_SPEED = 300.f; ///< The scroll speed in pixels/second. -const float SCROLL_THRESHOLD = 10.f; -const float SCROLL_SPEED = 300.f; -const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS; +const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS; ///< Distance in pixels scrolled in one second. -const float CURSOR_WIDTH = 1.f; +const float CURSOR_WIDTH = 1.f; ///< The cursor's width in pixels. /** * structure to hold coordinates of each quad, which will make up the mesh. @@ -208,6 +208,7 @@ struct Decorator::Impl : public ConnectionTracker { HandleImpl() : position(), + globalPosition(), size(), lineHeight( 0.0f ), grabDisplacementX( 0.f ), @@ -226,6 +227,7 @@ struct Decorator::Impl : public ConnectionTracker ImageView markerActor; Vector2 position; + Vector2 globalPosition; Size size; float lineHeight; ///< Not the handle height float grabDisplacementX; @@ -277,10 +279,12 @@ struct Decorator::Impl : public ConnectionTracker mFlipSelectionHandlesOnCross( false ), mFlipLeftSelectionHandleDirection( false ), mFlipRightSelectionHandleDirection( false ), - mHandlePanning( false ), - mHandleCurrentCrossed( false ), - mHandlePreviousCrossed( false ), - mNotifyEndOfScroll( false ) + mIsHandlePanning( false ), + mIsHandleCurrentlyCrossed( false ), + mIsHandlePreviouslyCrossed( false ), + mNotifyEndOfScroll( false ), + mHorizontalScrollingEnabled( false ), + mVerticalScrollingEnabled( false ) { mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2; mHighlightShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER ); @@ -304,7 +308,10 @@ struct Decorator::Impl : public ConnectionTracker if( mPrimaryCursor ) { const CursorImpl& cursor = mCursor[PRIMARY_CURSOR]; - mPrimaryCursorVisible = ( cursor.position.x + mCursorWidth <= mControlSize.width ) && ( cursor.position.x >= 0.f ); + mPrimaryCursorVisible = ( ( cursor.position.x + mCursorWidth <= mControlSize.width ) && + ( cursor.position.x >= 0.f ) && + ( cursor.position.y + cursor.cursorHeight <= mControlSize.height ) && + ( cursor.position.y >= 0.f ) ); if( mPrimaryCursorVisible ) { mPrimaryCursor.SetPosition( cursor.position.x, @@ -316,7 +323,10 @@ struct Decorator::Impl : public ConnectionTracker if( mSecondaryCursor ) { const CursorImpl& cursor = mCursor[SECONDARY_CURSOR]; - mSecondaryCursorVisible = ( cursor.position.x + mCursorWidth <= mControlSize.width ) && ( cursor.position.x >= 0.f ); + mSecondaryCursorVisible = ( ( cursor.position.x + mCursorWidth <= mControlSize.width ) && + ( cursor.position.x >= 0.f ) && + ( cursor.position.y + cursor.cursorHeight <= mControlSize.height ) && + ( cursor.position.y >= 0.f ) ); if( mSecondaryCursorVisible ) { mSecondaryCursor.SetPosition( cursor.position.x, @@ -331,7 +341,10 @@ struct Decorator::Impl : public ConnectionTracker bool newGrabHandlePosition = false; if( grabHandle.active ) { - const bool isVisible = ( grabHandle.position.x + floor( 0.5f * mCursorWidth ) <= mControlSize.width ) && ( grabHandle.position.x >= 0.f ); + const bool isVisible = ( ( grabHandle.position.x + floor( 0.5f * mCursorWidth ) <= mControlSize.width ) && + ( grabHandle.position.x >= 0.f ) && + ( grabHandle.position.y <= mControlSize.height - grabHandle.lineHeight ) && + ( grabHandle.position.y >= 0.f ) ); if( isVisible ) { @@ -363,8 +376,14 @@ struct Decorator::Impl : public ConnectionTracker bool newSecondaryHandlePosition = false; if( primary.active || secondary.active ) { - const bool isPrimaryVisible = ( primary.position.x <= mControlSize.width ) && ( primary.position.x >= 0.f ); - const bool isSecondaryVisible = ( secondary.position.x <= mControlSize.width ) && ( secondary.position.x >= 0.f ); + const bool isPrimaryVisible = ( ( primary.position.x <= mControlSize.width ) && + ( primary.position.x >= 0.f ) && + ( primary.position.y <= mControlSize.height - primary.lineHeight ) && + ( primary.position.y >= 0.f ) ); + const bool isSecondaryVisible = ( ( secondary.position.x <= mControlSize.width ) && + ( secondary.position.x >= 0.f ) && + ( secondary.position.y <= mControlSize.height - secondary.lineHeight ) && + ( secondary.position.y >= 0.f ) ); if( isPrimaryVisible || isSecondaryVisible ) { @@ -844,8 +863,8 @@ struct Decorator::Impl : public ConnectionTracker // The grab handle position in world coords. // The active layer's world position is the center of the active layer. The origin of the // coord system of the handles is the top left of the active layer. - position.x = parentWorldPosition.x - 0.5f * mControlSize.width + handle.position.x; - position.y = parentWorldPosition.y - 0.5f * mControlSize.height + handle.position.y; + position.x = parentWorldPosition.x - 0.5f * mControlSize.width + handle.position.x + ( mSmoothHandlePanEnabled ? handle.grabDisplacementX : 0.f ); + position.y = parentWorldPosition.y - 0.5f * mControlSize.height + handle.position.y + ( mSmoothHandlePanEnabled ? handle.grabDisplacementY : 0.f ); } void SetGrabHandlePosition() @@ -877,8 +896,8 @@ struct Decorator::Impl : public ConnectionTracker if( grabHandle.actor ) { - grabHandle.actor.SetPosition( grabHandle.position.x + floor( 0.5f * mCursorWidth ), - yLocalPosition ); // TODO : Fix for multiline. + grabHandle.actor.SetPosition( grabHandle.position.x + floor( 0.5f * mCursorWidth ) + ( mSmoothHandlePanEnabled ? grabHandle.grabDisplacementX : 0.f ), + yLocalPosition + ( mSmoothHandlePanEnabled ? grabHandle.grabDisplacementY : 0.f ) ); } } @@ -903,18 +922,21 @@ struct Decorator::Impl : public ConnectionTracker // Whether to flip the handles if they are crossed. bool crossFlip = false; - if( mFlipSelectionHandlesOnCross || !mHandlePanning ) + if( mFlipSelectionHandlesOnCross || !mIsHandlePanning ) { - crossFlip = mHandleCurrentCrossed; + crossFlip = mIsHandleCurrentlyCrossed; } + // Whether the handle was crossed before start the panning. + const bool isHandlePreviouslyCrossed = mFlipSelectionHandlesOnCross ? false : mIsHandlePreviouslyCrossed; + // Does not flip if both conditions are true (double flip) - flipHandle = flipHandle != ( crossFlip || mHandlePreviousCrossed ); + flipHandle = flipHandle != ( crossFlip || isHandlePreviouslyCrossed ); // Will flip the handles vertically if the user prefers it. bool verticallyFlippedPreferred = handle.verticallyFlippedPreferred; - if( crossFlip || mHandlePreviousCrossed ) + if( crossFlip || isHandlePreviouslyCrossed ) { if( isPrimaryHandle ) { @@ -967,8 +989,8 @@ struct Decorator::Impl : public ConnectionTracker if( handle.actor ) { - handle.actor.SetPosition( handle.position.x, - yLocalPosition ); // TODO : Fix for multiline. + handle.actor.SetPosition( handle.position.x + ( mSmoothHandlePanEnabled ? handle.grabDisplacementX : 0.f ), + yLocalPosition + ( mSmoothHandlePanEnabled ? handle.grabDisplacementY : 0.f ) ); } } @@ -1118,32 +1140,52 @@ struct Decorator::Impl : public ConnectionTracker if( Gesture::Started == gesture.state ) { handle.grabDisplacementX = handle.grabDisplacementY = 0.f; + + handle.globalPosition.x = handle.position.x; + handle.globalPosition.y = handle.position.y; } handle.grabDisplacementX += gesture.displacement.x; handle.grabDisplacementY += ( handle.verticallyFlipped ? -gesture.displacement.y : gesture.displacement.y ); - const float x = handle.position.x + handle.grabDisplacementX; - const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY; + const float x = handle.globalPosition.x + handle.grabDisplacementX; + const float y = handle.globalPosition.y + handle.grabDisplacementY + 0.5f * handle.lineHeight; + const float yVerticallyFlippedCorrected = y - ( handle.verticallyFlipped ? handle.lineHeight : 0.f ); - if( Gesture::Started == gesture.state || - Gesture::Continuing == gesture.state ) + if( ( Gesture::Started == gesture.state ) || + ( Gesture::Continuing == gesture.state ) ) { Vector2 targetSize; mController.GetTargetSize( targetSize ); - if( x < mScrollThreshold ) + if( mHorizontalScrollingEnabled && + ( x < mScrollThreshold ) ) { mScrollDirection = SCROLL_RIGHT; mHandleScrolling = type; StartScrollTimer(); } - else if( x > targetSize.width - mScrollThreshold ) + else if( mHorizontalScrollingEnabled && + ( x > targetSize.width - mScrollThreshold ) ) { mScrollDirection = SCROLL_LEFT; mHandleScrolling = type; StartScrollTimer(); } + else if( mVerticalScrollingEnabled && + ( yVerticallyFlippedCorrected < mScrollThreshold ) ) + { + mScrollDirection = SCROLL_TOP; + mHandleScrolling = type; + StartScrollTimer(); + } + else if( mVerticalScrollingEnabled && + ( yVerticallyFlippedCorrected + handle.lineHeight > targetSize.height - mScrollThreshold ) ) + { + mScrollDirection = SCROLL_BOTTOM; + mHandleScrolling = type; + StartScrollTimer(); + } else { mHandleScrolling = HANDLE_TYPE_COUNT; @@ -1151,10 +1193,10 @@ struct Decorator::Impl : public ConnectionTracker mController.DecorationEvent( type, HANDLE_PRESSED, x, y ); } - mHandlePanning = true; + mIsHandlePanning = true; } - else if( Gesture::Finished == gesture.state || - Gesture::Cancelled == gesture.state ) + else if( ( Gesture::Finished == gesture.state ) || + ( Gesture::Cancelled == gesture.state ) ) { if( mScrollTimer && ( mScrollTimer.IsRunning() || mNotifyEndOfScroll ) ) @@ -1175,7 +1217,7 @@ struct Decorator::Impl : public ConnectionTracker } handle.pressed = false; - mHandlePanning = false; + mIsHandlePanning = false; } } @@ -1201,20 +1243,22 @@ struct Decorator::Impl : public ConnectionTracker bool OnGrabHandleTouched( Actor actor, const TouchData& touch ) { + HandleImpl& grabHandle = mHandle[GRAB_HANDLE]; + // Switch between pressed/release grab-handle images if( touch.GetPointCount() > 0 && - mHandle[GRAB_HANDLE].actor ) + grabHandle.actor ) { const PointState::Type state = touch.GetState( 0 ); if( PointState::DOWN == state ) { - mHandle[GRAB_HANDLE].pressed = true; + grabHandle.pressed = true; } else if( ( PointState::UP == state ) || ( PointState::INTERRUPTED == state ) ) { - mHandle[GRAB_HANDLE].pressed = false; + grabHandle.pressed = false; } SetHandleImage( GRAB_HANDLE ); @@ -1226,22 +1270,24 @@ struct Decorator::Impl : public ConnectionTracker bool OnHandleOneTouched( Actor actor, const TouchData& touch ) { + HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE]; + // Switch between pressed/release selection handle images if( touch.GetPointCount() > 0 && - mHandle[LEFT_SELECTION_HANDLE].actor ) + primarySelectionHandle.actor ) { const PointState::Type state = touch.GetState( 0 ); if( PointState::DOWN == state ) { - mHandle[LEFT_SELECTION_HANDLE].pressed = true; + primarySelectionHandle.pressed = true; } else if( ( PointState::UP == state ) || ( PointState::INTERRUPTED == state ) ) { - mHandle[LEFT_SELECTION_HANDLE].pressed = false; - mHandlePreviousCrossed = mHandleCurrentCrossed; - mHandlePanning = false; + primarySelectionHandle.pressed = false; + mIsHandlePreviouslyCrossed = mIsHandleCurrentlyCrossed; + mIsHandlePanning = false; } SetHandleImage( LEFT_SELECTION_HANDLE ); @@ -1253,22 +1299,24 @@ struct Decorator::Impl : public ConnectionTracker bool OnHandleTwoTouched( Actor actor, const TouchData& touch ) { + HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE]; + // Switch between pressed/release selection handle images if( touch.GetPointCount() > 0 && - mHandle[RIGHT_SELECTION_HANDLE].actor ) + secondarySelectionHandle.actor ) { const PointState::Type state = touch.GetState( 0 ); if( PointState::DOWN == state ) { - mHandle[RIGHT_SELECTION_HANDLE].pressed = true; + secondarySelectionHandle.pressed = true; } else if( ( PointState::UP == state ) || ( PointState::INTERRUPTED == state ) ) { - mHandle[RIGHT_SELECTION_HANDLE].pressed = false; - mHandlePreviousCrossed = mHandleCurrentCrossed; - mHandlePanning = false; + secondarySelectionHandle.pressed = false; + mIsHandlePreviouslyCrossed = mIsHandleCurrentlyCrossed; + mIsHandlePanning = false; } SetHandleImage( RIGHT_SELECTION_HANDLE ); @@ -1646,10 +1694,39 @@ struct Decorator::Impl : public ConnectionTracker { if( HANDLE_TYPE_COUNT != mHandleScrolling ) { + float x = 0.f; + float y = 0.f; + + switch( mScrollDirection ) + { + case SCROLL_RIGHT: + { + x = mScrollDistance; + break; + } + case SCROLL_LEFT: + { + x = -mScrollDistance; + break; + } + case SCROLL_TOP: + { + y = mScrollDistance; + break; + } + case SCROLL_BOTTOM: + { + y = -mScrollDistance; + break; + } + default: + break; + } + mController.DecorationEvent( mHandleScrolling, HANDLE_SCROLLING, - mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance, - 0.f ); + x, + y ); } return true; @@ -1715,10 +1792,13 @@ struct Decorator::Impl : public ConnectionTracker bool mFlipSelectionHandlesOnCross : 1; ///< Whether to flip the selection handles as soon as they cross. bool mFlipLeftSelectionHandleDirection : 1; ///< Whether to flip the left selection handle image because of the character's direction. bool mFlipRightSelectionHandleDirection : 1; ///< Whether to flip the right selection handle image because of the character's direction. - bool mHandlePanning : 1; ///< Whether any of the handles is moving. - bool mHandleCurrentCrossed : 1; ///< Whether the handles are crossed. - bool mHandlePreviousCrossed : 1; ///< Whether the handles where crossed at the last handle touch up. + bool mIsHandlePanning : 1; ///< Whether any of the handles is moving. + bool mIsHandleCurrentlyCrossed : 1; ///< Whether the handles are crossed. + bool mIsHandlePreviouslyCrossed : 1; ///< Whether the handles where crossed at the last handle touch up. bool mNotifyEndOfScroll : 1; ///< Whether to notify the end of the scroll. + bool mHorizontalScrollingEnabled : 1; ///< Whether the horizontal scrolling is enabled. + bool mVerticalScrollingEnabled : 1; ///< Whether the vertical scrolling is enabled. + bool mSmoothHandlePanEnabled : 1; ///< Whether to pan smoothly the handles. }; DecoratorPtr Decorator::New( ControllerInterface& controller, @@ -1762,18 +1842,22 @@ unsigned int Decorator::GetActiveCursor() const void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight ) { - mImpl->mCursor[cursor].position.x = x; - mImpl->mCursor[cursor].position.y = y; - mImpl->mCursor[cursor].cursorHeight = cursorHeight; - mImpl->mCursor[cursor].lineHeight = lineHeight; + Impl::CursorImpl& cursorImpl = mImpl->mCursor[cursor]; + + cursorImpl.position.x = x; + cursorImpl.position.y = y; + cursorImpl.cursorHeight = cursorHeight; + cursorImpl.lineHeight = lineHeight; } void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const { - x = mImpl->mCursor[cursor].position.x; - y = mImpl->mCursor[cursor].position.y; - cursorHeight = mImpl->mCursor[cursor].cursorHeight; - lineHeight = mImpl->mCursor[cursor].lineHeight; + const Impl::CursorImpl& cursorImpl = mImpl->mCursor[cursor]; + + x = cursorImpl.position.x; + y = cursorImpl.position.y; + cursorHeight = cursorImpl.cursorHeight; + lineHeight = cursorImpl.lineHeight; } const Vector2& Decorator::GetPosition( Cursor cursor ) const @@ -1861,7 +1945,7 @@ void Decorator::SetHandleActive( HandleType handleType, bool active ) { if( ( LEFT_SELECTION_HANDLE == handleType ) || ( RIGHT_SELECTION_HANDLE == handleType ) ) { - mImpl->mHandlePreviousCrossed = false; + mImpl->mIsHandlePreviouslyCrossed = false; } // TODO: this is a work-around. @@ -1908,12 +1992,15 @@ void Decorator::SetPosition( HandleType handleType, float x, float y, float heig // Adjust handle's displacement Impl::HandleImpl& handle = mImpl->mHandle[handleType]; - handle.grabDisplacementX -= x - handle.position.x; - handle.grabDisplacementY -= y - handle.position.y; - handle.position.x = x; handle.position.y = y; handle.lineHeight = height; + + if( mImpl->mSmoothHandlePanEnabled ) + { + handle.grabDisplacementX = 0.f; + handle.grabDisplacementY = 0.f; + } } void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const @@ -1947,7 +2034,7 @@ void Decorator::FlipSelectionHandlesOnCrossEnabled( bool enable ) void Decorator::SetSelectionHandleFlipState( bool indicesSwapped, bool left, bool right ) { - mImpl->mHandleCurrentCrossed = indicesSwapped; + mImpl->mIsHandleCurrentlyCrossed = indicesSwapped; mImpl->mFlipLeftSelectionHandleDirection = left; mImpl->mFlipRightSelectionHandleDirection = right; } @@ -2037,6 +2124,36 @@ void Decorator::NotifyEndOfScroll() mImpl->NotifyEndOfScroll(); } +void Decorator::SetHorizontalScrollEnabled( bool enable ) +{ + mImpl->mHorizontalScrollingEnabled = enable; +} + +bool Decorator::IsHorizontalScrollEnabled() const +{ + return mImpl->mHorizontalScrollingEnabled; +} + +void Decorator::SetVerticalScrollEnabled( bool enable ) +{ + mImpl->mVerticalScrollingEnabled = enable; +} + +bool Decorator::IsVerticalScrollEnabled() const +{ + return mImpl->mVerticalScrollingEnabled; +} + +void Decorator::SetSmoothHandlePanEnabled( bool enable ) +{ + mImpl->mSmoothHandlePanEnabled = enable; +} + +bool Decorator::IsSmoothHandlePanEnabled() const +{ + return mImpl->mSmoothHandlePanEnabled; +} + Decorator::~Decorator() { delete mImpl; diff --git a/dali-toolkit/internal/text/decorator/text-decorator.h b/dali-toolkit/internal/text/decorator/text-decorator.h index 4bcd012..c52b7d0 100644 --- a/dali-toolkit/internal/text/decorator/text-decorator.h +++ b/dali-toolkit/internal/text/decorator/text-decorator.h @@ -284,7 +284,7 @@ public: /** * @brief Retrieves the blink-interval for a cursor. * - * @return The cursor blink-interval. + * @return The cursor blink-interval in seconds. */ float GetCursorBlinkInterval() const; @@ -298,7 +298,7 @@ public: /** * @brief Retrieves the blink-duration for a cursor. * - * @return The cursor blink-duration. + * @return The cursor blink-duration in seconds. */ float GetCursorBlinkDuration() const; @@ -501,14 +501,14 @@ public: * It defines a square area inside the control, close to the edge. * When the cursor enters this area, the decorator starts to send scroll events. * - * @param[in] threshold The scroll threshold. + * @param[in] threshold The scroll threshold in pixels. */ void SetScrollThreshold( float threshold ); /** * @brief Retrieves the scroll threshold. * - * @retunr The scroll threshold. + * @retunr The scroll threshold in pixels. */ float GetScrollThreshold() const; @@ -517,14 +517,14 @@ public: * * Is the distance the text is going to be scrolled during a scroll interval. * - * @param[in] speed The scroll speed. + * @param[in] speed The scroll speed in pixels/second. */ void SetScrollSpeed( float speed ); /** * @brief Retrieves the scroll speed. * - * @return The scroll speed. + * @return The scroll speed in pixels/second. */ float GetScrollSpeed() const; @@ -533,6 +533,36 @@ public: */ void NotifyEndOfScroll(); + /** + * @copydoc Text::Controller::SetHorizontalScrollEnabled() + */ + void SetHorizontalScrollEnabled( bool enable ); + + /** + * @copydoc Text::Controller::IsHorizontalScrollEnabled() + */ + bool IsHorizontalScrollEnabled() const; + + /** + * @copydoc Text::Controller::SetVerticalScrollEnabled() + */ + void SetVerticalScrollEnabled( bool enable ); + + /** + * @copydoc Text::Controller::IsVerticalScrollEnabled() + */ + bool IsVerticalScrollEnabled() const; + + /** + * @copydoc Text::Controller::SetSmoothHandlePanEnabled() + */ + void SetSmoothHandlePanEnabled( bool enable ); + + /** + * @copydoc Text::Controller::IsSmoothHandlePanEnabled() + */ + bool IsSmoothHandlePanEnabled() const; + protected: /** diff --git a/dali-toolkit/internal/text/text-controller-impl.cpp b/dali-toolkit/internal/text/text-controller-impl.cpp index 0263b64..9a57431 100644 --- a/dali-toolkit/internal/text/text-controller-impl.cpp +++ b/dali-toolkit/internal/text/text-controller-impl.cpp @@ -79,6 +79,7 @@ EventData::EventData( DecoratorPtr decorator ) mRightSelectionPosition( 0u ), mPreEditStartPosition( 0u ), mPreEditLength( 0u ), + mCursorHookPositionX( 0.f ), mIsShowingPlaceholderText( false ), mPreEditFlag( false ), mDecoratorUpdated( false ), @@ -86,11 +87,11 @@ EventData::EventData( DecoratorPtr decorator ) mGrabHandleEnabled( true ), mGrabHandlePopupEnabled( true ), mSelectionEnabled( true ), - mHorizontalScrollingEnabled( true ), - mVerticalScrollingEnabled( false ), mUpdateCursorPosition( false ), + mUpdateGrabHandlePosition( false ), mUpdateLeftSelectionPosition( false ), mUpdateRightSelectionPosition( false ), + mUpdateHighlightBox( false ), mScrollAfterUpdatePosition( false ), mScrollAfterDelete( false ), mAllTextSelected( false ), @@ -162,8 +163,7 @@ bool Controller::Impl::ProcessInputEvents() } if( mEventData->mUpdateCursorPosition || - mEventData->mUpdateLeftSelectionPosition || - mEventData->mUpdateRightSelectionPosition ) + mEventData->mUpdateHighlightBox ) { NotifyImfManager(); } @@ -173,9 +173,17 @@ bool Controller::Impl::ProcessInputEvents() { // Updates the cursor position and scrolls the text to make it visible. CursorInfo cursorInfo; + // Calculate the cursor position from the new cursor index. GetCursorPosition( mEventData->mPrimaryCursorPosition, cursorInfo ); + if( mEventData->mUpdateCursorHookPosition ) + { + // Update the cursor hook position. Used to move the cursor with the keys 'up' and 'down'. + mEventData->mCursorHookPositionX = cursorInfo.primaryPosition.x; + mEventData->mUpdateCursorHookPosition = false; + } + // Scroll first the text after delete ... if( mEventData->mScrollAfterDelete ) { @@ -185,7 +193,8 @@ bool Controller::Impl::ProcessInputEvents() // ... then, text can be scrolled to make the cursor visible. if( mEventData->mScrollAfterUpdatePosition ) { - ScrollToMakePositionVisible( cursorInfo.primaryPosition ); + const Vector2 currentCursorPosition( cursorInfo.primaryPosition.x, cursorInfo.lineOffset ); + ScrollToMakePositionVisible( currentCursorPosition, cursorInfo.lineHeight ); } mEventData->mScrollAfterUpdatePosition = false; mEventData->mScrollAfterDelete = false; @@ -194,36 +203,31 @@ bool Controller::Impl::ProcessInputEvents() mEventData->mDecoratorUpdated = true; mEventData->mUpdateCursorPosition = false; + mEventData->mUpdateGrabHandlePosition = false; } else { - bool leftScroll = false; - bool rightScroll = false; - CursorInfo leftHandleInfo; CursorInfo rightHandleInfo; - if( mEventData->mUpdateLeftSelectionPosition ) + if( mEventData->mUpdateHighlightBox ) { GetCursorPosition( mEventData->mLeftSelectionPosition, leftHandleInfo ); - if( mEventData->mScrollAfterUpdatePosition ) - { - ScrollToMakePositionVisible( leftHandleInfo.primaryPosition ); - leftScroll = true; - } - } - - if( mEventData->mUpdateRightSelectionPosition ) - { GetCursorPosition( mEventData->mRightSelectionPosition, rightHandleInfo ); - if( mEventData->mScrollAfterUpdatePosition ) + if( mEventData->mScrollAfterUpdatePosition && mEventData->mUpdateLeftSelectionPosition ) { - ScrollToMakePositionVisible( rightHandleInfo.primaryPosition ); - rightScroll = true; + const Vector2 currentCursorPosition( leftHandleInfo.primaryPosition.x, leftHandleInfo.lineOffset ); + ScrollToMakePositionVisible( currentCursorPosition, leftHandleInfo.lineHeight ); + } + + if( mEventData->mScrollAfterUpdatePosition && mEventData->mUpdateRightSelectionPosition ) + { + const Vector2 currentCursorPosition( rightHandleInfo.primaryPosition.x, rightHandleInfo.lineOffset ); + ScrollToMakePositionVisible( currentCursorPosition, rightHandleInfo.lineHeight ); } } @@ -234,6 +238,7 @@ bool Controller::Impl::ProcessInputEvents() SetPopupButtons(); mEventData->mDecoratorUpdated = true; + mEventData->mUpdateLeftSelectionPosition = false; } if( mEventData->mUpdateRightSelectionPosition ) @@ -243,20 +248,19 @@ bool Controller::Impl::ProcessInputEvents() SetPopupButtons(); mEventData->mDecoratorUpdated = true; + mEventData->mUpdateRightSelectionPosition = false; } - if( mEventData->mUpdateLeftSelectionPosition || mEventData->mUpdateRightSelectionPosition ) + if( mEventData->mUpdateHighlightBox ) { RepositionSelectionHandles(); mEventData->mUpdateLeftSelectionPosition = false; mEventData->mUpdateRightSelectionPosition = false; + mEventData->mUpdateHighlightBox = false; } - if( leftScroll || rightScroll ) - { - mEventData->mScrollAfterUpdatePosition = false; - } + mEventData->mScrollAfterUpdatePosition = false; } if( mEventData->mUpdateInputStyle ) @@ -1075,11 +1079,69 @@ void Controller::Impl::OnCursorKeyEvent( const Event& event ) } else if( Dali::DALI_KEY_CURSOR_UP == keyCode ) { - // TODO + // Get first the line index of the current cursor position index. + CharacterIndex characterIndex = 0u; + + if( mEventData->mPrimaryCursorPosition > 0u ) + { + characterIndex = mEventData->mPrimaryCursorPosition - 1u; + } + + const LineIndex lineIndex = mVisualModel->GetLineOfCharacter( characterIndex ); + + if( lineIndex > 0u ) + { + // Retrieve the cursor position info. + CursorInfo cursorInfo; + GetCursorPosition( mEventData->mPrimaryCursorPosition, + cursorInfo ); + + // Get the line above. + const LineRun& line = *( mVisualModel->mLines.Begin() + ( lineIndex - 1u ) ); + + // Get the next hit 'y' point. + const float hitPointY = cursorInfo.lineOffset - 0.5f * ( line.ascender - line.descender ); + + // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index. + mEventData->mPrimaryCursorPosition = Text::GetClosestCursorIndex( mVisualModel, + mLogicalModel, + mMetrics, + mEventData->mCursorHookPositionX, + hitPointY ); + } } else if( Dali::DALI_KEY_CURSOR_DOWN == keyCode ) { - // TODO + // Get first the line index of the current cursor position index. + CharacterIndex characterIndex = 0u; + + if( mEventData->mPrimaryCursorPosition > 0u ) + { + characterIndex = mEventData->mPrimaryCursorPosition - 1u; + } + + const LineIndex lineIndex = mVisualModel->GetLineOfCharacter( characterIndex ); + + if( lineIndex + 1u < mVisualModel->mLines.Count() ) + { + // Retrieve the cursor position info. + CursorInfo cursorInfo; + GetCursorPosition( mEventData->mPrimaryCursorPosition, + cursorInfo ); + + // Get the line below. + const LineRun& line = *( mVisualModel->mLines.Begin() + lineIndex + 1u ); + + // Get the next hit 'y' point. + const float hitPointY = cursorInfo.lineOffset + cursorInfo.lineHeight + 0.5f * ( line.ascender - line.descender ); + + // Use the cursor hook position 'x' and the next hit 'y' position to calculate the new cursor index. + mEventData->mPrimaryCursorPosition = Text::GetClosestCursorIndex( mVisualModel, + mLogicalModel, + mMetrics, + mEventData->mCursorHookPositionX, + hitPointY ); + } } mEventData->mUpdateCursorPosition = true; @@ -1101,6 +1163,9 @@ void Controller::Impl::OnTapEvent( const Event& event ) const float xPosition = event.p2.mFloat - mScrollPosition.x; const float yPosition = event.p3.mFloat - mScrollPosition.y; + // Keep the tap 'x' position. Used to move the cursor. + mEventData->mCursorHookPositionX = xPosition; + mEventData->mPrimaryCursorPosition = Text::GetClosestCursorIndex( mVisualModel, mLogicalModel, mMetrics, @@ -1116,6 +1181,7 @@ void Controller::Impl::OnTapEvent( const Event& event ) } mEventData->mUpdateCursorPosition = true; + mEventData->mUpdateGrabHandlePosition = true; mEventData->mScrollAfterUpdatePosition = true; mEventData->mUpdateInputStyle = true; @@ -1142,27 +1208,27 @@ void Controller::Impl::OnPanEvent( const Event& event ) if( ( Gesture::Started == state ) || ( Gesture::Continuing == state ) ) { - const Vector2& actualSize = mVisualModel->GetLayoutSize(); - const Vector2 currentScroll = mScrollPosition; - - if( mEventData->mHorizontalScrollingEnabled ) + if( mEventData->mDecorator ) { - const float displacementX = event.p2.mFloat; - mScrollPosition.x += displacementX; + const Vector2& layoutSize = mVisualModel->GetLayoutSize(); + const Vector2 currentScroll = mScrollPosition; - ClampHorizontalScroll( actualSize ); - } + if( mEventData->mDecorator->IsHorizontalScrollEnabled() ) + { + const float displacementX = event.p2.mFloat; + mScrollPosition.x += displacementX; - if( mEventData->mVerticalScrollingEnabled ) - { - const float displacementY = event.p3.mFloat; - mScrollPosition.y += displacementY; + ClampHorizontalScroll( layoutSize ); + } - ClampVerticalScroll( actualSize ); - } + if( mEventData->mDecorator->IsVerticalScrollEnabled() ) + { + const float displacementY = event.p3.mFloat; + mScrollPosition.y += displacementY; + + ClampVerticalScroll( layoutSize ); + } - if( mEventData->mDecorator ) - { mEventData->mDecorator->UpdatePositions( mScrollPosition - currentScroll ); } } @@ -1189,6 +1255,7 @@ void Controller::Impl::OnHandleEvent( const Event& event ) const unsigned int state = event.p1.mUint; const bool handleStopScrolling = ( HANDLE_STOP_SCROLLING == state ); + const bool isSmoothHandlePanEnabled = mEventData->mDecorator->IsSmoothHandlePanEnabled(); if( HANDLE_PRESSED == state ) { @@ -1196,6 +1263,7 @@ void Controller::Impl::OnHandleEvent( const Event& event ) const float xPosition = event.p2.mFloat - mScrollPosition.x; const float yPosition = event.p3.mFloat - mScrollPosition.y; + // Need to calculate the handle's new position. const CharacterIndex handleNewPosition = Text::GetClosestCursorIndex( mVisualModel, mLogicalModel, mMetrics, @@ -1208,9 +1276,15 @@ void Controller::Impl::OnHandleEvent( const Event& event ) if( handleNewPosition != mEventData->mPrimaryCursorPosition ) { - mEventData->mPrimaryCursorPosition = handleNewPosition; + // Updates the cursor position if the handle's new position is different than the current one. mEventData->mUpdateCursorPosition = true; + // Does not update the grab handle position if the smooth panning is enabled. (The decorator does it smooth). + mEventData->mUpdateGrabHandlePosition = !isSmoothHandlePanEnabled; + mEventData->mPrimaryCursorPosition = handleNewPosition; } + + // Updates the decorator if the soft handle panning is enabled. It triggers a relayout in the decorator and the new position of the handle is set. + mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled; } else if( Event::LEFT_SELECTION_HANDLE_EVENT == event.type ) { @@ -1219,10 +1293,15 @@ void Controller::Impl::OnHandleEvent( const Event& event ) if( ( handleNewPosition != mEventData->mLeftSelectionPosition ) && ( handleNewPosition != mEventData->mRightSelectionPosition ) ) { + // Updates the highlight box if the handle's new position is different than the current one. + mEventData->mUpdateHighlightBox = true; + // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth). + mEventData->mUpdateLeftSelectionPosition = !isSmoothHandlePanEnabled; mEventData->mLeftSelectionPosition = handleNewPosition; - - mEventData->mUpdateLeftSelectionPosition = true; } + + // Updates the decorator if the soft handle panning is enabled. It triggers a relayout in the decorator and the new position of the handle is set. + mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled; } else if( Event::RIGHT_SELECTION_HANDLE_EVENT == event.type ) { @@ -1231,17 +1310,22 @@ void Controller::Impl::OnHandleEvent( const Event& event ) if( ( handleNewPosition != mEventData->mRightSelectionPosition ) && ( handleNewPosition != mEventData->mLeftSelectionPosition ) ) { + // Updates the highlight box if the handle's new position is different than the current one. + mEventData->mUpdateHighlightBox = true; + // Does not update the selection handle position if the smooth panning is enabled. (The decorator does it smooth). + mEventData->mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled; mEventData->mRightSelectionPosition = handleNewPosition; - - mEventData->mUpdateRightSelectionPosition = true; } + + // Updates the decorator if the soft handle panning is enabled. It triggers a relayout in the decorator and the new position of the handle is set. + mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled; } } // end ( HANDLE_PRESSED == state ) else if( ( HANDLE_RELEASED == state ) || handleStopScrolling ) { CharacterIndex handlePosition = 0u; - if( handleStopScrolling ) + if( handleStopScrolling || isSmoothHandlePanEnabled ) { // Convert from decorator's coords to text's coords. const float xPosition = event.p2.mFloat - mScrollPosition.x; @@ -1257,6 +1341,7 @@ void Controller::Impl::OnHandleEvent( const Event& event ) if( Event::GRAB_HANDLE_EVENT == event.type ) { mEventData->mUpdateCursorPosition = true; + mEventData->mUpdateGrabHandlePosition = true; mEventData->mUpdateInputStyle = true; if( !IsClipboardEmpty() ) @@ -1264,9 +1349,9 @@ void Controller::Impl::OnHandleEvent( const Event& event ) ChangeState( EventData::EDITING_WITH_PASTE_POPUP ); // Moving grabhandle will show Paste Popup } - if( handleStopScrolling ) + if( handleStopScrolling || isSmoothHandlePanEnabled ) { - mEventData->mScrollAfterUpdatePosition = mEventData->mPrimaryCursorPosition != handlePosition; + mEventData->mScrollAfterUpdatePosition = true; mEventData->mPrimaryCursorPosition = handlePosition; } } @@ -1274,12 +1359,15 @@ void Controller::Impl::OnHandleEvent( const Event& event ) { ChangeState( EventData::SELECTING ); - if( handleStopScrolling ) + mEventData->mUpdateHighlightBox = true; + mEventData->mUpdateLeftSelectionPosition = true; + + if( handleStopScrolling || isSmoothHandlePanEnabled ) { - mEventData->mUpdateLeftSelectionPosition = ( mEventData->mRightSelectionPosition != handlePosition ); - mEventData->mScrollAfterUpdatePosition = mEventData->mUpdateLeftSelectionPosition; + mEventData->mScrollAfterUpdatePosition = true; - if( mEventData->mUpdateLeftSelectionPosition ) + if( ( handlePosition != mEventData->mRightSelectionPosition ) && + ( handlePosition != mEventData->mLeftSelectionPosition ) ) { mEventData->mLeftSelectionPosition = handlePosition; } @@ -1289,11 +1377,14 @@ void Controller::Impl::OnHandleEvent( const Event& event ) { ChangeState( EventData::SELECTING ); - if( handleStopScrolling ) + mEventData->mUpdateHighlightBox = true; + mEventData->mUpdateRightSelectionPosition = true; + + if( handleStopScrolling || isSmoothHandlePanEnabled ) { - mEventData->mUpdateRightSelectionPosition = ( mEventData->mLeftSelectionPosition != handlePosition ); - mEventData->mScrollAfterUpdatePosition = mEventData->mUpdateRightSelectionPosition; - if( mEventData->mUpdateRightSelectionPosition ) + mEventData->mScrollAfterUpdatePosition = true; + if( ( handlePosition != mEventData->mRightSelectionPosition ) && + ( handlePosition != mEventData->mLeftSelectionPosition ) ) { mEventData->mRightSelectionPosition = handlePosition; } @@ -1305,12 +1396,15 @@ void Controller::Impl::OnHandleEvent( const Event& event ) else if( HANDLE_SCROLLING == state ) { const float xSpeed = event.p2.mFloat; - const Vector2& actualSize = mVisualModel->GetLayoutSize(); + const float ySpeed = event.p3.mFloat; + const Vector2& layoutSize = mVisualModel->GetLayoutSize(); const Vector2 currentScrollPosition = mScrollPosition; mScrollPosition.x += xSpeed; + mScrollPosition.y += ySpeed; - ClampHorizontalScroll( actualSize ); + ClampHorizontalScroll( layoutSize ); + ClampVerticalScroll( layoutSize ); bool endOfScroll = false; if( Vector2::ZERO == ( currentScrollPosition - mScrollPosition ) ) @@ -1324,6 +1418,7 @@ void Controller::Impl::OnHandleEvent( const Event& event ) // Set the position of the handle. const bool scrollRightDirection = xSpeed > 0.f; + const bool scrollBottomDirection = ySpeed > 0.f; const bool leftSelectionHandleEvent = Event::LEFT_SELECTION_HANDLE_EVENT == event.type; const bool rightSelectionHandleEvent = Event::RIGHT_SELECTION_HANDLE_EVENT == event.type; @@ -1331,10 +1426,22 @@ void Controller::Impl::OnHandleEvent( const Event& event ) { ChangeState( EventData::GRAB_HANDLE_PANNING ); + // Get the grab handle position in decorator coords. Vector2 position = mEventData->mDecorator->GetPosition( GRAB_HANDLE ); - // Position the grag handle close to either the left or right edge. - position.x = scrollRightDirection ? 0.f : mVisualModel->mControlSize.width; + if( mEventData->mDecorator->IsHorizontalScrollEnabled() ) + { + // Position the grag handle close to either the left or right edge. + position.x = scrollRightDirection ? 0.f : mVisualModel->mControlSize.width; + } + + if( mEventData->mDecorator->IsVerticalScrollEnabled() ) + { + position.x = mEventData->mCursorHookPositionX; + + // Position the grag handle close to either the top or bottom edge. + position.y = scrollBottomDirection ? 0.f : mVisualModel->mControlSize.height; + } // Get the new handle position. // The grab handle's position is in decorator's coords. Need to transforms to text's coords. @@ -1344,22 +1451,38 @@ void Controller::Impl::OnHandleEvent( const Event& event ) position.x - mScrollPosition.x, position.y - mScrollPosition.y ); - mEventData->mUpdateCursorPosition = mEventData->mPrimaryCursorPosition != handlePosition; - mEventData->mScrollAfterUpdatePosition = mEventData->mUpdateCursorPosition; - mEventData->mPrimaryCursorPosition = handlePosition; + if( mEventData->mPrimaryCursorPosition != handlePosition ) + { + mEventData->mUpdateCursorPosition = true; + mEventData->mUpdateGrabHandlePosition = !isSmoothHandlePanEnabled; + mEventData->mScrollAfterUpdatePosition = true; + mEventData->mPrimaryCursorPosition = handlePosition; + } mEventData->mUpdateInputStyle = mEventData->mUpdateCursorPosition; + + // Updates the decorator if the soft handle panning is enabled. + mEventData->mDecoratorUpdated = isSmoothHandlePanEnabled; } else if( leftSelectionHandleEvent || rightSelectionHandleEvent ) { - // TODO: This is recalculating the selection box every time the text is scrolled with the selection handles. - // Think if something can be done to save power. - ChangeState( EventData::SELECTION_HANDLE_PANNING ); + // Get the selection handle position in decorator coords. Vector2 position = mEventData->mDecorator->GetPosition( leftSelectionHandleEvent ? Text::LEFT_SELECTION_HANDLE : Text::RIGHT_SELECTION_HANDLE ); - // Position the selection handle close to either the left or right edge. - position.x = scrollRightDirection ? 0.f : mVisualModel->mControlSize.width; + if( mEventData->mDecorator->IsHorizontalScrollEnabled() ) + { + // Position the selection handle close to either the left or right edge. + position.x = scrollRightDirection ? 0.f : mVisualModel->mControlSize.width; + } + + if( mEventData->mDecorator->IsVerticalScrollEnabled() ) + { + position.x = mEventData->mCursorHookPositionX; + + // Position the grag handle close to either the top or bottom edge. + position.y = scrollBottomDirection ? 0.f : mVisualModel->mControlSize.height; + } // Get the new handle position. // The selection handle's position is in decorator's coords. Need to transform to text's coords. @@ -1372,18 +1495,23 @@ void Controller::Impl::OnHandleEvent( const Event& event ) if( leftSelectionHandleEvent ) { const bool differentHandles = ( mEventData->mLeftSelectionPosition != handlePosition ) && ( mEventData->mRightSelectionPosition != handlePosition ); - mEventData->mUpdateLeftSelectionPosition = endOfScroll || differentHandles; - if( differentHandles ) + + if( differentHandles || endOfScroll ) { + mEventData->mUpdateHighlightBox = true; + mEventData->mUpdateLeftSelectionPosition = !isSmoothHandlePanEnabled; + mEventData->mUpdateRightSelectionPosition = isSmoothHandlePanEnabled; mEventData->mLeftSelectionPosition = handlePosition; } } else { const bool differentHandles = ( mEventData->mRightSelectionPosition != handlePosition ) && ( mEventData->mLeftSelectionPosition != handlePosition ); - mEventData->mUpdateRightSelectionPosition = endOfScroll || differentHandles; - if( differentHandles ) + if( differentHandles || endOfScroll ) { + mEventData->mUpdateHighlightBox = true; + mEventData->mUpdateRightSelectionPosition = !isSmoothHandlePanEnabled; + mEventData->mUpdateLeftSelectionPosition = isSmoothHandlePanEnabled; mEventData->mRightSelectionPosition = handlePosition; } } @@ -1392,7 +1520,7 @@ void Controller::Impl::OnHandleEvent( const Event& event ) { RepositionSelectionHandles(); - mEventData->mScrollAfterUpdatePosition = true; + mEventData->mScrollAfterUpdatePosition = !isSmoothHandlePanEnabled; } } mEventData->mDecoratorUpdated = true; @@ -1419,6 +1547,7 @@ void Controller::Impl::OnSelectEvent( const Event& event ) mEventData->mUpdateLeftSelectionPosition = true; mEventData->mUpdateRightSelectionPosition = true; + mEventData->mUpdateHighlightBox = true; mEventData->mUpdateCursorPosition = false; mEventData->mScrollAfterUpdatePosition = ( mEventData->mLeftSelectionPosition != mEventData->mRightSelectionPosition ); @@ -1443,6 +1572,7 @@ void Controller::Impl::OnSelectAllEvent() mEventData->mScrollAfterUpdatePosition = true; mEventData->mUpdateLeftSelectionPosition = true; mEventData->mUpdateRightSelectionPosition = true; + mEventData->mUpdateHighlightBox = true; } } @@ -1563,8 +1693,6 @@ void Controller::Impl::RepositionSelectionHandles() const CharacterIndex* const glyphToCharacterBuffer = mVisualModel->mGlyphsToCharacters.Begin(); const CharacterDirection* const modelCharacterDirectionsBuffer = ( 0u != mLogicalModel->mCharacterDirections.Count() ) ? mLogicalModel->mCharacterDirections.Begin() : NULL; - // TODO: Better algorithm to create the highlight box. - const bool isLastCharacter = selectionEnd >= mLogicalModel->mText.Count(); const CharacterDirection startDirection = ( ( NULL == modelCharacterDirectionsBuffer ) ? false : *( modelCharacterDirectionsBuffer + selectionStart ) ); const CharacterDirection endDirection = ( ( NULL == modelCharacterDirectionsBuffer ) ? false : *( modelCharacterDirectionsBuffer + ( selectionEnd - ( isLastCharacter ? 1u : 0u ) ) ) ); @@ -1610,6 +1738,8 @@ void Controller::Impl::RepositionSelectionHandles() // The line's vertical offset of all the lines before the line where the first glyph is laid-out. selectionBoxInfo->lineOffset = CalculateLineOffset( mVisualModel->mLines, firstLineIndex ); + selectionBoxInfo->lineOffset += mScrollPosition.y; + lineRun += firstLineIndex; // The line height is the addition of the line ascender and the line descender. @@ -1651,7 +1781,7 @@ void Controller::Impl::RepositionSelectionHandles() const float xPosition = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x + glyphAdvance * static_cast( isCurrentRightToLeft ? ( numberOfCharactersStart - interGlyphIndex - numberOfCharacters ) : interGlyphIndex ); const float xPositionAdvance = xPosition + static_cast( numberOfCharacters ) * glyphAdvance; - const float yPosition = selectionBoxInfo->lineOffset + mScrollPosition.y; + const float yPosition = selectionBoxInfo->lineOffset; // Store the min and max 'x' for each line. selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, xPosition ); @@ -1683,7 +1813,7 @@ void Controller::Impl::RepositionSelectionHandles() const float xPosition = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x + ( isCurrentRightToLeft ? ( glyphAdvance * static_cast( numberOfCharacters ) ) : 0.f ); const float xPositionAdvance = xPosition + static_cast( interGlyphIndex ) * glyphAdvance; - const float yPosition = selectionBoxInfo->lineOffset + mScrollPosition.y; + const float yPosition = selectionBoxInfo->lineOffset; // Store the min and max 'x' for each line. selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, xPosition ); @@ -1700,7 +1830,7 @@ void Controller::Impl::RepositionSelectionHandles() const float xPosition = lineRun->alignmentOffset + position.x - glyph.xBearing + mScrollPosition.x; const float xPositionAdvance = xPosition + glyph.advance; - const float yPosition = selectionBoxInfo->lineOffset + mScrollPosition.y; + const float yPosition = selectionBoxInfo->lineOffset; // Store the min and max 'x' for each line. selectionBoxInfo->minX = std::min( selectionBoxInfo->minX, xPosition ); @@ -1723,19 +1853,22 @@ void Controller::Impl::RepositionSelectionHandles() ++lineIndex; if( lineIndex < firstLineIndex + numberOfLines ) { - // Get the selection box info for the next line. + // Keep the offset and height of the current selection box. const float currentLineOffset = selectionBoxInfo->lineOffset; + const float currentLineHeight = selectionBoxInfo->lineHeight; + + // Get the selection box info for the next line. ++selectionBoxInfo; selectionBoxInfo->minX = MAX_FLOAT; selectionBoxInfo->maxX = MIN_FLOAT; + // Update the line's vertical offset. + selectionBoxInfo->lineOffset = currentLineOffset + currentLineHeight; + // The line height is the addition of the line ascender and the line descender. // However, the line descender has a negative value, hence the subtraction. selectionBoxInfo->lineHeight = lineRun->ascender - lineRun->descender; - - // Update the line's vertical offset. - selectionBoxInfo->lineOffset = currentLineOffset + selectionBoxInfo->lineHeight; } } } @@ -1817,27 +1950,30 @@ void Controller::Impl::RepositionSelectionHandles() } } + if( !mEventData->mDecorator->IsSmoothHandlePanEnabled() ) + { + CursorInfo primaryCursorInfo; + GetCursorPosition( mEventData->mLeftSelectionPosition, + primaryCursorInfo ); - CursorInfo primaryCursorInfo; - GetCursorPosition( mEventData->mLeftSelectionPosition, - primaryCursorInfo ); + const Vector2 primaryPosition = primaryCursorInfo.primaryPosition + mScrollPosition; - CursorInfo secondaryCursorInfo; - GetCursorPosition( mEventData->mRightSelectionPosition, - secondaryCursorInfo ); + mEventData->mDecorator->SetPosition( LEFT_SELECTION_HANDLE, + primaryPosition.x, + primaryCursorInfo.lineOffset + mScrollPosition.y, + primaryCursorInfo.lineHeight ); - const Vector2 primaryPosition = primaryCursorInfo.primaryPosition + mScrollPosition; - const Vector2 secondaryPosition = secondaryCursorInfo.primaryPosition + mScrollPosition; + CursorInfo secondaryCursorInfo; + GetCursorPosition( mEventData->mRightSelectionPosition, + secondaryCursorInfo ); - mEventData->mDecorator->SetPosition( LEFT_SELECTION_HANDLE, - primaryPosition.x, - primaryCursorInfo.lineOffset + mScrollPosition.y, - primaryCursorInfo.lineHeight ); + const Vector2 secondaryPosition = secondaryCursorInfo.primaryPosition + mScrollPosition; - mEventData->mDecorator->SetPosition( RIGHT_SELECTION_HANDLE, - secondaryPosition.x, - secondaryCursorInfo.lineOffset + mScrollPosition.y, - secondaryCursorInfo.lineHeight ); + mEventData->mDecorator->SetPosition( RIGHT_SELECTION_HANDLE, + secondaryPosition.x, + secondaryCursorInfo.lineOffset + mScrollPosition.y, + secondaryCursorInfo.lineHeight ); + } // Cursor to be positioned at end of selection so if selection interrupted and edit mode restarted the cursor will be at end of selection mEventData->mPrimaryCursorPosition = ( indicesSwapped ) ? mEventData->mLeftSelectionPosition : mEventData->mRightSelectionPosition; @@ -2219,6 +2355,9 @@ CharacterIndex Controller::Impl::CalculateNewCursorIndex( CharacterIndex index ) cursorIndex += numberOfCharacters; } + // Will update the cursor hook position. + mEventData->mUpdateCursorHookPosition = true; + return cursorIndex; } @@ -2242,11 +2381,14 @@ void Controller::Impl::UpdateCursorPosition( const CursorInfo& cursorInfo ) cursorInfo.lineHeight ); DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Primary cursor position: %f,%f\n", cursorPosition.x, cursorPosition.y ); - // Sets the grab handle position. - mEventData->mDecorator->SetPosition( GRAB_HANDLE, - cursorPosition.x, - cursorInfo.lineOffset + mScrollPosition.y, - cursorInfo.lineHeight ); + if( mEventData->mUpdateGrabHandlePosition ) + { + // Sets the grab handle position. + mEventData->mDecorator->SetPosition( GRAB_HANDLE, + cursorPosition.x, + cursorInfo.lineOffset + mScrollPosition.y, + cursorInfo.lineHeight ); + } if( cursorInfo.isSecondaryCursor ) { @@ -2336,24 +2478,37 @@ void Controller::Impl::ClampVerticalScroll( const Vector2& actualSize ) } } -void Controller::Impl::ScrollToMakePositionVisible( const Vector2& position ) +void Controller::Impl::ScrollToMakePositionVisible( const Vector2& position, float lineHeight ) { - const float cursorWidth = mEventData->mDecorator ? mEventData->mDecorator->GetCursorWidth() : 0.f; + const float cursorWidth = mEventData->mDecorator ? static_cast( mEventData->mDecorator->GetCursorWidth() ) : 0.f; // position is in actor's coords. - const float positionEnd = position.x + cursorWidth; + const float positionEndX = position.x + cursorWidth; + const float positionEndY = position.y + lineHeight; // Transform the position to decorator coords. - const float decoratorPositionBegin = position.x + mScrollPosition.x; - const float decoratorPositionEnd = positionEnd + mScrollPosition.x; + const float decoratorPositionBeginX = position.x + mScrollPosition.x; + const float decoratorPositionEndX = positionEndX + mScrollPosition.x; - if( decoratorPositionBegin < 0.f ) + const float decoratorPositionBeginY = position.y + mScrollPosition.y; + const float decoratorPositionEndY = positionEndY + mScrollPosition.y; + + if( decoratorPositionBeginX < 0.f ) { mScrollPosition.x = -position.x; } - else if( decoratorPositionEnd > mVisualModel->mControlSize.width ) + else if( decoratorPositionEndX > mVisualModel->mControlSize.width ) + { + mScrollPosition.x = mVisualModel->mControlSize.width - positionEndX; + } + + if( decoratorPositionBeginY < 0.f ) + { + mScrollPosition.y = -position.y; + } + else if( decoratorPositionEndY > mVisualModel->mControlSize.height ) { - mScrollPosition.x = mVisualModel->mControlSize.width - positionEnd; + mScrollPosition.y = mVisualModel->mControlSize.height - positionEndY; } } @@ -2364,11 +2519,13 @@ void Controller::Impl::ScrollTextToMatchCursor( const CursorInfo& cursorInfo ) // Calculate the offset to match the cursor position before the character was deleted. mScrollPosition.x = currentCursorPosition.x - cursorInfo.primaryPosition.x; + mScrollPosition.y = currentCursorPosition.y - cursorInfo.lineOffset; ClampHorizontalScroll( mVisualModel->GetLayoutSize() ); + ClampVerticalScroll( mVisualModel->GetLayoutSize() ); // Makes the new cursor position visible if needed. - ScrollToMakePositionVisible( cursorInfo.primaryPosition ); + ScrollToMakePositionVisible( cursorInfo.primaryPosition, cursorInfo.lineHeight ); } void Controller::Impl::RequestRelayout() diff --git a/dali-toolkit/internal/text/text-controller-impl.h b/dali-toolkit/internal/text/text-controller-impl.h index 003aec5..99693ad 100644 --- a/dali-toolkit/internal/text/text-controller-impl.h +++ b/dali-toolkit/internal/text/text-controller-impl.h @@ -126,6 +126,8 @@ struct EventData CharacterIndex mPreEditStartPosition; ///< Used to remove the pre-edit text if necessary. Length mPreEditLength; ///< Used to remove the pre-edit text if necessary. + float mCursorHookPositionX; ///< Used to move the cursor with the keys or when scrolling the text vertically with the handles. + bool mIsShowingPlaceholderText : 1; ///< True if the place-holder text is being displayed. bool mPreEditFlag : 1; ///< True if the model contains text in pre-edit state. bool mDecoratorUpdated : 1; ///< True if the decorator was updated during event processing. @@ -133,11 +135,12 @@ struct EventData bool mGrabHandleEnabled : 1; ///< True if grab handle is enabled. bool mGrabHandlePopupEnabled : 1; ///< True if the grab handle popu-up should be shown. bool mSelectionEnabled : 1; ///< True if selection handles, highlight etc. are enabled. - bool mHorizontalScrollingEnabled : 1; ///< True if horizontal scrolling is enabled. - bool mVerticalScrollingEnabled : 1; ///< True if vertical scrolling is enabled. + bool mUpdateCursorHookPosition : 1; ///< True if the cursor hook position must be updated. Used to move the cursor with the keys 'up' and 'down'. bool mUpdateCursorPosition : 1; ///< True if the visual position of the cursor must be recalculated. + bool mUpdateGrabHandlePosition : 1; ///< True if the visual position of the grab handle must be recalculated. bool mUpdateLeftSelectionPosition : 1; ///< True if the visual position of the left selection handle must be recalculated. bool mUpdateRightSelectionPosition : 1; ///< True if the visual position of the right selection handle must be recalculated. + bool mUpdateHighlightBox : 1; ///< True if the text selection high light box must be updated. bool mScrollAfterUpdatePosition : 1; ///< Whether to scroll after the cursor position is updated. bool mScrollAfterDelete : 1; ///< Whether to scroll after delete characters. bool mAllTextSelected : 1; ///< True if the selection handles are selecting all the text. @@ -647,11 +650,12 @@ struct Controller::Impl * @pre mEventData must not be NULL. (there is a text-input or selection capabilities). * * @param[in] position A position in text coords. + * @param[in] lineHeight The line height for the given position. * * This method is called after inserting text, moving the cursor with the grab handle or the keypad, * or moving the selection handles. */ - void ScrollToMakePositionVisible( const Vector2& position ); + void ScrollToMakePositionVisible( const Vector2& position, float lineHeight ); /** * @brief Scrolls the text to make the cursor visible. diff --git a/dali-toolkit/internal/text/text-controller.cpp b/dali-toolkit/internal/text/text-controller.cpp index 9e1d0dc..1cf95fd 100644 --- a/dali-toolkit/internal/text/text-controller.cpp +++ b/dali-toolkit/internal/text/text-controller.cpp @@ -93,6 +93,7 @@ FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData, // Recalculate the selection highlight as the metrics may have changed. eventData->mUpdateLeftSelectionPosition = true; eventData->mUpdateRightSelectionPosition = true; + eventData->mUpdateHighlightBox = true; return fontDescriptionRun; } @@ -193,6 +194,69 @@ float Controller::GetAutoScrollLineAlignment() const return offset; } +void Controller::SetHorizontalScrollEnabled( bool enable ) +{ + if( ( NULL != mImpl->mEventData ) && + mImpl->mEventData->mDecorator ) + { + mImpl->mEventData->mDecorator->SetHorizontalScrollEnabled( enable ); + } +} + +bool Controller::IsHorizontalScrollEnabled() const +{ + if( ( NULL != mImpl->mEventData ) && + mImpl->mEventData->mDecorator ) + { + return mImpl->mEventData->mDecorator->IsHorizontalScrollEnabled(); + } + + return false; +} + +void Controller::SetVerticalScrollEnabled( bool enable ) +{ + if( ( NULL != mImpl->mEventData ) && + mImpl->mEventData->mDecorator ) + { + if( mImpl->mEventData->mDecorator ) + { + mImpl->mEventData->mDecorator->SetVerticalScrollEnabled( enable ); + } + } +} + +bool Controller::IsVerticalScrollEnabled() const +{ + if( ( NULL != mImpl->mEventData ) && + mImpl->mEventData->mDecorator ) + { + return mImpl->mEventData->mDecorator->IsVerticalScrollEnabled(); + } + + return false; +} + +void Controller::SetSmoothHandlePanEnabled( bool enable ) +{ + if( ( NULL != mImpl->mEventData ) && + mImpl->mEventData->mDecorator ) + { + mImpl->mEventData->mDecorator->SetSmoothHandlePanEnabled( enable ); + } +} + +bool Controller::IsSmoothHandlePanEnabled() const +{ + if( ( NULL != mImpl->mEventData ) && + mImpl->mEventData->mDecorator ) + { + return mImpl->mEventData->mDecorator->IsSmoothHandlePanEnabled(); + } + + return false; +} + void Controller::SetText( const std::string& text ) { DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" ); @@ -874,6 +938,7 @@ void Controller::SetInputFontFamily( const std::string& fontFamily ) // As the font changes, recalculate the handle positions is needed. mImpl->mEventData->mUpdateLeftSelectionPosition = true; mImpl->mEventData->mUpdateRightSelectionPosition = true; + mImpl->mEventData->mUpdateHighlightBox = true; mImpl->mEventData->mScrollAfterUpdatePosition = true; } } @@ -947,6 +1012,7 @@ void Controller::SetInputFontWeight( FontWeight weight ) // As the font might change, recalculate the handle positions is needed. mImpl->mEventData->mUpdateLeftSelectionPosition = true; mImpl->mEventData->mUpdateRightSelectionPosition = true; + mImpl->mEventData->mUpdateHighlightBox = true; mImpl->mEventData->mScrollAfterUpdatePosition = true; } } @@ -1000,6 +1066,7 @@ void Controller::SetInputFontWidth( FontWidth width ) // As the font might change, recalculate the handle positions is needed. mImpl->mEventData->mUpdateLeftSelectionPosition = true; mImpl->mEventData->mUpdateRightSelectionPosition = true; + mImpl->mEventData->mUpdateHighlightBox = true; mImpl->mEventData->mScrollAfterUpdatePosition = true; } } @@ -1053,6 +1120,7 @@ void Controller::SetInputFontSlant( FontSlant slant ) // As the font might change, recalculate the handle positions is needed. mImpl->mEventData->mUpdateLeftSelectionPosition = true; mImpl->mEventData->mUpdateRightSelectionPosition = true; + mImpl->mEventData->mUpdateHighlightBox = true; mImpl->mEventData->mScrollAfterUpdatePosition = true; } } @@ -1105,6 +1173,7 @@ void Controller::SetInputFontPointSize( float size ) // As the font might change, recalculate the handle positions is needed. mImpl->mEventData->mUpdateLeftSelectionPosition = true; mImpl->mEventData->mUpdateRightSelectionPosition = true; + mImpl->mEventData->mUpdateHighlightBox = true; mImpl->mEventData->mScrollAfterUpdatePosition = true; } } @@ -2313,6 +2382,7 @@ void Controller::TapEvent( unsigned int tapCount, float x, float y ) } else { + // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE ); } relayoutNeeded = true; @@ -2363,7 +2433,6 @@ void Controller::TapEvent( unsigned int tapCount, float x, float y ) } void Controller::PanEvent( Gesture::State state, const Vector2& displacement ) - // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing { DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" ); diff --git a/dali-toolkit/internal/text/text-controller.h b/dali-toolkit/internal/text/text-controller.h index 8517ca5..304ad2f 100644 --- a/dali-toolkit/internal/text/text-controller.h +++ b/dali-toolkit/internal/text/text-controller.h @@ -188,6 +188,48 @@ public: float GetAutoScrollLineAlignment() const; /** + * @brief Enables the horizontal scrolling. + * + * @param[in] enable Whether to enable the horizontal scrolling. + */ + void SetHorizontalScrollEnabled( bool enable ); + + /** + * @brief Retrieves whether the horizontal scrolling is enabled. + * + * @return @e true if the horizontal scrolling is enabled, otherwise it returns @e false. + */ + bool IsHorizontalScrollEnabled() const; + + /** + * @brief Enables the vertical scrolling. + * + * @param[in] enable Whether to enable the vertical scrolling. + */ + void SetVerticalScrollEnabled( bool enable ); + + /** + * @brief Retrieves whether the verticall scrolling is enabled. + * + * @return @e true if the vertical scrolling is enabled, otherwise it returns @e false. + */ + bool IsVerticalScrollEnabled() const; + + /** + * @brief Enables the smooth handle panning. + * + * @param[in] enable Whether to enable the smooth handle panning. + */ + void SetSmoothHandlePanEnabled( bool enable ); + + /** + * @brief Retrieves whether the smooth handle panning is enabled. + * + * @return @e true if the smooth handle panning is enabled. + */ + bool IsSmoothHandlePanEnabled() const; + + /** * @brief Replaces any text previously set. * * @note This will be converted into UTF-32 when stored in the text model. -- 2.7.4