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/public-api/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/events/tap-gesture.h>
28 #include <dali/public-api/events/tap-gesture-detector.h>
29 #include <dali/public-api/events/pan-gesture.h>
30 #include <dali/public-api/events/pan-gesture-detector.h>
31 #include <dali/public-api/images/resource-image.h>
32 #include <dali/public-api/math/vector2.h>
33 #include <dali/public-api/math/vector4.h>
34 #include <dali/public-api/signals/connection-tracker.h>
37 #include <dali-toolkit/public-api/controls/control.h>
38 #include <dali-toolkit/public-api/controls/control-impl.h>
41 #define DECORATOR_DEBUG
48 const char* DEFAULT_GRAB_HANDLE_IMAGE( DALI_IMAGE_DIR "insertpoint-icon.png" );
49 const char* DEFAULT_SELECTION_HANDLE_ONE( DALI_IMAGE_DIR "text-input-selection-handle-left.png" );
50 const char* DEFAULT_SELECTION_HANDLE_TWO( DALI_IMAGE_DIR "text-input-selection-handle-right.png" );
51 //const char* DEFAULT_SELECTION_HANDLE_ONE_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-left-press.png" );
52 //const char* DEFAULT_SELECTION_HANDLE_TWO_PRESSED( DALI_IMAGE_DIR "text-input-selection-handle-right-press.png" );
53 const char* DEFAULT_CURSOR_IMAGE( DALI_IMAGE_DIR "decorator-cursor.png");
55 const Dali::Vector3 DEFAULT_GRAB_HANDLE_RELATIVE_SIZE( 1.5f, 2.0f, 1.0f );
56 const Dali::Vector3 DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE( 1.5f, 1.5f, 1.0f );
58 const std::size_t CURSOR_BLINK_INTERVAL = 500; // Cursor blink interval
71 struct Decorator::Impl : public ConnectionTracker
79 color(Dali::Color::WHITE)
90 struct SelectionHandleImpl
102 float cursorHeight; ///< Not the handle height
112 Impl( Dali::Toolkit::Internal::Control& parent, Observer& observer )
115 mActiveCursor(ACTIVE_CURSOR_NONE),
116 mActiveGrabHandle(false),
117 mActiveSelection( false ),
118 mCursorBlinkInterval( CURSOR_BLINK_INTERVAL ),
119 mCursorBlinkDuration(0.0f),
120 mCursorBlinkStatus( true ),
121 mGrabDisplacementX(0.0f),
122 mGrabDisplacementY(0.0f)
127 * Relayout of the decorations owned by the decorator.
128 * @param[in] size The Size of the UI control the decorater is adding it's decorations to.
130 void Relayout( const Vector2& size )
134 // Show or hide the grab handle
135 if( mActiveGrabHandle )
142 mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].x, mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height );
144 else if( mGrabHandle )
146 UnparentAndReset( mGrabHandle );
149 // Show or hide the selection handles/highlight
150 if( mActiveSelection )
155 CreateSelectionHandles();
157 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
158 primary.actor.SetPosition( primary.x, primary.y + primary.cursorHeight );
160 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
161 secondary.actor.SetPosition( secondary.x, secondary.y + secondary.cursorHeight );
163 //CreateHighlight(); TODO
167 UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
168 UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
175 void CreateCursor( ImageActor& cursor )
179 mCursorImage = ResourceImage::New( DEFAULT_CURSOR_IMAGE );
181 cursor = ImageActor::New( mCursorImage );
182 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
186 * Add / Remove cursor(s) from parent
190 Actor parent = mParent.Self();
191 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
192 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY )
194 if ( !mPrimaryCursor )
196 CreateCursor( mPrimaryCursor );
197 #ifdef DECORATOR_DEBUG
198 mPrimaryCursor.SetName( "PrimaryCursorActor" );
200 parent.Add( mPrimaryCursor);
203 mPrimaryCursor.SetPosition( mCursor[PRIMARY_CURSOR].x, mCursor[PRIMARY_CURSOR].y );
205 else if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
207 if ( !mSecondaryCursor )
209 CreateCursor( mSecondaryCursor );
210 #ifdef DECORATOR_DEBUG
211 mSecondaryCursor.SetName( "SecondaryCursorActor" );
213 parent.Add( mSecondaryCursor);
218 /* ACTIVE_CURSOR_NONE so unparent cursors*/
219 if ( mPrimaryCursor )
221 UnparentAndReset( mPrimaryCursor );
224 if ( mSecondaryCursor )
226 UnparentAndReset( mSecondaryCursor );
231 bool OnCursorBlinkTimerTick()
234 if ( ACTIVE_CURSOR_PRIMARY )
236 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
238 else if ( ACTIVE_CURSOR_BOTH )
240 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
241 mSecondaryCursor.SetVisible( mCursorBlinkStatus );
244 mCursorBlinkStatus = !mCursorBlinkStatus;
249 void SetupTouchEvents()
253 mTapDetector = TapGestureDetector::New();
254 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
257 if ( !mPanGestureDetector )
259 mPanGestureDetector = PanGestureDetector::New();
260 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
264 void CreateActiveLayer()
268 Actor parent = mParent.Self();
270 mActiveLayer = Layer::New();
271 #ifdef DECORATOR_DEBUG
272 mActiveLayer.SetName ( "ActiveLayerActor" );
275 mActiveLayer.SetAnchorPoint( AnchorPoint::CENTER);
276 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER);
277 mActiveLayer.SetSizeMode( SIZE_EQUAL_TO_PARENT );
278 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
280 parent.Add( mActiveLayer );
283 mActiveLayer.RaiseToTop();
286 void CreateGrabHandle()
290 if ( !mGrabHandleImage )
292 mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE );
295 mGrabHandle = ImageActor::New( mGrabHandleImage );
296 #ifdef DECORATOR_DEBUG
297 mGrabHandle.SetName( "GrabHandleActor" );
299 mGrabHandle.SetParentOrigin( ParentOrigin::TOP_LEFT );
300 mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
301 mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
303 mGrabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
304 #ifdef DECORATOR_DEBUG
305 mGrabArea.SetName( "GrabArea" );
307 mGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
308 mGrabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
309 mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
310 mGrabHandle.Add(mGrabArea);
312 mTapDetector.Attach( mGrabArea );
313 mPanGestureDetector.Attach( mGrabArea );
315 mActiveLayer.Add(mGrabHandle);
319 void CreateSelectionHandles()
321 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
322 if ( !primary.actor )
324 if ( !primary.releasedImage )
326 primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
329 primary.actor = ImageActor::New( primary.releasedImage );
330 #ifdef DECORATOR_DEBUG
331 primary.actor.SetName("SelectionHandleOne");
333 primary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
334 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
335 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
336 primary.flipped = false;
338 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
339 #ifdef DECORATOR_DEBUG
340 primary.grabArea.SetName("SelectionHandleOneGrabArea");
342 primary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
343 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
344 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
346 mTapDetector.Attach( primary.grabArea );
347 mPanGestureDetector.Attach( primary.grabArea );
348 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
350 primary.actor.Add( primary.grabArea );
351 mActiveLayer.Add( primary.actor );
354 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
355 if ( !secondary.actor )
357 if ( !secondary.releasedImage )
359 secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
362 secondary.actor = ImageActor::New( secondary.releasedImage );
363 #ifdef DECORATOR_DEBUG
364 secondary.actor.SetName("SelectionHandleTwo");
366 secondary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
367 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
368 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
369 secondary.flipped = false;
371 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
372 #ifdef DECORATOR_DEBUG
373 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
375 secondary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
376 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
377 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
379 mTapDetector.Attach( secondary.grabArea );
380 mPanGestureDetector.Attach( secondary.grabArea );
381 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
383 secondary.actor.Add( secondary.grabArea );
384 mActiveLayer.Add( secondary.actor );
387 //SetUpHandlePropertyNotifications(); TODO
390 void OnTap( Actor actor, const TapGesture& tap )
392 if( actor == mGrabHandle )
398 void OnPan( Actor actor, const PanGesture& gesture )
400 if( actor == mGrabArea )
402 if( Gesture::Started == gesture.state )
404 mGrabDisplacementX = mGrabDisplacementY = 0;
407 mGrabDisplacementX += gesture.displacement.x;
408 mGrabDisplacementY += gesture.displacement.y;
410 float x = mCursor[PRIMARY_CURSOR].x + mGrabDisplacementX;
411 float y = mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height*0.5f + mGrabDisplacementY;
413 if( Gesture::Started == gesture.state ||
414 Gesture::Continuing == gesture.state )
416 mObserver.GrabHandleEvent( GRAB_HANDLE_PRESSED, x, y );
418 else if( Gesture::Finished == gesture.state ||
419 Gesture::Cancelled == gesture.state )
421 mObserver.GrabHandleEvent( GRAB_HANDLE_RELEASED, x, y );
426 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
432 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
438 Internal::Control& mParent;
441 Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
443 unsigned int mActiveCursor;
444 bool mActiveGrabHandle;
445 bool mActiveSelection;
447 CursorImpl mCursor[CURSOR_COUNT];
449 Timer mCursorBlinkTimer; // Timer to signal cursor to blink
450 unsigned int mCursorBlinkInterval;
451 float mCursorBlinkDuration;
452 bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
454 ImageActor mPrimaryCursor;
455 ImageActor mSecondaryCursor;
457 ImageActor mGrabHandle;
459 float mGrabDisplacementX;
460 float mGrabDisplacementY;
462 SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
465 Image mGrabHandleImage;
467 TapGestureDetector mTapDetector;
468 PanGestureDetector mPanGestureDetector;
473 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
475 return DecoratorPtr( new Decorator(parent, observer) );
478 void Decorator::Relayout( const Vector2& size )
480 mImpl->Relayout( size );
485 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
487 mImpl->mActiveCursor = activeCursor;
490 unsigned int Decorator::GetActiveCursor() const
492 return mImpl->mActiveCursor;
495 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
497 // Adjust grab handle displacement
498 mImpl->mGrabDisplacementX -= x - mImpl->mCursor[cursor].x;
499 mImpl->mGrabDisplacementY -= y - mImpl->mCursor[cursor].y;
501 mImpl->mCursor[cursor].x = x;
502 mImpl->mCursor[cursor].y = y;
503 mImpl->mCursor[cursor].height = height;
506 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
508 x = mImpl->mCursor[cursor].x;
509 y = mImpl->mCursor[cursor].y;
510 height = mImpl->mCursor[cursor].height;
513 void Decorator::SetCursorImage( Dali::Image image )
515 mImpl->mCursorImage = image;
518 Dali::Image Decorator::GetCursorImage() const
520 return mImpl->mCursorImage;
523 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
525 mImpl->mCursor[cursor].color = color;
528 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
530 return mImpl->mCursor[cursor].color;
533 void Decorator::StartCursorBlink()
535 if ( !mImpl->mCursorBlinkTimer )
537 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
538 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
541 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
543 mImpl->mCursorBlinkTimer.Start();
547 void Decorator::StopCursorBlink()
549 if ( mImpl->mCursorBlinkTimer )
551 mImpl->mCursorBlinkTimer.Stop();
555 void Decorator::SetCursorBlinkInterval( float seconds )
557 mImpl->mCursorBlinkInterval = seconds*1000; // Convert to milliseconds
560 float Decorator::GetCursorBlinkInterval() const
562 return mImpl->mCursorBlinkInterval;
565 void Decorator::SetCursorBlinkDuration( float seconds )
567 mImpl->mCursorBlinkDuration = seconds;
570 float Decorator::GetCursorBlinkDuration() const
572 return mImpl->mCursorBlinkDuration;
577 void Decorator::SetGrabHandleActive( bool active )
579 mImpl->mActiveGrabHandle = active;
582 bool Decorator::IsGrabHandleActive() const
584 return mImpl->mActiveGrabHandle;
587 void Decorator::SetGrabHandleImage( Dali::Image image )
589 mImpl->mGrabHandleImage = image;
592 Dali::Image Decorator::GetGrabHandleImage() const
594 return mImpl->mGrabHandleImage;
599 void Decorator::SetSelectionActive( bool active )
601 mImpl->mActiveSelection = active;
604 bool Decorator::IsSelectionActive() const
606 return mImpl->mActiveSelection;
609 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
611 mImpl->mSelectionHandle[handle].x = x;
612 mImpl->mSelectionHandle[handle].y = y;
613 mImpl->mSelectionHandle[handle].cursorHeight = height;
616 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
618 x = mImpl->mSelectionHandle[handle].x;
619 y = mImpl->mSelectionHandle[handle].y;
620 height = mImpl->mSelectionHandle[handle].cursorHeight;
623 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
625 if( SELECTION_HANDLE_PRESSED == state )
627 mImpl->mSelectionHandle[handle].pressedImage = image;
631 mImpl->mSelectionHandle[handle].releasedImage = image;
635 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
637 if( SELECTION_HANDLE_PRESSED == state )
639 return mImpl->mSelectionHandle[handle].pressedImage;
642 return mImpl->mSelectionHandle[handle].releasedImage;
645 Decorator::~Decorator()
650 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
653 mImpl = new Decorator::Impl( parent, observer );
658 } // namespace Toolkit