Bläddra i källkod

GuiSpriteCtrl Update

Updated the GuiSpriteCtrl to allow the image to overflow the content area of the control. I also fixed a bug in rendering children of the control.
Peter Robinson 3 år sedan
förälder
incheckning
b636c4ebe5

+ 38 - 26
engine/source/2d/gui/guiSpriteCtrl.cc

@@ -67,6 +67,7 @@ GuiSpriteCtrl::GuiSpriteCtrl( void ) :
 	mImageSize.set(10, 10);
 	mFullSize = true;
 	mConstrainProportions = true;
+	mClampImage = true;
 	mSingleFrameBitmap = true;
 }
 
@@ -95,6 +96,7 @@ void GuiSpriteCtrl::initPersistFields()
 	addField("imageColor", TypeFluidColorI, Offset(mImageColor, GuiSpriteCtrl));
 	addField("imageSize", TypePoint2I, Offset(mImageSize, GuiSpriteCtrl));
 	addField("fullSize", TypeBool, Offset(mFullSize, GuiSpriteCtrl));
+	addField("clampImage", TypeBool, Offset(mClampImage, GuiSpriteCtrl));
 	addField("constrainProportions", TypeBool, Offset(mConstrainProportions, GuiSpriteCtrl));
 	endGroup("GuiSpriteCtrl");
 }
@@ -348,10 +350,18 @@ Point2I GuiSpriteCtrl::applyAlignment(RectI &bounds, Point2I &size)
 	}
 
 	//Apply the image offset
-	S32 maxX = bounds.extent.x - size.x;
-	S32 maxY = bounds.extent.y - size.y;
-	offset.x = mClamp(offset.x + mPositionOffset.x, 0, maxX);
-	offset.y = mClamp(offset.y + mPositionOffset.y, 0, maxY);
+	if (mClampImage)
+	{
+		S32 maxX = bounds.extent.x - size.x;
+		S32 maxY = bounds.extent.y - size.y;
+		offset.x = mClamp(offset.x + mPositionOffset.x, 0, maxX);
+		offset.y = mClamp(offset.y + mPositionOffset.y, 0, maxY);
+	}
+	else
+	{
+		offset.x = offset.x + mPositionOffset.x;
+		offset.y = offset.y + mPositionOffset.y;
+	}
 
 	return offset;
 }
@@ -546,50 +556,52 @@ void GuiSpriteCtrl::onRender(Point2I offset, const RectI &updateRect)
 			dglSetClipRect(clipRect);
 			dglSetBitmapModulation(ColorF(mImageColor));
 
-			Point2I offset = Point2I(0, 0);
+			Point2I imageOffset = Point2I(0, 0);
 			Point2I size = constrain(mImageSize);
 
 			if (mTileImage) //Tile the image
 			{
-				offset = mPositionOffset;
-				offset.x = (mPositionOffset.x % size.x);
-				if (offset.x > 0)
+				imageOffset = mPositionOffset;
+				imageOffset.x = (mPositionOffset.x % size.x);
+				if (imageOffset.x > 0)
 				{
-					offset.x -= size.x;
+					imageOffset.x -= size.x;
 				}
-				offset.y = (mPositionOffset.y % size.y);
-				if (offset.y > 0)
+				imageOffset.y = (mPositionOffset.y % size.y);
+				if (imageOffset.y > 0)
 				{
-					offset.y -= size.y;
+					imageOffset.y -= size.y;
 				}
 
-				RenderTiledImage(contentRect, offset, size);
+				RenderTiledImage(contentRect, imageOffset, size);
 			}
 			else if (mFullSize) //Fill with the image
 			{
 				size = constrain(contentRect.extent, false);
 				if (mConstrainProportions)
 				{
-					offset = applyAlignment(contentRect, size);
+					imageOffset = applyAlignment(contentRect, size);
 				}
-				RenderImage(contentRect, offset, size);
+				RenderImage(contentRect, imageOffset, size);
 			}
 			else //Position the image by profile alignment
 			{
-				size = constrain(mImageSize);
-				if (size.x > contentRect.extent.x)
-				{
-					size.x = contentRect.extent.x;
-					size = constrainLockX(size);
-				}
-				if (size.y > contentRect.extent.y)
+				if (mClampImage)
 				{
-					size.y = contentRect.extent.y;
-					size = constrainLockY(size);
+					if (size.x > contentRect.extent.x)
+					{
+						size.x = contentRect.extent.x;
+						size = constrainLockX(size);
+					}
+					if (size.y > contentRect.extent.y)
+					{
+						size.y = contentRect.extent.y;
+						size = constrainLockY(size);
+					}
 				}
-				offset = applyAlignment(contentRect, size);
+				imageOffset = applyAlignment(contentRect, size);
 
-				RenderImage(contentRect, offset, size);
+				RenderImage(contentRect, imageOffset, size);
 			}
 			dglClearBitmapModulation();
 			dglSetClipRect(oldClip);

+ 3 - 0
engine/source/2d/gui/guiSpriteCtrl.h

@@ -49,6 +49,7 @@ protected:
 	Point2I mImageSize; //The size of the image, reduced, if needed, to fit the content area
 	bool mFullSize; //If true, the image will take all available space
 	bool mConstrainProportions; //If true, the image will maintain its aspect ratio 
+	bool mClampImage; //If true, the image will be forced into the control's content area.
 	bool mSingleFrameBitmap; //If true and bitmaps are used, this will assume there's only one frame when scanning the bitmap
 
 	Fluid mFluidMoveTo;
@@ -110,6 +111,8 @@ public:
 	inline void setImageSize(Point2I size) { mImageSize = size; }
 	inline bool getFullSize() { return mFullSize; }
 	inline void setFullSize(bool isFull) { mFullSize = isFull; }
+	inline bool getClampImage() { return mClampImage; }
+	inline void setClampImage(bool clamp) { mClampImage = clamp; }
 	inline bool getConstrainProportions() { return mConstrainProportions; }
 	inline void setConstrainProportions(bool setting) { mConstrainProportions = setting; }
 	inline bool getSingleFrameBitmap() { return mSingleFrameBitmap; }

+ 23 - 0
engine/source/2d/gui/guiSpriteCtrl_ScriptBindings.h

@@ -761,6 +761,29 @@ ConsoleMethodWithDocs(GuiSpriteCtrl, getConstrainProportions, ConsoleBool, 2, 2,
 	return object->getConstrainProportions();
 }
 
+/*! Sets if the image should be able to overflow the content area.
+	@param clamp If true, the image will be forced into the content area. False will allow the image to overflow the content area, although it will still be cut off.
+	@return No return value.
+*/
+ConsoleMethodWithDocs(GuiSpriteCtrl, setClampImage, ConsoleVoid, 3, 3, "(bool clamp)")
+{
+	if (argc != 3)
+	{
+		Con::warnf("GuiSpriteCtrl::setClampImage() - Invalid number of parameters!");
+		return;
+	}
+
+	object->setClampImage(dAtob(argv[2]));
+}
+
+/*! Gets if the image is able to overflow the content area.
+	@return True if overflow is possible and false otherwise.
+*/
+ConsoleMethodWithDocs(GuiSpriteCtrl, getClampImage, ConsoleBool, 2, 2, "()")
+{
+	return object->getClampImage();
+}
+
 /*! Animates the position offset from its current value to a target value over time.
 	@param x/y The space-delimited x and y values to change the image offset to.
 	@param time The time in miliseconds it should take to complete the transformation.