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( (0xb2 / 255.0f), (0xeb / 255.0f), (0xf2 / 255.0f), 0.5f ); // 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 ),
228 mSwapSelectionHandles( false )
233 * Relayout of the decorations owned by the decorator.
234 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
236 void Relayout( const Vector2& size )
238 // TODO - Remove this if nothing is active
241 // Show or hide the cursors
245 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
246 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
247 if( mPrimaryCursorVisible )
249 Vector2 position = cursor.position;
250 if( GRAB_HANDLE == mHandleScrolling )
252 if( mScrollDirection == SCROLL_RIGHT )
258 position.x = size.width;
262 mPrimaryCursor.SetPosition( position.x,
264 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
266 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
268 if( mSecondaryCursor )
270 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
271 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
272 if( mSecondaryCursorVisible )
274 mSecondaryCursor.SetPosition( cursor.position.x,
276 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
278 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
281 // Show or hide the grab handle
282 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
283 if( grabHandle.active )
285 Vector2 position = grabHandle.position;
287 if( GRAB_HANDLE == mHandleScrolling )
289 if( mScrollDirection == SCROLL_RIGHT )
295 position.x = size.width;
299 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
307 grabHandle.actor.SetPosition( position.x,
308 position.y + grabHandle.lineHeight );
310 grabHandle.actor.SetVisible( isVisible );
312 else if( grabHandle.actor )
314 UnparentAndReset( grabHandle.actor );
317 // Show or hide the selection handles/highlight
318 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
319 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
320 if( primary.active || secondary.active )
322 Vector2 primaryPosition = primary.position;
323 Vector2 secondaryPosition = secondary.position;
325 if( LEFT_SELECTION_HANDLE == mHandleScrolling )
327 if( mScrollDirection == SCROLL_RIGHT )
329 primaryPosition.x = 0.f;
333 primaryPosition.x = size.width;
336 else if( RIGHT_SELECTION_HANDLE == mHandleScrolling )
338 if( mScrollDirection == SCROLL_RIGHT )
340 secondaryPosition.x = 0.f;
344 secondaryPosition.x = size.width;
348 const bool isPrimaryVisible = ( primaryPosition.x <= size.width ) && ( primaryPosition.x >= 0.f );
349 const bool isSecondaryVisible = ( secondaryPosition.x <= size.width ) && ( secondaryPosition.x >= 0.f );
351 if( isPrimaryVisible || isSecondaryVisible )
355 CreateSelectionHandles();
357 if( isPrimaryVisible )
359 primary.actor.SetPosition( primaryPosition.x,
360 primaryPosition.y + primary.lineHeight );
362 const bool flip = mSwapSelectionHandles ^ primary.flipped;
363 primary.actor.SetImage( flip ? mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] : mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
365 primary.actor.SetAnchorPoint( flip ? AnchorPoint::TOP_LEFT : AnchorPoint::TOP_RIGHT );
368 if( isSecondaryVisible )
370 secondary.actor.SetPosition( secondaryPosition.x,
371 secondaryPosition.y + secondary.lineHeight );
373 const bool flip = mSwapSelectionHandles ^ secondary.flipped;
375 secondary.actor.SetImage( ( mSwapSelectionHandles ^ secondary.flipped ) ? mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] : mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
376 secondary.actor.SetAnchorPoint( flip ? AnchorPoint::TOP_RIGHT : AnchorPoint::TOP_LEFT );
379 primary.actor.SetVisible( isPrimaryVisible );
380 secondary.actor.SetVisible( isSecondaryVisible );
387 UnparentAndReset( primary.actor );
388 UnparentAndReset( secondary.actor );
389 UnparentAndReset( mHighlightMeshActor );
392 if ( mActiveCopyPastePopup )
394 if ( !mCopyPastePopup )
396 mCopyPastePopup = TextSelectionPopup::New();
397 #ifdef DECORATOR_DEBUG
398 mCopyPastePopup.SetName("mCopyPastePopup");
400 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
401 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
402 mActiveLayer.Add ( mCopyPastePopup );
407 if ( mCopyPastePopup )
409 UnparentAndReset( mCopyPastePopup );
414 void UpdatePositions( const Vector2& scrollOffset )
416 mCursor[PRIMARY_CURSOR].position += scrollOffset;
417 mCursor[SECONDARY_CURSOR].position += scrollOffset;
418 mHandle[ GRAB_HANDLE ].position += scrollOffset;
419 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
420 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
421 mHighlightPosition += scrollOffset;
424 void PopUpRelayoutComplete( Actor actor )
426 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
428 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
430 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
432 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
434 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
436 SetUpPopUpPositionNotifications();
438 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
441 void CreateCursor( ImageActor& cursor, const Vector4& color )
443 cursor = CreateSolidColorActor( color );
444 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
445 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
448 // Add or Remove cursor(s) from parent
451 if( mActiveCursor == ACTIVE_CURSOR_NONE )
453 UnparentAndReset( mPrimaryCursor );
454 UnparentAndReset( mSecondaryCursor );
458 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
459 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
460 mActiveCursor == ACTIVE_CURSOR_BOTH )
462 if ( !mPrimaryCursor )
464 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
465 #ifdef DECORATOR_DEBUG
466 mPrimaryCursor.SetName( "PrimaryCursorActor" );
468 mActiveLayer.Add( mPrimaryCursor );
472 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
474 if ( !mSecondaryCursor )
476 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
477 #ifdef DECORATOR_DEBUG
478 mSecondaryCursor.SetName( "SecondaryCursorActor" );
480 mActiveLayer.Add( mSecondaryCursor );
485 UnparentAndReset( mSecondaryCursor );
490 bool OnCursorBlinkTimerTick()
493 if ( mPrimaryCursor )
495 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
497 if ( mSecondaryCursor )
499 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
502 mCursorBlinkStatus = !mCursorBlinkStatus;
507 void SetupTouchEvents()
511 mTapDetector = TapGestureDetector::New();
512 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
515 if ( !mPanGestureDetector )
517 mPanGestureDetector = PanGestureDetector::New();
518 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
522 void CreateActiveLayer()
526 mActiveLayer = Layer::New();
527 #ifdef DECORATOR_DEBUG
528 mActiveLayer.SetName ( "ActiveLayerActor" );
531 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
532 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
533 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
535 // Add the active layer telling the controller it doesn't need clipping.
536 mController.AddDecoration( mActiveLayer, false );
539 mActiveLayer.RaiseToTop();
542 void CreateGrabHandle()
544 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
545 if( !grabHandle.actor )
547 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
549 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
551 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
553 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
556 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
557 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
558 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
559 // Area that Grab handle responds to, larger than actual handle so easier to move
560 #ifdef DECORATOR_DEBUG
561 grabHandle.actor.SetName( "GrabHandleActor" );
562 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
564 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
565 grabHandle.grabArea.SetName( "GrabArea" );
569 grabHandle.grabArea = Actor::New();
570 grabHandle.grabArea.SetName( "GrabArea" );
573 grabHandle.grabArea = Actor::New();
576 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
577 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
578 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
579 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
580 grabHandle.actor.Add( grabHandle.grabArea );
582 grabHandle.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnGrabHandleTouched );
583 mTapDetector.Attach( grabHandle.grabArea );
584 mPanGestureDetector.Attach( grabHandle.grabArea );
586 mActiveLayer.Add( grabHandle.actor );
590 void CreateSelectionHandles()
592 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
595 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
597 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
600 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
601 #ifdef DECORATOR_DEBUG
602 primary.actor.SetName("SelectionHandleOne");
604 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
605 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
606 primary.flipped = false;
608 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
609 #ifdef DECORATOR_DEBUG
610 primary.grabArea.SetName("SelectionHandleOneGrabArea");
612 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
613 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
614 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
616 mTapDetector.Attach( primary.grabArea );
617 mPanGestureDetector.Attach( primary.grabArea );
618 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
620 primary.actor.Add( primary.grabArea );
621 mActiveLayer.Add( primary.actor );
624 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
625 if( !secondary.actor )
627 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
629 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
632 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
633 #ifdef DECORATOR_DEBUG
634 secondary.actor.SetName("SelectionHandleTwo");
636 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
637 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
638 secondary.flipped = false;
640 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
641 #ifdef DECORATOR_DEBUG
642 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
644 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
645 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
646 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
648 mTapDetector.Attach( secondary.grabArea );
649 mPanGestureDetector.Attach( secondary.grabArea );
650 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
652 secondary.actor.Add( secondary.grabArea );
653 mActiveLayer.Add( secondary.actor );
657 void CreateHighlight()
659 if ( !mHighlightMeshActor )
661 mHighlightMaterial = Material::New( "HighlightMaterial" );
662 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
664 mHighlightMeshData.SetMaterial( mHighlightMaterial );
665 mHighlightMeshData.SetHasNormals( true );
667 mHighlightMesh = Mesh::New( mHighlightMeshData );
669 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
670 #ifdef DECORATOR_DEBUG
671 mHighlightMeshActor.SetName( "HighlightMeshActor" );
673 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
675 // Add the highlight box telling the controller it needs clipping.
676 mController.AddDecoration( mHighlightMeshActor, true );
679 mHighlightMeshActor.SetPosition( mHighlightPosition.x, mHighlightPosition.y, DISPLAYED_HIGHLIGHT_Z_OFFSET );
682 void UpdateHighlight()
684 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
686 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
688 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
689 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
690 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
691 // [BOTTOM] [ MIDDLE ]
694 // Each quad is created as 2 triangles.
695 // Middle is just 1 quad regardless of its size.
709 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
711 MeshData::VertexContainer vertices;
712 Dali::MeshData::FaceIndices faceIndices;
714 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
715 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
717 // vertex position defaults to (0 0 0)
718 MeshData::Vertex vertex;
719 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
722 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
724 // Add each quad geometry (a sub-selection) to the mesh data.
734 QuadCoordinates& quad = *iter;
736 vertex.x = quad.min.x;
737 vertex.y = quad.min.y;
738 vertices.push_back( vertex );
741 vertex.x = quad.max.x;
742 vertex.y = quad.min.y;
743 vertices.push_back( vertex );
746 vertex.x = quad.min.x;
747 vertex.y = quad.max.y;
748 vertices.push_back( vertex );
750 // bottom-right (v+3)
751 vertex.x = quad.max.x;
752 vertex.y = quad.max.y;
753 vertices.push_back( vertex );
755 // triangle A (3, 1, 0)
756 faceIndices.push_back( v + 3 );
757 faceIndices.push_back( v + 1 );
758 faceIndices.push_back( v );
760 // triangle B (0, 2, 3)
761 faceIndices.push_back( v );
762 faceIndices.push_back( v + 2 );
763 faceIndices.push_back( v + 3 );
765 mHighlightMeshData.SetFaceIndices( faceIndices );
768 BoneContainer bones(0); // passed empty as bones not required
769 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
770 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
774 void OnTap( Actor actor, const TapGesture& tap )
776 if( actor == mHandle[GRAB_HANDLE].actor )
782 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
784 if( Gesture::Started == gesture.state )
786 handle.grabDisplacementX = handle.grabDisplacementY = 0;
789 handle.grabDisplacementX += gesture.displacement.x;
790 handle.grabDisplacementY += gesture.displacement.y;
792 const float x = handle.position.x + handle.grabDisplacementX;
793 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
795 if( Gesture::Started == gesture.state ||
796 Gesture::Continuing == gesture.state )
799 mController.GetTargetSize( targetSize );
801 if( x < mScrollThreshold )
803 mScrollDirection = SCROLL_RIGHT;
804 mHandleScrolling = type;
807 else if( x > targetSize.width - mScrollThreshold )
809 mScrollDirection = SCROLL_LEFT;
810 mHandleScrolling = type;
815 mHandleScrolling = HANDLE_TYPE_COUNT;
817 mController.DecorationEvent( type, HANDLE_PRESSED, x, y );
820 else if( Gesture::Finished == gesture.state ||
821 Gesture::Cancelled == gesture.state )
823 if( mScrollTimer && mScrollTimer.IsRunning() )
825 mHandleScrolling = HANDLE_TYPE_COUNT;
827 mController.DecorationEvent( type, HANDLE_STOP_SCROLLING, x, y );
831 mController.DecorationEvent( type, HANDLE_RELEASED, x, y );
833 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
837 void OnPan( Actor actor, const PanGesture& gesture )
839 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
840 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
841 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
843 if( actor == grabHandle.grabArea )
845 DoPan( grabHandle, GRAB_HANDLE, gesture );
847 else if( actor == primarySelectionHandle.grabArea )
849 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
851 else if( actor == secondarySelectionHandle.grabArea )
853 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
857 bool OnGrabHandleTouched( Actor actor, const TouchEvent& event )
859 // Switch between pressed/release grab-handle images
860 if( event.GetPointCount() > 0 &&
861 mHandle[GRAB_HANDLE].actor )
863 const TouchPoint& point = event.GetPoint(0);
865 if( TouchPoint::Down == point.state &&
866 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
868 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] );
870 else if( TouchPoint::Up == point.state &&
871 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
873 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
877 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
881 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
884 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
888 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
891 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
897 float AlternatePopUpPositionRelativeToCursor()
899 float alternativePosition=0.0f;;
901 if ( mPrimaryCursor ) // Secondary cursor not used for paste
903 Cursor cursor = PRIMARY_CURSOR;
904 alternativePosition = mCursor[cursor].position.y;
907 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
909 if( mHandle[GRAB_HANDLE].active )
911 // If grab handle enabled then position pop-up below the grab handle.
912 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
913 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
914 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
918 alternativePosition += popupHeight;
921 return alternativePosition;
924 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
926 float alternativeYPosition=0.0f;
927 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
928 // if can't be positioned above, then position below row.
929 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
931 mCopyPastePopup.SetY( alternativeYPosition );
935 void SetUpPopUpPositionNotifications( )
937 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
939 // Exceeding vertical boundary
941 Vector4 worldCoordinatesBoundingBox;
942 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
944 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
946 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
947 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
948 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
950 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
953 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
955 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
957 // Parent must already by added to Stage for these Get calls to work
958 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
959 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
960 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
961 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
963 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
964 Vector4 boundingRectangleWorld;
965 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
967 // Calculate distance to move popup (in local space) so fits within the boundary
968 float xOffSetToKeepWithinBounds = 0.0f;
969 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
971 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
973 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
975 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
978 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
979 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
981 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
984 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
987 void SetScrollThreshold( float threshold )
989 mScrollThreshold = threshold;
992 float GetScrollThreshold() const
994 return mScrollThreshold;
997 void SetScrollSpeed( float speed )
999 mScrollSpeed = speed;
1000 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
1003 float GetScrollSpeed() const
1005 return mScrollSpeed;
1009 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
1011 * It only starts the timer if it's already created.
1013 void StartScrollTimer()
1017 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
1018 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
1021 if( !mScrollTimer.IsRunning() )
1023 mScrollTimer.Start();
1028 * Stops the timer used to scroll the text.
1030 void StopScrollTimer()
1034 mScrollTimer.Stop();
1039 * Callback called by the timer used to scroll the text.
1041 * It calculates and sets a new scroll position.
1043 bool OnScrollTimerTick()
1045 if( HANDLE_TYPE_COUNT != mHandleScrolling )
1047 mController.DecorationEvent( mHandleScrolling,
1049 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
1056 ControllerInterface& mController;
1058 TapGestureDetector mTapDetector;
1059 PanGestureDetector mPanGestureDetector;
1060 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
1061 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
1063 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
1064 ImageActor mPrimaryCursor;
1065 ImageActor mSecondaryCursor;
1066 MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
1067 TextSelectionPopup mCopyPastePopup;
1069 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1071 Mesh mHighlightMesh; ///< Mesh for highlight
1072 MeshData mHighlightMeshData; ///< Mesh Data for highlight
1073 Material mHighlightMaterial; ///< Material used for highlight
1075 CursorImpl mCursor[CURSOR_COUNT];
1076 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1077 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1078 Vector2 mHighlightPosition; ///< The position of the highlight actor.
1080 Rect<int> mBoundingBox;
1081 Vector4 mHighlightColor; ///< Color of the highlight
1083 unsigned int mActiveCursor;
1084 unsigned int mCursorBlinkInterval;
1085 float mCursorBlinkDuration;
1086 HandleType mHandleScrolling; ///< The handle which is scrolling.
1087 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1088 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1089 float mScrollSpeed; ///< The scroll speed in pixels per second.
1090 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1091 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1093 bool mActiveCopyPastePopup : 1;
1094 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1095 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1096 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1097 bool mSwapSelectionHandles : 1; ///< Whether to swap the selection handle images.
1100 DecoratorPtr Decorator::New( ControllerInterface& controller )
1102 return DecoratorPtr( new Decorator(controller) );
1105 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1107 mImpl->mBoundingBox = boundingBox;
1110 const Rect<int>& Decorator::GetBoundingBox() const
1112 return mImpl->mBoundingBox;
1115 void Decorator::Relayout( const Vector2& size )
1117 mImpl->Relayout( size );
1120 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1122 mImpl->UpdatePositions( scrollOffset );
1127 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1129 mImpl->mActiveCursor = activeCursor;
1132 unsigned int Decorator::GetActiveCursor() const
1134 return mImpl->mActiveCursor;
1137 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1139 mImpl->mCursor[cursor].position.x = x;
1140 mImpl->mCursor[cursor].position.y = y;
1141 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1142 mImpl->mCursor[cursor].lineHeight = lineHeight;
1145 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1147 x = mImpl->mCursor[cursor].position.x;
1148 y = mImpl->mCursor[cursor].position.y;
1149 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1150 lineHeight = mImpl->mCursor[cursor].lineHeight;
1153 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1155 return mImpl->mCursor[cursor].position;
1158 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1160 mImpl->mCursor[cursor].color = color;
1163 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1165 return mImpl->mCursor[cursor].color;
1168 void Decorator::StartCursorBlink()
1170 if ( !mImpl->mCursorBlinkTimer )
1172 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1173 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1176 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1178 mImpl->mCursorBlinkTimer.Start();
1182 void Decorator::StopCursorBlink()
1184 if ( mImpl->mCursorBlinkTimer )
1186 mImpl->mCursorBlinkTimer.Stop();
1190 void Decorator::SetCursorBlinkInterval( float seconds )
1192 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1195 float Decorator::GetCursorBlinkInterval() const
1197 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1200 void Decorator::SetCursorBlinkDuration( float seconds )
1202 mImpl->mCursorBlinkDuration = seconds;
1205 float Decorator::GetCursorBlinkDuration() const
1207 return mImpl->mCursorBlinkDuration;
1212 void Decorator::SetHandleActive( HandleType handleType, bool active )
1214 mImpl->mHandle[handleType].active = active;
1217 bool Decorator::IsHandleActive( HandleType handleType ) const
1219 return mImpl->mHandle[handleType].active ;
1222 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1224 mImpl->mHandleImages[handleType][handleImageType] = image;
1227 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1229 return mImpl->mHandleImages[handleType][handleImageType];
1232 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1234 // Adjust grab handle displacement
1235 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1237 handle.grabDisplacementX -= x - handle.position.x;
1238 handle.grabDisplacementY -= y - handle.position.y;
1240 handle.position.x = x;
1241 handle.position.y = y;
1242 handle.lineHeight = height;
1245 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1247 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1249 x = handle.position.x;
1250 y = handle.position.y;
1251 height = handle.lineHeight;
1254 const Vector2& Decorator::GetPosition( HandleType handleType ) const
1256 return mImpl->mHandle[handleType].position;
1259 void Decorator::SwapSelectionHandlesEnabled( bool enable )
1261 mImpl->mSwapSelectionHandles = enable;
1264 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1266 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1269 void Decorator::ClearHighlights()
1271 mImpl->mHighlightQuadList.clear();
1272 mImpl->mHighlightPosition = Vector2::ZERO;
1275 void Decorator::SetHighlightColor( const Vector4& color )
1277 mImpl->mHighlightColor = color;
1280 const Vector4& Decorator::GetHighlightColor() const
1282 return mImpl->mHighlightColor;
1285 void Decorator::SetPopupActive( bool active )
1287 mImpl->mActiveCopyPastePopup = active;
1290 bool Decorator::IsPopupActive() const
1292 return mImpl->mActiveCopyPastePopup ;
1297 void Decorator::SetScrollThreshold( float threshold )
1299 mImpl->SetScrollThreshold( threshold );
1302 float Decorator::GetScrollThreshold() const
1304 return mImpl->GetScrollThreshold();
1307 void Decorator::SetScrollSpeed( float speed )
1309 mImpl->SetScrollSpeed( speed );
1312 float Decorator::GetScrollSpeed() const
1314 return mImpl->GetScrollSpeed();
1317 Decorator::~Decorator()
1322 Decorator::Decorator( ControllerInterface& controller )
1325 mImpl = new Decorator::Impl( controller );
1330 } // namespace Toolkit