Преглед изворни кода

Automatically reapplying layout after someone accessed a component's region.

David Piuva пре 4 година
родитељ
комит
4dc8a4e3c6
2 измењених фајлова са 25 додато и 6 уклоњено
  1. 15 4
      Source/DFPSR/gui/VisualComponent.cpp
  2. 10 2
      Source/DFPSR/gui/VisualComponent.h

+ 15 - 4
Source/DFPSR/gui/VisualComponent.cpp

@@ -42,12 +42,18 @@ bool VisualComponent::isContainer() const {
 	return true;
 	return true;
 }
 }
 
 
-IRect VisualComponent::getLocation() const {
+IRect VisualComponent::getLocation() {
+	// If someone requested access to Left, Top, Right or Bottom, regionAccessed will be true
+	if (this->regionAccessed) {
+		// Now that a fixed location is requested, we need to recalculate the location from the flexible region based on parent dimensions
+		this->updateLayout();
+		this->regionAccessed = false;
+	}
 	return this->location;
 	return this->location;
 }
 }
 
 
-IVector2D VisualComponent::getSize() const {
-	return this->location.size();
+IVector2D VisualComponent::getSize() {
+	return this->getLocation().size();
 }
 }
 
 
 void VisualComponent::setRegion(const FlexRegion &newRegion) {
 void VisualComponent::setRegion(const FlexRegion &newRegion) {
@@ -91,8 +97,13 @@ void VisualComponent::setLocation(const IRect &newLocation) {
 	this->changedLocation(oldLocation, newLocation);
 	this->changedLocation(oldLocation, newLocation);
 }
 }
 
 
+void VisualComponent::updateLayout() {
+	this->setLocation(this->region.getNewLocation(this->parentSize));
+}
+
 void VisualComponent::applyLayout(IVector2D parentSize) {
 void VisualComponent::applyLayout(IVector2D parentSize) {
-	this->setLocation(this->region.getNewLocation(parentSize));
+	this->parentSize = parentSize;
+	this->updateLayout();
 }
 }
 
 
 void VisualComponent::updateLocationEvent(const IRect& oldLocation, const IRect& newLocation) {
 void VisualComponent::updateLocationEvent(const IRect& oldLocation, const IRect& newLocation) {

+ 10 - 2
Source/DFPSR/gui/VisualComponent.h

@@ -39,6 +39,8 @@ PERSISTENT_DECLARATION(VisualComponent)
 protected:
 protected:
 	// Parent component
 	// Parent component
 	VisualComponent *parent = nullptr;
 	VisualComponent *parent = nullptr;
+	IVector2D parentSize; // Remembering the parent's size so that the root component can remember the window's size when moving
+	bool regionAccessed = false; // If someone requested access to the region, remember to update layout in case of new settings
 	// Child components
 	// Child components
 	List<std::shared_ptr<VisualComponent>> children;
 	List<std::shared_ptr<VisualComponent>> children;
 	// Remember the component used for a drag event
 	// Remember the component used for a drag event
@@ -70,12 +72,16 @@ public:
 		} else if (string_caseInsensitiveMatch(name, U"Visible")) {
 		} else if (string_caseInsensitiveMatch(name, U"Visible")) {
 			return &(this->visible);
 			return &(this->visible);
 		} else if (string_caseInsensitiveMatch(name, U"Left")) {
 		} else if (string_caseInsensitiveMatch(name, U"Left")) {
+			this->regionAccessed = true;
 			return &(this->region.sides[0]);
 			return &(this->region.sides[0]);
 		} else if (string_caseInsensitiveMatch(name, U"Top")) {
 		} else if (string_caseInsensitiveMatch(name, U"Top")) {
+			this->regionAccessed = true;
 			return &(this->region.sides[1]);
 			return &(this->region.sides[1]);
 		} else if (string_caseInsensitiveMatch(name, U"Right")) {
 		} else if (string_caseInsensitiveMatch(name, U"Right")) {
+			this->regionAccessed = true;
 			return &(this->region.sides[2]);
 			return &(this->region.sides[2]);
 		} else if (string_caseInsensitiveMatch(name, U"Bottom")) {
 		} else if (string_caseInsensitiveMatch(name, U"Bottom")) {
+			this->regionAccessed = true;
 			return &(this->region.sides[3]);
 			return &(this->region.sides[3]);
 		} else {
 		} else {
 			return nullptr;
 			return nullptr;
@@ -97,8 +103,8 @@ public:
 	virtual ~VisualComponent();
 	virtual ~VisualComponent();
 public:
 public:
 	virtual bool isContainer() const;
 	virtual bool isContainer() const;
-	IRect getLocation() const;
-	IVector2D getSize() const;
+	IRect getLocation();
+	IVector2D getSize();
 	void setRegion(const FlexRegion &newRegion);
 	void setRegion(const FlexRegion &newRegion);
 	FlexRegion getRegion() const;
 	FlexRegion getRegion() const;
 	void setVisible(bool visible);
 	void setVisible(bool visible);
@@ -178,6 +184,8 @@ public:
 	//   parentHeight must be the current height of the parent container
 	//   parentHeight must be the current height of the parent container
 	// Override to apply a custom behaviour, which may be useful for fixed size components.
 	// Override to apply a custom behaviour, which may be useful for fixed size components.
 	virtual void applyLayout(IVector2D parentSize);
 	virtual void applyLayout(IVector2D parentSize);
+	// Update layout when the component moved but the parent has the same dimensions
+	void updateLayout();
 	// Called after the component has been created, moved or resized.
 	// Called after the component has been created, moved or resized.
 	virtual void updateLocationEvent(const IRect& oldLocation, const IRect& newLocation);
 	virtual void updateLocationEvent(const IRect& oldLocation, const IRect& newLocation);
 	// Returns true iff the pixel with its upper left corner at pixelPosition is inside the component.
 	// Returns true iff the pixel with its upper left corner at pixelPosition is inside the component.