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/pan-gesture.h>
33 #include <dali/public-api/events/pan-gesture-detector.h>
34 #include <dali/public-api/images/resource-image.h>
35 #include <dali/public-api/math/rect.h>
36 #include <dali/public-api/math/vector2.h>
37 #include <dali/public-api/math/vector4.h>
38 #include <dali/public-api/object/property-notification.h>
39 #include <dali/public-api/signals/connection-tracker.h>
42 #include <dali-toolkit/public-api/controls/control.h>
43 #include <dali-toolkit/public-api/controls/control-impl.h>
44 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
45 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
46 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
47 #include <dali-toolkit/public-api/controls/text-controls/text-selection-popup.h>
50 #define DECORATOR_DEBUG
60 #ifdef DECORATOR_DEBUG
61 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_TEXT_DECORATOR") );
72 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "insertpoint-icon.png" );
73 const char* DEFAULT_GRAB_HANDLE_IMAGE_PRESSED( DALI_IMAGE_DIR "insertpoint-icon-pressed.png" );
74 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
75 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
77 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
78 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
80 const Dali::Vector4 LIGHT_BLUE( 0.07f, 0.41f, 0.59f, 1.0f ); // The text highlight color.
82 const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval
83 const float TO_MILLISECONDS = 1000.f;
84 const float TO_SECONDS = 1.f / TO_MILLISECONDS;
86 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
88 const unsigned int SCROLL_TICK_INTERVAL = 50u;
90 const float SCROLL_THRESHOLD = 10.f;
91 const float SCROLL_SPEED = 300.f;
92 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
95 * structure to hold coordinates of each quad, which will make up the mesh.
97 struct QuadCoordinates
100 * Default constructor
108 * @param[in] x1 left co-ordinate
109 * @param[in] y1 top co-ordinate
110 * @param[in] x2 right co-ordinate
111 * @param[in] y2 bottom co-ordinate
113 QuadCoordinates(float x1, float y1, float x2, float y2)
119 Dali::Vector2 min; ///< top-left (minimum) position of quad
120 Dali::Vector2 max; ///< bottom-right (maximum) position of quad
123 typedef std::vector<QuadCoordinates> QuadContainer;
126 * @brief Takes a bounding rectangle in the local coordinates of an actor and returns the world coordinates Bounding Box.
127 * @param[in] boundingRectangle local bounding
128 * @param[out] Vector4 World coordinate bounding Box.
130 void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangle, Dali::Vector4& boundingBox )
132 // Convert to world coordinates and store as a Vector4 to be compatible with Property Notifications.
133 Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
135 const float originX = boundingRectangle.x - 0.5f * stageSize.width;
136 const float originY = boundingRectangle.y - 0.5f * stageSize.height;
138 boundingBox = Dali::Vector4( originX,
140 originX + boundingRectangle.width,
141 originY + boundingRectangle.height );
145 } // end of namespace
156 struct Decorator::Impl : public ConnectionTracker
170 : color( Dali::Color::BLACK ),
172 cursorHeight( 0.0f ),
188 grabDisplacementX( 0.f ),
189 grabDisplacementY( 0.f ),
200 float lineHeight; ///< Not the handle height
201 float grabDisplacementX;
202 float grabDisplacementY;
208 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
209 : mTextControlParent( parent ),
210 mObserver( observer ),
211 mBoundingBox( Rect<int>() ),
212 mHighlightColor( LIGHT_BLUE ),
213 mActiveCursor( ACTIVE_CURSOR_NONE ),
214 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
215 mCursorBlinkDuration( 0.0f ),
216 mHandleScrolling( HANDLE_TYPE_COUNT ),
217 mScrollDirection( SCROLL_NONE ),
218 mScrollThreshold( SCROLL_THRESHOLD ),
219 mScrollSpeed( SCROLL_SPEED ),
220 mScrollDistance( SCROLL_DISTANCE ),
221 mActiveCopyPastePopup( false ),
222 mCursorBlinkStatus( true ),
223 mPrimaryCursorVisible( false ),
224 mSecondaryCursorVisible( false )
229 * Relayout of the decorations owned by the decorator.
230 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
232 void Relayout( const Vector2& size )
234 // TODO - Remove this if nothing is active
238 // Show or hide the cursors
242 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
243 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
244 if( mPrimaryCursorVisible )
246 Vector2 position = cursor.position;
247 if( GRAB_HANDLE == mHandleScrolling )
249 if( mScrollDirection == SCROLL_RIGHT )
255 position.x = size.width;
259 mPrimaryCursor.SetPosition( position.x,
261 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
263 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
265 if( mSecondaryCursor )
267 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
268 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
269 if( mSecondaryCursorVisible )
271 mSecondaryCursor.SetPosition( cursor.position.x,
273 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
275 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
278 // Show or hide the grab handle
279 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
280 if( grabHandle.active )
282 Vector2 position = grabHandle.position;
284 if( GRAB_HANDLE == mHandleScrolling )
286 if( mScrollDirection == SCROLL_RIGHT )
292 position.x = size.width;
296 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
304 grabHandle.actor.SetPosition( position.x,
305 position.y + grabHandle.lineHeight );
307 grabHandle.actor.SetVisible( isVisible );
309 else if( grabHandle.actor )
311 UnparentAndReset( grabHandle.actor );
314 // Show or hide the selection handles/highlight
315 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
316 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
317 if( primary.active || secondary.active )
321 CreateSelectionHandles();
323 primary.actor.SetPosition( primary.position.x,
324 primary.position.y + primary.lineHeight );
326 secondary.actor.SetPosition( secondary.position.x,
327 secondary.position.y + secondary.lineHeight );
334 UnparentAndReset( primary.actor );
335 UnparentAndReset( secondary.actor );
336 //UnparentAndReset( mHighlightMeshActor );
339 if ( mActiveCopyPastePopup )
341 if ( !mCopyPastePopup )
343 mCopyPastePopup = TextSelectionPopup::New();
344 #ifdef DECORATOR_DEBUG
345 mCopyPastePopup.SetName("mCopyPastePopup");
347 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
348 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
349 mActiveLayer.Add ( mCopyPastePopup );
354 if ( mCopyPastePopup )
356 UnparentAndReset( mCopyPastePopup );
361 void UpdatePositions( const Vector2& scrollOffset )
363 mCursor[PRIMARY_CURSOR].position += scrollOffset;
364 mCursor[SECONDARY_CURSOR].position += scrollOffset;
365 mHandle[ GRAB_HANDLE ].position += scrollOffset;
366 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
367 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
369 // TODO Highlight box??
372 void PopUpRelayoutComplete( Actor actor )
374 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
376 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
378 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
380 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
382 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
384 SetUpPopUpPositionNotifications();
386 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
389 void CreateCursor( ImageActor& cursor, const Vector4& color )
391 cursor = CreateSolidColorActor( color );
392 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
393 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
396 // Add or Remove cursor(s) from parent
400 if( mActiveCursor == ACTIVE_CURSOR_NONE )
402 UnparentAndReset( mPrimaryCursor );
403 UnparentAndReset( mSecondaryCursor );
407 // Create Primary and or Secondary Cursor(s) if active and add to parent
408 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
409 mActiveCursor == ACTIVE_CURSOR_BOTH )
411 if ( !mPrimaryCursor )
413 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
414 #ifdef DECORATOR_DEBUG
415 mPrimaryCursor.SetName( "PrimaryCursorActor" );
417 mActiveLayer.Add( mPrimaryCursor );
421 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
423 if ( !mSecondaryCursor )
425 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
426 #ifdef DECORATOR_DEBUG
427 mSecondaryCursor.SetName( "SecondaryCursorActor" );
429 mActiveLayer.Add( mSecondaryCursor );
434 UnparentAndReset( mSecondaryCursor );
440 bool OnCursorBlinkTimerTick()
444 if ( mPrimaryCursor )
446 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
448 if ( mSecondaryCursor )
450 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
453 mCursorBlinkStatus = !mCursorBlinkStatus;
458 void SetupTouchEvents()
462 mTapDetector = TapGestureDetector::New();
463 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
466 if ( !mPanGestureDetector )
468 mPanGestureDetector = PanGestureDetector::New();
469 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
473 void CreateActiveLayer()
477 Actor parent = mTextControlParent.Self();
479 mActiveLayer = Layer::New();
480 #ifdef DECORATOR_DEBUG
481 mActiveLayer.SetName ( "ActiveLayerActor" );
484 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
485 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
486 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
488 parent.Add( mActiveLayer );
491 mActiveLayer.RaiseToTop();
494 void CreateGrabHandle()
496 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
497 if( !grabHandle.actor )
499 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
501 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
503 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
505 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
508 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
509 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
510 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
511 // Area that Grab handle responds to, larger than actual handle so easier to move
512 #ifdef DECORATOR_DEBUG
513 grabHandle.actor.SetName( "GrabHandleActor" );
514 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
516 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
517 grabHandle.grabArea.SetName( "GrabArea" );
521 grabHandle.grabArea = Actor::New();
522 grabHandle.grabArea.SetName( "GrabArea" );
525 grabHandle.grabArea = Actor::New();
528 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
529 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
530 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
531 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
532 grabHandle.actor.Add( grabHandle.grabArea );
534 mTapDetector.Attach( grabHandle.grabArea );
535 mPanGestureDetector.Attach( grabHandle.grabArea );
537 mActiveLayer.Add( grabHandle.actor );
541 void CreateSelectionHandles()
543 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
546 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
548 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
551 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
552 #ifdef DECORATOR_DEBUG
553 primary.actor.SetName("SelectionHandleOne");
555 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
556 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
557 primary.flipped = false;
559 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
560 #ifdef DECORATOR_DEBUG
561 primary.grabArea.SetName("SelectionHandleOneGrabArea");
563 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
564 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
565 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
567 mTapDetector.Attach( primary.grabArea );
568 mPanGestureDetector.Attach( primary.grabArea );
569 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
571 primary.actor.Add( primary.grabArea );
572 mActiveLayer.Add( primary.actor );
575 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
576 if( !secondary.actor )
578 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
580 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
583 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
584 #ifdef DECORATOR_DEBUG
585 secondary.actor.SetName("SelectionHandleTwo");
587 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
588 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
589 secondary.flipped = false;
591 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
592 #ifdef DECORATOR_DEBUG
593 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
595 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
596 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
597 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
599 mTapDetector.Attach( secondary.grabArea );
600 mPanGestureDetector.Attach( secondary.grabArea );
601 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
603 secondary.actor.Add( secondary.grabArea );
604 mActiveLayer.Add( secondary.actor );
608 void CreateHighlight()
611 if ( !mHighlightMeshActor )
613 mHighlightMaterial = Material::New( "HighlightMaterial" );
614 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
616 mHighlightMeshData.SetMaterial( mHighlightMaterial );
617 mHighlightMeshData.SetHasNormals( true );
619 mHighlightMesh = Mesh::New( mHighlightMeshData );
621 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
622 #ifdef DECORATOR_DEBUG
623 mHighlightMeshActor.SetName( "HighlightMeshActor" );
625 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
626 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
628 Actor parent = mTextControlParent.Self();
629 parent.Add( mHighlightMeshActor );
634 void UpdateHighlight()
636 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
638 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
640 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
641 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
642 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
643 // [BOTTOM] [ MIDDLE ]
646 // Each quad is created as 2 triangles.
647 // Middle is just 1 quad regardless of its size.
662 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
664 MeshData::VertexContainer vertices;
665 Dali::MeshData::FaceIndices faceIndices;
667 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
668 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
670 // vertex position defaults to (0 0 0)
671 MeshData::Vertex vertex;
672 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
675 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
677 // Add each quad geometry (a sub-selection) to the mesh data.
687 QuadCoordinates& quad = *iter;
689 vertex.x = quad.min.x;
690 vertex.y = quad.min.y;
691 vertices.push_back( vertex );
694 vertex.x = quad.max.x;
695 vertex.y = quad.min.y;
696 vertices.push_back( vertex );
699 vertex.x = quad.min.x;
700 vertex.y = quad.max.y;
701 vertices.push_back( vertex );
703 // bottom-right (v+3)
704 vertex.x = quad.max.x;
705 vertex.y = quad.max.y;
706 vertices.push_back( vertex );
708 // triangle A (3, 1, 0)
709 faceIndices.push_back( v + 3 );
710 faceIndices.push_back( v + 1 );
711 faceIndices.push_back( v );
713 // triangle B (0, 2, 3)
714 faceIndices.push_back( v );
715 faceIndices.push_back( v + 2 );
716 faceIndices.push_back( v + 3 );
718 mHighlightMeshData.SetFaceIndices( faceIndices );
721 BoneContainer bones(0); // passed empty as bones not required
722 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
723 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
728 void OnTap( Actor actor, const TapGesture& tap )
730 if( actor == mHandle[GRAB_HANDLE].actor )
736 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
738 if( Gesture::Started == gesture.state )
740 handle.grabDisplacementX = handle.grabDisplacementY = 0;
741 if( mHandleImages[type][HANDLE_IMAGE_PRESSED] )
743 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_PRESSED] );
747 handle.grabDisplacementX += gesture.displacement.x;
748 handle.grabDisplacementY += gesture.displacement.y;
750 const float x = handle.position.x + handle.grabDisplacementX;
751 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
753 if( Gesture::Started == gesture.state ||
754 Gesture::Continuing == gesture.state )
756 if( x < mScrollThreshold )
758 mScrollDirection = SCROLL_RIGHT;
759 mHandleScrolling = type;
762 else if( x > mTextControlParent.GetControlSize().width - mScrollThreshold )
764 mScrollDirection = SCROLL_LEFT;
765 mHandleScrolling = type;
770 mHandleScrolling = HANDLE_TYPE_COUNT;
772 mObserver.HandleEvent( type, HANDLE_PRESSED, x, y );
775 else if( Gesture::Finished == gesture.state ||
776 Gesture::Cancelled == gesture.state )
778 if( mScrollTimer && mScrollTimer.IsRunning() )
780 mHandleScrolling = HANDLE_TYPE_COUNT;
782 mObserver.HandleEvent( type, HANDLE_STOP_SCROLLING, x, y );
786 mObserver.HandleEvent( type, HANDLE_RELEASED, x, y );
788 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
792 void OnPan( Actor actor, const PanGesture& gesture )
794 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
795 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
796 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
798 if( actor == grabHandle.grabArea )
800 DoPan( grabHandle, GRAB_HANDLE, gesture );
802 else if( actor == primarySelectionHandle.grabArea )
804 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
806 else if( actor == secondarySelectionHandle.grabArea )
808 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
812 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
818 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
826 float AlternatePopUpPositionRelativeToCursor()
828 float alternativePosition=0.0f;;
831 if ( mPrimaryCursor ) // Secondary cursor not used for paste
833 Cursor cursor = PRIMARY_CURSOR;
834 alternativePosition = mCursor[cursor].position.y;
837 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
839 if( mHandle[GRAB_HANDLE].active )
841 // If grab handle enabled then position pop-up below the grab handle.
842 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
843 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
844 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
848 alternativePosition += popupHeight;
851 return alternativePosition;
854 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
856 float alternativeYPosition=0.0f;
857 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
858 // if can't be positioned above, then position below row.
859 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
861 mCopyPastePopup.SetY( alternativeYPosition );
865 void SetUpPopUpPositionNotifications( )
867 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
869 // Exceeding vertical boundary
871 Vector4 worldCoordinatesBoundingBox;
872 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
874 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
876 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
877 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
878 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
880 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
883 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
885 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
887 // Parent must already by added to Stage for these Get calls to work
888 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
889 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
890 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
891 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
893 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
894 Vector4 boundingRectangleWorld;
895 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
897 // Calculate distance to move popup (in local space) so fits within the boundary
898 float xOffSetToKeepWithinBounds = 0.0f;
899 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
901 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
903 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
905 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
908 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
909 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
911 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
914 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
917 void SetScrollThreshold( float threshold )
919 mScrollThreshold = threshold;
922 float GetScrollThreshold() const
924 return mScrollThreshold;
927 void SetScrollSpeed( float speed )
929 mScrollSpeed = speed;
930 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
933 float GetScrollSpeed() const
939 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
941 * It only starts the timer if it's already created.
943 void StartScrollTimer()
947 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
948 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
951 if( !mScrollTimer.IsRunning() )
953 mScrollTimer.Start();
958 * Stops the timer used to scroll the text.
960 void StopScrollTimer()
969 * Callback called by the timer used to scroll the text.
971 * It calculates and sets a new scroll position.
973 bool OnScrollTimerTick()
975 if( HANDLE_TYPE_COUNT != mHandleScrolling )
977 mObserver.HandleEvent( mHandleScrolling,
979 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
986 Internal::Control& mTextControlParent;
989 TapGestureDetector mTapDetector;
990 PanGestureDetector mPanGestureDetector;
991 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
992 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
994 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
995 //ImageActor mPrimaryCursor;
996 //ImageActor mSecondaryCursor;
997 //MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
998 TextSelectionPopup mCopyPastePopup;
1000 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1002 //Mesh mHighlightMesh; ///< Mesh for highlight
1003 //MeshData mHighlightMeshData; ///< Mesh Data for highlight
1004 //Material mHighlightMaterial; ///< Material used for highlight
1006 CursorImpl mCursor[CURSOR_COUNT];
1007 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1008 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1010 Rect<int> mBoundingBox;
1011 Vector4 mHighlightColor; ///< Color of the highlight
1013 unsigned int mActiveCursor;
1014 unsigned int mCursorBlinkInterval;
1015 float mCursorBlinkDuration;
1016 HandleType mHandleScrolling; ///< The handle which is scrolling.
1017 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1018 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1019 float mScrollSpeed; ///< The scroll speed in pixels per second.
1020 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1021 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1023 bool mActiveCopyPastePopup : 1;
1024 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1025 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1026 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1029 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
1031 return DecoratorPtr( new Decorator(parent, observer) );
1034 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1036 mImpl->mBoundingBox = boundingBox;
1039 const Rect<int>& Decorator::GetBoundingBox() const
1041 return mImpl->mBoundingBox;
1044 void Decorator::Relayout( const Vector2& size )
1046 mImpl->Relayout( size );
1049 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1051 mImpl->UpdatePositions( scrollOffset );
1056 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1058 mImpl->mActiveCursor = activeCursor;
1061 unsigned int Decorator::GetActiveCursor() const
1063 return mImpl->mActiveCursor;
1066 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1068 mImpl->mCursor[cursor].position.x = x;
1069 mImpl->mCursor[cursor].position.y = y;
1070 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1071 mImpl->mCursor[cursor].lineHeight = lineHeight;
1074 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1076 x = mImpl->mCursor[cursor].position.x;
1077 y = mImpl->mCursor[cursor].position.y;
1078 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1079 lineHeight = mImpl->mCursor[cursor].lineHeight;
1082 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1084 return mImpl->mCursor[cursor].position;
1087 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1089 mImpl->mCursor[cursor].color = color;
1092 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1094 return mImpl->mCursor[cursor].color;
1097 void Decorator::StartCursorBlink()
1099 if ( !mImpl->mCursorBlinkTimer )
1101 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1102 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1105 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1107 mImpl->mCursorBlinkTimer.Start();
1111 void Decorator::StopCursorBlink()
1113 if ( mImpl->mCursorBlinkTimer )
1115 mImpl->mCursorBlinkTimer.Stop();
1119 void Decorator::SetCursorBlinkInterval( float seconds )
1121 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1124 float Decorator::GetCursorBlinkInterval() const
1126 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1129 void Decorator::SetCursorBlinkDuration( float seconds )
1131 mImpl->mCursorBlinkDuration = seconds;
1134 float Decorator::GetCursorBlinkDuration() const
1136 return mImpl->mCursorBlinkDuration;
1141 void Decorator::SetHandleActive( HandleType handleType, bool active )
1143 mImpl->mHandle[handleType].active = active;
1146 bool Decorator::IsHandleActive( HandleType handleType ) const
1148 return mImpl->mHandle[handleType].active ;
1151 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1153 mImpl->mHandleImages[handleType][handleImageType] = image;
1156 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1158 return mImpl->mHandleImages[handleType][handleImageType];
1161 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1163 // Adjust grab handle displacement
1164 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1166 handle.grabDisplacementX -= x - handle.position.x;
1167 handle.grabDisplacementY -= y - handle.position.y;
1169 handle.position.x = x;
1170 handle.position.y = y;
1171 handle.lineHeight = height;
1174 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1176 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1178 x = handle.position.x;
1179 y = handle.position.y;
1180 height = handle.lineHeight;
1183 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1185 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1188 void Decorator::ClearHighlights()
1190 mImpl->mHighlightQuadList.clear();
1193 void Decorator::SetHighlightColor( const Vector4& color )
1195 mImpl->mHighlightColor = color;
1198 const Vector4& Decorator::GetHighlightColor() const
1200 return mImpl->mHighlightColor;
1203 void Decorator::SetPopupActive( bool active )
1205 mImpl->mActiveCopyPastePopup = active;
1208 bool Decorator::IsPopupActive() const
1210 return mImpl->mActiveCopyPastePopup ;
1215 void Decorator::SetScrollThreshold( float threshold )
1217 mImpl->SetScrollThreshold( threshold );
1220 float Decorator::GetScrollThreshold() const
1222 return mImpl->GetScrollThreshold();
1225 void Decorator::SetScrollSpeed( float speed )
1227 mImpl->SetScrollSpeed( speed );
1230 float Decorator::GetScrollSpeed() const
1232 return mImpl->GetScrollSpeed();
1235 Decorator::~Decorator()
1240 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
1243 mImpl = new Decorator::Impl( parent, observer );
1248 } // namespace Toolkit