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/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/public-api/geometry/mesh.h>
36 #include <dali/public-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/public-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" );
79 //const char* DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
80 //const char* DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
82 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
83 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
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::WHITE ),
175 cursorHeight( 0.0f ),
186 struct SelectionHandleImpl
188 SelectionHandleImpl()
199 float lineHeight; ///< Not the handle height
203 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
204 : mTextControlParent( parent ),
205 mObserver( observer ),
206 mBoundingBox( Rect<int>() ),
207 mHighlightColor( 0.07f, 0.41f, 0.59f, 1.0f ), // light blue
208 mActiveCursor( ACTIVE_CURSOR_NONE ),
209 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
210 mCursorBlinkDuration( 0.0f ),
211 mGrabDisplacementX( 0.0f ),
212 mGrabDisplacementY( 0.0f ),
213 mScrollDirection( SCROLL_NONE ),
214 mScrollThreshold( SCROLL_THRESHOLD ),
215 mScrollSpeed( SCROLL_SPEED ),
216 mScrollDistance( SCROLL_DISTANCE ),
217 mActiveGrabHandle( false ),
218 mActiveSelection( false ),
219 mActiveCopyPastePopup( false ),
220 mCursorBlinkStatus( true ),
221 mPrimaryCursorVisible( false ),
222 mSecondaryCursorVisible( false )
227 * Relayout of the decorations owned by the decorator.
228 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
230 void Relayout( const Vector2& size )
232 // TODO - Remove this if nothing is active
235 // Show or hide the cursors
239 mPrimaryCursorVisible = ( mCursor[PRIMARY_CURSOR].position.x <= size.width ) && ( mCursor[PRIMARY_CURSOR].position.x >= 0.f );
240 if( mPrimaryCursorVisible )
242 mPrimaryCursor.SetPosition( mCursor[PRIMARY_CURSOR].position.x,
243 mCursor[PRIMARY_CURSOR].position.y );
244 mPrimaryCursor.SetSize( Size( 1.0f, mCursor[PRIMARY_CURSOR].cursorHeight ) );
246 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
248 if( mSecondaryCursor )
250 mSecondaryCursorVisible = ( mCursor[SECONDARY_CURSOR].position.x <= size.width ) && ( mCursor[SECONDARY_CURSOR].position.x >= 0.f );
251 if( mSecondaryCursorVisible )
253 mSecondaryCursor.SetPosition( mCursor[SECONDARY_CURSOR].position.x,
254 mCursor[SECONDARY_CURSOR].position.y );
255 mSecondaryCursor.SetSize( Size( 1.0f, mCursor[SECONDARY_CURSOR].cursorHeight ) );
257 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
260 // Show or hide the grab handle
261 if( mActiveGrabHandle )
263 const bool isVisible = ( mCursor[PRIMARY_CURSOR].position.x <= size.width ) && ( mCursor[PRIMARY_CURSOR].position.x >= 0.f );
271 mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].position.x,
272 mCursor[PRIMARY_CURSOR].position.y + mCursor[PRIMARY_CURSOR].lineHeight );
274 mGrabHandle.SetVisible( isVisible );
276 else if( mGrabHandle )
278 UnparentAndReset( mGrabHandle );
281 // Show or hide the selection handles/highlight
282 if( mActiveSelection )
286 CreateSelectionHandles();
288 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
289 primary.actor.SetPosition( primary.position.x,
290 primary.position.y + primary.lineHeight );
292 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
293 secondary.actor.SetPosition( secondary.position.x,
294 secondary.position.y + secondary.lineHeight );
301 UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
302 UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
303 UnparentAndReset( mHighlightMeshActor );
306 if ( mActiveCopyPastePopup )
308 if ( !mCopyPastePopup )
310 mCopyPastePopup = TextSelectionPopup::New();
311 #ifdef DECORATOR_DEBUG
312 mCopyPastePopup.SetName("mCopyPastePopup");
314 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
315 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
316 mActiveLayer.Add ( mCopyPastePopup );
321 if ( mCopyPastePopup )
323 UnparentAndReset( mCopyPastePopup );
328 void UpdatePositions( const Vector2& scrollOffset )
330 mCursor[PRIMARY_CURSOR].position += scrollOffset;
331 mCursor[SECONDARY_CURSOR].position += scrollOffset;
332 mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].position += scrollOffset;
333 mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].position += scrollOffset;
335 // TODO Highlight box??
338 void PopUpRelayoutComplete( Actor actor )
340 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
342 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
344 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
346 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
348 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
350 SetUpPopUpPositionNotifications();
352 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
355 void CreateCursor( ImageActor& cursor, const Vector4& color )
357 cursor = CreateSolidColorActor( color );
358 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
359 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
362 // Add or Remove cursor(s) from parent
365 if( mActiveCursor == ACTIVE_CURSOR_NONE )
367 UnparentAndReset( mPrimaryCursor );
368 UnparentAndReset( mSecondaryCursor );
372 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
373 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
374 mActiveCursor == ACTIVE_CURSOR_BOTH )
376 if ( !mPrimaryCursor )
378 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
379 #ifdef DECORATOR_DEBUG
380 mPrimaryCursor.SetName( "PrimaryCursorActor" );
382 mActiveLayer.Add( mPrimaryCursor );
386 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
388 if ( !mSecondaryCursor )
390 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
391 #ifdef DECORATOR_DEBUG
392 mSecondaryCursor.SetName( "SecondaryCursorActor" );
394 mActiveLayer.Add( mSecondaryCursor );
399 UnparentAndReset( mSecondaryCursor );
404 bool OnCursorBlinkTimerTick()
407 if ( mPrimaryCursor )
409 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
411 if ( mSecondaryCursor )
413 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
416 mCursorBlinkStatus = !mCursorBlinkStatus;
421 void SetupTouchEvents()
425 mTapDetector = TapGestureDetector::New();
426 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
429 if ( !mPanGestureDetector )
431 mPanGestureDetector = PanGestureDetector::New();
432 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
436 void CreateActiveLayer()
440 Actor parent = mTextControlParent.Self();
442 mActiveLayer = Layer::New();
443 #ifdef DECORATOR_DEBUG
444 mActiveLayer.SetName ( "ActiveLayerActor" );
447 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
448 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
449 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
451 parent.Add( mActiveLayer );
454 mActiveLayer.RaiseToTop();
457 void CreateGrabHandle()
461 if ( !mGrabHandleImageReleased )
463 mGrabHandleImageReleased = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
465 if ( !mGrabHandleImagePressed )
467 mGrabHandleImagePressed = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
470 mGrabHandle = ImageActor::New( mGrabHandleImageReleased );
471 mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
472 mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
473 // Area that Grab handle responds to, larger than actual handle so easier to move
474 #ifdef DECORATOR_DEBUG
475 mGrabHandle.SetName( "GrabHandleActor" );
476 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
478 mGrabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
479 mGrabArea.SetName( "GrabArea" );
483 mGrabArea = Actor::New();
484 mGrabArea.SetName( "GrabArea" );
487 mGrabArea = Actor::New();
490 mGrabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
491 mGrabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
492 mGrabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
493 mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
494 mGrabHandle.Add( mGrabArea );
496 mTapDetector.Attach( mGrabArea );
497 mPanGestureDetector.Attach( mGrabArea );
499 mActiveLayer.Add( mGrabHandle );
503 void CreateSelectionHandles()
505 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
506 if ( !primary.actor )
508 if ( !mSelectionReleasedLeft )
510 mSelectionReleasedLeft = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
513 primary.actor = ImageActor::New( mSelectionReleasedLeft );
514 #ifdef DECORATOR_DEBUG
515 primary.actor.SetName("SelectionHandleOne");
517 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
518 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
519 primary.flipped = false;
521 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
522 #ifdef DECORATOR_DEBUG
523 primary.grabArea.SetName("SelectionHandleOneGrabArea");
525 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
526 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
527 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
529 mTapDetector.Attach( primary.grabArea );
530 mPanGestureDetector.Attach( primary.grabArea );
531 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
533 primary.actor.Add( primary.grabArea );
534 mActiveLayer.Add( primary.actor );
537 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
538 if ( !secondary.actor )
540 if ( !mSelectionReleasedRight )
542 mSelectionReleasedRight = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
545 secondary.actor = ImageActor::New( mSelectionReleasedRight );
546 #ifdef DECORATOR_DEBUG
547 secondary.actor.SetName("SelectionHandleTwo");
549 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
550 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
551 secondary.flipped = false;
553 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
554 #ifdef DECORATOR_DEBUG
555 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
557 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
558 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
559 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
561 mTapDetector.Attach( secondary.grabArea );
562 mPanGestureDetector.Attach( secondary.grabArea );
563 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
565 secondary.actor.Add( secondary.grabArea );
566 mActiveLayer.Add( secondary.actor );
569 //SetUpHandlePropertyNotifications(); TODO
572 void CreateHighlight()
574 if ( !mHighlightMeshActor )
576 mHighlightMaterial = Material::New( "HighlightMaterial" );
577 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
579 mHighlightMeshData.SetMaterial( mHighlightMaterial );
580 mHighlightMeshData.SetHasNormals( true );
582 mHighlightMesh = Mesh::New( mHighlightMeshData );
584 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
585 #ifdef DECORATOR_DEBUG
586 mHighlightMeshActor.SetName( "HighlightMeshActor" );
588 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
589 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
591 Actor parent = mTextControlParent.Self();
592 parent.Add( mHighlightMeshActor );
596 void UpdateHighlight()
598 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
600 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
602 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
603 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
604 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
605 // [BOTTOM] [ MIDDLE ]
608 // Each quad is created as 2 triangles.
609 // Middle is just 1 quad regardless of its size.
623 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
625 MeshData::VertexContainer vertices;
626 Dali::MeshData::FaceIndices faceIndices;
628 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
629 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
631 // vertex position defaults to (0 0 0)
632 MeshData::Vertex vertex;
633 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
636 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
638 // Add each quad geometry (a sub-selection) to the mesh data.
648 QuadCoordinates& quad = *iter;
650 vertex.x = quad.min.x;
651 vertex.y = quad.min.y;
652 vertices.push_back( vertex );
655 vertex.x = quad.max.x;
656 vertex.y = quad.min.y;
657 vertices.push_back( vertex );
660 vertex.x = quad.min.x;
661 vertex.y = quad.max.y;
662 vertices.push_back( vertex );
664 // bottom-right (v+3)
665 vertex.x = quad.max.x;
666 vertex.y = quad.max.y;
667 vertices.push_back( vertex );
669 // triangle A (3, 1, 0)
670 faceIndices.push_back( v + 3 );
671 faceIndices.push_back( v + 1 );
672 faceIndices.push_back( v );
674 // triangle B (0, 2, 3)
675 faceIndices.push_back( v );
676 faceIndices.push_back( v + 2 );
677 faceIndices.push_back( v + 3 );
679 mHighlightMeshData.SetFaceIndices( faceIndices );
682 BoneContainer bones(0); // passed empty as bones not required
683 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
684 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
688 void OnTap( Actor actor, const TapGesture& tap )
690 if( actor == mGrabHandle )
696 void OnPan( Actor actor, const PanGesture& gesture )
698 if( actor == mGrabArea )
700 if( Gesture::Started == gesture.state )
702 mGrabDisplacementX = mGrabDisplacementY = 0;
703 mGrabHandle.SetImage( mGrabHandleImagePressed );
706 mGrabDisplacementX += gesture.displacement.x;
707 mGrabDisplacementY += gesture.displacement.y;
709 const float x = mCursor[PRIMARY_CURSOR].position.x + mGrabDisplacementX;
710 const float y = mCursor[PRIMARY_CURSOR].position.y + mCursor[PRIMARY_CURSOR].lineHeight*0.5f + mGrabDisplacementY;
712 if( Gesture::Started == gesture.state ||
713 Gesture::Continuing == gesture.state )
715 if( x < mScrollThreshold )
717 mScrollDirection = SCROLL_RIGHT;
718 mGrabDisplacementX -= x;
719 mCursor[PRIMARY_CURSOR].position.x = 0.f;
722 else if( x > mTextControlParent.GetControlSize().width - mScrollThreshold )
724 mScrollDirection = SCROLL_LEFT;
725 mGrabDisplacementX += ( mTextControlParent.GetControlSize().width - x );
726 mCursor[PRIMARY_CURSOR].position.x = mTextControlParent.GetControlSize().width;
732 mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
735 else if( Gesture::Finished == gesture.state ||
736 Gesture::Cancelled == gesture.state )
738 if( mScrollTimer && mScrollTimer.IsRunning() )
741 mObserver.GrabHandleEvent( GRAB_HANDLE_STOP_SCROLLING, x, y );
745 mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
748 mGrabHandle.SetImage( mGrabHandleImageReleased );
753 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
759 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
767 float AlternatePopUpPositionRelativeToCursor()
769 float alternativePosition=0.0f;;
771 if ( mPrimaryCursor ) // Secondary cursor not used for paste
773 Cursor cursor = PRIMARY_CURSOR;
774 alternativePosition = mCursor[cursor].position.y;
777 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
779 if( mActiveGrabHandle )
781 // If grab handle enabled then position pop-up below the grab handle.
782 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
783 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
784 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
788 alternativePosition += popupHeight;
791 return alternativePosition;
794 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
796 float alternativeYPosition=0.0f;
798 // if( mHighlightMeshActor ) // Text Selection mode
800 // alternativePosition = AlternatePopUpPositionRelativeToSelectionHandles();
802 // else // Not in Text Selection mode
804 // if can't be positioned above, then position below row.
805 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
807 mCopyPastePopup.SetY( alternativeYPosition );
811 void SetUpPopUpPositionNotifications( )
813 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
815 // Exceeding vertical boundary
817 Vector4 worldCoordinatesBoundingBox;
818 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
820 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
822 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
823 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
824 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
826 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
829 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
831 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
833 // Parent must already by added to Stage for these Get calls to work
834 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
835 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
836 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
837 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
839 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
840 Vector4 boundingRectangleWorld;
841 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
843 // Calculate distance to move popup (in local space) so fits within the boundary
844 float xOffSetToKeepWithinBounds = 0.0f;
845 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
847 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
849 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
851 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
854 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
855 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
857 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
860 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
863 void SetScrollThreshold( float threshold )
865 mScrollThreshold = threshold;
868 float GetScrollThreshold() const
870 return mScrollThreshold;
873 void SetScrollSpeed( float speed )
875 mScrollSpeed = speed;
876 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
879 float GetScrollSpeed() const
885 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
887 * It only starts the timer if it's already created.
889 void StartScrollTimer()
893 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
894 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
897 if( !mScrollTimer.IsRunning() )
899 mScrollTimer.Start();
904 * Stops the timer used to scroll the text.
906 void StopScrollTimer()
915 * Callback called by the timer used to scroll the text.
917 * It calculates and sets a new scroll position.
919 bool OnScrollTimerTick()
921 mObserver.GrabHandleEvent( GRAB_HANDLE_SCROLLING,
922 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
927 Internal::Control& mTextControlParent;
930 TapGestureDetector mTapDetector;
931 PanGestureDetector mPanGestureDetector;
932 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
933 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
935 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
936 ImageActor mPrimaryCursor;
937 ImageActor mSecondaryCursor;
938 ImageActor mGrabHandle;
940 MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
941 TextSelectionPopup mCopyPastePopup;
944 Image mGrabHandleImageReleased;
945 Image mGrabHandleImagePressed;
946 Mesh mHighlightMesh; ///< Mesh for highlight
947 MeshData mHighlightMeshData; ///< Mesh Data for highlight
948 Material mHighlightMaterial; ///< Material used for highlight
950 CursorImpl mCursor[CURSOR_COUNT];
951 SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
953 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
955 Image mSelectionReleasedLeft; ///< Selection handle images
956 Image mSelectionReleasedRight;
957 Image mSelectionPressedLeft;
958 Image mSelectionPressedRight;
960 Rect<int> mBoundingBox;
961 Vector4 mHighlightColor; ///< Color of the highlight
963 unsigned int mActiveCursor;
964 unsigned int mCursorBlinkInterval;
965 float mCursorBlinkDuration;
966 float mGrabDisplacementX;
967 float mGrabDisplacementY;
968 ScrollDirection mScrollDirection; ///< The direction of the scroll.
969 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
970 float mScrollSpeed; ///< The scroll speed in pixels per second.
971 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
972 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
974 bool mActiveGrabHandle : 1;
975 bool mActiveSelection : 1;
976 bool mActiveCopyPastePopup : 1;
977 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
978 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
979 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
982 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
984 return DecoratorPtr( new Decorator(parent, observer) );
987 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
989 mImpl->mBoundingBox = boundingBox;
992 const Rect<int>& Decorator::GetBoundingBox() const
994 return mImpl->mBoundingBox;
997 void Decorator::Relayout( const Vector2& size )
999 mImpl->Relayout( size );
1002 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1004 mImpl->UpdatePositions( scrollOffset );
1009 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1011 mImpl->mActiveCursor = activeCursor;
1014 unsigned int Decorator::GetActiveCursor() const
1016 return mImpl->mActiveCursor;
1019 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1021 // Adjust grab handle displacement
1022 if( PRIMARY_CURSOR == cursor )
1024 mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].position.x;
1025 mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].position.y;
1028 mImpl->mCursor[cursor].position.x = x;
1029 mImpl->mCursor[cursor].position.y = y;
1030 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1031 mImpl->mCursor[cursor].lineHeight = lineHeight;
1034 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1036 x = mImpl->mCursor[cursor].position.x;
1037 y = mImpl->mCursor[cursor].position.y;
1038 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1039 lineHeight = mImpl->mCursor[cursor].lineHeight;
1042 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1044 return mImpl->mCursor[cursor].position;
1047 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1049 mImpl->mCursor[cursor].color = color;
1052 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1054 return mImpl->mCursor[cursor].color;
1057 void Decorator::StartCursorBlink()
1059 if ( !mImpl->mCursorBlinkTimer )
1061 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1062 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1065 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1067 mImpl->mCursorBlinkTimer.Start();
1071 void Decorator::StopCursorBlink()
1073 if ( mImpl->mCursorBlinkTimer )
1075 mImpl->mCursorBlinkTimer.Stop();
1079 void Decorator::SetCursorBlinkInterval( float seconds )
1081 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1084 float Decorator::GetCursorBlinkInterval() const
1086 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1089 void Decorator::SetCursorBlinkDuration( float seconds )
1091 mImpl->mCursorBlinkDuration = seconds;
1094 float Decorator::GetCursorBlinkDuration() const
1096 return mImpl->mCursorBlinkDuration;
1101 void Decorator::SetGrabHandleActive( bool active )
1103 mImpl->mActiveGrabHandle = active;
1106 bool Decorator::IsGrabHandleActive() const
1108 return mImpl->mActiveGrabHandle;
1111 void Decorator::SetGrabHandleImage( GrabHandleImageType type, Dali::Image image )
1113 if( GRAB_HANDLE_IMAGE_PRESSED == type )
1115 mImpl->mGrabHandleImagePressed = image;
1119 mImpl->mGrabHandleImageReleased = image;
1123 Dali::Image Decorator::GetGrabHandleImage( GrabHandleImageType type ) const
1125 if( GRAB_HANDLE_IMAGE_PRESSED == type )
1127 return mImpl->mGrabHandleImagePressed;
1130 return mImpl->mGrabHandleImageReleased;
1135 void Decorator::SetSelectionActive( bool active )
1137 mImpl->mActiveSelection = active;
1140 bool Decorator::IsSelectionActive() const
1142 return mImpl->mActiveSelection;
1145 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
1147 mImpl->mSelectionHandle[handle].position.x = x;
1148 mImpl->mSelectionHandle[handle].position.y = y;
1149 mImpl->mSelectionHandle[handle].lineHeight = height;
1152 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
1154 x = mImpl->mSelectionHandle[handle].position.x;
1155 y = mImpl->mSelectionHandle[handle].position.y;
1156 height = mImpl->mSelectionHandle[handle].lineHeight;
1159 void Decorator::SetLeftSelectionImage( SelectionHandleState state, Dali::Image image )
1161 if( SELECTION_HANDLE_PRESSED == state )
1163 mImpl->mSelectionPressedLeft = image;
1167 mImpl->mSelectionReleasedLeft = image;
1171 Dali::Image Decorator::GetLeftSelectionImage( SelectionHandleState state ) const
1173 if( SELECTION_HANDLE_PRESSED == state )
1175 return mImpl->mSelectionPressedLeft;
1178 return mImpl->mSelectionReleasedLeft;
1181 void Decorator::SetRightSelectionImage( SelectionHandleState state, Dali::Image image )
1183 if( SELECTION_HANDLE_PRESSED == state )
1185 mImpl->mSelectionPressedRight = image;
1189 mImpl->mSelectionReleasedRight = image;
1193 Dali::Image Decorator::GetRightSelectionImage( SelectionHandleState state ) const
1195 if( SELECTION_HANDLE_PRESSED == state )
1197 return mImpl->mSelectionPressedRight;
1200 return mImpl->mSelectionReleasedRight;
1203 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1205 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1208 void Decorator::ClearHighlights()
1210 mImpl->mHighlightQuadList.clear();
1213 void Decorator::SetHighlightColor( const Vector4& color )
1215 mImpl->mHighlightColor = color;
1218 const Vector4& Decorator::GetHighlightColor() const
1220 return mImpl->mHighlightColor;
1223 void Decorator::SetPopupActive( bool active )
1225 mImpl->mActiveCopyPastePopup = active;
1228 bool Decorator::IsPopupActive() const
1230 return mImpl->mActiveCopyPastePopup ;
1235 void Decorator::SetScrollThreshold( float threshold )
1237 mImpl->SetScrollThreshold( threshold );
1240 float Decorator::GetScrollThreshold() const
1242 return mImpl->GetScrollThreshold();
1245 void Decorator::SetScrollSpeed( float speed )
1247 mImpl->SetScrollSpeed( speed );
1250 float Decorator::GetScrollSpeed() const
1252 return mImpl->GetScrollSpeed();
1255 Decorator::~Decorator()
1260 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
1263 mImpl = new Decorator::Impl( parent, observer );
1268 } // namespace Toolkit