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/devel-api/actors/mesh-actor.h>
28 #include <dali/public-api/animation/constraint.h>
29 #include <dali/public-api/common/constants.h>
30 #include <dali/public-api/common/stage.h>
31 #include <dali/public-api/events/tap-gesture.h>
32 #include <dali/public-api/events/tap-gesture-detector.h>
33 #include <dali/public-api/events/pan-gesture.h>
34 #include <dali/public-api/events/pan-gesture-detector.h>
35 #include <dali/devel-api/geometry/mesh.h>
36 #include <dali/devel-api/geometry/mesh-data.h>
37 #include <dali/public-api/images/resource-image.h>
38 #include <dali/public-api/math/rect.h>
39 #include <dali/public-api/math/vector2.h>
40 #include <dali/public-api/math/vector4.h>
41 #include <dali/public-api/object/property-notification.h>
42 #include <dali/public-api/signals/connection-tracker.h>
45 #include <dali-toolkit/public-api/controls/control.h>
46 #include <dali-toolkit/public-api/controls/control-impl.h>
47 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
48 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
49 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
50 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
53 #define DECORATOR_DEBUG
63 #ifdef DECORATOR_DEBUG
64 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_TEXT_DECORATOR") );
75 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "insertpoint-icon.png" );
76 const char* DEFAULT_GRAB_HANDLE_IMAGE_PRESSED( DALI_IMAGE_DIR "insertpoint-icon-pressed.png" );
77 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
78 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
80 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
81 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
83 const Dali::Vector4 LIGHT_BLUE( 0.07f, 0.41f, 0.59f, 1.0f ); // The text highlight color.
85 const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval
86 const float TO_MILLISECONDS = 1000.f;
87 const float TO_SECONDS = 1.f / TO_MILLISECONDS;
89 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
91 const unsigned int SCROLL_TICK_INTERVAL = 50u;
93 const float SCROLL_THRESHOLD = 10.f;
94 const float SCROLL_SPEED = 300.f;
95 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
98 * structure to hold coordinates of each quad, which will make up the mesh.
100 struct QuadCoordinates
103 * Default constructor
111 * @param[in] x1 left co-ordinate
112 * @param[in] y1 top co-ordinate
113 * @param[in] x2 right co-ordinate
114 * @param[in] y2 bottom co-ordinate
116 QuadCoordinates(float x1, float y1, float x2, float y2)
122 Dali::Vector2 min; ///< top-left (minimum) position of quad
123 Dali::Vector2 max; ///< bottom-right (maximum) position of quad
126 typedef std::vector<QuadCoordinates> QuadContainer;
129 * @brief Takes a bounding rectangle in the local coordinates of an actor and returns the world coordinates Bounding Box.
130 * @param[in] boundingRectangle local bounding
131 * @param[out] Vector4 World coordinate bounding Box.
133 void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangle, Dali::Vector4& boundingBox )
135 // Convert to world coordinates and store as a Vector4 to be compatible with Property Notifications.
136 Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
138 const float originX = boundingRectangle.x - 0.5f * stageSize.width;
139 const float originY = boundingRectangle.y - 0.5f * stageSize.height;
141 boundingBox = Dali::Vector4( originX,
143 originX + boundingRectangle.width,
144 originY + boundingRectangle.height );
148 } // end of namespace
159 struct Decorator::Impl : public ConnectionTracker
173 : color( Dali::Color::BLACK ),
175 cursorHeight( 0.0f ),
191 grabDisplacementX( 0.f ),
192 grabDisplacementY( 0.f ),
203 float lineHeight; ///< Not the handle height
204 float grabDisplacementX;
205 float grabDisplacementY;
211 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
212 : mTextControlParent( parent ),
213 mObserver( observer ),
214 mBoundingBox( Rect<int>() ),
215 mHighlightColor( LIGHT_BLUE ),
216 mActiveCursor( ACTIVE_CURSOR_NONE ),
217 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
218 mCursorBlinkDuration( 0.0f ),
219 mHandleScrolling( HANDLE_TYPE_COUNT ),
220 mScrollDirection( SCROLL_NONE ),
221 mScrollThreshold( SCROLL_THRESHOLD ),
222 mScrollSpeed( SCROLL_SPEED ),
223 mScrollDistance( SCROLL_DISTANCE ),
224 mActiveCopyPastePopup( false ),
225 mCursorBlinkStatus( true ),
226 mPrimaryCursorVisible( false ),
227 mSecondaryCursorVisible( false )
232 * Relayout of the decorations owned by the decorator.
233 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
235 void Relayout( const Vector2& size )
237 // TODO - Remove this if nothing is active
240 // Show or hide the cursors
244 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
245 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
246 if( mPrimaryCursorVisible )
248 Vector2 position = cursor.position;
249 if( GRAB_HANDLE == mHandleScrolling )
251 if( mScrollDirection == SCROLL_RIGHT )
257 position.x = size.width;
261 mPrimaryCursor.SetPosition( position.x,
263 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
265 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
267 if( mSecondaryCursor )
269 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
270 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
271 if( mSecondaryCursorVisible )
273 mSecondaryCursor.SetPosition( cursor.position.x,
275 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
277 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
280 // Show or hide the grab handle
281 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
282 if( grabHandle.active )
284 Vector2 position = grabHandle.position;
286 if( GRAB_HANDLE == mHandleScrolling )
288 if( mScrollDirection == SCROLL_RIGHT )
294 position.x = size.width;
298 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
306 grabHandle.actor.SetPosition( position.x,
307 position.y + grabHandle.lineHeight );
309 grabHandle.actor.SetVisible( isVisible );
311 else if( grabHandle.actor )
313 UnparentAndReset( grabHandle.actor );
316 // Show or hide the selection handles/highlight
317 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
318 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
319 if( primary.active || secondary.active )
323 CreateSelectionHandles();
325 primary.actor.SetPosition( primary.position.x,
326 primary.position.y + primary.lineHeight );
328 secondary.actor.SetPosition( secondary.position.x,
329 secondary.position.y + secondary.lineHeight );
336 UnparentAndReset( primary.actor );
337 UnparentAndReset( secondary.actor );
338 UnparentAndReset( mHighlightMeshActor );
341 if ( mActiveCopyPastePopup )
343 if ( !mCopyPastePopup )
345 mCopyPastePopup = TextSelectionPopup::New();
346 #ifdef DECORATOR_DEBUG
347 mCopyPastePopup.SetName("mCopyPastePopup");
349 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
350 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
351 mActiveLayer.Add ( mCopyPastePopup );
356 if ( mCopyPastePopup )
358 UnparentAndReset( mCopyPastePopup );
363 void UpdatePositions( const Vector2& scrollOffset )
365 mCursor[PRIMARY_CURSOR].position += scrollOffset;
366 mCursor[SECONDARY_CURSOR].position += scrollOffset;
367 mHandle[ GRAB_HANDLE ].position += scrollOffset;
368 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
369 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
371 // TODO Highlight box??
374 void PopUpRelayoutComplete( Actor actor )
376 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
378 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
380 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
382 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
384 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
386 SetUpPopUpPositionNotifications();
388 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
391 void CreateCursor( ImageActor& cursor, const Vector4& color )
393 cursor = CreateSolidColorActor( color );
394 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
395 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
398 // 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 );
440 bool OnCursorBlinkTimerTick()
443 if ( mPrimaryCursor )
445 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
447 if ( mSecondaryCursor )
449 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
452 mCursorBlinkStatus = !mCursorBlinkStatus;
457 void SetupTouchEvents()
461 mTapDetector = TapGestureDetector::New();
462 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
465 if ( !mPanGestureDetector )
467 mPanGestureDetector = PanGestureDetector::New();
468 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
472 void CreateActiveLayer()
476 Actor parent = mTextControlParent.Self();
478 mActiveLayer = Layer::New();
479 #ifdef DECORATOR_DEBUG
480 mActiveLayer.SetName ( "ActiveLayerActor" );
483 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
484 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
485 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
487 parent.Add( mActiveLayer );
490 mActiveLayer.RaiseToTop();
493 void CreateGrabHandle()
495 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
496 if( !grabHandle.actor )
498 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
500 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
502 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
504 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
507 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
508 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
509 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
510 // Area that Grab handle responds to, larger than actual handle so easier to move
511 #ifdef DECORATOR_DEBUG
512 grabHandle.actor.SetName( "GrabHandleActor" );
513 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
515 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
516 grabHandle.grabArea.SetName( "GrabArea" );
520 grabHandle.grabArea = Actor::New();
521 grabHandle.grabArea.SetName( "GrabArea" );
524 grabHandle.grabArea = Actor::New();
527 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
528 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
529 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
530 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
531 grabHandle.actor.Add( grabHandle.grabArea );
533 mTapDetector.Attach( grabHandle.grabArea );
534 mPanGestureDetector.Attach( grabHandle.grabArea );
536 mActiveLayer.Add( grabHandle.actor );
540 void CreateSelectionHandles()
542 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
545 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
547 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
550 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
551 #ifdef DECORATOR_DEBUG
552 primary.actor.SetName("SelectionHandleOne");
554 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
555 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
556 primary.flipped = false;
558 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
559 #ifdef DECORATOR_DEBUG
560 primary.grabArea.SetName("SelectionHandleOneGrabArea");
562 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
563 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
564 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
566 mTapDetector.Attach( primary.grabArea );
567 mPanGestureDetector.Attach( primary.grabArea );
568 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
570 primary.actor.Add( primary.grabArea );
571 mActiveLayer.Add( primary.actor );
574 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
575 if( !secondary.actor )
577 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
579 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
582 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
583 #ifdef DECORATOR_DEBUG
584 secondary.actor.SetName("SelectionHandleTwo");
586 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
587 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
588 secondary.flipped = false;
590 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
591 #ifdef DECORATOR_DEBUG
592 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
594 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
595 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
596 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
598 mTapDetector.Attach( secondary.grabArea );
599 mPanGestureDetector.Attach( secondary.grabArea );
600 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
602 secondary.actor.Add( secondary.grabArea );
603 mActiveLayer.Add( secondary.actor );
607 void CreateHighlight()
609 if ( !mHighlightMeshActor )
611 mHighlightMaterial = Material::New( "HighlightMaterial" );
612 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
614 mHighlightMeshData.SetMaterial( mHighlightMaterial );
615 mHighlightMeshData.SetHasNormals( true );
617 mHighlightMesh = Mesh::New( mHighlightMeshData );
619 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
620 #ifdef DECORATOR_DEBUG
621 mHighlightMeshActor.SetName( "HighlightMeshActor" );
623 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
624 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
626 Actor parent = mTextControlParent.Self();
627 parent.Add( mHighlightMeshActor );
631 void UpdateHighlight()
633 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
635 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
637 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
638 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
639 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
640 // [BOTTOM] [ MIDDLE ]
643 // Each quad is created as 2 triangles.
644 // Middle is just 1 quad regardless of its size.
658 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
660 MeshData::VertexContainer vertices;
661 Dali::MeshData::FaceIndices faceIndices;
663 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
664 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
666 // vertex position defaults to (0 0 0)
667 MeshData::Vertex vertex;
668 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
671 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
673 // Add each quad geometry (a sub-selection) to the mesh data.
683 QuadCoordinates& quad = *iter;
685 vertex.x = quad.min.x;
686 vertex.y = quad.min.y;
687 vertices.push_back( vertex );
690 vertex.x = quad.max.x;
691 vertex.y = quad.min.y;
692 vertices.push_back( vertex );
695 vertex.x = quad.min.x;
696 vertex.y = quad.max.y;
697 vertices.push_back( vertex );
699 // bottom-right (v+3)
700 vertex.x = quad.max.x;
701 vertex.y = quad.max.y;
702 vertices.push_back( vertex );
704 // triangle A (3, 1, 0)
705 faceIndices.push_back( v + 3 );
706 faceIndices.push_back( v + 1 );
707 faceIndices.push_back( v );
709 // triangle B (0, 2, 3)
710 faceIndices.push_back( v );
711 faceIndices.push_back( v + 2 );
712 faceIndices.push_back( v + 3 );
714 mHighlightMeshData.SetFaceIndices( faceIndices );
717 BoneContainer bones(0); // passed empty as bones not required
718 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
719 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
723 void OnTap( Actor actor, const TapGesture& tap )
725 if( actor == mHandle[GRAB_HANDLE].actor )
731 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
733 if( Gesture::Started == gesture.state )
735 handle.grabDisplacementX = handle.grabDisplacementY = 0;
736 if( mHandleImages[type][HANDLE_IMAGE_PRESSED] )
738 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_PRESSED] );
742 handle.grabDisplacementX += gesture.displacement.x;
743 handle.grabDisplacementY += gesture.displacement.y;
745 const float x = handle.position.x + handle.grabDisplacementX;
746 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
748 if( Gesture::Started == gesture.state ||
749 Gesture::Continuing == gesture.state )
751 if( x < mScrollThreshold )
753 mScrollDirection = SCROLL_RIGHT;
754 mHandleScrolling = type;
757 else if( x > mTextControlParent.Self().GetTargetSize().width - mScrollThreshold )
759 mScrollDirection = SCROLL_LEFT;
760 mHandleScrolling = type;
765 mHandleScrolling = HANDLE_TYPE_COUNT;
767 mObserver.HandleEvent( type, HANDLE_PRESSED, x, y );
770 else if( Gesture::Finished == gesture.state ||
771 Gesture::Cancelled == gesture.state )
773 if( mScrollTimer && mScrollTimer.IsRunning() )
775 mHandleScrolling = HANDLE_TYPE_COUNT;
777 mObserver.HandleEvent( type, HANDLE_STOP_SCROLLING, x, y );
781 mObserver.HandleEvent( type, HANDLE_RELEASED, x, y );
783 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
787 void OnPan( Actor actor, const PanGesture& gesture )
789 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
790 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
791 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
793 if( actor == grabHandle.grabArea )
795 DoPan( grabHandle, GRAB_HANDLE, gesture );
797 else if( actor == primarySelectionHandle.grabArea )
799 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
801 else if( actor == secondarySelectionHandle.grabArea )
803 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
807 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
813 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
821 float AlternatePopUpPositionRelativeToCursor()
823 float alternativePosition=0.0f;;
825 if ( mPrimaryCursor ) // Secondary cursor not used for paste
827 Cursor cursor = PRIMARY_CURSOR;
828 alternativePosition = mCursor[cursor].position.y;
831 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
833 if( mHandle[GRAB_HANDLE].active )
835 // If grab handle enabled then position pop-up below the grab handle.
836 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
837 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
838 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
842 alternativePosition += popupHeight;
845 return alternativePosition;
848 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
850 float alternativeYPosition=0.0f;
851 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
852 // if can't be positioned above, then position below row.
853 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
855 mCopyPastePopup.SetY( alternativeYPosition );
859 void SetUpPopUpPositionNotifications( )
861 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
863 // Exceeding vertical boundary
865 Vector4 worldCoordinatesBoundingBox;
866 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
868 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
870 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
871 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
872 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
874 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
877 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
879 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
881 // Parent must already by added to Stage for these Get calls to work
882 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
883 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
884 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
885 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
887 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
888 Vector4 boundingRectangleWorld;
889 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
891 // Calculate distance to move popup (in local space) so fits within the boundary
892 float xOffSetToKeepWithinBounds = 0.0f;
893 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
895 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
897 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
899 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
902 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
903 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
905 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
908 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
911 void SetScrollThreshold( float threshold )
913 mScrollThreshold = threshold;
916 float GetScrollThreshold() const
918 return mScrollThreshold;
921 void SetScrollSpeed( float speed )
923 mScrollSpeed = speed;
924 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
927 float GetScrollSpeed() const
933 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
935 * It only starts the timer if it's already created.
937 void StartScrollTimer()
941 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
942 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
945 if( !mScrollTimer.IsRunning() )
947 mScrollTimer.Start();
952 * Stops the timer used to scroll the text.
954 void StopScrollTimer()
963 * Callback called by the timer used to scroll the text.
965 * It calculates and sets a new scroll position.
967 bool OnScrollTimerTick()
969 if( HANDLE_TYPE_COUNT != mHandleScrolling )
971 mObserver.HandleEvent( mHandleScrolling,
973 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
980 Internal::Control& mTextControlParent;
983 TapGestureDetector mTapDetector;
984 PanGestureDetector mPanGestureDetector;
985 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
986 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
988 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
989 ImageActor mPrimaryCursor;
990 ImageActor mSecondaryCursor;
991 MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
992 TextSelectionPopup mCopyPastePopup;
994 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
996 Mesh mHighlightMesh; ///< Mesh for highlight
997 MeshData mHighlightMeshData; ///< Mesh Data for highlight
998 Material mHighlightMaterial; ///< Material used for highlight
1000 CursorImpl mCursor[CURSOR_COUNT];
1001 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1002 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1004 Rect<int> mBoundingBox;
1005 Vector4 mHighlightColor; ///< Color of the highlight
1007 unsigned int mActiveCursor;
1008 unsigned int mCursorBlinkInterval;
1009 float mCursorBlinkDuration;
1010 HandleType mHandleScrolling; ///< The handle which is scrolling.
1011 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1012 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1013 float mScrollSpeed; ///< The scroll speed in pixels per second.
1014 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1015 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1017 bool mActiveCopyPastePopup : 1;
1018 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1019 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1020 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1023 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
1025 return DecoratorPtr( new Decorator(parent, observer) );
1028 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1030 mImpl->mBoundingBox = boundingBox;
1033 const Rect<int>& Decorator::GetBoundingBox() const
1035 return mImpl->mBoundingBox;
1038 void Decorator::Relayout( const Vector2& size )
1040 mImpl->Relayout( size );
1043 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1045 mImpl->UpdatePositions( scrollOffset );
1050 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1052 mImpl->mActiveCursor = activeCursor;
1055 unsigned int Decorator::GetActiveCursor() const
1057 return mImpl->mActiveCursor;
1060 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1062 mImpl->mCursor[cursor].position.x = x;
1063 mImpl->mCursor[cursor].position.y = y;
1064 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1065 mImpl->mCursor[cursor].lineHeight = lineHeight;
1068 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1070 x = mImpl->mCursor[cursor].position.x;
1071 y = mImpl->mCursor[cursor].position.y;
1072 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1073 lineHeight = mImpl->mCursor[cursor].lineHeight;
1076 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1078 return mImpl->mCursor[cursor].position;
1081 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1083 mImpl->mCursor[cursor].color = color;
1086 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1088 return mImpl->mCursor[cursor].color;
1091 void Decorator::StartCursorBlink()
1093 if ( !mImpl->mCursorBlinkTimer )
1095 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1096 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1099 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1101 mImpl->mCursorBlinkTimer.Start();
1105 void Decorator::StopCursorBlink()
1107 if ( mImpl->mCursorBlinkTimer )
1109 mImpl->mCursorBlinkTimer.Stop();
1113 void Decorator::SetCursorBlinkInterval( float seconds )
1115 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1118 float Decorator::GetCursorBlinkInterval() const
1120 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1123 void Decorator::SetCursorBlinkDuration( float seconds )
1125 mImpl->mCursorBlinkDuration = seconds;
1128 float Decorator::GetCursorBlinkDuration() const
1130 return mImpl->mCursorBlinkDuration;
1135 void Decorator::SetHandleActive( HandleType handleType, bool active )
1137 mImpl->mHandle[handleType].active = active;
1140 bool Decorator::IsHandleActive( HandleType handleType ) const
1142 return mImpl->mHandle[handleType].active ;
1145 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1147 mImpl->mHandleImages[handleType][handleImageType] = image;
1150 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1152 return mImpl->mHandleImages[handleType][handleImageType];
1155 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1157 // Adjust grab handle displacement
1158 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1160 handle.grabDisplacementX -= x - handle.position.x;
1161 handle.grabDisplacementY -= y - handle.position.y;
1163 handle.position.x = x;
1164 handle.position.y = y;
1165 handle.lineHeight = height;
1168 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1170 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1172 x = handle.position.x;
1173 y = handle.position.y;
1174 height = handle.lineHeight;
1177 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1179 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1182 void Decorator::ClearHighlights()
1184 mImpl->mHighlightQuadList.clear();
1187 void Decorator::SetHighlightColor( const Vector4& color )
1189 mImpl->mHighlightColor = color;
1192 const Vector4& Decorator::GetHighlightColor() const
1194 return mImpl->mHighlightColor;
1197 void Decorator::SetPopupActive( bool active )
1199 mImpl->mActiveCopyPastePopup = active;
1202 bool Decorator::IsPopupActive() const
1204 return mImpl->mActiveCopyPastePopup ;
1209 void Decorator::SetScrollThreshold( float threshold )
1211 mImpl->SetScrollThreshold( threshold );
1214 float Decorator::GetScrollThreshold() const
1216 return mImpl->GetScrollThreshold();
1219 void Decorator::SetScrollSpeed( float speed )
1221 mImpl->SetScrollSpeed( speed );
1224 float Decorator::GetScrollSpeed() const
1226 return mImpl->GetScrollSpeed();
1229 Decorator::~Decorator()
1234 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
1237 mImpl = new Decorator::Impl( parent, observer );
1242 } // namespace Toolkit