2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/adaptor-framework/timer.h>
25 #include <dali/public-api/actors/image-actor.h>
26 #include <dali/public-api/actors/layer.h>
27 #include <dali/public-api/animation/constraint.h>
28 #include <dali/public-api/common/constants.h>
29 #include <dali/public-api/common/stage.h>
30 #include <dali/public-api/events/tap-gesture.h>
31 #include <dali/public-api/events/tap-gesture-detector.h>
32 #include <dali/public-api/events/touch-event.h>
33 #include <dali/public-api/events/pan-gesture.h>
34 #include <dali/public-api/events/pan-gesture-detector.h>
35 #include <dali/public-api/images/resource-image.h>
36 #include <dali/public-api/math/rect.h>
37 #include <dali/public-api/math/vector2.h>
38 #include <dali/public-api/math/vector4.h>
39 #include <dali/public-api/object/property-notification.h>
40 #include <dali/public-api/signals/connection-tracker.h>
43 #include <dali-toolkit/public-api/controls/control.h>
44 #include <dali-toolkit/public-api/controls/control-impl.h>
45 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
46 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
47 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
48 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
51 #define DECORATOR_DEBUG
61 #ifdef DECORATOR_DEBUG
62 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_TEXT_DECORATOR") );
73 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "insertpoint-icon.png" );
74 const char* DEFAULT_GRAB_HANDLE_IMAGE_PRESSED( DALI_IMAGE_DIR "insertpoint-icon-pressed.png" );
75 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
76 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
78 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
79 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
81 const Dali::Vector4 LIGHT_BLUE( 0.07f, 0.41f, 0.59f, 1.0f ); // The text highlight color.
83 const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval
84 const float TO_MILLISECONDS = 1000.f;
85 const float TO_SECONDS = 1.f / TO_MILLISECONDS;
87 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
89 const unsigned int SCROLL_TICK_INTERVAL = 50u;
91 const float SCROLL_THRESHOLD = 10.f;
92 const float SCROLL_SPEED = 300.f;
93 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
96 * structure to hold coordinates of each quad, which will make up the mesh.
98 struct QuadCoordinates
101 * Default constructor
109 * @param[in] x1 left co-ordinate
110 * @param[in] y1 top co-ordinate
111 * @param[in] x2 right co-ordinate
112 * @param[in] y2 bottom co-ordinate
114 QuadCoordinates(float x1, float y1, float x2, float y2)
120 Dali::Vector2 min; ///< top-left (minimum) position of quad
121 Dali::Vector2 max; ///< bottom-right (maximum) position of quad
124 typedef std::vector<QuadCoordinates> QuadContainer;
127 * @brief Takes a bounding rectangle in the local coordinates of an actor and returns the world coordinates Bounding Box.
128 * @param[in] boundingRectangle local bounding
129 * @param[out] Vector4 World coordinate bounding Box.
131 void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangle, Dali::Vector4& boundingBox )
133 // Convert to world coordinates and store as a Vector4 to be compatible with Property Notifications.
134 Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
136 const float originX = boundingRectangle.x - 0.5f * stageSize.width;
137 const float originY = boundingRectangle.y - 0.5f * stageSize.height;
139 boundingBox = Dali::Vector4( originX,
141 originX + boundingRectangle.width,
142 originY + boundingRectangle.height );
146 } // end of namespace
157 struct Decorator::Impl : public ConnectionTracker
171 : color( Dali::Color::BLACK ),
173 cursorHeight( 0.0f ),
189 grabDisplacementX( 0.f ),
190 grabDisplacementY( 0.f ),
201 float lineHeight; ///< Not the handle height
202 float grabDisplacementX;
203 float grabDisplacementY;
209 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
210 : mTextControlParent( parent ),
211 mObserver( observer ),
212 mBoundingBox( Rect<int>() ),
213 mHighlightColor( LIGHT_BLUE ),
214 mActiveCursor( ACTIVE_CURSOR_NONE ),
215 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
216 mCursorBlinkDuration( 0.0f ),
217 mHandleScrolling( HANDLE_TYPE_COUNT ),
218 mScrollDirection( SCROLL_NONE ),
219 mScrollThreshold( SCROLL_THRESHOLD ),
220 mScrollSpeed( SCROLL_SPEED ),
221 mScrollDistance( SCROLL_DISTANCE ),
222 mActiveCopyPastePopup( false ),
223 mCursorBlinkStatus( true ),
224 mPrimaryCursorVisible( false ),
225 mSecondaryCursorVisible( false )
230 * Relayout of the decorations owned by the decorator.
231 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
233 void Relayout( const Vector2& size )
235 // TODO - Remove this if nothing is active
239 // Show or hide the cursors
243 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
244 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
245 if( mPrimaryCursorVisible )
247 Vector2 position = cursor.position;
248 if( GRAB_HANDLE == mHandleScrolling )
250 if( mScrollDirection == SCROLL_RIGHT )
256 position.x = size.width;
260 mPrimaryCursor.SetPosition( position.x,
262 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
264 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
266 if( mSecondaryCursor )
268 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
269 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
270 if( mSecondaryCursorVisible )
272 mSecondaryCursor.SetPosition( cursor.position.x,
274 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
276 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
279 // Show or hide the grab handle
280 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
281 if( grabHandle.active )
283 Vector2 position = grabHandle.position;
285 if( GRAB_HANDLE == mHandleScrolling )
287 if( mScrollDirection == SCROLL_RIGHT )
293 position.x = size.width;
297 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
305 grabHandle.actor.SetPosition( position.x,
306 position.y + grabHandle.lineHeight );
308 grabHandle.actor.SetVisible( isVisible );
310 else if( grabHandle.actor )
312 UnparentAndReset( grabHandle.actor );
315 // Show or hide the selection handles/highlight
316 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
317 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
318 if( primary.active || secondary.active )
322 CreateSelectionHandles();
324 primary.actor.SetPosition( primary.position.x,
325 primary.position.y + primary.lineHeight );
327 secondary.actor.SetPosition( secondary.position.x,
328 secondary.position.y + secondary.lineHeight );
335 UnparentAndReset( primary.actor );
336 UnparentAndReset( secondary.actor );
337 //UnparentAndReset( mHighlightMeshActor );
340 if ( mActiveCopyPastePopup )
342 if ( !mCopyPastePopup )
344 mCopyPastePopup = TextSelectionPopup::New();
345 #ifdef DECORATOR_DEBUG
346 mCopyPastePopup.SetName("mCopyPastePopup");
348 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
349 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
350 mActiveLayer.Add ( mCopyPastePopup );
355 if ( mCopyPastePopup )
357 UnparentAndReset( mCopyPastePopup );
362 void UpdatePositions( const Vector2& scrollOffset )
364 mCursor[PRIMARY_CURSOR].position += scrollOffset;
365 mCursor[SECONDARY_CURSOR].position += scrollOffset;
366 mHandle[ GRAB_HANDLE ].position += scrollOffset;
367 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
368 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
370 // TODO Highlight box??
373 void PopUpRelayoutComplete( Actor actor )
375 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
377 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
379 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
381 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
383 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
385 SetUpPopUpPositionNotifications();
387 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
390 void CreateCursor( ImageActor& cursor, const Vector4& color )
392 cursor = CreateSolidColorActor( color );
393 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
394 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
397 // Add or Remove cursor(s) from parent
401 if( mActiveCursor == ACTIVE_CURSOR_NONE )
403 UnparentAndReset( mPrimaryCursor );
404 UnparentAndReset( mSecondaryCursor );
408 // Create Primary and or Secondary Cursor(s) if active and add to parent
409 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
410 mActiveCursor == ACTIVE_CURSOR_BOTH )
412 if ( !mPrimaryCursor )
414 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
415 #ifdef DECORATOR_DEBUG
416 mPrimaryCursor.SetName( "PrimaryCursorActor" );
418 mActiveLayer.Add( mPrimaryCursor );
422 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
424 if ( !mSecondaryCursor )
426 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
427 #ifdef DECORATOR_DEBUG
428 mSecondaryCursor.SetName( "SecondaryCursorActor" );
430 mActiveLayer.Add( mSecondaryCursor );
435 UnparentAndReset( mSecondaryCursor );
441 bool OnCursorBlinkTimerTick()
445 if ( mPrimaryCursor )
447 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
449 if ( mSecondaryCursor )
451 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
454 mCursorBlinkStatus = !mCursorBlinkStatus;
459 void SetupTouchEvents()
463 mTapDetector = TapGestureDetector::New();
464 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
467 if ( !mPanGestureDetector )
469 mPanGestureDetector = PanGestureDetector::New();
470 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
474 void CreateActiveLayer()
478 Actor parent = mTextControlParent.Self();
480 mActiveLayer = Layer::New();
481 #ifdef DECORATOR_DEBUG
482 mActiveLayer.SetName ( "ActiveLayerActor" );
485 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
486 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
487 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
489 parent.Add( mActiveLayer );
492 mActiveLayer.RaiseToTop();
495 void CreateGrabHandle()
497 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
498 if( !grabHandle.actor )
500 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
502 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
504 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
506 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
509 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
510 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
511 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
512 // Area that Grab handle responds to, larger than actual handle so easier to move
513 #ifdef DECORATOR_DEBUG
514 grabHandle.actor.SetName( "GrabHandleActor" );
515 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
517 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
518 grabHandle.grabArea.SetName( "GrabArea" );
522 grabHandle.grabArea = Actor::New();
523 grabHandle.grabArea.SetName( "GrabArea" );
526 grabHandle.grabArea = Actor::New();
529 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
530 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
531 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
532 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
533 grabHandle.actor.Add( grabHandle.grabArea );
535 grabHandle.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnGrabHandleTouched );
536 mTapDetector.Attach( grabHandle.grabArea );
537 mPanGestureDetector.Attach( grabHandle.grabArea );
539 mActiveLayer.Add( grabHandle.actor );
543 void CreateSelectionHandles()
545 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
548 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
550 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
553 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
554 #ifdef DECORATOR_DEBUG
555 primary.actor.SetName("SelectionHandleOne");
557 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
558 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
559 primary.flipped = false;
561 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
562 #ifdef DECORATOR_DEBUG
563 primary.grabArea.SetName("SelectionHandleOneGrabArea");
565 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
566 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
567 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
569 mTapDetector.Attach( primary.grabArea );
570 mPanGestureDetector.Attach( primary.grabArea );
571 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
573 primary.actor.Add( primary.grabArea );
574 mActiveLayer.Add( primary.actor );
577 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
578 if( !secondary.actor )
580 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
582 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
585 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
586 #ifdef DECORATOR_DEBUG
587 secondary.actor.SetName("SelectionHandleTwo");
589 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
590 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
591 secondary.flipped = false;
593 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
594 #ifdef DECORATOR_DEBUG
595 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
597 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
598 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
599 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
601 mTapDetector.Attach( secondary.grabArea );
602 mPanGestureDetector.Attach( secondary.grabArea );
603 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
605 secondary.actor.Add( secondary.grabArea );
606 mActiveLayer.Add( secondary.actor );
610 void CreateHighlight()
613 if ( !mHighlightMeshActor )
615 mHighlightMaterial = Material::New( "HighlightMaterial" );
616 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
618 mHighlightMeshData.SetMaterial( mHighlightMaterial );
619 mHighlightMeshData.SetHasNormals( true );
621 mHighlightMesh = Mesh::New( mHighlightMeshData );
623 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
624 #ifdef DECORATOR_DEBUG
625 mHighlightMeshActor.SetName( "HighlightMeshActor" );
627 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
628 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
630 Actor parent = mTextControlParent.Self();
631 parent.Add( mHighlightMeshActor );
636 void UpdateHighlight()
638 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
640 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
642 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
643 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
644 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
645 // [BOTTOM] [ MIDDLE ]
648 // Each quad is created as 2 triangles.
649 // Middle is just 1 quad regardless of its size.
664 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
666 MeshData::VertexContainer vertices;
667 Dali::MeshData::FaceIndices faceIndices;
669 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
670 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
672 // vertex position defaults to (0 0 0)
673 MeshData::Vertex vertex;
674 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
677 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
679 // Add each quad geometry (a sub-selection) to the mesh data.
689 QuadCoordinates& quad = *iter;
691 vertex.x = quad.min.x;
692 vertex.y = quad.min.y;
693 vertices.push_back( vertex );
696 vertex.x = quad.max.x;
697 vertex.y = quad.min.y;
698 vertices.push_back( vertex );
701 vertex.x = quad.min.x;
702 vertex.y = quad.max.y;
703 vertices.push_back( vertex );
705 // bottom-right (v+3)
706 vertex.x = quad.max.x;
707 vertex.y = quad.max.y;
708 vertices.push_back( vertex );
710 // triangle A (3, 1, 0)
711 faceIndices.push_back( v + 3 );
712 faceIndices.push_back( v + 1 );
713 faceIndices.push_back( v );
715 // triangle B (0, 2, 3)
716 faceIndices.push_back( v );
717 faceIndices.push_back( v + 2 );
718 faceIndices.push_back( v + 3 );
720 mHighlightMeshData.SetFaceIndices( faceIndices );
723 BoneContainer bones(0); // passed empty as bones not required
724 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
725 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
730 void OnTap( Actor actor, const TapGesture& tap )
732 if( actor == mHandle[GRAB_HANDLE].actor )
738 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
740 if( Gesture::Started == gesture.state )
742 handle.grabDisplacementX = handle.grabDisplacementY = 0;
745 handle.grabDisplacementX += gesture.displacement.x;
746 handle.grabDisplacementY += gesture.displacement.y;
748 const float x = handle.position.x + handle.grabDisplacementX;
749 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
751 if( Gesture::Started == gesture.state ||
752 Gesture::Continuing == gesture.state )
754 if( x < mScrollThreshold )
756 mScrollDirection = SCROLL_RIGHT;
757 mHandleScrolling = type;
760 else if( x > mTextControlParent.Self().GetTargetSize().width - mScrollThreshold )
762 mScrollDirection = SCROLL_LEFT;
763 mHandleScrolling = type;
768 mHandleScrolling = HANDLE_TYPE_COUNT;
770 mObserver.HandleEvent( type, HANDLE_PRESSED, x, y );
773 else if( Gesture::Finished == gesture.state ||
774 Gesture::Cancelled == gesture.state )
776 if( mScrollTimer && mScrollTimer.IsRunning() )
778 mHandleScrolling = HANDLE_TYPE_COUNT;
780 mObserver.HandleEvent( type, HANDLE_STOP_SCROLLING, x, y );
784 mObserver.HandleEvent( type, HANDLE_RELEASED, x, y );
786 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
790 void OnPan( Actor actor, const PanGesture& gesture )
792 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
793 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
794 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
796 if( actor == grabHandle.grabArea )
798 DoPan( grabHandle, GRAB_HANDLE, gesture );
800 else if( actor == primarySelectionHandle.grabArea )
802 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
804 else if( actor == secondarySelectionHandle.grabArea )
806 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
810 bool OnGrabHandleTouched( Actor actor, const TouchEvent& event )
812 // Switch between pressed/release grab-handle images
813 if( event.GetPointCount() > 0 &&
814 mHandle[GRAB_HANDLE].actor )
816 const TouchPoint& point = event.GetPoint(0);
818 if( TouchPoint::Down == point.state &&
819 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
821 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] );
823 else if( TouchPoint::Up == point.state &&
824 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
826 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
830 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
834 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
837 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
841 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
844 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
850 float AlternatePopUpPositionRelativeToCursor()
852 float alternativePosition=0.0f;;
855 if ( mPrimaryCursor ) // Secondary cursor not used for paste
857 Cursor cursor = PRIMARY_CURSOR;
858 alternativePosition = mCursor[cursor].position.y;
861 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
863 if( mHandle[GRAB_HANDLE].active )
865 // If grab handle enabled then position pop-up below the grab handle.
866 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
867 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
868 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
872 alternativePosition += popupHeight;
875 return alternativePosition;
878 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
880 float alternativeYPosition=0.0f;
881 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
882 // if can't be positioned above, then position below row.
883 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
885 mCopyPastePopup.SetY( alternativeYPosition );
889 void SetUpPopUpPositionNotifications( )
891 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
893 // Exceeding vertical boundary
895 Vector4 worldCoordinatesBoundingBox;
896 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
898 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
900 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
901 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
902 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
904 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
907 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
909 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
911 // Parent must already by added to Stage for these Get calls to work
912 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
913 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
914 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
915 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
917 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
918 Vector4 boundingRectangleWorld;
919 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
921 // Calculate distance to move popup (in local space) so fits within the boundary
922 float xOffSetToKeepWithinBounds = 0.0f;
923 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
925 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
927 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
929 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
932 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
933 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
935 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
938 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
941 void SetScrollThreshold( float threshold )
943 mScrollThreshold = threshold;
946 float GetScrollThreshold() const
948 return mScrollThreshold;
951 void SetScrollSpeed( float speed )
953 mScrollSpeed = speed;
954 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
957 float GetScrollSpeed() const
963 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
965 * It only starts the timer if it's already created.
967 void StartScrollTimer()
971 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
972 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
975 if( !mScrollTimer.IsRunning() )
977 mScrollTimer.Start();
982 * Stops the timer used to scroll the text.
984 void StopScrollTimer()
993 * Callback called by the timer used to scroll the text.
995 * It calculates and sets a new scroll position.
997 bool OnScrollTimerTick()
999 if( HANDLE_TYPE_COUNT != mHandleScrolling )
1001 mObserver.HandleEvent( mHandleScrolling,
1003 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
1010 Internal::Control& mTextControlParent;
1011 Observer& mObserver;
1013 TapGestureDetector mTapDetector;
1014 PanGestureDetector mPanGestureDetector;
1015 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
1016 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
1018 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
1019 //ImageActor mPrimaryCursor;
1020 //ImageActor mSecondaryCursor;
1021 //MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
1022 TextSelectionPopup mCopyPastePopup;
1024 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1026 //Mesh mHighlightMesh; ///< Mesh for highlight
1027 //MeshData mHighlightMeshData; ///< Mesh Data for highlight
1028 //Material mHighlightMaterial; ///< Material used for highlight
1030 CursorImpl mCursor[CURSOR_COUNT];
1031 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1032 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1034 Rect<int> mBoundingBox;
1035 Vector4 mHighlightColor; ///< Color of the highlight
1037 unsigned int mActiveCursor;
1038 unsigned int mCursorBlinkInterval;
1039 float mCursorBlinkDuration;
1040 HandleType mHandleScrolling; ///< The handle which is scrolling.
1041 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1042 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1043 float mScrollSpeed; ///< The scroll speed in pixels per second.
1044 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1045 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1047 bool mActiveCopyPastePopup : 1;
1048 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1049 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1050 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1053 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
1055 return DecoratorPtr( new Decorator(parent, observer) );
1058 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1060 mImpl->mBoundingBox = boundingBox;
1063 const Rect<int>& Decorator::GetBoundingBox() const
1065 return mImpl->mBoundingBox;
1068 void Decorator::Relayout( const Vector2& size )
1070 mImpl->Relayout( size );
1073 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1075 mImpl->UpdatePositions( scrollOffset );
1080 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1082 mImpl->mActiveCursor = activeCursor;
1085 unsigned int Decorator::GetActiveCursor() const
1087 return mImpl->mActiveCursor;
1090 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1092 mImpl->mCursor[cursor].position.x = x;
1093 mImpl->mCursor[cursor].position.y = y;
1094 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1095 mImpl->mCursor[cursor].lineHeight = lineHeight;
1098 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1100 x = mImpl->mCursor[cursor].position.x;
1101 y = mImpl->mCursor[cursor].position.y;
1102 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1103 lineHeight = mImpl->mCursor[cursor].lineHeight;
1106 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1108 return mImpl->mCursor[cursor].position;
1111 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1113 mImpl->mCursor[cursor].color = color;
1116 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1118 return mImpl->mCursor[cursor].color;
1121 void Decorator::StartCursorBlink()
1123 if ( !mImpl->mCursorBlinkTimer )
1125 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1126 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1129 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1131 mImpl->mCursorBlinkTimer.Start();
1135 void Decorator::StopCursorBlink()
1137 if ( mImpl->mCursorBlinkTimer )
1139 mImpl->mCursorBlinkTimer.Stop();
1143 void Decorator::SetCursorBlinkInterval( float seconds )
1145 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1148 float Decorator::GetCursorBlinkInterval() const
1150 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1153 void Decorator::SetCursorBlinkDuration( float seconds )
1155 mImpl->mCursorBlinkDuration = seconds;
1158 float Decorator::GetCursorBlinkDuration() const
1160 return mImpl->mCursorBlinkDuration;
1165 void Decorator::SetHandleActive( HandleType handleType, bool active )
1167 mImpl->mHandle[handleType].active = active;
1170 bool Decorator::IsHandleActive( HandleType handleType ) const
1172 return mImpl->mHandle[handleType].active ;
1175 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1177 mImpl->mHandleImages[handleType][handleImageType] = image;
1180 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1182 return mImpl->mHandleImages[handleType][handleImageType];
1185 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1187 // Adjust grab handle displacement
1188 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1190 handle.grabDisplacementX -= x - handle.position.x;
1191 handle.grabDisplacementY -= y - handle.position.y;
1193 handle.position.x = x;
1194 handle.position.y = y;
1195 handle.lineHeight = height;
1198 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1200 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1202 x = handle.position.x;
1203 y = handle.position.y;
1204 height = handle.lineHeight;
1207 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1209 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1212 void Decorator::ClearHighlights()
1214 mImpl->mHighlightQuadList.clear();
1217 void Decorator::SetHighlightColor( const Vector4& color )
1219 mImpl->mHighlightColor = color;
1222 const Vector4& Decorator::GetHighlightColor() const
1224 return mImpl->mHighlightColor;
1227 void Decorator::SetPopupActive( bool active )
1229 mImpl->mActiveCopyPastePopup = active;
1232 bool Decorator::IsPopupActive() const
1234 return mImpl->mActiveCopyPastePopup ;
1239 void Decorator::SetScrollThreshold( float threshold )
1241 mImpl->SetScrollThreshold( threshold );
1244 float Decorator::GetScrollThreshold() const
1246 return mImpl->GetScrollThreshold();
1249 void Decorator::SetScrollSpeed( float speed )
1251 mImpl->SetScrollSpeed( speed );
1254 float Decorator::GetScrollSpeed() const
1256 return mImpl->GetScrollSpeed();
1259 Decorator::~Decorator()
1264 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
1267 mImpl = new Decorator::Impl( parent, observer );
1272 } // namespace Toolkit