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/public-api/actors/actor.h>
23 #include <dali/public-api/adaptor-framework/timer.h>
24 #include <dali/public-api/actors/image-actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/public-api/common/constants.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/events/tap-gesture.h>
29 #include <dali/public-api/events/tap-gesture-detector.h>
30 #include <dali/public-api/events/pan-gesture.h>
31 #include <dali/public-api/events/pan-gesture-detector.h>
32 #include <dali/public-api/images/resource-image.h>
33 #include <dali/public-api/math/vector2.h>
34 #include <dali/public-api/math/vector4.h>
35 #include <dali/public-api/signals/connection-tracker.h>
38 #include <dali-toolkit/public-api/controls/control.h>
39 #include <dali-toolkit/public-api/controls/control-impl.h>
42 #define DECORATOR_DEBUG
49 const char* DEFAULT_GRAB_HANDLE_IMAGE( DALI_IMAGE_DIR "insertpoint-icon.png" );
50 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
51 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
52 //const char* DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
53 //const char* DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
54 const char* DEFAULT_CURSOR_IMAGE( DALI_IMAGE_DIR "decorator-cursor.png");
56 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
57 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
59 const std::size_t CURSOR_BLINK_INTERVAL = 500; // Cursor blink interval
60 const std::size_t MILLISECONDS = 1000;
73 struct Decorator::Impl : public ConnectionTracker
81 color(Dali::Color::WHITE)
92 struct SelectionHandleImpl
104 float cursorHeight; ///< Not the handle height
114 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
117 mActiveCursor(ACTIVE_CURSOR_NONE),
118 mActiveGrabHandle(false),
119 mActiveSelection( false ),
120 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
121 mCursorBlinkDuration( 0.0f ),
122 mCursorBlinkStatus( true ),
123 mGrabDisplacementX( 0.0f ),
124 mGrabDisplacementY( 0.0f ),
125 mBoundingBox( Rect<int>() )
130 * Relayout of the decorations owned by the decorator.
131 * @param[in] size The Size of the UI control the decorater is adding it's decorations to.
133 void Relayout( const Vector2& size )
137 // Show or hide the grab handle
138 if( mActiveGrabHandle )
145 mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].x, mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height );
147 else if( mGrabHandle )
149 UnparentAndReset( mGrabHandle );
152 // Show or hide the selection handles/highlight
153 if( mActiveSelection )
158 CreateSelectionHandles();
160 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
161 primary.actor.SetPosition( primary.x, primary.y + primary.cursorHeight );
163 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
164 secondary.actor.SetPosition( secondary.x, secondary.y + secondary.cursorHeight );
166 //CreateHighlight(); TODO
170 UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
171 UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
178 void CreateCursor( ImageActor& cursor )
182 mCursorImage = ResourceImage::New( DEFAULT_CURSOR_IMAGE );
184 cursor = ImageActor::New( mCursorImage );
185 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
189 * Add / Remove cursor(s) from parent
193 Actor parent = mParent.Self();
194 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
195 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY )
197 if ( !mPrimaryCursor )
199 CreateCursor( mPrimaryCursor );
200 #ifdef DECORATOR_DEBUG
201 mPrimaryCursor.SetName( "PrimaryCursorActor" );
203 parent.Add( mPrimaryCursor);
206 mPrimaryCursor.SetPosition( mCursor[PRIMARY_CURSOR].x, mCursor[PRIMARY_CURSOR].y );
208 else if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
210 if ( !mSecondaryCursor )
212 CreateCursor( mSecondaryCursor );
213 #ifdef DECORATOR_DEBUG
214 mSecondaryCursor.SetName( "SecondaryCursorActor" );
216 parent.Add( mSecondaryCursor);
221 /* ACTIVE_CURSOR_NONE so unparent cursors*/
222 if ( mPrimaryCursor )
224 UnparentAndReset( mPrimaryCursor );
227 if ( mSecondaryCursor )
229 UnparentAndReset( mSecondaryCursor );
234 bool OnCursorBlinkTimerTick()
237 if ( ACTIVE_CURSOR_PRIMARY )
239 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
241 else if ( ACTIVE_CURSOR_BOTH )
243 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
244 mSecondaryCursor.SetVisible( mCursorBlinkStatus );
247 mCursorBlinkStatus = !mCursorBlinkStatus;
252 void SetupTouchEvents()
256 mTapDetector = TapGestureDetector::New();
257 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
260 if ( !mPanGestureDetector )
262 mPanGestureDetector = PanGestureDetector::New();
263 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
267 void CreateActiveLayer()
271 Actor parent = mParent.Self();
273 mActiveLayer = Layer::New();
274 #ifdef DECORATOR_DEBUG
275 mActiveLayer.SetName ( "ActiveLayerActor" );
278 mActiveLayer.SetAnchorPoint( AnchorPoint::CENTER);
279 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER);
280 mActiveLayer.SetSizeMode( SIZE_EQUAL_TO_PARENT );
281 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
283 parent.Add( mActiveLayer );
286 mActiveLayer.RaiseToTop();
289 void CreateGrabHandle()
293 if ( !mGrabHandleImage )
295 mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE );
298 mGrabHandle = ImageActor::New( mGrabHandleImage );
299 #ifdef DECORATOR_DEBUG
300 mGrabHandle.SetName( "GrabHandleActor" );
302 mGrabHandle.SetParentOrigin( ParentOrigin::TOP_LEFT );
303 mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
304 mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
306 mGrabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
307 #ifdef DECORATOR_DEBUG
308 mGrabArea.SetName( "GrabArea" );
310 mGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
311 mGrabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
312 mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
313 mGrabHandle.Add(mGrabArea);
315 mTapDetector.Attach( mGrabArea );
316 mPanGestureDetector.Attach( mGrabArea );
318 mActiveLayer.Add(mGrabHandle);
322 void CreateSelectionHandles()
324 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
325 if ( !primary.actor )
327 if ( !primary.releasedImage )
329 primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
332 primary.actor = ImageActor::New( primary.releasedImage );
333 #ifdef DECORATOR_DEBUG
334 primary.actor.SetName("SelectionHandleOne");
336 primary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
337 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
338 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
339 primary.flipped = false;
341 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
342 #ifdef DECORATOR_DEBUG
343 primary.grabArea.SetName("SelectionHandleOneGrabArea");
345 primary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
346 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
347 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
349 mTapDetector.Attach( primary.grabArea );
350 mPanGestureDetector.Attach( primary.grabArea );
351 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
353 primary.actor.Add( primary.grabArea );
354 mActiveLayer.Add( primary.actor );
357 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
358 if ( !secondary.actor )
360 if ( !secondary.releasedImage )
362 secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
365 secondary.actor = ImageActor::New( secondary.releasedImage );
366 #ifdef DECORATOR_DEBUG
367 secondary.actor.SetName("SelectionHandleTwo");
369 secondary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
370 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
371 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
372 secondary.flipped = false;
374 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
375 #ifdef DECORATOR_DEBUG
376 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
378 secondary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
379 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
380 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
382 mTapDetector.Attach( secondary.grabArea );
383 mPanGestureDetector.Attach( secondary.grabArea );
384 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
386 secondary.actor.Add( secondary.grabArea );
387 mActiveLayer.Add( secondary.actor );
390 //SetUpHandlePropertyNotifications(); TODO
393 void OnTap( Actor actor, const TapGesture& tap )
395 if( actor == mGrabHandle )
401 void OnPan( Actor actor, const PanGesture& gesture )
403 if( actor == mGrabArea )
405 if( Gesture::Started == gesture.state )
407 mGrabDisplacementX = mGrabDisplacementY = 0;
410 mGrabDisplacementX += gesture.displacement.x;
411 mGrabDisplacementY += gesture.displacement.y;
413 float x = mCursor[PRIMARY_CURSOR].x + mGrabDisplacementX;
414 float y = mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height*0.5f + mGrabDisplacementY;
416 if( Gesture::Started == gesture.state ||
417 Gesture::Continuing == gesture.state )
419 mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
421 else if( Gesture::Finished == gesture.state ||
422 Gesture::Cancelled == gesture.state )
424 mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
429 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
435 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
441 Internal::Control& mParent;
444 Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
446 unsigned int mActiveCursor;
447 bool mActiveGrabHandle;
448 bool mActiveSelection;
450 CursorImpl mCursor[CURSOR_COUNT];
452 Timer mCursorBlinkTimer; // Timer to signal cursor to blink
453 unsigned int mCursorBlinkInterval;
454 float mCursorBlinkDuration;
455 bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
457 ImageActor mPrimaryCursor;
458 ImageActor mSecondaryCursor;
460 ImageActor mGrabHandle;
462 float mGrabDisplacementX;
463 float mGrabDisplacementY;
465 SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
468 Image mGrabHandleImage;
470 TapGestureDetector mTapDetector;
471 PanGestureDetector mPanGestureDetector;
473 Rect<int> mBoundingBox;
476 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
478 return DecoratorPtr( new Decorator(parent, observer) );
481 void Decorator::SetBoundingBox( const Rect<int>& boundingBox )
483 mImpl->mBoundingBox = boundingBox;
486 const Rect<int>& Decorator::GetBoundingBox() const
488 return mImpl->mBoundingBox;
491 void Decorator::Relayout( const Vector2& size )
493 mImpl->Relayout( size );
498 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
500 mImpl->mActiveCursor = activeCursor;
503 unsigned int Decorator::GetActiveCursor() const
505 return mImpl->mActiveCursor;
508 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
510 // Adjust grab handle displacement
511 mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].x;
512 mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].y;
514 mImpl->mCursor[cursor].x = x;
515 mImpl->mCursor[cursor].y = y;
516 mImpl->mCursor[cursor].height = height;
519 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
521 x = mImpl->mCursor[cursor].x;
522 y = mImpl->mCursor[cursor].y;
523 height = mImpl->mCursor[cursor].height;
526 void Decorator::SetCursorImage( Dali::Image image )
528 mImpl->mCursorImage = image;
531 Dali::Image Decorator::GetCursorImage() const
533 return mImpl->mCursorImage;
536 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
538 mImpl->mCursor[cursor].color = color;
541 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
543 return mImpl->mCursor[cursor].color;
546 void Decorator::StartCursorBlink()
548 if ( !mImpl->mCursorBlinkTimer )
550 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
551 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
554 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
556 mImpl->mCursorBlinkTimer.Start();
560 void Decorator::StopCursorBlink()
562 if ( mImpl->mCursorBlinkTimer )
564 mImpl->mCursorBlinkTimer.Stop();
568 void Decorator::SetCursorBlinkInterval( float seconds )
570 mImpl->mCursorBlinkInterval = seconds*MILLISECONDS; // Convert to milliseconds
573 float Decorator::GetCursorBlinkInterval() const
575 return mImpl->mCursorBlinkInterval;
578 void Decorator::SetCursorBlinkDuration( float seconds )
580 mImpl->mCursorBlinkDuration = seconds;
583 float Decorator::GetCursorBlinkDuration() const
585 return mImpl->mCursorBlinkDuration;
590 void Decorator::SetGrabHandleActive( bool active )
592 mImpl->mActiveGrabHandle = active;
595 bool Decorator::IsGrabHandleActive() const
597 return mImpl->mActiveGrabHandle;
600 void Decorator::SetGrabHandleImage( Dali::Image image )
602 mImpl->mGrabHandleImage = image;
605 Dali::Image Decorator::GetGrabHandleImage() const
607 return mImpl->mGrabHandleImage;
612 void Decorator::SetSelectionActive( bool active )
614 mImpl->mActiveSelection = active;
617 bool Decorator::IsSelectionActive() const
619 return mImpl->mActiveSelection;
622 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
624 mImpl->mSelectionHandle[handle].x = x;
625 mImpl->mSelectionHandle[handle].y = y;
626 mImpl->mSelectionHandle[handle].cursorHeight = height;
629 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
631 x = mImpl->mSelectionHandle[handle].x;
632 y = mImpl->mSelectionHandle[handle].y;
633 height = mImpl->mSelectionHandle[handle].cursorHeight;
636 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
638 if( SELECTION_HANDLE_PRESSED == state )
640 mImpl->mSelectionHandle[handle].pressedImage = image;
644 mImpl->mSelectionHandle[handle].releasedImage = image;
648 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
650 if( SELECTION_HANDLE_PRESSED == state )
652 return mImpl->mSelectionHandle[handle].pressedImage;
655 return mImpl->mSelectionHandle[handle].releasedImage;
658 Decorator::~Decorator()
663 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
666 mImpl = new Decorator::Impl( parent, observer );
671 } // namespace Toolkit