Browse Source

Updated int and float GUI fields so that getValue() always returns currently input value

BearishSun 9 years ago
parent
commit
ee0e78d8d9

+ 10 - 6
Source/BansheeEditor/Include/BsGUIFloatField.h

@@ -28,7 +28,7 @@ namespace BansheeEngine
 			const String& style, const GUIDimensions& dimensions, bool withLabel);
 
 		/**	Returns the value of the input field. */
-		float getValue() const { return mValue; }
+		float getValue() const;
 
 		/**	Sets a new value in the input field, it returns the clamped value according to range and step. */
 		float setValue(float value);
@@ -72,7 +72,7 @@ namespace BansheeEngine
 		bool _hasCustomCursor(const Vector2I position, CursorType& type) const override;
 
 		/** @copydoc GUIElementContainer::_mouseEvent */
-		virtual bool _mouseEvent(const GUIMouseEvent& ev) override;
+		bool _mouseEvent(const GUIMouseEvent& ev) override;
 
 		/** @copydoc GUIElementContainer::styleUpdated */
 		void styleUpdated() override;
@@ -80,10 +80,8 @@ namespace BansheeEngine
 		/**	Triggered when the input box value changes. */
 		void valueChanging(const WString& newValue);
 
-		/**
-		 * Triggered when the input box value changes and is confirmed.
-		 */
-		void valueChanged(float newValue, bool confirmed = true);
+		/** Triggered when the input box value changes and is confirmed. */
+		void valueChanged(float newValue);
 
 		/**	Triggers when the input box receives or loses keyboard focus. */
 		void focusChanged(bool focus);
@@ -91,6 +89,12 @@ namespace BansheeEngine
 		/**	Triggered when the users confirms input in the input box. */
 		void inputConfirmed();
 
+		/** Updates the underlying input box with the text representing the provided floating point value. */
+		void setText(float value);
+
+		/** Clamps the provided value to current valid range, and step interval. */
+		float applyRangeAndStep(float value) const;
+
 		/** Callback that checks can the provided string be converted to a floating point value. */
 		static bool floatFilter(const WString& str);
 

+ 7 - 1
Source/BansheeEditor/Include/BsGUIIntField.h

@@ -28,7 +28,7 @@ namespace BansheeEngine
 			const String& style, const GUIDimensions& dimensions, bool withLabel);
 
 		/**	Returns the value of the input field. */
-		INT32 getValue() const { return mValue; }
+		INT32 getValue() const;
 
 		/**	Sets a new value in the input field, it returns the clamped value according to range and step. */
 		INT32 setValue(INT32 value);
@@ -92,6 +92,12 @@ namespace BansheeEngine
 		/**	Triggered when the users confirms input in the input box. */
 		void inputConfirmed();
 
+		/** Updates the underlying input box with the text representing the provided integer value. */
+		void setText(INT32 value);
+
+		/** Clamps the provided value to current valid range, and step interval. */
+		INT32 applyRangeAndStep(INT32 value) const;
+
 		/**	Callback that checks can the provided string be converted to an integer value. */
 		static bool intFilter(const WString& str);
 

+ 39 - 26
Source/BansheeEditor/Source/BsGUIFloatField.cpp

@@ -114,10 +114,7 @@ namespace BansheeEngine
 					mLastDragPos = event.getPosition().x + jumpAmount;
 
 					if (oldValue != newValue)
-					{
 						setValue(newValue);
-						valueChanged(newValue, true);
-					}
 				}
 			}
 
@@ -134,21 +131,22 @@ namespace BansheeEngine
 		return false;
 	}
 
-	float GUIFloatField::setValue(float value)
+	float GUIFloatField::getValue() const
 	{
-		if (mStep != 0.0f)
-			value = value - fmod(value, mStep);
+		return applyRangeAndStep(mValue);
+	}
 
-		mValue = Math::clamp(value, mMinValue, mMaxValue);
+	float GUIFloatField::setValue(float value)
+	{
+		if (mValue == value)
+			return value;
 
-		// Only update with new value if it actually changed, otherwise
-		// problems can occur when user types in "0." and the field
-		// updates back to "0" effectively making "." unusable
-		float curValue = parseFloat(mInputBox->getText());
-		if (mValue != curValue)
-			mInputBox->setText(toWString(mValue));
+		mValue = value;
+		
+		value = applyRangeAndStep(value);
+		setText(value);
 
-		return mValue;
+		return value;
 	}
 
 	void GUIFloatField::setRange(float min, float max)
@@ -172,7 +170,8 @@ namespace BansheeEngine
 
 	void GUIFloatField::_setValue(float value, bool triggerEvent)
 	{
-		setValue(value);
+		mValue = value;
+		setText(value);
 
 		if(triggerEvent)
 			onValueChanged(mValue);
@@ -200,18 +199,12 @@ namespace BansheeEngine
 
 	void GUIFloatField::valueChanging(const WString& newValue)
 	{
-		valueChanged(parseFloat(newValue), false);
+		valueChanged(parseFloat(newValue));
 	}
 
-	void GUIFloatField::valueChanged(float newValue, bool confirmed)
+	void GUIFloatField::valueChanged(float newValue)
 	{
-		if (confirmed) {
-			CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
-		}
-		else
-		{
-			onValueChanged(newValue);
-		}
+		CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
 	}
 
 	void GUIFloatField::focusChanged(bool focus)
@@ -224,17 +217,37 @@ namespace BansheeEngine
 		else
 		{
 			UndoRedo::instance().popGroup("InputBox");
-			valueChanged(parseFloat(mInputBox->getText()));
+
+			setText(applyRangeAndStep(mValue));
 			mHasInputFocus = false;
 		}
 	}
 
 	void GUIFloatField::inputConfirmed()
 	{
-		valueChanged(parseFloat(mInputBox->getText()));
+		setText(applyRangeAndStep(mValue));
+
 		onConfirm();
 	}
 
+	void GUIFloatField::setText(float value)
+	{
+		// Only update with new value if it actually changed, otherwise
+		// problems can occur when user types in "0." and the field
+		// updates back to "0" effectively making "." unusable
+		float curValue = parseFloat(mInputBox->getText());
+		if (value != curValue)
+			mInputBox->setText(toWString(value));
+	}
+
+	float GUIFloatField::applyRangeAndStep(float value) const
+	{
+		if (mStep != 0.0f)
+			value = value - fmod(value, mStep);
+
+		return Math::clamp(value, mMinValue, mMaxValue);
+	}
+
 	bool GUIFloatField::floatFilter(const WString& str)
 	{
 		return std::regex_match(str, std::wregex(L"-?(\\d*(\\.\\d*)?)?"));

+ 33 - 14
Source/BansheeEditor/Source/BsGUIIntField.cpp

@@ -132,10 +132,7 @@ namespace BansheeEngine
 					mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
 
 					if (oldValue != newValue)
-					{
-						setValue(newValue);
 						valueChanged(newValue);
-					}
 				}
 			}
 
@@ -160,21 +157,22 @@ namespace BansheeEngine
 		mInputBox->setStyle(getSubStyleName(getInputStyleType()));
 	}
 
+	INT32 GUIIntField::getValue() const
+	{
+		return applyRangeAndStep(mValue);
+	}
+
 	INT32 GUIIntField::setValue(INT32 value)
 	{
-		if (mStep != 0)
-			mValue = mValue - mValue % mStep;
+		if (mValue == value)
+			return value;
 
-		mValue = Math::clamp(value, mMinValue, mMaxValue);
+		mValue = value;
 
-		// Only update with new value if it actually changed, otherwise
-		// problems can occur when user types in "0." and the field
-		// updates back to "0" effectively making "." unusable
-		float curValue = parseFloat(mInputBox->getText());
-		if (mValue != curValue)
-			mInputBox->setText(toWString(mValue));
+		value = applyRangeAndStep(value);
+		setText(value);
 
-		return mValue;
+		return value;
 	}
 
 	void GUIIntField::setRange(INT32 min, INT32 max)
@@ -198,7 +196,8 @@ namespace BansheeEngine
 
 	void GUIIntField::_setValue(INT32 value, bool triggerEvent)
 	{
-		setValue(value);
+		mValue = value;
+		setText(value);
 
 		if (triggerEvent)
 			onValueChanged(mValue);
@@ -236,6 +235,8 @@ namespace BansheeEngine
 		else
 		{
 			UndoRedo::instance().popGroup("InputBox");
+
+			setText(applyRangeAndStep(mValue));
 			mHasInputFocus = false;
 		}
 	}
@@ -245,6 +246,24 @@ namespace BansheeEngine
 		onConfirm();
 	}
 
+	void GUIIntField::setText(INT32 value)
+	{
+		// Only update with new value if it actually changed, otherwise
+		// problems can occur when user types in "0." and the field
+		// updates back to "0" effectively making "." unusable
+		float curValue = parseFloat(mInputBox->getText());
+		if (value != curValue)
+			mInputBox->setText(toWString(value));
+	}
+
+	INT32 GUIIntField::applyRangeAndStep(INT32 value) const
+	{
+		if (mStep != 0)
+			value = value - value % mStep;
+
+		return Math::clamp(value, mMinValue, mMaxValue);
+	}
+
 	bool GUIIntField::intFilter(const WString& str)
 	{
 		return std::regex_match(str, std::wregex(L"-?(\\d+)?"));