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 mEnabledPopupButtons( TextSelectionPopup::NONE ),
215 mBoundingBox( Rect<int>() ),
216 mHighlightColor( LIGHT_BLUE ),
217 mActiveCursor( ACTIVE_CURSOR_NONE ),
218 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
219 mCursorBlinkDuration( 0.0f ),
220 mHandleScrolling( HANDLE_TYPE_COUNT ),
221 mScrollDirection( SCROLL_NONE ),
222 mScrollThreshold( SCROLL_THRESHOLD ),
223 mScrollSpeed( SCROLL_SPEED ),
224 mScrollDistance( SCROLL_DISTANCE ),
225 mActiveCopyPastePopup( false ),
226 mCursorBlinkStatus( true ),
227 mPrimaryCursorVisible( false ),
228 mSecondaryCursorVisible( false ),
229 mSwapSelectionHandles( false )
234 * Relayout of the decorations owned by the decorator.
235 * @param[in] size The Size of the UI control the decorator is adding it's decorations to.
237 void Relayout( const Vector2& size )
239 // TODO - Remove this if nothing is active
242 // Show or hide the cursors
246 const CursorImpl& cursor = mCursor[PRIMARY_CURSOR];
247 mPrimaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
248 if( mPrimaryCursorVisible )
250 Vector2 position = cursor.position;
251 if( GRAB_HANDLE == mHandleScrolling )
253 if( mScrollDirection == SCROLL_RIGHT )
259 position.x = size.width;
263 mPrimaryCursor.SetPosition( position.x,
265 mPrimaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
267 mPrimaryCursor.SetVisible( mPrimaryCursorVisible );
269 if( mSecondaryCursor )
271 const CursorImpl& cursor = mCursor[SECONDARY_CURSOR];
272 mSecondaryCursorVisible = ( cursor.position.x <= size.width ) && ( cursor.position.x >= 0.f );
273 if( mSecondaryCursorVisible )
275 mSecondaryCursor.SetPosition( cursor.position.x,
277 mSecondaryCursor.SetSize( Size( 1.0f, cursor.cursorHeight ) );
279 mSecondaryCursor.SetVisible( mSecondaryCursorVisible );
282 // Show or hide the grab handle
283 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
284 if( grabHandle.active )
286 Vector2 position = grabHandle.position;
288 if( GRAB_HANDLE == mHandleScrolling )
290 if( mScrollDirection == SCROLL_RIGHT )
296 position.x = size.width;
300 const bool isVisible = ( position.x <= size.width ) && ( position.x >= 0.f );
308 grabHandle.actor.SetPosition( position.x,
309 position.y + grabHandle.lineHeight );
311 grabHandle.actor.SetVisible( isVisible );
313 else if( grabHandle.actor )
315 UnparentAndReset( grabHandle.actor );
318 // Show or hide the selection handles/highlight
319 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
320 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
321 if( primary.active || secondary.active )
323 Vector2 primaryPosition = primary.position;
324 Vector2 secondaryPosition = secondary.position;
326 if( LEFT_SELECTION_HANDLE == mHandleScrolling )
328 if( mScrollDirection == SCROLL_RIGHT )
330 primaryPosition.x = 0.f;
334 primaryPosition.x = size.width;
337 else if( RIGHT_SELECTION_HANDLE == mHandleScrolling )
339 if( mScrollDirection == SCROLL_RIGHT )
341 secondaryPosition.x = 0.f;
345 secondaryPosition.x = size.width;
349 const bool isPrimaryVisible = ( primaryPosition.x <= size.width ) && ( primaryPosition.x >= 0.f );
350 const bool isSecondaryVisible = ( secondaryPosition.x <= size.width ) && ( secondaryPosition.x >= 0.f );
352 if( isPrimaryVisible || isSecondaryVisible )
356 CreateSelectionHandles();
358 if( isPrimaryVisible )
360 primary.actor.SetPosition( primaryPosition.x,
361 primaryPosition.y + primary.lineHeight );
363 const bool flip = mSwapSelectionHandles ^ primary.flipped;
364 primary.actor.SetImage( flip ? mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] : mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
366 primary.actor.SetAnchorPoint( flip ? AnchorPoint::TOP_LEFT : AnchorPoint::TOP_RIGHT );
369 if( isSecondaryVisible )
371 secondary.actor.SetPosition( secondaryPosition.x,
372 secondaryPosition.y + secondary.lineHeight );
374 const bool flip = mSwapSelectionHandles ^ secondary.flipped;
376 secondary.actor.SetImage( ( mSwapSelectionHandles ^ secondary.flipped ) ? mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] : mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
377 secondary.actor.SetAnchorPoint( flip ? AnchorPoint::TOP_RIGHT : AnchorPoint::TOP_LEFT );
380 primary.actor.SetVisible( isPrimaryVisible );
381 secondary.actor.SetVisible( isSecondaryVisible );
388 UnparentAndReset( primary.actor );
389 UnparentAndReset( secondary.actor );
390 UnparentAndReset( mHighlightMeshActor );
393 if ( mActiveCopyPastePopup )
395 if ( !mCopyPastePopup )
397 mCopyPastePopup = TextSelectionPopup::New( mEnabledPopupButtons );
398 #ifdef DECORATOR_DEBUG
399 mCopyPastePopup.SetName("mCopyPastePopup");
401 mCopyPastePopup.SetAnchorPoint( AnchorPoint::CENTER );
402 mCopyPastePopup.OnRelayoutSignal().Connect( this, &Decorator::Impl::PopUpRelayoutComplete ); // Position popup after size negotiation
403 mActiveLayer.Add ( mCopyPastePopup );
408 if ( mCopyPastePopup )
410 UnparentAndReset( mCopyPastePopup );
415 void UpdatePositions( const Vector2& scrollOffset )
417 mCursor[PRIMARY_CURSOR].position += scrollOffset;
418 mCursor[SECONDARY_CURSOR].position += scrollOffset;
419 mHandle[ GRAB_HANDLE ].position += scrollOffset;
420 mHandle[ LEFT_SELECTION_HANDLE ].position += scrollOffset;
421 mHandle[ RIGHT_SELECTION_HANDLE ].position += scrollOffset;
422 mHighlightPosition += scrollOffset;
425 void PopUpRelayoutComplete( Actor actor )
427 // Size negotiation for CopyPastePopup complete so can get the size and constrain position within bounding box.
429 mCopyPastePopup.OnRelayoutSignal().Disconnect( this, &Decorator::Impl::PopUpRelayoutComplete );
431 Vector3 popupPosition( mCursor[PRIMARY_CURSOR].position.x, mCursor[PRIMARY_CURSOR].position.y -100.0f , 0.0f); //todo 100 to be an offset Property
433 Vector3 popupSize = Vector3( mCopyPastePopup.GetRelayoutSize( Dimension::WIDTH ), mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT ), 0.0f );
435 GetConstrainedPopupPosition( popupPosition, popupSize, AnchorPoint::CENTER, mActiveLayer, mBoundingBox );
437 SetUpPopUpPositionNotifications();
439 mCopyPastePopup.SetPosition( popupPosition ); //todo grabhandle(cursor) or selection handle positions to be used
442 void CreateCursor( ImageActor& cursor, const Vector4& color )
444 cursor = CreateSolidColorActor( color );
445 cursor.SetParentOrigin( ParentOrigin::TOP_LEFT ); // Need to set the default parent origin as CreateSolidColorActor() sets a different one.
446 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
449 // Add or Remove cursor(s) from parent
452 if( mActiveCursor == ACTIVE_CURSOR_NONE )
454 UnparentAndReset( mPrimaryCursor );
455 UnparentAndReset( mSecondaryCursor );
459 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
460 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY ||
461 mActiveCursor == ACTIVE_CURSOR_BOTH )
463 if ( !mPrimaryCursor )
465 CreateCursor( mPrimaryCursor, mCursor[PRIMARY_CURSOR].color );
466 #ifdef DECORATOR_DEBUG
467 mPrimaryCursor.SetName( "PrimaryCursorActor" );
469 mActiveLayer.Add( mPrimaryCursor );
473 if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
475 if ( !mSecondaryCursor )
477 CreateCursor( mSecondaryCursor, mCursor[SECONDARY_CURSOR].color );
478 #ifdef DECORATOR_DEBUG
479 mSecondaryCursor.SetName( "SecondaryCursorActor" );
481 mActiveLayer.Add( mSecondaryCursor );
486 UnparentAndReset( mSecondaryCursor );
491 bool OnCursorBlinkTimerTick()
494 if ( mPrimaryCursor )
496 mPrimaryCursor.SetVisible( mPrimaryCursorVisible && mCursorBlinkStatus );
498 if ( mSecondaryCursor )
500 mSecondaryCursor.SetVisible( mSecondaryCursorVisible && mCursorBlinkStatus );
503 mCursorBlinkStatus = !mCursorBlinkStatus;
508 void SetupTouchEvents()
512 mTapDetector = TapGestureDetector::New();
513 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
516 if ( !mPanGestureDetector )
518 mPanGestureDetector = PanGestureDetector::New();
519 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
523 void CreateActiveLayer()
527 mActiveLayer = Layer::New();
528 #ifdef DECORATOR_DEBUG
529 mActiveLayer.SetName ( "ActiveLayerActor" );
532 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER );
533 mActiveLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
534 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
536 // Add the active layer telling the controller it doesn't need clipping.
537 mController.AddDecoration( mActiveLayer, false );
540 mActiveLayer.RaiseToTop();
543 void CreateGrabHandle()
545 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
546 if( !grabHandle.actor )
548 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
550 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_RELEASED );
552 if( !mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
554 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE_PRESSED );
557 grabHandle.actor = ImageActor::New( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
558 grabHandle.actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
559 grabHandle.actor.SetDrawMode( DrawMode::OVERLAY );
560 // Area that Grab handle responds to, larger than actual handle so easier to move
561 #ifdef DECORATOR_DEBUG
562 grabHandle.actor.SetName( "GrabHandleActor" );
563 if ( Dali::Internal::gLogFilter->IsEnabledFor( Debug::Verbose ) )
565 grabHandle.grabArea = Toolkit::CreateSolidColorActor( Vector4(0.0f, 0.0f, 0.0f, 0.0f), true, Color::RED, 1 );
566 grabHandle.grabArea.SetName( "GrabArea" );
570 grabHandle.grabArea = Actor::New();
571 grabHandle.grabArea.SetName( "GrabArea" );
574 grabHandle.grabArea = Actor::New();
577 grabHandle.grabArea.SetParentOrigin( ParentOrigin::TOP_CENTER );
578 grabHandle.grabArea.SetAnchorPoint( AnchorPoint::TOP_CENTER );
579 grabHandle.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
580 grabHandle.grabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
581 grabHandle.actor.Add( grabHandle.grabArea );
583 grabHandle.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnGrabHandleTouched );
584 mTapDetector.Attach( grabHandle.grabArea );
585 mPanGestureDetector.Attach( grabHandle.grabArea );
587 mActiveLayer.Add( grabHandle.actor );
591 void CreateSelectionHandles()
593 HandleImpl& primary = mHandle[ LEFT_SELECTION_HANDLE ];
596 if( !mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
598 mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
601 primary.actor = ImageActor::New( mHandleImages[LEFT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
602 #ifdef DECORATOR_DEBUG
603 primary.actor.SetName("SelectionHandleOne");
605 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
606 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
607 primary.flipped = false;
609 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
610 #ifdef DECORATOR_DEBUG
611 primary.grabArea.SetName("SelectionHandleOneGrabArea");
613 primary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
614 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
615 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
617 mTapDetector.Attach( primary.grabArea );
618 mPanGestureDetector.Attach( primary.grabArea );
619 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
621 primary.actor.Add( primary.grabArea );
622 mActiveLayer.Add( primary.actor );
625 HandleImpl& secondary = mHandle[ RIGHT_SELECTION_HANDLE ];
626 if( !secondary.actor )
628 if( !mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] )
630 mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
633 secondary.actor = ImageActor::New( mHandleImages[RIGHT_SELECTION_HANDLE][HANDLE_IMAGE_RELEASED] );
634 #ifdef DECORATOR_DEBUG
635 secondary.actor.SetName("SelectionHandleTwo");
637 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_LEFT ); // Change to BOTTOM_LEFT if Look'n'Feel requires handle above text.
638 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
639 secondary.flipped = false;
641 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
642 #ifdef DECORATOR_DEBUG
643 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
645 secondary.grabArea.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
646 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
647 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
649 mTapDetector.Attach( secondary.grabArea );
650 mPanGestureDetector.Attach( secondary.grabArea );
651 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
653 secondary.actor.Add( secondary.grabArea );
654 mActiveLayer.Add( secondary.actor );
658 void CreateHighlight()
660 if ( !mHighlightMeshActor )
662 mHighlightMaterial = Material::New( "HighlightMaterial" );
663 mHighlightMaterial.SetDiffuseColor( mHighlightColor );
665 mHighlightMeshData.SetMaterial( mHighlightMaterial );
666 mHighlightMeshData.SetHasNormals( true );
668 mHighlightMesh = Mesh::New( mHighlightMeshData );
670 mHighlightMeshActor = MeshActor::New( mHighlightMesh );
671 #ifdef DECORATOR_DEBUG
672 mHighlightMeshActor.SetName( "HighlightMeshActor" );
674 mHighlightMeshActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
676 // Add the highlight box telling the controller it needs clipping.
677 mController.AddDecoration( mHighlightMeshActor, true );
680 mHighlightMeshActor.SetPosition( mHighlightPosition.x, mHighlightPosition.y, DISPLAYED_HIGHLIGHT_Z_OFFSET );
683 void UpdateHighlight()
685 // Construct a Mesh with a texture to be used as the highlight 'box' for selected text
687 // Example scenarios where mesh is made from 3, 1, 2, 2 ,3 or 3 quads.
689 // [ TOP ] [ TOP ] [TOP ] [ TOP ] [ TOP ] [ TOP ]
690 // [ MIDDLE ] [BOTTOM] [BOTTOM] [ MIDDLE ] [ MIDDLE ]
691 // [ BOTTOM] [ MIDDLE ] [ MIDDLE ]
692 // [BOTTOM] [ MIDDLE ]
695 // Each quad is created as 2 triangles.
696 // Middle is just 1 quad regardless of its size.
710 if ( mHighlightMesh && mHighlightMaterial && !mHighlightQuadList.empty() )
712 MeshData::VertexContainer vertices;
713 Dali::MeshData::FaceIndices faceIndices;
715 std::vector<QuadCoordinates>::iterator iter = mHighlightQuadList.begin();
716 std::vector<QuadCoordinates>::iterator endIter = mHighlightQuadList.end();
718 // vertex position defaults to (0 0 0)
719 MeshData::Vertex vertex;
720 // set normal for all vertices as (0 0 1) pointing outward from TextInput Actor.
723 for(std::size_t v = 0; iter != endIter; ++iter,v+=4 )
725 // Add each quad geometry (a sub-selection) to the mesh data.
735 QuadCoordinates& quad = *iter;
737 vertex.x = quad.min.x;
738 vertex.y = quad.min.y;
739 vertices.push_back( vertex );
742 vertex.x = quad.max.x;
743 vertex.y = quad.min.y;
744 vertices.push_back( vertex );
747 vertex.x = quad.min.x;
748 vertex.y = quad.max.y;
749 vertices.push_back( vertex );
751 // bottom-right (v+3)
752 vertex.x = quad.max.x;
753 vertex.y = quad.max.y;
754 vertices.push_back( vertex );
756 // triangle A (3, 1, 0)
757 faceIndices.push_back( v + 3 );
758 faceIndices.push_back( v + 1 );
759 faceIndices.push_back( v );
761 // triangle B (0, 2, 3)
762 faceIndices.push_back( v );
763 faceIndices.push_back( v + 2 );
764 faceIndices.push_back( v + 3 );
766 mHighlightMeshData.SetFaceIndices( faceIndices );
769 BoneContainer bones(0); // passed empty as bones not required
770 mHighlightMeshData.SetData( vertices, faceIndices, bones, mHighlightMaterial );
771 mHighlightMesh.UpdateMeshData( mHighlightMeshData );
775 void OnTap( Actor actor, const TapGesture& tap )
777 if( actor == mHandle[GRAB_HANDLE].actor )
783 void DoPan( HandleImpl& handle, HandleType type, const PanGesture& gesture )
785 if( Gesture::Started == gesture.state )
787 handle.grabDisplacementX = handle.grabDisplacementY = 0;
790 handle.grabDisplacementX += gesture.displacement.x;
791 handle.grabDisplacementY += gesture.displacement.y;
793 const float x = handle.position.x + handle.grabDisplacementX;
794 const float y = handle.position.y + handle.lineHeight*0.5f + handle.grabDisplacementY;
796 if( Gesture::Started == gesture.state ||
797 Gesture::Continuing == gesture.state )
800 mController.GetTargetSize( targetSize );
802 if( x < mScrollThreshold )
804 mScrollDirection = SCROLL_RIGHT;
805 mHandleScrolling = type;
808 else if( x > targetSize.width - mScrollThreshold )
810 mScrollDirection = SCROLL_LEFT;
811 mHandleScrolling = type;
816 mHandleScrolling = HANDLE_TYPE_COUNT;
818 mController.DecorationEvent( type, HANDLE_PRESSED, x, y );
821 else if( Gesture::Finished == gesture.state ||
822 Gesture::Cancelled == gesture.state )
824 if( mScrollTimer && mScrollTimer.IsRunning() )
826 mHandleScrolling = HANDLE_TYPE_COUNT;
828 mController.DecorationEvent( type, HANDLE_STOP_SCROLLING, x, y );
832 mController.DecorationEvent( type, HANDLE_RELEASED, x, y );
834 handle.actor.SetImage( mHandleImages[type][HANDLE_IMAGE_RELEASED] );
838 void OnPan( Actor actor, const PanGesture& gesture )
840 HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
841 HandleImpl& primarySelectionHandle = mHandle[LEFT_SELECTION_HANDLE];
842 HandleImpl& secondarySelectionHandle = mHandle[RIGHT_SELECTION_HANDLE];
844 if( actor == grabHandle.grabArea )
846 DoPan( grabHandle, GRAB_HANDLE, gesture );
848 else if( actor == primarySelectionHandle.grabArea )
850 DoPan( primarySelectionHandle, LEFT_SELECTION_HANDLE, gesture );
852 else if( actor == secondarySelectionHandle.grabArea )
854 DoPan( secondarySelectionHandle, RIGHT_SELECTION_HANDLE, gesture );
858 bool OnGrabHandleTouched( Actor actor, const TouchEvent& event )
860 // Switch between pressed/release grab-handle images
861 if( event.GetPointCount() > 0 &&
862 mHandle[GRAB_HANDLE].actor )
864 const TouchPoint& point = event.GetPoint(0);
866 if( TouchPoint::Down == point.state &&
867 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] )
869 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_PRESSED] );
871 else if( TouchPoint::Up == point.state &&
872 mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] )
874 mHandle[GRAB_HANDLE].actor.SetImage( mHandleImages[GRAB_HANDLE][HANDLE_IMAGE_RELEASED] );
878 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
882 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
885 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
889 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
892 // Consume to avoid pop-ups accidentally closing, when handle is outside of pop-up area
898 float AlternatePopUpPositionRelativeToCursor()
900 float alternativePosition=0.0f;;
902 if ( mPrimaryCursor ) // Secondary cursor not used for paste
904 Cursor cursor = PRIMARY_CURSOR;
905 alternativePosition = mCursor[cursor].position.y;
908 const float popupHeight = 120.0f; // todo Set as a MaxSize Property in Control or retrieve from CopyPastePopup class.
910 if( mHandle[GRAB_HANDLE].active )
912 // If grab handle enabled then position pop-up below the grab handle.
913 const Vector2 grabHandleSize( 59.0f, 56.0f ); // todo
914 const float BOTTOM_HANDLE_BOTTOM_OFFSET = 1.5; //todo Should be a property
915 alternativePosition += grabHandleSize.height + popupHeight + BOTTOM_HANDLE_BOTTOM_OFFSET ;
919 alternativePosition += popupHeight;
922 return alternativePosition;
925 void PopUpLeavesVerticalBoundary( PropertyNotification& source )
927 float alternativeYPosition=0.0f;
928 // todo use AlternatePopUpPositionRelativeToSelectionHandles() if text is highlighted
929 // if can't be positioned above, then position below row.
930 alternativeYPosition = AlternatePopUpPositionRelativeToCursor();
932 mCopyPastePopup.SetY( alternativeYPosition );
936 void SetUpPopUpPositionNotifications( )
938 // Note Property notifications ignore any set anchor point so conditions must allow for this. Default is Top Left.
940 // Exceeding vertical boundary
942 Vector4 worldCoordinatesBoundingBox;
943 LocalToWorldCoordinatesBoundingBox( mBoundingBox, worldCoordinatesBoundingBox );
945 float popupHeight = mCopyPastePopup.GetRelayoutSize( Dimension::HEIGHT);
947 PropertyNotification verticalExceedNotification = mCopyPastePopup.AddPropertyNotification( Actor::Property::WORLD_POSITION_Y,
948 OutsideCondition( worldCoordinatesBoundingBox.y + popupHeight/2,
949 worldCoordinatesBoundingBox.w - popupHeight/2 ) );
951 verticalExceedNotification.NotifySignal().Connect( this, &Decorator::Impl::PopUpLeavesVerticalBoundary );
954 void GetConstrainedPopupPosition( Vector3& requiredPopupPosition, Vector3& popupSize, Vector3 anchorPoint, Actor& parent, Rect<int>& boundingBox )
956 DALI_ASSERT_DEBUG ( "Popup parent not on stage" && parent.OnStage() )
958 // Parent must already by added to Stage for these Get calls to work
959 Vector3 parentAnchorPoint = parent.GetCurrentAnchorPoint();
960 Vector3 parentWorldPositionLeftAnchor = parent.GetCurrentWorldPosition() - parent.GetCurrentSize()*parentAnchorPoint;
961 Vector3 popupWorldPosition = parentWorldPositionLeftAnchor + requiredPopupPosition; // Parent World position plus popup local position gives World Position
962 Vector3 popupDistanceFromAnchorPoint = popupSize*anchorPoint;
964 // Bounding rectangle is supplied as screen coordinates, bounding will be done in world coordinates.
965 Vector4 boundingRectangleWorld;
966 LocalToWorldCoordinatesBoundingBox( boundingBox, boundingRectangleWorld );
968 // Calculate distance to move popup (in local space) so fits within the boundary
969 float xOffSetToKeepWithinBounds = 0.0f;
970 if( popupWorldPosition.x - popupDistanceFromAnchorPoint.x < boundingRectangleWorld.x )
972 xOffSetToKeepWithinBounds = boundingRectangleWorld.x - ( popupWorldPosition.x - popupDistanceFromAnchorPoint.x );
974 else if ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x > boundingRectangleWorld.z )
976 xOffSetToKeepWithinBounds = boundingRectangleWorld.z - ( popupWorldPosition.x + popupDistanceFromAnchorPoint.x );
979 // Ensure initial display of Popup is in alternative position if can not fit above. As Property notification will be a frame behind.
980 if ( popupWorldPosition.y - popupDistanceFromAnchorPoint.y < boundingRectangleWorld.y )
982 requiredPopupPosition.y = AlternatePopUpPositionRelativeToCursor();
985 requiredPopupPosition.x = requiredPopupPosition.x + xOffSetToKeepWithinBounds;
988 void SetScrollThreshold( float threshold )
990 mScrollThreshold = threshold;
993 float GetScrollThreshold() const
995 return mScrollThreshold;
998 void SetScrollSpeed( float speed )
1000 mScrollSpeed = speed;
1001 mScrollDistance = speed * SCROLL_TICK_INTERVAL * TO_SECONDS;
1004 float GetScrollSpeed() const
1006 return mScrollSpeed;
1010 * Creates and starts a timer to scroll the text when handles are close to the edges of the text.
1012 * It only starts the timer if it's already created.
1014 void StartScrollTimer()
1018 mScrollTimer = Timer::New( SCROLL_TICK_INTERVAL );
1019 mScrollTimer.TickSignal().Connect( this, &Decorator::Impl::OnScrollTimerTick );
1022 if( !mScrollTimer.IsRunning() )
1024 mScrollTimer.Start();
1029 * Stops the timer used to scroll the text.
1031 void StopScrollTimer()
1035 mScrollTimer.Stop();
1040 * Callback called by the timer used to scroll the text.
1042 * It calculates and sets a new scroll position.
1044 bool OnScrollTimerTick()
1046 if( HANDLE_TYPE_COUNT != mHandleScrolling )
1048 mController.DecorationEvent( mHandleScrolling,
1050 mScrollDirection == SCROLL_RIGHT ? mScrollDistance : -mScrollDistance,
1057 ControllerInterface& mController;
1059 TapGestureDetector mTapDetector;
1060 PanGestureDetector mPanGestureDetector;
1061 Timer mCursorBlinkTimer; ///< Timer to signal cursor to blink
1062 Timer mScrollTimer; ///< Timer used to scroll the text when the grab handle is moved close to the edges.
1064 Layer mActiveLayer; ///< Layer for active handles and alike that ensures they are above all else.
1065 ImageActor mPrimaryCursor;
1066 ImageActor mSecondaryCursor;
1067 MeshActor mHighlightMeshActor; ///< Mesh Actor to display highlight
1068 TextSelectionPopup mCopyPastePopup;
1069 TextSelectionPopup::Buttons mEnabledPopupButtons; /// Bit mask of currently enabled Popup buttons
1071 Image mHandleImages[HANDLE_TYPE_COUNT][HANDLE_IMAGE_TYPE_COUNT];
1073 Mesh mHighlightMesh; ///< Mesh for highlight
1074 MeshData mHighlightMeshData; ///< Mesh Data for highlight
1075 Material mHighlightMaterial; ///< Material used for highlight
1077 CursorImpl mCursor[CURSOR_COUNT];
1078 HandleImpl mHandle[HANDLE_TYPE_COUNT];
1079 QuadContainer mHighlightQuadList; ///< Sub-selections that combine to create the complete selection highlight
1080 Vector2 mHighlightPosition; ///< The position of the highlight actor.
1082 Rect<int> mBoundingBox;
1083 Vector4 mHighlightColor; ///< Color of the highlight
1085 unsigned int mActiveCursor;
1086 unsigned int mCursorBlinkInterval;
1087 float mCursorBlinkDuration;
1088 HandleType mHandleScrolling; ///< The handle which is scrolling.
1089 ScrollDirection mScrollDirection; ///< The direction of the scroll.
1090 float mScrollThreshold; ///< Defines a square area inside the control, close to the edge. A cursor entering this area will trigger scroll events.
1091 float mScrollSpeed; ///< The scroll speed in pixels per second.
1092 float mScrollDistance; ///< Distance the text scrolls during a scroll interval.
1093 unsigned int mScrollInterval; ///< Time in milliseconds of a scroll interval.
1095 bool mActiveCopyPastePopup : 1;
1096 bool mCursorBlinkStatus : 1; ///< Flag to switch between blink on and blink off.
1097 bool mPrimaryCursorVisible : 1; ///< Whether the primary cursor is visible.
1098 bool mSecondaryCursorVisible : 1; ///< Whether the secondary cursor is visible.
1099 bool mSwapSelectionHandles : 1; ///< Whether to swap the selection handle images.
1102 DecoratorPtr Decorator::New( ControllerInterface& controller )
1104 return DecoratorPtr( new Decorator(controller) );
1107 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
1109 mImpl->mBoundingBox = boundingBox;
1112 const Rect<int>& Decorator::GetBoundingBox() const
1114 return mImpl->mBoundingBox;
1117 void Decorator::Relayout( const Vector2& size )
1119 mImpl->Relayout( size );
1122 void Decorator::UpdatePositions( const Vector2& scrollOffset )
1124 mImpl->UpdatePositions( scrollOffset );
1129 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
1131 mImpl->mActiveCursor = activeCursor;
1134 unsigned int Decorator::GetActiveCursor() const
1136 return mImpl->mActiveCursor;
1139 void Decorator::SetPosition( Cursor cursor, float x, float y, float cursorHeight, float lineHeight )
1141 mImpl->mCursor[cursor].position.x = x;
1142 mImpl->mCursor[cursor].position.y = y;
1143 mImpl->mCursor[cursor].cursorHeight = cursorHeight;
1144 mImpl->mCursor[cursor].lineHeight = lineHeight;
1147 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& cursorHeight, float& lineHeight ) const
1149 x = mImpl->mCursor[cursor].position.x;
1150 y = mImpl->mCursor[cursor].position.y;
1151 cursorHeight = mImpl->mCursor[cursor].cursorHeight;
1152 lineHeight = mImpl->mCursor[cursor].lineHeight;
1155 const Vector2& Decorator::GetPosition( Cursor cursor ) const
1157 return mImpl->mCursor[cursor].position;
1160 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
1162 mImpl->mCursor[cursor].color = color;
1165 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
1167 return mImpl->mCursor[cursor].color;
1170 void Decorator::StartCursorBlink()
1172 if ( !mImpl->mCursorBlinkTimer )
1174 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
1175 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
1178 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
1180 mImpl->mCursorBlinkTimer.Start();
1184 void Decorator::StopCursorBlink()
1186 if ( mImpl->mCursorBlinkTimer )
1188 mImpl->mCursorBlinkTimer.Stop();
1192 void Decorator::SetCursorBlinkInterval( float seconds )
1194 mImpl->mCursorBlinkInterval = static_cast<unsigned int>( seconds * TO_MILLISECONDS ); // Convert to milliseconds
1197 float Decorator::GetCursorBlinkInterval() const
1199 return static_cast<float>( mImpl->mCursorBlinkInterval ) * TO_SECONDS;
1202 void Decorator::SetCursorBlinkDuration( float seconds )
1204 mImpl->mCursorBlinkDuration = seconds;
1207 float Decorator::GetCursorBlinkDuration() const
1209 return mImpl->mCursorBlinkDuration;
1214 void Decorator::SetHandleActive( HandleType handleType, bool active )
1216 mImpl->mHandle[handleType].active = active;
1219 bool Decorator::IsHandleActive( HandleType handleType ) const
1221 return mImpl->mHandle[handleType].active ;
1224 void Decorator::SetHandleImage( HandleType handleType, HandleImageType handleImageType, Dali::Image image )
1226 mImpl->mHandleImages[handleType][handleImageType] = image;
1229 Dali::Image Decorator::GetHandleImage( HandleType handleType, HandleImageType handleImageType ) const
1231 return mImpl->mHandleImages[handleType][handleImageType];
1234 void Decorator::SetPosition( HandleType handleType, float x, float y, float height )
1236 // Adjust grab handle displacement
1237 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1239 handle.grabDisplacementX -= x - handle.position.x;
1240 handle.grabDisplacementY -= y - handle.position.y;
1242 handle.position.x = x;
1243 handle.position.y = y;
1244 handle.lineHeight = height;
1247 void Decorator::GetPosition( HandleType handleType, float& x, float& y, float& height ) const
1249 Impl::HandleImpl& handle = mImpl->mHandle[handleType];
1251 x = handle.position.x;
1252 y = handle.position.y;
1253 height = handle.lineHeight;
1256 const Vector2& Decorator::GetPosition( HandleType handleType ) const
1258 return mImpl->mHandle[handleType].position;
1261 void Decorator::SwapSelectionHandlesEnabled( bool enable )
1263 mImpl->mSwapSelectionHandles = enable;
1266 void Decorator::AddHighlight( float x1, float y1, float x2, float y2 )
1268 mImpl->mHighlightQuadList.push_back( QuadCoordinates(x1, y1, x2, y2) );
1271 void Decorator::ClearHighlights()
1273 mImpl->mHighlightQuadList.clear();
1274 mImpl->mHighlightPosition = Vector2::ZERO;
1277 void Decorator::SetHighlightColor( const Vector4& color )
1279 mImpl->mHighlightColor = color;
1282 const Vector4& Decorator::GetHighlightColor() const
1284 return mImpl->mHighlightColor;
1287 void Decorator::SetPopupActive( bool active )
1289 mImpl->mActiveCopyPastePopup = active;
1292 bool Decorator::IsPopupActive() const
1294 return mImpl->mActiveCopyPastePopup ;
1297 void Decorator::SetEnabledPopupButtons( TextSelectionPopup::Buttons& enabledButtonsBitMask )
1299 mImpl->mEnabledPopupButtons = enabledButtonsBitMask;
1302 TextSelectionPopup::Buttons& Decorator::GetEnabledPopupButtons()
1304 return mImpl->mEnabledPopupButtons;
1309 void Decorator::SetScrollThreshold( float threshold )
1311 mImpl->SetScrollThreshold( threshold );
1314 float Decorator::GetScrollThreshold() const
1316 return mImpl->GetScrollThreshold();
1319 void Decorator::SetScrollSpeed( float speed )
1321 mImpl->SetScrollSpeed( speed );
1324 float Decorator::GetScrollSpeed() const
1326 return mImpl->GetScrollSpeed();
1329 Decorator::~Decorator()
1334 Decorator::Decorator( ControllerInterface& controller )
1337 mImpl = new Decorator::Impl( controller );
1342 } // namespace Toolkit