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/touch-event.h>
34 #include <dali/public-api/events/pan-gesture.h>
35 #include <dali/public-api/events/pan-gesture-detector.h>
36 #include <dali/devel-api/geometry/mesh.h>
37 #include <dali/devel-api/geometry/mesh-data.h>
38 #include <dali/public-api/images/resource-image.h>
39 #include <dali/public-api/math/rect.h>
40 #include <dali/public-api/math/vector2.h>
41 #include <dali/public-api/math/vector4.h>
42 #include <dali/public-api/object/property-notification.h>
43 #include <dali/public-api/signals/connection-tracker.h>
46 #include <dali-toolkit/public-api/controls/control.h>
47 #include <dali-toolkit/public-api/controls/control-impl.h>
48 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
49 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
50 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
51 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup.h>
54 #define DECORATOR_DEBUG
64 #ifdef DECORATOR_DEBUG
65 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_TEXT_DECORATOR") );
76 const char* DEFAULT_GRAB_HANDLE_IMAGE_RELEASED( DALI_IMAGE_DIR "insertpoint-icon.png" );
77 const char* DEFAULT_GRAB_HANDLE_IMAGE_PRESSED( DALI_IMAGE_DIR "insertpoint-icon-pressed.png" );
78 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
79 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
81 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
82 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
84 const Dali::Vector4 LIGHT_BLUE( 0.07f, 0.41f, 0.59f, 1.0f ); // The text highlight color.
86 const unsigned int CURSOR_BLINK_INTERVAL = 500u; // Cursor blink interval
87 const float TO_MILLISECONDS = 1000.f;
88 const float TO_SECONDS = 1.f / TO_MILLISECONDS;
90 const float DISPLAYED_HIGHLIGHT_Z_OFFSET( -0.05f );
92 const unsigned int SCROLL_TICK_INTERVAL = 50u;
94 const float SCROLL_THRESHOLD = 10.f;
95 const float SCROLL_SPEED = 300.f;
96 const float SCROLL_DISTANCE = SCROLL_SPEED * SCROLL_TICK_INTERVAL * TO_SECONDS;
99 * structure to hold coordinates of each quad, which will make up the mesh.
101 struct QuadCoordinates
104 * Default constructor
112 * @param[in] x1 left co-ordinate
113 * @param[in] y1 top co-ordinate
114 * @param[in] x2 right co-ordinate
115 * @param[in] y2 bottom co-ordinate
117 QuadCoordinates(float x1, float y1, float x2, float y2)
123 Dali::Vector2 min; ///< top-left (minimum) position of quad
124 Dali::Vector2 max; ///< bottom-right (maximum) position of quad
127 typedef std::vector<QuadCoordinates> QuadContainer;
130 * @brief Takes a bounding rectangle in the local coordinates of an actor and returns the world coordinates Bounding Box.
131 * @param[in] boundingRectangle local bounding
132 * @param[out] Vector4 World coordinate bounding Box.
134 void LocalToWorldCoordinatesBoundingBox( const Dali::Rect<int>& boundingRectangle, Dali::Vector4& boundingBox )
136 // Convert to world coordinates and store as a Vector4 to be compatible with Property Notifications.
137 Dali::Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
139 const float originX = boundingRectangle.x - 0.5f * stageSize.width;
140 const float originY = boundingRectangle.y - 0.5f * stageSize.height;
142 boundingBox = Dali::Vector4( originX,
144 originX + boundingRectangle.width,
145 originY + boundingRectangle.height );
149 } // end of namespace
160 struct Decorator::Impl : public ConnectionTracker
174 : color( Dali::Color::BLACK ),
176 cursorHeight( 0.0f ),
192 grabDisplacementX( 0.f ),
193 grabDisplacementY( 0.f ),
204 float lineHeight; ///< Not the handle height
205 float grabDisplacementX;
206 float grabDisplacementY;
212 Impl( ControllerInterface& controller )
213 : mController( controller ),
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 mActiveLayer = Layer::New();
477 #ifdef DECORATOR_DEBUG
478 mActiveLayer.SetName ( "ActiveLayerActor" );
481 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
482 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
483 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
485 mController.AddDecoration( mActiveLayer );
488 mActiveLayer.RaiseToTop();
491 void CreateGrabHandle()
493 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
494 if( !grabHandle.actor )
496 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
498 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
500 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
502 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
505 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
506 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
507 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
508 // Area that Grab handle responds to, larger than actual handle so easier to move
509 #ifdef DECORATOR_DEBUG
510 grabHandle.actor.SetName( "GrabHandleActor" );
511 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
513 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
514 grabHandle.grabArea.SetName( "GrabArea" );
518 grabHandle.grabArea = Actor::New();
519 grabHandle.grabArea.SetName( "GrabArea" );
522 grabHandle.grabArea = Actor::New();
525 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
526 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
527 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
528 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
529 grabHandle.actor.Add( grabHandle.grabArea );
531 grabHandle.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnGrabHandleTouched );
532 mTapDetector.Attach( grabHandle.grabArea );
533 mPanGestureDetector.Attach( grabHandle.grabArea );
535 mActiveLayer.Add( grabHandle.actor );
539 void CreateSelectionHandles()
541 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
544 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
546 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
549 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
550 #ifdef DECORATOR_DEBUG
551 primary.actor.SetName("SelectionHandleOne");
553 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
554 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
555 primary.flipped = false;
557 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
558 #ifdef DECORATOR_DEBUG
559 primary.grabArea.SetName("SelectionHandleOneGrabArea");
561 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
562 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
563 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
565 mTapDetector.Attach( primary.grabArea );
566 mPanGestureDetector.Attach( primary.grabArea );
567 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
569 primary.actor.Add( primary.grabArea );
570 mActiveLayer.Add( primary.actor );
573 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
574 if( !secondary.actor )
576 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
578 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
581 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
582 #ifdef DECORATOR_DEBUG
583 secondary.actor.SetName("SelectionHandleTwo");
585 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
586 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
587 secondary.flipped = false;
589 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
590 #ifdef DECORATOR_DEBUG
591 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
593 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
594 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
595 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
597 mTapDetector.Attach( secondary.grabArea );
598 mPanGestureDetector.Attach( secondary.grabArea );
599 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
601 secondary.actor.Add( secondary.grabArea );
602 mActiveLayer.Add( secondary.actor );
606 void CreateHighlight()
608 if ( !mHighlightMeshActor )
610 mHighlightMaterial = Material::New( "HighlightMaterial" );
611 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
613 mHighlightMeshData.SetMaterial( mHighlightMaterial );
614 mHighlightMeshData.SetHasNormals( true );
616 mHighlightMesh = Mesh::New( mHighlightMeshData );
618 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
619 #ifdef DECORATOR_DEBUG
620 mHighlightMeshActor.SetName( "HighlightMeshActor" );
622 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
623 mHighlightMeshActor.SetPosition( 0.0f, 0.0f, DISPLAYED_HIGHLIGHT_Z_OFFSET );
625 mController.AddDecoration( mHighlightMeshActor );
629 void UpdateHighlight()
631 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
633 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
635 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
636 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
637 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
638 // [BOTTOM] [ MIDDLE ]
641 // Each quad is created as 2 triangles.
642 // Middle is just 1 quad regardless of its size.
656 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
658 MeshData::VertexContainer vertices;
659 Dali::MeshData::FaceIndices faceIndices;
661 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
662 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
664 // vertex position defaults to (0 0 0)
665 MeshData::Vertex vertex;
666 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
669 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
671 // Add each quad geometry (a sub-selection) to the mesh data.
681 QuadCoordinates& quad = *iter;
683 vertex.x = quad.min.x;
684 vertex.y = quad.min.y;
685 vertices.push_back( vertex );
688 vertex.x = quad.max.x;
689 vertex.y = quad.min.y;
690 vertices.push_back( vertex );
693 vertex.x = quad.min.x;
694 vertex.y = quad.max.y;
695 vertices.push_back( vertex );
697 // bottom-right (v+3)
698 vertex.x = quad.max.x;
699 vertex.y = quad.max.y;
700 vertices.push_back( vertex );
702 // triangle A (3, 1, 0)
703 faceIndices.push_back( v + 3 );
704 faceIndices.push_back( v + 1 );
705 faceIndices.push_back( v );
707 // triangle B (0, 2, 3)
708 faceIndices.push_back( v );
709 faceIndices.push_back( v + 2 );
710 faceIndices.push_back( v + 3 );
712 mHighlightMeshData.SetFaceIndices( faceIndices );
715 BoneContainer bones(0); // passed empty as bones not required
716 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
717 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
721 void OnTap( Actor actor, const TapGesture& tap )
723 if( actor == mHandle[GRAB_HANDLE].actor )
729 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
731 if( Gesture::Started == gesture.state )
733 handle.grabDisplacementX = handle.grabDisplacementY = 0;
736 handle.grabDisplacementX += gesture.displacement.x;
737 handle.grabDisplacementY += gesture.displacement.y;
739 const float x = handle.position.x + handle.grabDisplacementX;
740 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
742 if( Gesture::Started == gesture.state ||
743 Gesture::Continuing == gesture.state )
746 mController.GetTargetSize( targetSize );
748 if( x < mScrollThreshold )
750 mScrollDirection = SCROLL_RIGHT;
751 mHandleScrolling = type;
754 else if( x > targetSize.width - mScrollThreshold )
756 mScrollDirection = SCROLL_LEFT;
757 mHandleScrolling = type;
762 mHandleScrolling = HANDLE_TYPE_COUNT;
764 mController.DecorationEvent( type, HANDLE_PRESSED, x, y );
767 else if( Gesture::Finished == gesture.state ||
768 Gesture::Cancelled == gesture.state )
770 if( mScrollTimer && mScrollTimer.IsRunning() )
772 mHandleScrolling = HANDLE_TYPE_COUNT;
774 mController.DecorationEvent( type, HANDLE_STOP_SCROLLING, x, y );
778 mController.DecorationEvent( type, HANDLE_RELEASED, x, y );
780 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
784 void OnPan( Actor actor, const PanGesture& gesture )
786 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
787 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
788 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
790 if( actor == grabHandle.grabArea )
792 DoPan( grabHandle, GRAB_HANDLE, gesture );
794 else if( actor == primarySelectionHandle.grabArea )
796 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
798 else if( actor == secondarySelectionHandle.grabArea )
800 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
804 bool OnGrabHandleTouched( Actor actor, const TouchEvent& event )
806 // Switch between pressed/release grab-handle images
807 if( event.GetPointCount() > 0 &&
808 mHandle[GRAB_HANDLE].actor )
810 const TouchPoint& point = event.GetPoint(0);
812 if( TouchPoint::Down == point.state &&
813 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
815 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] );
817 else if( TouchPoint::Up == point.state &&
818 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
820 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
824 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
828 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
831 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
835 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
838 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
844 float AlternatePopUpPositionRelativeToCursor()
846 float alternativePosition=0.0f;;
848 if ( mPrimaryCursor ) // Secondary cursor not used for paste
850 Cursor cursor = PRIMARY_CURSOR;
851 alternativePosition = mCursor[cursor].position.y;
854 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
856 if( mHandle[GRAB_HANDLE].active )
858 // If grab handle enabled then position pop-up below the grab handle.
859 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
860 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
861 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
865 alternativePosition += popupHeight;
868 return alternativePosition;
871 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
873 float alternativeYPosition=0.0f;
874 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
875 // if can't be positioned above, then position below row.
876 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
878 mCopyPastePopup.SetY( alternativeYPosition );
882 void SetUpPopUpPositionNotifications( )
884 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
886 // Exceeding vertical boundary
888 Vector4 worldCoordinatesBoundingBox;
889 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
891 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
893 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
894 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
895 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
897 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
900 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
902 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
904 // Parent must already by added to Stage for these Get calls to work
905 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
906 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
907 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
908 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
910 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
911 Vector4 boundingRectangleWorld;
912 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
914 // Calculate distance to move popup (in local space) so fits within the boundary
915 float xOffSetToKeepWithinBounds = 0.0f;
916 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
918 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
920 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
922 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
925 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
926 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
928 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
931 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
934 void SetScrollThreshold( float threshold )
936 mScrollThreshold = threshold;
939 float GetScrollThreshold() const
941 return mScrollThreshold;
944 void SetScrollSpeed( float speed )
946 mScrollSpeed = speed;
947 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
950 float GetScrollSpeed() const
956 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
958 * It only starts the timer if it's already created.
960 void StartScrollTimer()
964 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
965 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
968 if( !mScrollTimer.IsRunning() )
970 mScrollTimer.Start();
975 * Stops the timer used to scroll the text.
977 void StopScrollTimer()
986 * Callback called by the timer used to scroll the text.
988 * It calculates and sets a new scroll position.
990 bool OnScrollTimerTick()
992 if( HANDLE_TYPE_COUNT != mHandleScrolling )
994 mController.DecorationEvent( mHandleScrolling,
996 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
1003 ControllerInterface& mController;
1005 TapGestureDetector mTapDetector;
1006 PanGestureDetector mPanGestureDetector;
1007 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
1008 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
1010 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
1011 ImageActor mPrimaryCursor;
1012 ImageActor mSecondaryCursor;
1013 MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
1014 TextSelectionPopup mCopyPastePopup;
1016 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1018 Mesh mHighlightMesh; ///< Mesh for highlight
1019 MeshData mHighlightMeshData; ///< Mesh Data for highlight
1020 Material mHighlightMaterial; ///< Material used for highlight
1022 CursorImpl mCursor[CURSOR_COUNT];
1023 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1024 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1026 Rect<int> mBoundingBox;
1027 Vector4 mHighlightColor; ///< Color of the highlight
1029 unsigned int mActiveCursor;
1030 unsigned int mCursorBlinkInterval;
1031 float mCursorBlinkDuration;
1032 HandleType mHandleScrolling; ///< The handle which is scrolling.
1033 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1034 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1035 float mScrollSpeed; ///< The scroll speed in pixels per second.
1036 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1037 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1039 bool mActiveCopyPastePopup : 1;
1040 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1041 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1042 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1045 DecoratorPtr Decorator::New( ControllerInterface& controller )
1047 return DecoratorPtr( new Decorator(controller) );
1050 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1052 mImpl->mBoundingBox = boundingBox;
1055 const Rect<int>& Decorator::GetBoundingBox() const
1057 return mImpl->mBoundingBox;
1060 void Decorator::Relayout( const Vector2& size )
1062 mImpl->Relayout( size );
1065 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1067 mImpl->UpdatePositions( scrollOffset );
1072 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1074 mImpl->mActiveCursor = activeCursor;
1077 unsigned int Decorator::GetActiveCursor() const
1079 return mImpl->mActiveCursor;
1082 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1084 mImpl->mCursor[cursor].position.x = x;
1085 mImpl->mCursor[cursor].position.y = y;
1086 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1087 mImpl->mCursor[cursor].lineHeight = lineHeight;
1090 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1092 x = mImpl->mCursor[cursor].position.x;
1093 y = mImpl->mCursor[cursor].position.y;
1094 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1095 lineHeight = mImpl->mCursor[cursor].lineHeight;
1098 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1100 return mImpl->mCursor[cursor].position;
1103 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1105 mImpl->mCursor[cursor].color = color;
1108 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1110 return mImpl->mCursor[cursor].color;
1113 void Decorator::StartCursorBlink()
1115 if ( !mImpl->mCursorBlinkTimer )
1117 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1118 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1121 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1123 mImpl->mCursorBlinkTimer.Start();
1127 void Decorator::StopCursorBlink()
1129 if ( mImpl->mCursorBlinkTimer )
1131 mImpl->mCursorBlinkTimer.Stop();
1135 void Decorator::SetCursorBlinkInterval( float seconds )
1137 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1140 float Decorator::GetCursorBlinkInterval() const
1142 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1145 void Decorator::SetCursorBlinkDuration( float seconds )
1147 mImpl->mCursorBlinkDuration = seconds;
1150 float Decorator::GetCursorBlinkDuration() const
1152 return mImpl->mCursorBlinkDuration;
1157 void Decorator::SetHandleActive( HandleType handleType, bool active )
1159 mImpl->mHandle[handleType].active = active;
1162 bool Decorator::IsHandleActive( HandleType handleType ) const
1164 return mImpl->mHandle[handleType].active ;
1167 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1169 mImpl->mHandleImages[handleType][handleImageType] = image;
1172 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1174 return mImpl->mHandleImages[handleType][handleImageType];
1177 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1179 // Adjust grab handle displacement
1180 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1182 handle.grabDisplacementX -= x - handle.position.x;
1183 handle.grabDisplacementY -= y - handle.position.y;
1185 handle.position.x = x;
1186 handle.position.y = y;
1187 handle.lineHeight = height;
1190 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1192 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1194 x = handle.position.x;
1195 y = handle.position.y;
1196 height = handle.lineHeight;
1199 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1201 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1204 void Decorator::ClearHighlights()
1206 mImpl->mHighlightQuadList.clear();
1209 void Decorator::SetHighlightColor( const Vector4& color )
1211 mImpl->mHighlightColor = color;
1214 const Vector4& Decorator::GetHighlightColor() const
1216 return mImpl->mHighlightColor;
1219 void Decorator::SetPopupActive( bool active )
1221 mImpl->mActiveCopyPastePopup = active;
1224 bool Decorator::IsPopupActive() const
1226 return mImpl->mActiveCopyPastePopup ;
1231 void Decorator::SetScrollThreshold( float threshold )
1233 mImpl->SetScrollThreshold( threshold );
1236 float Decorator::GetScrollThreshold() const
1238 return mImpl->GetScrollThreshold();
1241 void Decorator::SetScrollSpeed( float speed )
1243 mImpl->SetScrollSpeed( speed );
1246 float Decorator::GetScrollSpeed() const
1248 return mImpl->GetScrollSpeed();
1251 Decorator::~Decorator()
1256 Decorator::Decorator( ControllerInterface& controller )
1259 mImpl = new Decorator::Impl( controller );
1264 } // namespace Toolkit