+ void CalculateHandleWorldCoordinates( HandleImpl& handle, Vector2& position )
+ {
+ // Gets the world position of the active layer. The active layer is where the handles are added.
+ const Vector3 parentWorldPosition = mActiveLayer.GetCurrentWorldPosition();
+
+ // The grab handle position in world coords.
+ // The active layer's world position is the center of the active layer. The origin of the
+ // coord system of the handles is the top left of the active layer.
+ position.x = parentWorldPosition.x - 0.5f * mControlSize.width + handle.position.x;
+ position.y = parentWorldPosition.y - 0.5f * mControlSize.height + handle.position.y;
+ }
+
+ void SetGrabHandlePosition()
+ {
+ // Reference to the grab handle.
+ HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+
+ // Transforms the handle position into world coordinates.
+ // @note This is not the same value as grabHandle.actor.GetCurrentWorldPosition()
+ // as it's transforming the handle's position set by the text-controller and not
+ // the final position set to the actor. Another difference is the GetCurrentWorldPosition()
+ // retrieves the position of the center of the actor but the handle's position set
+ // by the text controller is not the center of the actor.
+ Vector2 grabHandleWorldPosition;
+ CalculateHandleWorldCoordinates( grabHandle, grabHandleWorldPosition );
+
+ // Check if the grab handle exceeds the boundaries of the decoration box.
+ // At the moment only the height is checked for the grab handle.
+
+ grabHandle.verticallyFlipped = ( grabHandle.verticallyFlippedPreferred &&
+ ( ( grabHandleWorldPosition.y - grabHandle.size.height ) > mBoundingBox.y ) ) ||
+ ( grabHandleWorldPosition.y + grabHandle.lineHeight + grabHandle.size.height > mBoundingBox.w );
+
+ // The grab handle 'y' position in local coords.
+ // If the grab handle exceeds the bottom of the decoration box,
+ // set the 'y' position to the top of the line.
+ // The SetGrabHandleImage() method will change the orientation.
+ const float yLocalPosition = grabHandle.verticallyFlipped ? grabHandle.position.y : grabHandle.position.y + grabHandle.lineHeight;
+
+ if( grabHandle.actor )
+ {
+ grabHandle.actor.SetPosition( grabHandle.position.x + floor( 0.5f * mCursorWidth ),
+ yLocalPosition ); // TODO : Fix for multiline.
+ }
+ }
+
+ void SetSelectionHandlePosition( HandleType type )
+ {
+ const bool isPrimaryHandle = LEFT_SELECTION_HANDLE == type;
+
+ // Reference to the selection handle.
+ HandleImpl& handle = mHandle[type];
+
+ // Transforms the handle position into world coordinates.
+ // @note This is not the same value as handle.actor.GetCurrentWorldPosition()
+ // as it's transforming the handle's position set by the text-controller and not
+ // the final position set to the actor. Another difference is the GetCurrentWorldPosition()
+ // retrieves the position of the center of the actor but the handle's position set
+ // by the text controller is not the center of the actor.
+ Vector2 handleWorldPosition;
+ CalculateHandleWorldCoordinates( handle, handleWorldPosition );
+
+ // Whether to flip the handle (horizontally).
+ bool flipHandle = isPrimaryHandle ? mFlipLeftSelectionHandleDirection : mFlipRightSelectionHandleDirection;
+
+ // Whether to flip the handles if they are crossed.
+ bool crossFlip = false;
+ if( mFlipSelectionHandlesOnCross || !mHandlePanning )
+ {
+ crossFlip = mHandleCurrentCrossed;
+ }
+
+ // Does not flip if both conditions are true (double flip)
+ flipHandle = flipHandle != ( crossFlip || mHandlePreviousCrossed );
+
+ // Will flip the handles vertically if the user prefers it.
+ bool verticallyFlippedPreferred = handle.verticallyFlippedPreferred;
+
+ if( crossFlip || mHandlePreviousCrossed )
+ {
+ if( isPrimaryHandle )
+ {
+ verticallyFlippedPreferred = mHandle[RIGHT_SELECTION_HANDLE].verticallyFlippedPreferred;
+ }
+ else
+ {
+ verticallyFlippedPreferred = mHandle[LEFT_SELECTION_HANDLE].verticallyFlippedPreferred;
+ }
+ }
+
+ // Check if the selection handle exceeds the boundaries of the decoration box.
+ const bool exceedsLeftEdge = ( isPrimaryHandle ? !flipHandle : flipHandle ) && ( handleWorldPosition.x - handle.size.width < mBoundingBox.x );
+ const bool exceedsRightEdge = ( isPrimaryHandle ? flipHandle : !flipHandle ) && ( handleWorldPosition.x + handle.size.width > mBoundingBox.z );
+
+ // Does not flip if both conditions are true (double flip)
+ flipHandle = flipHandle != ( exceedsLeftEdge || exceedsRightEdge );
+
+ if( flipHandle )
+ {
+ if( handle.actor && !handle.horizontallyFlipped )
+ {
+ // Change the anchor point to flip the image.
+ handle.actor.SetAnchorPoint( isPrimaryHandle ? AnchorPoint::TOP_LEFT : AnchorPoint::TOP_RIGHT );
+
+ handle.horizontallyFlipped = true;
+ }
+ }
+ else
+ {
+ if( handle.actor && handle.horizontallyFlipped )
+ {
+ // Reset the anchor point.
+ handle.actor.SetAnchorPoint( isPrimaryHandle ? AnchorPoint::TOP_RIGHT : AnchorPoint::TOP_LEFT );
+
+ handle.horizontallyFlipped = false;
+ }
+ }
+
+ // Whether to flip the handle vertically.
+ handle.verticallyFlipped = ( verticallyFlippedPreferred &&
+ ( ( handleWorldPosition.y - handle.size.height ) > mBoundingBox.y ) ) ||
+ ( handleWorldPosition.y + handle.lineHeight + handle.size.height > mBoundingBox.w );
+
+ // The primary selection handle 'y' position in local coords.
+ // If the handle exceeds the bottom of the decoration box,
+ // set the 'y' position to the top of the line.
+ // The SetHandleImage() method will change the orientation.
+ const float yLocalPosition = handle.verticallyFlipped ? handle.position.y : handle.position.y + handle.lineHeight;
+
+ if( handle.actor )
+ {
+ handle.actor.SetPosition( handle.position.x,
+ yLocalPosition ); // TODO : Fix for multiline.
+ }
+ }
+
+ void SetHandleImage( HandleType type )
+ {
+ HandleImpl& handle = mHandle[type];
+
+ HandleType markerType = HANDLE_TYPE_COUNT;
+ // If the selection handle is flipped it chooses the image of the other selection handle. Does nothing for the grab handle.
+ if( LEFT_SELECTION_HANDLE == type )
+ {
+ type = handle.horizontallyFlipped ? RIGHT_SELECTION_HANDLE : LEFT_SELECTION_HANDLE;
+ markerType = handle.horizontallyFlipped ? RIGHT_SELECTION_HANDLE_MARKER : LEFT_SELECTION_HANDLE_MARKER;
+ }
+ else if( RIGHT_SELECTION_HANDLE == type )
+ {
+ type = handle.horizontallyFlipped ? LEFT_SELECTION_HANDLE : RIGHT_SELECTION_HANDLE;
+ markerType = handle.horizontallyFlipped ? LEFT_SELECTION_HANDLE_MARKER : RIGHT_SELECTION_HANDLE_MARKER;
+ }
+
+ // Chooses between the released or pressed image. It checks whether the pressed image exists.
+ if( handle.actor )
+ {
+ const HandleImageType imageType = ( handle.pressed ? ( mHandleImages[type][HANDLE_IMAGE_PRESSED] ? HANDLE_IMAGE_PRESSED : HANDLE_IMAGE_RELEASED ) : HANDLE_IMAGE_RELEASED );
+
+ handle.actor.SetImage( mHandleImages[type][imageType] );
+ }
+
+ if( HANDLE_TYPE_COUNT != markerType )
+ {
+ if( handle.markerActor )
+ {
+ const HandleImageType markerImageType = ( handle.pressed ? ( mHandleImages[markerType][HANDLE_IMAGE_PRESSED] ? HANDLE_IMAGE_PRESSED : HANDLE_IMAGE_RELEASED ) : HANDLE_IMAGE_RELEASED );
+ handle.markerActor.SetImage( mHandleImages[markerType][markerImageType] );
+ }
+ }
+
+ // Whether to flip the handle vertically.
+ if( handle.actor )
+ {
+ handle.actor.SetOrientation( handle.verticallyFlipped ? ANGLE_180 : ANGLE_0, Vector3::XAXIS );
+ }
+ }
+