Browse Source

Layout change does not always require text widget formatting, be more restrictive for perfomance reasons

Michael Ragazzon 3 years ago
parent
commit
f054057d65

+ 11 - 4
Source/Core/Elements/ElementFormControlTextArea.cpp

@@ -27,10 +27,11 @@
  */
  */
 
 
 #include "../../../Include/RmlUi/Core/Elements/ElementFormControlTextArea.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementFormControlTextArea.h"
-#include "../../../Include/RmlUi/Core/Math.h"
-#include "../../../Include/RmlUi/Core/ElementUtilities.h"
 #include "../../../Include/RmlUi/Core/ElementText.h"
 #include "../../../Include/RmlUi/Core/ElementText.h"
+#include "../../../Include/RmlUi/Core/ElementUtilities.h"
+#include "../../../Include/RmlUi/Core/Math.h"
 #include "../../../Include/RmlUi/Core/PropertyIdSet.h"
 #include "../../../Include/RmlUi/Core/PropertyIdSet.h"
+#include "../../../Include/RmlUi/Core/StyleSheetSpecification.h"
 #include "WidgetTextInputMultiLine.h"
 #include "WidgetTextInputMultiLine.h"
 
 
 namespace Rml {
 namespace Rml {
@@ -181,8 +182,14 @@ void ElementFormControlTextArea::OnPropertyChange(const PropertyIdSet& changed_p
 {
 {
 	ElementFormControl::OnPropertyChange(changed_properties);
 	ElementFormControl::OnPropertyChange(changed_properties);
 
 
-	if (changed_properties.Contains(PropertyId::Color) ||
-		changed_properties.Contains(PropertyId::BackgroundColor))
+	// Some inherited properties require text formatting update, mainly font and line-height properties.
+	const PropertyIdSet changed_inherited_layout_properties = changed_properties &
+		(StyleSheetSpecification::GetRegisteredInheritedProperties() & StyleSheetSpecification::GetRegisteredPropertiesForcingLayout());
+
+	if (!changed_inherited_layout_properties.Empty())
+		widget->ForceFormattingOnNextLayout();
+
+	if (changed_properties.Contains(PropertyId::Color) || changed_properties.Contains(PropertyId::BackgroundColor))
 		widget->UpdateSelectionColours();
 		widget->UpdateSelectionColours();
 
 
 	if (changed_properties.Contains(PropertyId::CaretColor))
 	if (changed_properties.Contains(PropertyId::CaretColor))

+ 11 - 4
Source/Core/Elements/InputTypeText.cpp

@@ -28,10 +28,11 @@
 
 
 #include "InputTypeText.h"
 #include "InputTypeText.h"
 #include "../../../Include/RmlUi/Core/ElementUtilities.h"
 #include "../../../Include/RmlUi/Core/ElementUtilities.h"
-#include "WidgetTextInputSingleLine.h"
-#include "WidgetTextInputSingleLinePassword.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementFormControlInput.h"
 #include "../../../Include/RmlUi/Core/Elements/ElementFormControlInput.h"
 #include "../../../Include/RmlUi/Core/PropertyIdSet.h"
 #include "../../../Include/RmlUi/Core/PropertyIdSet.h"
+#include "../../../Include/RmlUi/Core/StyleSheetSpecification.h"
+#include "WidgetTextInputSingleLine.h"
+#include "WidgetTextInputSingleLinePassword.h"
 
 
 namespace Rml {
 namespace Rml {
 
 
@@ -96,8 +97,14 @@ bool InputTypeText::OnAttributeChange(const ElementAttributes& changed_attribute
 // Called when properties on the control are changed.
 // Called when properties on the control are changed.
 void InputTypeText::OnPropertyChange(const PropertyIdSet& changed_properties)
 void InputTypeText::OnPropertyChange(const PropertyIdSet& changed_properties)
 {
 {
-	if (changed_properties.Contains(PropertyId::Color) ||
-		changed_properties.Contains(PropertyId::BackgroundColor))
+	// Some inherited properties require text formatting update, mainly font and line-height properties.
+	const PropertyIdSet changed_inherited_layout_properties = changed_properties &
+		(StyleSheetSpecification::GetRegisteredInheritedProperties() & StyleSheetSpecification::GetRegisteredPropertiesForcingLayout());
+
+	if (!changed_inherited_layout_properties.Empty())
+		widget->ForceFormattingOnNextLayout();
+
+	if (changed_properties.Contains(PropertyId::Color) || changed_properties.Contains(PropertyId::BackgroundColor))
 		widget->UpdateSelectionColours();
 		widget->UpdateSelectionColours();
 
 
 	if (changed_properties.Contains(PropertyId::CaretColor))
 	if (changed_properties.Contains(PropertyId::CaretColor))

+ 12 - 17
Source/Core/Elements/WidgetTextInput.cpp

@@ -106,6 +106,7 @@ WidgetTextInput::WidgetTextInput(ElementFormControl* _parent) : internal_dimensi
 	cursor_wrap_down = false;
 	cursor_wrap_down = false;
 	ideal_cursor_position_to_the_right_of_cursor = true;
 	ideal_cursor_position_to_the_right_of_cursor = true;
 	cancel_next_drag = false;
 	cancel_next_drag = false;
+	force_formatting_on_next_layout = false;
 
 
 	ideal_cursor_position = 0;
 	ideal_cursor_position = 0;
 
 
@@ -259,6 +260,8 @@ void WidgetTextInput::OnResize()
 	Vector2f text_position = parent->GetBox().GetPosition(Box::CONTENT);
 	Vector2f text_position = parent->GetBox().GetPosition(Box::CONTENT);
 	text_element->SetOffset(text_position, parent);
 	text_element->SetOffset(text_position, parent);
 	selected_text_element->SetOffset(text_position, parent);
 	selected_text_element->SetOffset(text_position, parent);
+
+	ForceFormattingOnNextLayout();
 }
 }
 
 
 // Renders the cursor, if it is visible.
 // Renders the cursor, if it is visible.
@@ -279,31 +282,18 @@ void WidgetTextInput::OnRender()
 // Formats the widget's internal content.
 // Formats the widget's internal content.
 void WidgetTextInput::OnLayout()
 void WidgetTextInput::OnLayout()
 {
 {
-	FormatElement();
-
-	Vector2f new_internal_dimensions = parent->GetBox().GetSize(Box::CONTENT);
-	if (new_internal_dimensions != internal_dimensions)
+	if (force_formatting_on_next_layout)
 	{
 	{
-		internal_dimensions = new_internal_dimensions;
+		internal_dimensions = parent->GetBox().GetSize(Box::CONTENT);
+		FormatElement();
 		UpdateCursorPosition(true);
 		UpdateCursorPosition(true);
+		force_formatting_on_next_layout = false;
 	}
 	}
 
 
 	parent->SetScrollLeft(scroll_offset.x);
 	parent->SetScrollLeft(scroll_offset.x);
 	parent->SetScrollTop(scroll_offset.y);
 	parent->SetScrollTop(scroll_offset.y);
 }
 }
 
 
-// Returns the input element's underlying text element.
-ElementText* WidgetTextInput::GetTextElement()
-{
-	return text_element;
-}
-
-// Returns the input element's maximum allowed text dimensions.
-Vector2f WidgetTextInput::GetTextDimensions() const
-{
-	return internal_dimensions;
-}
-
 // Gets the parent element containing the widget.
 // Gets the parent element containing the widget.
 Element* WidgetTextInput::GetElement() const
 Element* WidgetTextInput::GetElement() const
 {
 {
@@ -1159,6 +1149,11 @@ void WidgetTextInput::GenerateCursor()
 	GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), cursor_size, color);
 	GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), cursor_size, color);
 }
 }
 
 
+void WidgetTextInput::ForceFormattingOnNextLayout()
+{
+	force_formatting_on_next_layout = true;
+}
+
 void WidgetTextInput::UpdateCursorPosition(bool update_ideal_cursor_position)
 void WidgetTextInput::UpdateCursorPosition(bool update_ideal_cursor_position)
 {
 {
 	if (text_element->GetFontFaceHandle() == 0)
 	if (text_element->GetFontFaceHandle() == 0)

+ 4 - 5
Source/Core/Elements/WidgetTextInput.h

@@ -32,6 +32,7 @@
 #include "../../../Include/RmlUi/Core/EventListener.h"
 #include "../../../Include/RmlUi/Core/EventListener.h"
 #include "../../../Include/RmlUi/Core/Geometry.h"
 #include "../../../Include/RmlUi/Core/Geometry.h"
 #include "../../../Include/RmlUi/Core/Vertex.h"
 #include "../../../Include/RmlUi/Core/Vertex.h"
+#include <float.h>
 
 
 namespace Rml {
 namespace Rml {
 
 
@@ -68,6 +69,8 @@ public:
 	void UpdateSelectionColours();
 	void UpdateSelectionColours();
 	/// Generates the text cursor.
 	/// Generates the text cursor.
 	void GenerateCursor();
 	void GenerateCursor();
+	/// Force text formatting on the next layout update.
+	void ForceFormattingOnNextLayout();
 
 
 	/// Updates the cursor, if necessary.
 	/// Updates the cursor, if necessary.
 	void OnUpdate();
 	void OnUpdate();
@@ -78,11 +81,6 @@ public:
 	/// Called when the parent element's size changes.
 	/// Called when the parent element's size changes.
 	void OnResize();
 	void OnResize();
 
 
-	/// Returns the input element's underlying text element.
-	ElementText* GetTextElement();
-	/// Returns the input element's maximum allowed text dimensions.
-	Vector2f GetTextDimensions() const;
-
 protected:
 protected:
 	enum class CursorMovement { Begin = -4, BeginLine = -3, PreviousWord = -2, Left = -1, Right = 1, NextWord = 2, EndLine = 3, End = 4 };
 	enum class CursorMovement { Begin = -4, BeginLine = -3, PreviousWord = -2, Left = -1, Right = 1, NextWord = 2, EndLine = 3, End = 4 };
 
 
@@ -216,6 +214,7 @@ private:
 
 
 	bool ideal_cursor_position_to_the_right_of_cursor;
 	bool ideal_cursor_position_to_the_right_of_cursor;
 	bool cancel_next_drag;
 	bool cancel_next_drag;
+	bool force_formatting_on_next_layout;
 
 
 	// Selection. The start and end indices of the selection are in absolute coordinates.
 	// Selection. The start and end indices of the selection are in absolute coordinates.
 	Element* selection_element;
 	Element* selection_element;