Browse Source

Made a new system for updating graphics in visual components.

David Piuva 5 years ago
parent
commit
db94ed1333

+ 1 - 0
Source/DFPSR/api/guiAPI.cpp

@@ -207,6 +207,7 @@ ReturnCode dsr::component_setProperty(const Component& component, const Readable
 		return ReturnCode::KeyNotFound;
 	} else {
 		if (target->assignValue(value)) {
+			component->changedAttribute(propertyName);
 			return ReturnCode::Good;
 		} else {
 			if (mustAssign) {

+ 1 - 0
Source/DFPSR/gui/VisualComponent.cpp

@@ -88,6 +88,7 @@ void VisualComponent::setLocation(IRect newLocation) {
 	if (oldLocation != newLocation) {
 		this->updateLocationEvent(oldLocation, newLocation);
 	}
+	this->changedLocation(oldLocation, newLocation);
 }
 
 void VisualComponent::applyLayout(IVector2D parentSize) {

+ 4 - 0
Source/DFPSR/gui/VisualComponent.h

@@ -201,6 +201,10 @@ public:
 	virtual void receiveKeyboardEvent(const KeyboardEvent& event);
 	// Notifies when the theme has been changed, so that temporary data depending on the theme can be replaced
 	virtual void changedTheme(VisualTheme newTheme);
+	// Override to be notified about individual attribute changes
+	virtual void changedAttribute(const ReadableString &name) {};
+	// Override to be notified about location changes
+	virtual void changedLocation(IRect &oldLocation, IRect &newLocation) {};
 };
 
 }

+ 13 - 5
Source/DFPSR/gui/components/Button.cpp

@@ -68,14 +68,10 @@ static OrderedImageRgbaU8 generateButtonImage(MediaMethod imageGenerator, int pr
 void Button::generateGraphics() {
 	int width = this->location.width();
 	int height = this->location.height();
-	if (!this->hasImages || this->lastWidth != width || this->lastHeight != height || !string_match(this->text.value, this->lastText) || this->color.value != this->lastColor) {
+	if (!this->hasImages) {
 		completeAssets();
 		this->imageUp = generateButtonImage(this->button, 0, width, height, this->color.value, this->text.value, this->font);
 		this->imageDown = generateButtonImage(this->button, 1, width, height, this->color.value, this->text.value, this->font);
-		this->lastWidth = width;
-		this->lastHeight = height;
-		this->lastText = this->text.value;
-		this->lastColor = this->color.value;
 		this->hasImages = true;
 	}
 }
@@ -119,3 +115,15 @@ void Button::completeAssets() {
 		this->font = font_getDefault();
 	}
 }
+
+void Button::changedLocation(IRect &oldLocation, IRect &newLocation) {
+	// If the component has changed dimensions then redraw the image
+	if (oldLocation.size() != newLocation.size()) {
+		this->hasImages = false;
+	}
+}
+
+void Button::changedAttribute(const ReadableString &name) {
+	// If an attribute has changed then force the image to be redrawn
+	this->hasImages = false;
+}

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

@@ -48,10 +48,6 @@ private:
 	void generateGraphics();
 	// Generated
 	bool hasImages = false;
-	int lastWidth = 0;
-	int lastHeight = 0;
-	String lastText;
-	ColorRgbI32 lastColor;
 	OrderedImageRgbaU8 imageUp;
 	OrderedImageRgbaU8 imageDown;
 public:
@@ -62,6 +58,8 @@ public:
 	void receiveMouseEvent(const MouseEvent& event) override;
 	bool pointIsInside(const IVector2D& pixelPosition) override;
 	void changedTheme(VisualTheme newTheme) override;
+	void changedLocation(IRect &oldLocation, IRect &newLocation) override;
+	void changedAttribute(const ReadableString &name) override;
 };
 
 }

+ 13 - 4
Source/DFPSR/gui/components/Panel.cpp

@@ -52,12 +52,9 @@ bool Panel::isContainer() const {
 void Panel::generateGraphics() {
 	int width = this->location.width();
 	int height = this->location.height();
-	if (!this->hasImages || this->lastWidth != width || this->lastHeight != height || this->color.value != this->lastColor) {
+	if (!this->hasImages) {
 		completeAssets();
 		this->background(width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->imageBackground);
-		this->lastWidth = width;
-		this->lastHeight = height;
-		this->lastColor = this->color.value;
 		this->hasImages = true;
 	}
 }
@@ -81,3 +78,15 @@ void Panel::completeAssets() {
 		this->background = theme_getScalableImage(theme_getDefault(), U"Panel");
 	}
 }
+
+void Panel::changedLocation(IRect &oldLocation, IRect &newLocation) {
+	// If the component has changed dimensions then redraw the image
+	if (oldLocation.size() != newLocation.size()) {
+		this->hasImages = false;
+	}
+}
+
+void Panel::changedAttribute(const ReadableString &name) {
+	// If an attribute has changed then force the image to be redrawn
+	this->hasImages = false;
+}

+ 2 - 4
Source/DFPSR/gui/components/Panel.h

@@ -41,18 +41,16 @@ private:
 	void generateGraphics();
 	MediaMethod background;
 	OrderedImageRgbaU8 imageBackground; // Alpha is copied to the target and should be 255
-	//OrderedImageRgbaU8 imagePassive; // TODO: Use for passive rendering of child components in cluttered panels
 	// Generated
 	bool hasImages = false;
-	int lastWidth = 0;
-	int lastHeight = 0;
-	ColorRgbI32 lastColor;
 public:
 	Panel();
 public:
 	bool isContainer() const;
 	void drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) override;
 	void changedTheme(VisualTheme newTheme) override;
+	void changedLocation(IRect &oldLocation, IRect &newLocation) override;
+	void changedAttribute(const ReadableString &name) override;
 };
 
 }