diff --git a/source/graphics/Color.h b/source/graphics/Color.h index 18251f5ab3..94f49b4550 100644 --- a/source/graphics/Color.h +++ b/source/graphics/Color.h @@ -43,6 +43,14 @@ struct CColor CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {} CColor(float cr, float cg, float cb, float ca) : r(cr), g(cg), b(cb), a(ca) {} + /** + * Returns whether this has been set to a valid color. + */ + operator bool() const + { + return r >= 0 && g >= 0 && b >= 0 && a >= 0; + } + /** * Try to parse @p Value as a color. Returns true on success, false otherwise. * @param value Should be "r g b" or "r g b a" where each value is an integer in [0,255]. diff --git a/source/gui/CDropDown.cpp b/source/gui/CDropDown.cpp index 5db6934ce8..b100b8775a 100644 --- a/source/gui/CDropDown.cpp +++ b/source/gui/CDropDown.cpp @@ -493,7 +493,7 @@ void CDropDown::Draw() GUI::GetSetting(this, enabled ? "textcolor_selected" : "textcolor_disabled", color); GUI::GetSettingPointer(this, enabled ? "sprite" : "sprite_disabled", sprite); - GetGUI()->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize); + m_pGUI->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize); if (button_width > 0.f) { @@ -503,20 +503,20 @@ void CDropDown::Draw() if (!enabled) { GUI::GetSettingPointer(this, "sprite2_disabled", sprite2_second); - GetGUI()->DrawSprite(GUI<>::FallBackSprite(*sprite2_second, *sprite2), cell_id, bz+0.05f, rect); + m_pGUI->DrawSprite(*sprite2_second || *sprite2, cell_id, bz + 0.05f, rect); } else if (m_Open) { GUI::GetSettingPointer(this, "sprite2_pressed", sprite2_second); - GetGUI()->DrawSprite(GUI<>::FallBackSprite(*sprite2_second, *sprite2), cell_id, bz+0.05f, rect); + m_pGUI->DrawSprite(*sprite2_second || *sprite2, cell_id, bz + 0.05f, rect); } else if (m_MouseHovering) { GUI::GetSettingPointer(this, "sprite2_over", sprite2_second); - GetGUI()->DrawSprite(GUI<>::FallBackSprite(*sprite2_second, *sprite2), cell_id, bz+0.05f, rect); + m_pGUI->DrawSprite(*sprite2_second || *sprite2, cell_id, bz + 0.05f, rect); } else - GetGUI()->DrawSprite(*sprite2, cell_id, bz+0.05f, rect); + m_pGUI->DrawSprite(*sprite2, cell_id, bz + 0.05f, rect); } if (selected != -1) // TODO: Maybe check validity completely? diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 261a31ac96..55b4c72a4a 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -353,7 +353,7 @@ void CGUI::Draw() void CGUI::DrawSprite(const CGUISpriteInstance& Sprite, int CellID, const float& Z, const CRect& Rect, const CRect& UNUSED(Clipping)) { // If the sprite doesn't exist (name == ""), don't bother drawing anything - if (Sprite.IsEmpty()) + if (!Sprite) return; // TODO: Clipping? diff --git a/source/gui/CGUIColor.h b/source/gui/CGUIColor.h index 33e28286cb..45b5796ac9 100644 --- a/source/gui/CGUIColor.h +++ b/source/gui/CGUIColor.h @@ -32,6 +32,16 @@ struct CGUIColor : CColor CGUIColor(float r, float g, float b, float a) : CColor(r, g, b, a) {} + /** + * Returns this color if it has been set, otherwise the given fallback color. + */ + const CGUIColor& operator||(const CGUIColor& fallback) const + { + if (*this) + return *this; + return fallback; + } + /** * Load color depending on current GUI page. */ diff --git a/source/gui/CGUIScrollBarVertical.cpp b/source/gui/CGUIScrollBarVertical.cpp index 571fbd0836..4416f4870c 100644 --- a/source/gui/CGUIScrollBarVertical.cpp +++ b/source/gui/CGUIScrollBarVertical.cpp @@ -57,11 +57,11 @@ void CGUIScrollBarVertical::Draw() return; } - if (GetGUI() && IsVisible()) + if (IsVisible()) { CRect outline = GetOuterRect(); - GetGUI()->DrawSprite( + m_pGUI->DrawSprite( GetStyle()->m_SpriteBackVertical, 0, m_Z+0.1f, @@ -81,9 +81,9 @@ void CGUIScrollBarVertical::Draw() if (m_ButtonMinusHovered) { if (m_ButtonMinusPressed) - button_top = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonTopPressed, GetStyle()->m_SpriteButtonTop); + button_top = &(GetStyle()->m_SpriteButtonTopPressed || GetStyle()->m_SpriteButtonTop); else - button_top = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonTopOver, GetStyle()->m_SpriteButtonTop); + button_top = &(GetStyle()->m_SpriteButtonTopOver || GetStyle()->m_SpriteButtonTop); } else button_top = &GetStyle()->m_SpriteButtonTop; @@ -91,14 +91,14 @@ void CGUIScrollBarVertical::Draw() if (m_ButtonPlusHovered) { if (m_ButtonPlusPressed) - button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomPressed, GetStyle()->m_SpriteButtonBottom); + button_bottom = &(GetStyle()->m_SpriteButtonBottomPressed || GetStyle()->m_SpriteButtonBottom); else - button_bottom = &GUI<>::FallBackSprite(GetStyle()->m_SpriteButtonBottomOver, GetStyle()->m_SpriteButtonBottom); + button_bottom = &(GetStyle()->m_SpriteButtonBottomOver || GetStyle()->m_SpriteButtonBottom); } else button_bottom = &GetStyle()->m_SpriteButtonBottom; - GetGUI()->DrawSprite( + m_pGUI->DrawSprite( *button_top, 0, m_Z+0.2f, @@ -110,7 +110,7 @@ void CGUIScrollBarVertical::Draw() ) ); - GetGUI()->DrawSprite( + m_pGUI->DrawSprite( *button_bottom, 0, m_Z+0.2f, @@ -123,7 +123,7 @@ void CGUIScrollBarVertical::Draw() ); } - GetGUI()->DrawSprite( + m_pGUI->DrawSprite( GetStyle()->m_SpriteBarVertical, 0, m_Z + 0.2f, diff --git a/source/gui/CGUISprite.cpp b/source/gui/CGUISprite.cpp index 3195388edf..c9b98c7f46 100644 --- a/source/gui/CGUISprite.cpp +++ b/source/gui/CGUISprite.cpp @@ -41,11 +41,6 @@ void CGUISpriteInstance::Draw(const CGUI* pGUI, const CRect& Size, int CellID, s GUIRenderer::Draw(m_DrawCallCache, Z); } -bool CGUISpriteInstance::IsEmpty() const -{ - return m_SpriteName.empty(); -} - // Plus a load of constructors / assignment operators, which don't copy the // DrawCall cache (to avoid losing track of who has allocated various bits // of data): diff --git a/source/gui/CGUISprite.h b/source/gui/CGUISprite.h index d564087406..ccd96044f2 100644 --- a/source/gui/CGUISprite.h +++ b/source/gui/CGUISprite.h @@ -162,8 +162,31 @@ public: CGUISpriteInstance(const CStr& SpriteName); void Draw(const CGUI* pGUI, const CRect& Size, int CellID, std::map& Sprites, float Z) const; - bool IsEmpty() const; + + /** + * Whether this Sprite has no texture name set. + */ + operator bool() const { return !m_SpriteName.empty(); }; + + /** + * Returns this spirte if it has been set, otherwise the given fallback sprite. + */ + const CGUISpriteInstance& operator||(const CGUISpriteInstance& fallback) const + { + if (*this) + return *this; + return fallback; + } + + /** + * Returns the sprite texture name. + */ const CStr& GetName() const { return m_SpriteName; } + + /** + * Changes the texture name. + * Use as rarely as possible, because it clears the draw cache. + */ void SetName(const CStr& SpriteName); private: diff --git a/source/gui/GUIutil.h b/source/gui/GUIutil.h index 3556f082c4..1dade6a8e3 100644 --- a/source/gui/GUIutil.h +++ b/source/gui/GUIutil.h @@ -165,34 +165,6 @@ public: */ static PSRETURN SetSetting(IGUIObject* pObject, const CStr& Setting, const T& Value, const bool& SkipMessage = false); - /** - * This will return the value of the first sprite if it's not null, - * if it is null, it will return the value of the second sprite, if - * that one is null, then null it is. - * - * @param prim Primary sprite that should be used - * @param sec Secondary sprite if Primary should fail - * @return Resulting string - */ - static const CGUISpriteInstance& FallBackSprite(const CGUISpriteInstance& prim, const CGUISpriteInstance& sec) - { - return (prim.IsEmpty() ? sec : prim); - } - - /** - * Same principle as FallBackSprite - * - * @param prim Primary color that should be used - * @param sec Secondary color if Primary should fail - * @return Resulting color - * @see FallBackSprite - */ - static CGUIColor FallBackColor(const CGUIColor& prim, const CGUIColor& sec) - { - // CGUIColor() == null. - return ((prim!=CGUIColor())?(prim):(sec)); - } - /** * Sets a value by setting and object name using a real * datatype as input. diff --git a/source/gui/IGUIButtonBehavior.cpp b/source/gui/IGUIButtonBehavior.cpp index 685a019de1..f9b04f1271 100644 --- a/source/gui/IGUIButtonBehavior.cpp +++ b/source/gui/IGUIButtonBehavior.cpp @@ -145,19 +145,19 @@ CGUIColor IGUIButtonBehavior::ChooseColor() if (!enabled) { GUI::GetSetting(this, "textcolor_disabled", color2); - return GUI<>::FallBackColor(color2, color); + return color2 || color; } else if (m_MouseHovering) { if (m_Pressed) { GUI::GetSetting(this, "textcolor_pressed", color2); - return GUI<>::FallBackColor(color2, color); + return color2 || color; } else { GUI::GetSetting(this, "textcolor_over", color2); - return GUI<>::FallBackColor(color2, color); + return color2 || color; } } else @@ -170,14 +170,14 @@ void IGUIButtonBehavior::DrawButton(const CRect& rect, const float& z, CGUISprit GUI::GetSetting(this, "enabled", enabled); if (!enabled) - GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_disabled, sprite), cell_id, z, rect); + m_pGUI->DrawSprite(sprite_disabled || sprite, cell_id, z, rect); else if (m_MouseHovering) { if (m_Pressed) - GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_pressed, sprite), cell_id, z, rect); + m_pGUI->DrawSprite(sprite_pressed || sprite, cell_id, z, rect); else - GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_over, sprite), cell_id, z, rect); + m_pGUI->DrawSprite(sprite_over || sprite, cell_id, z, rect); } else - GetGUI()->DrawSprite(sprite, cell_id, z, rect); + m_pGUI->DrawSprite(sprite, cell_id, z, rect); }