From e247326b51fea2bae4bdcaddda096ba75eb93217 Mon Sep 17 00:00:00 2001
From: Aloshi <allofquist@yahoo.com>
Date: Thu, 10 Oct 2013 16:14:33 -0500
Subject: [PATCH] Better opacity support for TextComponent

---
 src/GuiComponent.cpp             |  4 ++++
 src/GuiComponent.h               |  4 ++--
 src/components/TextComponent.cpp | 28 ++++++++++++++++++++++++++--
 src/components/TextComponent.h   |  4 ++++
 4 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/src/GuiComponent.cpp b/src/GuiComponent.cpp
index 501e5dd8b..4f793713e 100644
--- a/src/GuiComponent.cpp
+++ b/src/GuiComponent.cpp
@@ -149,6 +149,10 @@ unsigned char GuiComponent::getOpacity() const
 void GuiComponent::setOpacity(unsigned char opacity)
 {
 	mOpacity = opacity;
+	for(auto it = mChildren.begin(); it != mChildren.end(); it++)
+	{
+		(*it)->setOpacity(opacity);
+	}
 }
 
 const Eigen::Affine3f GuiComponent::getTransform()
diff --git a/src/GuiComponent.h b/src/GuiComponent.h
index aad06ffa3..928d6f2b7 100644
--- a/src/GuiComponent.h
+++ b/src/GuiComponent.h
@@ -48,8 +48,8 @@ public:
 	unsigned int getChildCount() const;
 	GuiComponent* getChild(unsigned int i) const;
 
-	unsigned char getOpacity() const;
-	void setOpacity(unsigned char opacity);
+	virtual unsigned char getOpacity() const;
+	virtual void setOpacity(unsigned char opacity);
 
 	const Eigen::Affine3f getTransform();
 
diff --git a/src/components/TextComponent.cpp b/src/components/TextComponent.cpp
index 293624ef1..1d172f97f 100644
--- a/src/components/TextComponent.cpp
+++ b/src/components/TextComponent.cpp
@@ -32,8 +32,24 @@ void TextComponent::setFont(std::shared_ptr<Font> font)
 void TextComponent::setColor(unsigned int color)
 {
 	mColor = color;
-	mOpacity = mColor & 0x000000FF;
-	onTextChanged();
+
+	unsigned char opacity = mColor & 0x000000FF;
+	GuiComponent::setOpacity(opacity);
+
+	onColorChanged();
+}
+
+void TextComponent::setOpacity(unsigned char opacity)
+{
+	mColor = (mColor & 0xFFFFFF00) | opacity;
+	onColorChanged();
+
+	GuiComponent::setOpacity(opacity);
+}
+
+unsigned char TextComponent::getOpacity() const
+{
+	return mColor & 0x000000FF;
 }
 
 void TextComponent::setText(const std::string& text)
@@ -104,6 +120,14 @@ void TextComponent::onTextChanged()
 	mTextCache = std::shared_ptr<TextCache>(f->buildTextCache(f->wrapText(mText, mSize.x()), 0, 0, (mColor >> 8 << 8) | mOpacity));
 }
 
+void TextComponent::onColorChanged()
+{
+	if(mTextCache)
+	{
+		mTextCache->setColor(mColor);
+	}
+}
+
 void TextComponent::setValue(const std::string& value)
 {
 	setText(value);
diff --git a/src/components/TextComponent.h b/src/components/TextComponent.h
index 641d5cb4a..4cac3da4f 100644
--- a/src/components/TextComponent.h
+++ b/src/components/TextComponent.h
@@ -20,6 +20,9 @@ public:
 
 	std::string getValue() const override;
 	void setValue(const std::string& value) override;
+
+	unsigned char getOpacity() const override;
+	void setOpacity(unsigned char opacity) override;
 	
 	std::shared_ptr<Font> getFont() const;
 
@@ -27,6 +30,7 @@ private:
 	void calculateExtent();
 
 	void onTextChanged();
+	void onColorChanged();
 
 	unsigned int mColor;
 	std::shared_ptr<Font> mFont;