Browse Source

Letting buttons and labels say how big they want to be in toolbars.

David Piuva 2 years ago
parent
commit
580937448c

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

@@ -186,7 +186,7 @@ public:
 	//   If the parent has decorations around the child components, the region may include some padding from which the flexible regions calculate the locations from in percents.
 	//     For example: A given space from 10 to 90 pixels will have 0% at 10 and 100% at 90.
 	//   A toolbar may give non-overlapping spaces that are assigned automatically to simplify the process of maintaining the layout while adding and removing child components.
-	// TODO: How can internal changes to inner dimensions be detected by the parent to run this method again? Call the parent and tell it to update next time something needs drawing or mouse input?
+	// TODO: How can internal changes to inner dimensions be detected by the parent to run this method again?
 	virtual void applyLayout(const IRect& givenSpace);
 	// Update layout when the component moved but the parent has the same dimensions
 	void updateLayout();

+ 9 - 0
Source/DFPSR/gui/components/Button.cpp

@@ -32,6 +32,7 @@ void Button::declareAttributes(StructureDefinition &target) const {
 	VisualComponent::declareAttributes(target);
 	target.declareAttribute(U"Color");
 	target.declareAttribute(U"Text");
+	target.declareAttribute(U"Padding");
 }
 
 Persistent* Button::findAttribute(const ReadableString &name) {
@@ -42,6 +43,8 @@ Persistent* Button::findAttribute(const ReadableString &name) {
 		return &(this->foreColor);
 	} else if (string_caseInsensitiveMatch(name, U"Text")) {
 		return &(this->text);
+	} else if (string_caseInsensitiveMatch(name, U"Padding")) {
+		return &(this->padding);
 	} else {
 		return VisualComponent::findAttribute(name);
 	}
@@ -133,3 +136,9 @@ void Button::changedAttribute(const ReadableString &name) {
 		this->hasImages = false;
 	}
 }
+
+IVector2D Button::getDesiredDimensions() {
+	this->completeAssets();
+	int sizeAdder = this->padding.value * 2;
+	return IVector2D(font_getLineWidth(this->font, this->text.value) + sizeAdder, font_getSize(this->font) + sizeAdder);
+}

+ 2 - 0
Source/DFPSR/gui/components/Button.h

@@ -36,6 +36,7 @@ public:
 	PersistentColor backColor;
 	PersistentColor foreColor;
 	PersistentString text;
+	PersistentInteger padding = PersistentInteger(5); // How many pixels of padding are applied on each side of the text when calculating desired dimensions for placing in toolbars.
 	void declareAttributes(StructureDefinition &target) const override;
 	Persistent* findAttribute(const ReadableString &name) override;
 private:
@@ -61,6 +62,7 @@ public:
 	void changedTheme(VisualTheme newTheme) override;
 	void changedLocation(const IRect &oldLocation, const IRect &newLocation) override;
 	void changedAttribute(const ReadableString &name) override;
+	IVector2D getDesiredDimensions() override;
 };
 
 }

+ 8 - 0
Source/DFPSR/gui/components/Label.cpp

@@ -33,6 +33,7 @@ void Label::declareAttributes(StructureDefinition &target) const {
 	target.declareAttribute(U"Color");
 	target.declareAttribute(U"Opacity");
 	target.declareAttribute(U"Text");
+	target.declareAttribute(U"Padding");
 }
 
 Persistent* Label::findAttribute(const ReadableString &name) {
@@ -43,6 +44,8 @@ Persistent* Label::findAttribute(const ReadableString &name) {
 		return &(this->opacity);
 	} else if (string_caseInsensitiveMatch(name, U"Text")) {
 		return &(this->text);
+	} else if (string_caseInsensitiveMatch(name, U"Padding")) {
+		return &(this->padding);
 	} else {
 		return VisualComponent::findAttribute(name);
 	}
@@ -75,3 +78,8 @@ void Label::completeAssets() {
 	}
 }
 
+IVector2D Label::getDesiredDimensions() {
+	this->completeAssets();
+	int sizeAdder = this->padding.value * 2;
+	return IVector2D(font_getLineWidth(this->font, this->text.value) + sizeAdder, font_getSize(this->font) + sizeAdder);
+}

+ 2 - 0
Source/DFPSR/gui/components/Label.h

@@ -37,6 +37,7 @@ public:
 	// TODO: Why is "PersistentInteger opacity(255);" not recognizing the constructor?
 	PersistentInteger opacity = PersistentInteger(255); // 0 is fully invisible, 255 is fully opaque
 	PersistentString text;
+	PersistentInteger padding = PersistentInteger(3); // How many pixels of padding are applied on each side of the text when calculating desired dimensions for placing in toolbars.
 	// Attribute access
 	void declareAttributes(StructureDefinition &target) const override;
 	Persistent* findAttribute(const ReadableString &name) override;
@@ -50,6 +51,7 @@ public:
 	bool isContainer() const override;
 	void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
 	bool pointIsInside(const IVector2D& pixelPosition) override;
+	IVector2D getDesiredDimensions() override;
 };
 
 }

+ 3 - 2
Source/DFPSR/gui/components/Toolbar.h

@@ -35,8 +35,9 @@ public:
 	PersistentBoolean solid; // If true, the panel itself will be drawn.
 	PersistentBoolean plain; // If true, a solid color will be drawn instead of a buffered image to save time and memory.
 	PersistentColor color; // The color being used when drawn is set to true.
-	PersistentInteger padding; // Empty space around child components.
-	PersistentInteger spacing; // Empty space between child components.
+	PersistentInteger padding = PersistentInteger(2); // Empty space around child components.
+	PersistentInteger spacing = PersistentInteger(3); // Empty space between child components.
+	// TODO: Start, center, end and fill alignment options.
 	void declareAttributes(StructureDefinition &target) const override;
 	Persistent* findAttribute(const ReadableString &name) override;
 private: