From eddeffa5b7be4fe562ef0a302989d7af4e386ba6 Mon Sep 17 00:00:00 2001 From: rachytski Date: Tue, 5 Jun 2012 01:22:19 +0400 Subject: [PATCH] using TextView inside Button to draw text uniformly. --- gui/button.cpp | 80 ++++++++++++++++++++++++++-------- gui/button.hpp | 28 +++++++----- gui/element.hpp | 4 +- gui/text_view.cpp | 15 +++---- gui/text_view.hpp | 3 +- map/country_status_display.cpp | 4 +- 6 files changed, 91 insertions(+), 43 deletions(-) diff --git a/gui/button.cpp b/gui/button.cpp index dd1ac461c9..d9b79e9915 100644 --- a/gui/button.cpp +++ b/gui/button.cpp @@ -5,8 +5,17 @@ namespace gui { Button::Button(Params const & p) : Element(p) { - setWidth(p.m_width); - setHeight(p.m_height); + TextView::Params tp; + + tp.m_depth = p.m_depth + 1; + tp.m_pivot = p.m_pivot; + tp.m_position = yg::EPosCenter; + tp.m_text = p.m_text; + + m_textView.reset(new TextView(tp)); + + setMinWidth(p.m_minWidth); + setMinHeight(p.m_minHeight); setText(p.m_text); setFont(EActive, yg::FontDesc(12, yg::Color(0, 0, 0, 255))); @@ -52,39 +61,40 @@ namespace gui void Button::setText(string const & text) { - m_text = text; + m_textView->setText(text); } string const & Button::text() const { - return m_text; + return m_textView->text(); } - void Button::setWidth(unsigned widthInDIP) + void Button::setMinWidth(unsigned minWidth) { - m_widthInDIP = widthInDIP; + m_minWidth = minWidth; invalidate(); } - unsigned Button::width() const + unsigned Button::minWidth() const { - return m_widthInDIP; + return m_minWidth; } - void Button::setHeight(unsigned heightInDIP) + void Button::setMinHeight(unsigned minHeight) { - m_heightInDIP = heightInDIP; + m_minHeight = minHeight; invalidate(); } - unsigned Button::height() const + unsigned Button::minHeight() const { - return m_heightInDIP; + return m_minHeight; } - yg::OverlayElement * Button::clone(math::Matrix const & m) const + void Button::setController(Controller *controller) { - return new Button(*this); + m_textView->setController(controller); + Element::setController(controller); } vector const & Button::boundRects() const @@ -93,7 +103,23 @@ namespace gui { m_boundRects.clear(); double k = visualScale(); - m2::RectD rc(0, 0, width() * k, height() * k); + + m2::RectD tr(m_textView->roughBoundRect()); + m2::RectD rc(0, 0, tr.SizeX(), tr.SizeY()); + + rc.Inflate(20 * k, 10 * k); + + double dx = 0; + double dy = 0; + + if (rc.SizeX() < m_minWidth * k) + dx = (m_minWidth * k - rc.SizeX()) / 2; + if (rc.SizeY() < m_minHeight * k) + dy = (m_minHeight * k - rc.SizeY()) / 2; + + rc.Inflate(dx, dy); + rc.Offset(-rc.minX(), -rc.minY()); + rc.Offset(tieRect(rc, math::Identity())); m_boundRects.push_back(m2::AnyRectD(rc)); setIsDirtyRect(false); @@ -109,13 +135,29 @@ namespace gui double k = visualScale(); - m2::RectD rc(0, 0, width() * k, height() * k); - rc.Offset(tieRect(rc, m)); - r->drawRoundedRectangle(rc, 10 * k, color(state()), depth() - 1); + r->drawRoundedRectangle(roughBoundRect(), 10 * k, color(state()), depth()); yg::FontDesc desc = font(state()); desc.m_size *= k; - r->drawText(desc, pivot(), position(), text(), depth(), false, false); + m_textView->draw(r, m); + } + + void Button::setPivot(m2::PointD const &pv) + { + m_textView->setPivot(pv); + Element::setPivot(pv); + } + + void Button::setFont(EState state, yg::FontDesc const & font) + { + m_textView->setFont(state, font); + Element::setFont(state, font); + } + + void Button::setColor(EState state, yg::Color const & c) + { + m_textView->setColor(state, c); + Element::setColor(state, c); } } diff --git a/gui/button.hpp b/gui/button.hpp index 5c3ec98bc6..5eedad1ad8 100644 --- a/gui/button.hpp +++ b/gui/button.hpp @@ -1,9 +1,11 @@ #pragma once #include "element.hpp" +#include "text_view.hpp" #include "../std/function.hpp" #include "../std/string.hpp" +#include "../std/scoped_ptr.hpp" namespace yg { @@ -27,18 +29,19 @@ namespace gui TOnClickListener m_OnClickListener; - unsigned m_widthInDIP; - unsigned m_heightInDIP; + unsigned m_minWidth; + unsigned m_minHeight; + + scoped_ptr m_textView; - string m_text; mutable vector m_boundRects; public: struct Params : Element::Params { - unsigned m_width; - unsigned m_height; + unsigned m_minWidth; + unsigned m_minHeight; string m_text; }; @@ -51,19 +54,24 @@ namespace gui void setOnClickListener(TOnClickListener const & l); + void setPivot(m2::PointD const & pv); + void setFont(EState state, yg::FontDesc const & font); + void setColor(EState state, yg::Color const & c); + void setText(string const & text); string const & text() const; - void setWidth(unsigned widthInDIP); - unsigned width() const; + void setMinWidth(unsigned minWidthInDIP); + unsigned minWidth() const; - void setHeight(unsigned heightInDIP); - unsigned height() const; + void setMinHeight(unsigned minHeightInDIP); + unsigned minHeight() const; + + void setController(Controller * controller); /// Inherited from OverlayElement /// @{ - yg::OverlayElement * clone(math::Matrix const & m) const; vector const & boundRects() const; void draw(yg::gl::OverlayRenderer * r, math::Matrix const & m) const; diff --git a/gui/element.hpp b/gui/element.hpp index 201f3feb61..8efbbae5d3 100644 --- a/gui/element.hpp +++ b/gui/element.hpp @@ -52,10 +52,10 @@ namespace gui void setState(EState state); EState state() const; - void setFont(EState state, yg::FontDesc const & font); + virtual void setFont(EState state, yg::FontDesc const & font); yg::FontDesc const & font(EState state) const; - void setColor(EState state, yg::Color const & c); + virtual void setColor(EState state, yg::Color const & c); yg::Color const & color(EState state) const; /// Implement this method to handle single tap on the GUI element. diff --git a/gui/text_view.cpp b/gui/text_view.cpp index 17f960b9f5..68ec235b3c 100644 --- a/gui/text_view.cpp +++ b/gui/text_view.cpp @@ -17,8 +17,12 @@ namespace gui void TextView::setText(string const & text) { - m_text = text; - setIsDirtyDrawing(true); + if (m_text != text) + { + m_text = text; + setIsDirtyDrawing(true); + setIsDirtyRect(true); + } } string const & TextView::text() const @@ -44,12 +48,7 @@ namespace gui m_elem.reset(new yg::StraightTextElement(params)); } - yg::OverlayElement * TextView::clone(math::Matrix const & m) const - { - return new TextView(*this); - } - - void TextView::draw(yg::gl::OverlayRenderer *r, const math::Matrix &m) + void TextView::draw(yg::gl::OverlayRenderer *r, math::Matrix const & m) const { checkDirtyDrawing(); m_elem->draw(r, m); diff --git a/gui/text_view.hpp b/gui/text_view.hpp index 9d8fcd7890..459089e7b9 100644 --- a/gui/text_view.hpp +++ b/gui/text_view.hpp @@ -35,9 +35,8 @@ namespace gui void setText(string const & text); string const & text() const; - yg::OverlayElement * clone(math::Matrix const & m) const; vector const & boundRects() const; - void draw(yg::gl::OverlayRenderer * r, math::Matrix const & m); + void draw(yg::gl::OverlayRenderer * r, math::Matrix const & m) const; bool onTapStarted(m2::PointD const & pt); bool onTapMoved(m2::PointD const & pt); diff --git a/map/country_status_display.cpp b/map/country_status_display.cpp index c488282875..f6a7e5d4af 100644 --- a/map/country_status_display.cpp +++ b/map/country_status_display.cpp @@ -120,8 +120,8 @@ CountryStatusDisplay::CountryStatusDisplay(Params const & p) gui::Button::Params bp; bp.m_depth = yg::maxDepth; - bp.m_width = 200; - bp.m_height = 40; + bp.m_minWidth = 200; + bp.m_minHeight = 40; bp.m_pivot = m2::PointD(0, 0); bp.m_position = yg::EPosCenter; bp.m_text = "Download";