+ float CalculateVerticalPopUpPosition( float halfHeight, bool preferBelow )
+ {
+ float yPosition = 0.f;
+
+ const HandleImpl& primaryHandle = mHandle[LEFT_SELECTION_HANDLE];
+ const HandleImpl& secondaryHandle = mHandle[RIGHT_SELECTION_HANDLE];
+ const HandleImpl& grabHandle = mHandle[GRAB_HANDLE];
+
+ if( primaryHandle.active || secondaryHandle.active )
+ {
+ // The origin of the decorator's coordinate system in world coords.
+ const Vector3 originWorldCoords = mActiveLayer.GetCurrentWorldPosition() - mActiveLayer.GetCurrentSize() * ACTIVE_LAYER_ANCHOR_POINT;
+
+ if( preferBelow )
+ {
+ // Find out if there is enough space for the popup at the bottom.
+ const float primaryBelowY = primaryHandle.position.y + primaryHandle.lineHeight + primaryHandle.size.height;
+ const float secondaryBelowY = secondaryHandle.position.y + secondaryHandle.lineHeight + secondaryHandle.size.height;
+
+ float maxY = std::max( primaryBelowY, secondaryBelowY );
+
+ yPosition = halfHeight + maxY;
+
+ if( originWorldCoords.y + yPosition + halfHeight > mBoundingBox.w )
+ {
+ // Does not fit below.
+
+ // Try to fit first below the non active handle. Otherwise above the active handle.
+ if( RIGHT_SELECTION_HANDLE == mHandleReleased )
+ {
+ if( primaryBelowY < secondaryBelowY )
+ {
+ yPosition = halfHeight + primaryBelowY;
+ }
+ else
+ {
+ yPosition = primaryHandle.position.y - primaryHandle.size.height - halfHeight;
+ }
+ }
+ else if( LEFT_SELECTION_HANDLE == mHandleReleased )
+ {
+ if( secondaryBelowY < primaryBelowY )
+ {
+ yPosition = halfHeight + secondaryBelowY;
+ }
+ else
+ {
+ yPosition = secondaryHandle.position.y - secondaryHandle.size.height - halfHeight;
+ }
+ }
+
+ // Check the handle is whithin the decoration box.
+ if( originWorldCoords.y + yPosition < mBoundingBox.y )
+ {
+ yPosition = mBoundingBox.y - originWorldCoords.y + halfHeight;
+ }
+
+ if( originWorldCoords.y + yPosition > mBoundingBox.w )
+ {
+ yPosition = mBoundingBox.w - originWorldCoords.y - halfHeight;
+ }
+ }
+ } // preferBelow
+ else
+ {
+ // Find out if there is enough space for the popup at the top.
+ const float primaryTopY = primaryHandle.position.y - primaryHandle.size.height;
+ const float secondaryTopY = secondaryHandle.position.y - secondaryHandle.size.height;
+
+ float minY = std::min( primaryTopY, secondaryTopY );
+
+ yPosition = -halfHeight + minY;
+ } // !preferBelow
+ } // ( primaryHandle.active || secondaryHandle.active )
+ else if( grabHandle.active )
+ {
+ if( preferBelow )
+ {
+ yPosition = halfHeight + grabHandle.lineHeight + grabHandle.size.height + grabHandle.position.y;
+ }
+ else
+ {
+ yPosition = -halfHeight + grabHandle.position.y - POPUP_PADDING;
+ }
+ }
+
+ return yPosition;
+ }
+
+ void ConstrainPopupPosition( const Vector3& popupHalfSize )
+ {
+ // Check if the popup is within the boundaries of the decoration box.
+
+ // Check first the horizontal dimension. If is not within the boundaries, it calculates the offset.
+
+ // The origin of the decorator's coordinate system in world coords.
+ const Vector3 originWorldCoords = mActiveLayer.GetCurrentWorldPosition() - mActiveLayer.GetCurrentSize() * ACTIVE_LAYER_ANCHOR_POINT;
+
+ // The popup's position in world coords.
+ Vector3 popupPositionWorldCoords = originWorldCoords + mCopyPastePopup.position;
+
+ if( popupPositionWorldCoords.x - popupHalfSize.width < mBoundingBox.x )
+ {
+ mCopyPastePopup.position.x += mBoundingBox.x - ( popupPositionWorldCoords.x - popupHalfSize.width );
+ }
+ else if( popupPositionWorldCoords.x + popupHalfSize.width > mBoundingBox.z )
+ {
+ mCopyPastePopup.position.x += mBoundingBox.z - ( popupPositionWorldCoords.x + popupHalfSize.width );
+ }
+
+ // Check the vertical dimension. If the popup doesn't fit above the handles, it looks for a valid position below.
+ if( popupPositionWorldCoords.y - popupHalfSize.height < mBoundingBox.y )
+ {
+ mCopyPastePopup.position.y = CalculateVerticalPopUpPosition( popupHalfSize.height, true ); // true -> prefer to set the popup's position below.
+ }
+ }
+
+ void SetPopupPosition( Actor actor )