Browse Source

Added GuiControl.textExtend

TextExtend allows a gui control to change its extent based on the text content.
Peter Robinson 3 years ago
parent
commit
0b650edde1

BIN
engine/compilers/VisualStudio 2019/Torque 2D.aps


+ 40 - 10
engine/source/gui/guiControl.cc

@@ -94,6 +94,7 @@ GuiControl::GuiControl()
    mTooltipWidth		= 250;
    mIsContainer         = false;
    mTextWrap			= false;
+   mTextExtend          = false;
 }
 
 GuiControl::~GuiControl()
@@ -202,6 +203,7 @@ void GuiControl::initPersistFields()
    addProtectedField("text", TypeCaseString, Offset(mText, GuiControl), setTextProperty, getTextProperty, "");
    addField("textID", TypeString, Offset(mTextID, GuiControl));
    addField("textWrap", TypeBool, Offset(mTextWrap, GuiControl), &writeTextWrapFn, "If true, text will wrap to additional lines.");
+   addField("textExtend", TypeBool, Offset(mTextExtend, GuiControl), &writeTextExtendFn, "If true, extent will change based on the size of the control's text when possible.");
    endGroup("Text");
 }
 
@@ -609,18 +611,31 @@ RectI GuiControl::getInnerRect(Point2I &offset, Point2I &extent, GuiControlState
 
 Point2I GuiControl::getOuterExtent(Point2I &innerExtent, GuiControlState currentState, GuiControlProfile *profile)
 {
-	//Get the border profiles
-	GuiBorderProfile *leftProfile = profile->getLeftBorder();
-	GuiBorderProfile *rightProfile = profile->getRightBorder();
-	GuiBorderProfile *topProfile = profile->getTopBorder();
-	GuiBorderProfile *bottomProfile = profile->getBottomBorder();
+    return Point2I(getOuterWidth(innerExtent.x, currentState, profile), getOuterHeight(innerExtent.y, currentState, profile));
+}
 
-	S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
-	S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
-	S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
-	S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
+S32 GuiControl::getOuterWidth(S32 innerWidth, GuiControlState currentState, GuiControlProfile* profile)
+{
+    //Get the border profiles
+    GuiBorderProfile* leftProfile = profile->getLeftBorder();
+    GuiBorderProfile* rightProfile = profile->getRightBorder();
+
+    S32 leftSize = (leftProfile) ? leftProfile->getMargin(currentState) + leftProfile->getBorder(currentState) + leftProfile->getPadding(currentState) : 0;
+    S32 rightSize = (rightProfile) ? rightProfile->getMargin(currentState) + rightProfile->getBorder(currentState) + rightProfile->getPadding(currentState) : 0;
 
-	return Point2I(innerExtent.x + leftSize + rightSize, innerExtent.y + topSize + bottomSize);
+    return innerWidth + leftSize + rightSize;
+}
+
+S32 GuiControl::getOuterHeight(S32 innerHeight, GuiControlState currentState, GuiControlProfile* profile)
+{
+    //Get the border profiles
+    GuiBorderProfile* topProfile = profile->getTopBorder();
+    GuiBorderProfile* bottomProfile = profile->getBottomBorder();
+
+    S32 topSize = (topProfile) ? topProfile->getMargin(currentState) + topProfile->getBorder(currentState) + topProfile->getPadding(currentState) : 0;
+    S32 bottomSize = (bottomProfile) ? bottomProfile->getMargin(currentState) + bottomProfile->getBorder(currentState) + bottomProfile->getPadding(currentState) : 0;
+
+    return innerHeight + topSize + bottomSize;
 }
 
 bool GuiControl::renderTooltip(Point2I &cursorPos, const char* tipText )
@@ -1706,6 +1721,21 @@ void GuiControl::renderText(const Point2I& offset, const Point2I& extent, const
 
         //first align vertical
         S32 blockHeight = textHeight * lineList.size();
+
+        if (mTextExtend)
+        {
+            Point2I extent = getExtent();
+            if (mTextWrap)
+            {
+                extent.y = getOuterHeight(blockHeight, NormalState, profile);
+            }
+            else
+            {
+                extent.x = getOuterWidth(profile->mFont->getStrWidth(text), NormalState, profile);
+            }
+            setExtent(extent);
+        }
+
         if (blockHeight < totalHeight)
         {
             startOffsetY = getTextVerticalOffset(blockHeight, totalHeight, profile->mVAlignment);

+ 8 - 2
engine/source/gui/guiControl.h

@@ -207,6 +207,7 @@ protected:
 	StringTableEntry    mText;
 	StringTableEntry    mTextID;
 	bool				mTextWrap;
+    bool                mTextExtend;
 
     /// @}
 
@@ -310,13 +311,16 @@ public:
 	virtual void             setTextID(S32 id);
 	virtual void             setTextID(const char *id);
 	virtual const char*      getText();
-	inline void				 setTextWrap(const bool wrap) { mTextWrap = wrap; }
-	inline bool				 getTextWrap() { return mTextWrap; }
+    inline void				 setTextWrap(const bool wrap) { mTextWrap = wrap; }
+    inline bool				 getTextWrap() { return mTextWrap; }
+    inline void				 setTextExtend(const bool extend) { mTextExtend = extend; }
+    inline bool				 getTextExtend() { return mTextExtend; }
 
 	// Text Property Accessors
 	static bool setTextProperty(void* obj, const char* data) { static_cast<GuiControl*>(obj)->setText(data); return false; }
 	static const char* getTextProperty(void* obj, const char* data) { return static_cast<GuiControl*>(obj)->getText(); }
 	static bool writeTextWrapFn(void* obj, const char* data) { return static_cast<GuiControl*>(obj)->getTextWrap(); }
+    static bool writeTextExtendFn(void* obj, const char* data) { return static_cast<GuiControl*>(obj)->getTextExtend(); }
 
 	static bool setExtentFn(void* obj, const char* data) { GuiControl* ctrl = static_cast<GuiControl*>(obj); Vector2 v = Vector2(data); ctrl->setExtent(Point2I(v.x, v.y)); ctrl->resetStoredExtent(); return false; }
 	static bool setMinExtentFn(void* obj, const char* data) { GuiControl* ctrl = static_cast<GuiControl*>(obj); Vector2 v = Vector2(data); ctrl->mMinExtent.set(v.x, v.y); ctrl->resetStoredExtent(); return false; }
@@ -744,6 +748,8 @@ public:
 
 	/// Returns the extent of the outer rect given the extent of the inner rect.
 	Point2I getOuterExtent(Point2I &innerExtent, GuiControlState currentState, GuiControlProfile *profile);
+    S32 getOuterWidth(S32 innerExtent, GuiControlState currentState, GuiControlProfile* profile);
+    S32 getOuterHeight(S32 innerExtent, GuiControlState currentState, GuiControlProfile* profile);
 
     virtual void inspectPostApply();
     virtual void inspectPreApply();

+ 22 - 5
engine/source/gui/guiControl_ScriptBinding.h

@@ -338,20 +338,37 @@ ConsoleMethodWithDocs(GuiControl, getText, ConsoleString, 2, 2, ())
 }
 
 /*! Turns on or off text wrap.
-	@param setting True turns on text wrap.
-	@return No return value
+    @param setting True turns on text wrap.
+    @return No return value
 */
 ConsoleMethodWithDocs(GuiControl, setTextWrap, ConsoleVoid, 3, 3, (setting))
 {
-	object->setTextWrap(dAtob(argv[2]));
+    object->setTextWrap(dAtob(argv[2]));
 }
 
 /*! Returns if text wrap is on.
-	@return Returns the state of text wrap.
+    @return Returns the state of text wrap.
 */
 ConsoleMethodWithDocs(GuiControl, getTextWrap, ConsoleBool, 2, 2, ())
 {
-	return object->getTextWrap();
+    return object->getTextWrap();
+}
+
+/*! Turns on or off text Extend.
+    @param setting True turns on text extend.
+    @return No return value
+*/
+ConsoleMethodWithDocs(GuiControl, setTextExtend, ConsoleVoid, 3, 3, (setting))
+{
+    object->setTextExtend(dAtob(argv[2]));
+}
+
+/*! Returns if text extend is on.
+    @return Returns the state of text extend.
+*/
+ConsoleMethodWithDocs(GuiControl, getTextExtend, ConsoleBool, 2, 2, ())
+{
+    return object->getTextExtend();
 }
 
 ConsoleMethodGroupEndWithDocs(GuiControl)

+ 1 - 1
engine/source/gui/guiTypes.cc

@@ -135,7 +135,7 @@ GuiBorderProfile::GuiBorderProfile()
 		mPadding[i] = 0;
 	}
 
-   GuiBorderProfile *def = dynamic_cast<GuiBorderProfile*>(Sim::findObject("GuiDefaultProfile"));
+   GuiBorderProfile *def = dynamic_cast<GuiBorderProfile*>(Sim::findObject("GuiDefaultBorderProfile"));
    if (def)
    {
       for (S32 i = 0; i < 4; i++)

+ 27 - 0
engine/source/sim/simObject_ScriptBinding.h

@@ -906,6 +906,33 @@ ConsoleMethodWithDocs(SimObject, clone, ConsoleInt, 2, 3, ([copyDynamicFields =
     return pClonedObject->getId();
 }
 
+/*! Takes all values from one object and puts them into anther object of the same class. This includes dynamic fields.
+	@return No return value.
+*/
+ConsoleMethodWithDocs(SimObject, assignFieldsFrom, ConsoleVoid, 3, 3, (SimObject))
+{
+	// Find the specified object.
+	SimObject* pSimObject = dynamic_cast<SimObject*>(Sim::findObject(argv[2]));
+
+	// Did we find the object?
+	if (!pSimObject)
+	{
+		// No, so warn.
+		Con::warnf("SimObject::assignFieldsFrom() - Could not find the source object '%s'.", argv[2]);
+		return;
+	}
+
+	// Do the objects have the same class?
+	if (object->getClassRep() != pSimObject->getClassRep())
+	{
+		// No, so warn.
+		Con::warnf("SimObject::assignFieldsFrom() - target object and source object must have the same class.");
+		return;
+	}
+
+	object->assignFieldsFrom(pSimObject);
+}
+
 /*! @name Timer/Scheduled Events
 	Perform timed callbacks on the object.
 	@{