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 )
125 * Relayout of the decorations owned by the decorator.
126 * @param[in] size The Size of the UI control the decorater is adding it's decorations to.
128 void Relayout( const Vector2& size )
132 // Show or hide the grab handle
133 if( mActiveGrabHandle )
140 mGrabHandle.SetPosition( mCursor[PRIMARY_CURSOR].x, mCursor[PRIMARY_CURSOR].y + mCursor[PRIMARY_CURSOR].height );
142 else if( mGrabHandle )
144 UnparentAndReset( mGrabHandle );
147 // Show or hide the selection handles/highlight
148 if( mActiveSelection )
153 CreateSelectionHandles();
155 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
156 primary.actor.SetPosition( primary.x, primary.y + primary.cursorHeight );
158 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
159 secondary.actor.SetPosition( secondary.x, secondary.y + secondary.cursorHeight );
161 //CreateHighlight(); TODO
165 UnparentAndReset( mSelectionHandle[ PRIMARY_SELECTION_HANDLE ].actor );
166 UnparentAndReset( mSelectionHandle[ SECONDARY_SELECTION_HANDLE ].actor );
173 void CreateCursor( ImageActor& cursor )
177 mCursorImage = ResourceImage::New( DEFAULT_CURSOR_IMAGE );
179 cursor = ImageActor::New( mCursorImage );
180 cursor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
184 * Add / Remove cursor(s) from parent
188 Actor parent = mParent.Self();
189 /* Create Primary and or Secondary Cursor(s) if active and add to parent */
190 if ( mActiveCursor == ACTIVE_CURSOR_PRIMARY )
192 if ( !mPrimaryCursor )
194 CreateCursor( mPrimaryCursor );
195 #ifdef DECORATOR_DEBUG
196 mPrimaryCursor.SetName( "PrimaryCursorActor" );
198 parent.Add( mPrimaryCursor);
201 else if ( mActiveCursor == ACTIVE_CURSOR_BOTH )
203 if ( !mSecondaryCursor )
205 CreateCursor( mSecondaryCursor );
206 #ifdef DECORATOR_DEBUG
207 mSecondaryCursor.SetName( "SecondaryCursorActor" );
209 parent.Add( mSecondaryCursor);
214 /* ACTIVE_CURSOR_NONE so unparent cursors*/
215 if ( mPrimaryCursor )
217 UnparentAndReset( mPrimaryCursor );
220 if ( mSecondaryCursor )
222 UnparentAndReset( mSecondaryCursor );
227 bool OnCursorBlinkTimerTick()
230 if ( ACTIVE_CURSOR_PRIMARY )
232 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
234 else if ( ACTIVE_CURSOR_BOTH )
236 mPrimaryCursor.SetVisible( mCursorBlinkStatus );
237 mSecondaryCursor.SetVisible( mCursorBlinkStatus );
240 mCursorBlinkStatus = !mCursorBlinkStatus;
245 void SetupTouchEvents()
249 mTapDetector = TapGestureDetector::New();
250 mTapDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnTap );
253 if ( !mPanGestureDetector )
255 mPanGestureDetector = PanGestureDetector::New();
256 mPanGestureDetector.DetectedSignal().Connect( this, &Decorator::Impl::OnPan );
260 void CreateActiveLayer()
264 Actor parent = mParent.Self();
266 mActiveLayer = Layer::New();
267 #ifdef DECORATOR_DEBUG
268 mActiveLayer.SetName ( "ActiveLayerActor" );
271 mActiveLayer.SetAnchorPoint( AnchorPoint::CENTER);
272 mActiveLayer.SetParentOrigin( ParentOrigin::CENTER);
273 mActiveLayer.SetSizeMode( SIZE_EQUAL_TO_PARENT );
274 mActiveLayer.SetPositionInheritanceMode( USE_PARENT_POSITION );
276 parent.Add( mActiveLayer );
279 mActiveLayer.RaiseToTop();
282 void CreateGrabHandle()
286 if ( !mGrabHandleImage )
288 mGrabHandleImage = ResourceImage::New( DEFAULT_GRAB_HANDLE_IMAGE );
291 mGrabHandle = ImageActor::New( mGrabHandleImage );
292 #ifdef DECORATOR_DEBUG
293 mGrabHandle.SetName( "GrabHandleActor" );
295 mGrabHandle.SetParentOrigin( ParentOrigin::TOP_LEFT );
296 mGrabHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
297 mGrabHandle.SetDrawMode( DrawMode::OVERLAY );
299 mGrabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
300 #ifdef DECORATOR_DEBUG
301 mGrabArea.SetName( "GrabArea" );
303 mGrabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
304 mGrabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
305 mGrabArea.SetSizeModeFactor( DEFAULT_GRAB_HANDLE_RELATIVE_SIZE );
306 mGrabHandle.Add(mGrabArea);
308 mTapDetector.Attach( mGrabArea );
309 mPanGestureDetector.Attach( mGrabArea );
311 mActiveLayer.Add(mGrabHandle);
315 void CreateSelectionHandles()
317 SelectionHandleImpl& primary = mSelectionHandle[ PRIMARY_SELECTION_HANDLE ];
318 if ( !primary.actor )
320 if ( !primary.releasedImage )
322 primary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_ONE );
325 primary.actor = ImageActor::New( primary.releasedImage );
326 #ifdef DECORATOR_DEBUG
327 primary.actor.SetName("SelectionHandleOne");
329 primary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
330 primary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
331 primary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
332 primary.flipped = false;
334 primary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
335 #ifdef DECORATOR_DEBUG
336 primary.grabArea.SetName("SelectionHandleOneGrabArea");
338 primary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
339 primary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
340 primary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
342 mTapDetector.Attach( primary.grabArea );
343 mPanGestureDetector.Attach( primary.grabArea );
344 primary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleOneTouched );
346 primary.actor.Add( primary.grabArea );
347 mActiveLayer.Add( primary.actor );
350 SelectionHandleImpl& secondary = mSelectionHandle[ SECONDARY_SELECTION_HANDLE ];
351 if ( !secondary.actor )
353 if ( !secondary.releasedImage )
355 secondary.releasedImage = ResourceImage::New( DEFAULT_SELECTION_HANDLE_TWO );
358 secondary.actor = ImageActor::New( secondary.releasedImage );
359 #ifdef DECORATOR_DEBUG
360 secondary.actor.SetName("SelectionHandleTwo");
362 secondary.actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
363 secondary.actor.SetAnchorPoint( AnchorPoint::TOP_RIGHT ); // Change to BOTTOM_RIGHT if Look'n'Feel requires handle above text.
364 secondary.actor.SetDrawMode( DrawMode::OVERLAY ); // ensure grab handle above text
365 secondary.flipped = false;
367 secondary.grabArea = Actor::New(); // Area that Grab handle responds to, larger than actual handle so easier to move
368 #ifdef DECORATOR_DEBUG
369 secondary.grabArea.SetName("SelectionHandleTwoGrabArea");
371 secondary.grabArea.SetSizeMode( SIZE_RELATIVE_TO_PARENT );
372 secondary.grabArea.SetSizeModeFactor( DEFAULT_SELECTION_HANDLE_RELATIVE_SIZE );
373 secondary.grabArea.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
375 mTapDetector.Attach( secondary.grabArea );
376 mPanGestureDetector.Attach( secondary.grabArea );
377 secondary.grabArea.TouchedSignal().Connect( this, &Decorator::Impl::OnHandleTwoTouched );
379 secondary.actor.Add( secondary.grabArea );
380 mActiveLayer.Add( secondary.actor );
383 //SetUpHandlePropertyNotifications(); TODO
386 void OnTap( Actor actor, const TapGesture& tap )
388 if( actor == mGrabHandle )
394 void OnPan( Actor actor, const PanGesture& gesture )
396 if( actor == mGrabHandle )
402 bool OnHandleOneTouched( Actor actor, const TouchEvent& touch )
408 bool OnHandleTwoTouched( Actor actor, const TouchEvent& touch )
414 Internal::Control& mParent;
417 Layer mActiveLayer; // Layer for active handles and alike that ensures they are above all else.
419 unsigned int mActiveCursor;
420 bool mActiveGrabHandle;
421 bool mActiveSelection;
423 CursorImpl mCursor[CURSOR_COUNT];
425 Timer mCursorBlinkTimer; // Timer to signal cursor to blink
426 unsigned int mCursorBlinkInterval;
427 float mCursorBlinkDuration;
428 bool mCursorBlinkStatus; // Flag to switch between blink on and blink off
430 ImageActor mPrimaryCursor;
431 ImageActor mSecondaryCursor;
433 ImageActor mGrabHandle;
436 SelectionHandleImpl mSelectionHandle[SELECTION_HANDLE_COUNT];
439 Image mGrabHandleImage;
441 TapGestureDetector mTapDetector;
442 PanGestureDetector mPanGestureDetector;
447 DecoratorPtr Decorator::New( Internal::Control& parent, Observer& observer )
449 return DecoratorPtr( new Decorator(parent, observer) );
452 void Decorator::Relayout( const Vector2& size )
454 mImpl->Relayout( size );
459 void Decorator::SetActiveCursor( ActiveCursor activeCursor )
461 mImpl->mActiveCursor = activeCursor;
464 unsigned int Decorator::GetActiveCursor() const
466 return mImpl->mActiveCursor;
469 void Decorator::SetPosition( Cursor cursor, float x, float y, float height )
471 mImpl->mCursor[cursor].x = x;
472 mImpl->mCursor[cursor].y = y;
473 mImpl->mCursor[cursor].height = height;
476 void Decorator::GetPosition( Cursor cursor, float& x, float& y, float& height ) const
478 x = mImpl->mCursor[cursor].x;
479 y = mImpl->mCursor[cursor].y;
480 height = mImpl->mCursor[cursor].height;
483 void Decorator::SetCursorImage( Dali::Image image )
485 mImpl->mCursorImage = image;
488 Dali::Image Decorator::GetCursorImage() const
490 return mImpl->mCursorImage;
493 void Decorator::SetColor( Cursor cursor, const Dali::Vector4& color )
495 mImpl->mCursor[cursor].color = color;
498 const Dali::Vector4& Decorator::GetColor( Cursor cursor ) const
500 return mImpl->mCursor[cursor].color;
503 void Decorator::StartCursorBlink()
505 if ( !mImpl->mCursorBlinkTimer )
507 mImpl->mCursorBlinkTimer = Timer::New( mImpl->mCursorBlinkInterval );
508 mImpl->mCursorBlinkTimer.TickSignal().Connect( mImpl, &Decorator::Impl::OnCursorBlinkTimerTick );
511 if ( !mImpl->mCursorBlinkTimer.IsRunning() )
513 mImpl->mCursorBlinkTimer.Start();
517 void Decorator::StopCursorBlink()
519 if ( mImpl->mCursorBlinkTimer )
521 mImpl->mCursorBlinkTimer.Stop();
525 void Decorator::SetCursorBlinkInterval( float seconds )
527 mImpl->mCursorBlinkInterval = seconds*1000; // Convert to milliseconds
530 float Decorator::GetCursorBlinkInterval() const
532 return mImpl->mCursorBlinkInterval;
535 void Decorator::SetCursorBlinkDuration( float seconds )
537 mImpl->mCursorBlinkDuration = seconds;
540 float Decorator::GetCursorBlinkDuration() const
542 return mImpl->mCursorBlinkDuration;
547 void Decorator::SetGrabHandleActive( bool active )
549 mImpl->mActiveGrabHandle = active;
552 bool Decorator::IsGrabHandleActive() const
554 return mImpl->mActiveGrabHandle;
557 void Decorator::SetGrabHandleImage( Dali::Image image )
559 mImpl->mGrabHandleImage = image;
562 Dali::Image Decorator::GetGrabHandleImage() const
564 return mImpl->mGrabHandleImage;
569 void Decorator::SetSelectionActive( bool active )
571 mImpl->mActiveSelection = active;
574 bool Decorator::IsSelectionActive() const
576 return mImpl->mActiveSelection;
579 void Decorator::SetPosition( SelectionHandle handle, float x, float y, float height )
581 mImpl->mSelectionHandle[handle].x = x;
582 mImpl->mSelectionHandle[handle].y = y;
583 mImpl->mSelectionHandle[handle].cursorHeight = height;
586 void Decorator::GetPosition( SelectionHandle handle, float& x, float& y, float& height ) const
588 x = mImpl->mSelectionHandle[handle].x;
589 y = mImpl->mSelectionHandle[handle].y;
590 height = mImpl->mSelectionHandle[handle].cursorHeight;
593 void Decorator::SetImage( SelectionHandle handle, SelectionHandleState state, Dali::Image image )
595 if( SELECTION_HANDLE_PRESSED == state )
597 mImpl->mSelectionHandle[handle].pressedImage = image;
601 mImpl->mSelectionHandle[handle].releasedImage = image;
605 Dali::Image Decorator::GetImage( SelectionHandle handle, SelectionHandleState state ) const
607 if( SELECTION_HANDLE_PRESSED == state )
609 return mImpl->mSelectionHandle[handle].pressedImage;
612 return mImpl->mSelectionHandle[handle].releasedImage;
615 Decorator::~Decorator()
620 Decorator::Decorator( Dali::Toolkit::Internal::Control& parent, Observer& observer )
623 mImpl = new Decorator::Impl( parent, observer );
628 } // namespace Toolkit