Browse Source

Improving vertical toolbar detection and using named sides in FlexRegion.

David Piuva 2 years ago
parent
commit
66fa7f54a9

+ 4 - 4
Source/DFPSR/gui/FlexRegion.cpp

@@ -59,10 +59,10 @@ String& FlexValue::toStreamIndented(String& out, const ReadableString& indentati
 
 IRect FlexRegion::getNewLocation(const IRect &givenSpace) {
 	return IRect::FromBounds(
-		this->sides[0].getValue(givenSpace.left(), givenSpace.right()),
-		this->sides[1].getValue(givenSpace.top(), givenSpace.bottom()),
-		this->sides[2].getValue(givenSpace.left(), givenSpace.right()),
-		this->sides[3].getValue(givenSpace.top(), givenSpace.bottom())
+		this->left.getValue(givenSpace.left(), givenSpace.right()),
+		this->top.getValue(givenSpace.top(), givenSpace.bottom()),
+		this->right.getValue(givenSpace.left(), givenSpace.right()),
+		this->bottom.getValue(givenSpace.top(), givenSpace.bottom())
 	);
 }
 

+ 21 - 22
Source/DFPSR/gui/FlexRegion.h

@@ -57,38 +57,37 @@ inline bool operator!=(const FlexValue &left, const FlexValue &right) {
 
 struct FlexRegion {
 public:
-	// Indices: 0 = left, 1 = top, 2 = right, 3 = bottom
-	FlexValue sides[4];
+	FlexValue left, top, right, bottom;
 public:
-	void setLeft(const FlexValue &left) { this->sides[0] = left; }
-	void setTop(const FlexValue &top) { this->sides[1] = top; }
-	void setRight(const FlexValue &right) { this->sides[2] = right; }
-	void setBottom(const FlexValue &bottom) { this->sides[3] = bottom; }
-	void setLeft(const ReadableString &left) { this->sides[0] = FlexValue(left, U""); }
-	void setTop(const ReadableString &top) { this->sides[1] = FlexValue(top, U""); }
-	void setRight(const ReadableString &right) { this->sides[2] = FlexValue(right, U""); }
-	void setBottom(const ReadableString &bottom) { this->sides[3] = FlexValue(bottom, U""); }
+	void setLeft(const FlexValue &left) { this->left = left; }
+	void setTop(const FlexValue &top) { this->top = top; }
+	void setRight(const FlexValue &right) { this->right = right; }
+	void setBottom(const FlexValue &bottom) { this->bottom = bottom; }
+	void setLeft(const ReadableString &left) { this->left = FlexValue(left, U""); }
+	void setTop(const ReadableString &top) { this->top = FlexValue(top, U""); }
+	void setRight(const ReadableString &right) { this->right = FlexValue(right, U""); }
+	void setBottom(const ReadableString &bottom) { this->bottom = FlexValue(bottom, U""); }
 public:
 	// Full region
 	FlexRegion() {
-		this->sides[0] = FlexValue(0, 0);
-		this->sides[1] = FlexValue(0, 0);
-		this->sides[2] = FlexValue(100, 0);
-		this->sides[3] = FlexValue(100, 0);
+		this->left = FlexValue(0, 0);
+		this->top = FlexValue(0, 0);
+		this->right = FlexValue(100, 0);
+		this->bottom = FlexValue(100, 0);
 	}
 	// Upper left aligned region
 	explicit FlexRegion(const IRect &location) {
-		this->sides[0] = FlexValue(0, location.left());
-		this->sides[1] = FlexValue(0, location.top());
-		this->sides[2] = FlexValue(0, location.right());
-		this->sides[3] = FlexValue(0, location.bottom());
+		this->left = FlexValue(0, location.left());
+		this->top = FlexValue(0, location.top());
+		this->right = FlexValue(0, location.right());
+		this->bottom = FlexValue(0, location.bottom());
 	}
 	// Flexible region
 	FlexRegion(int leftRatio, int leftOffset, int topRatio, int topOffset, int rightRatio, int rightOffset, int bottomRatio, int bottomOffset) {
-		this->sides[0] = FlexValue(leftRatio, leftOffset);
-		this->sides[1] = FlexValue(topRatio, topOffset);
-		this->sides[2] = FlexValue(rightRatio, rightOffset);
-		this->sides[3] = FlexValue(bottomRatio, bottomOffset);
+		this->left = FlexValue(leftRatio, leftOffset);
+		this->top = FlexValue(topRatio, topOffset);
+		this->right = FlexValue(rightRatio, rightOffset);
+		this->bottom = FlexValue(bottomRatio, bottomOffset);
 	}
 	// Parse individual flex values from text
 	FlexRegion(const ReadableString &left, const ReadableString &top, const ReadableString &right, const ReadableString &bottom) {

+ 5 - 5
Source/DFPSR/gui/VisualComponent.h

@@ -76,16 +76,16 @@ public:
 			return &(this->visible);
 		} else if (string_caseInsensitiveMatch(name, U"Left")) {
 			this->regionAccessed = true;
-			return &(this->region.sides[0]);
+			return &(this->region.left);
 		} else if (string_caseInsensitiveMatch(name, U"Top")) {
 			this->regionAccessed = true;
-			return &(this->region.sides[1]);
+			return &(this->region.top);
 		} else if (string_caseInsensitiveMatch(name, U"Right")) {
 			this->regionAccessed = true;
-			return &(this->region.sides[2]);
+			return &(this->region.right);
 		} else if (string_caseInsensitiveMatch(name, U"Bottom")) {
 			this->regionAccessed = true;
-			return &(this->region.sides[3]);
+			return &(this->region.bottom);
 		} else {
 			return nullptr;
 		}
@@ -205,7 +205,7 @@ public:
 	virtual bool pointIsInside(const IVector2D& pixelPosition);
 	// Get a pointer to the topmost child
 	// Invisible components are ignored by default, but includeInvisible can be enabled to change that.
-	// Returns an empty reference if the pixel position didn't hit anything inside.
+	// Returns an empty reference if the pixel position didn't hit anything in
 	// Since the root component might not be heap allocated, it cannot return itself by reference.
 	//   Use pointIsInside if your root component doesn't cover the whole window.
 	std::shared_ptr<VisualComponent> getTopChild(const IVector2D& pixelPosition, bool includeInvisible = false);

+ 4 - 2
Source/DFPSR/gui/components/Toolbar.cpp

@@ -108,8 +108,10 @@ void Toolbar::changedAttribute(const ReadableString &name) {
 }
 
 void Toolbar::updateLocationEvent(const IRect& oldLocation, const IRect& newLocation) {
-	// TODO: Find out if the toolbar is vertical or horizontal from its dimensions.
-	bool vertical = newLocation.width() < newLocation.height();
+	int widthStretch = this->region.right.getRatio() - this->region.left.getRatio();
+	int heightStretch = this->region.bottom.getRatio() - this->region.top.getRatio();
+	// Place members vertically if the toolbar mostly stretches vertically or if it has uniform stretch and is taller than wide.
+	bool vertical = (widthStretch < heightStretch) || ((widthStretch == heightStretch) && (newLocation.width() < newLocation.height()));
 	if (vertical) {
 		// TODO: Add scroll buttons on the sides if there is not enough space for all child components.
 		// Place each child component in order.