using TextView inside Button to draw text uniformly.

This commit is contained in:
rachytski 2012-06-05 01:22:19 +04:00 committed by Alex Zolotarev
parent 95c4ede478
commit eddeffa5b7
6 changed files with 91 additions and 43 deletions

View file

@ -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<double, 3, 3> const & m) const
void Button::setController(Controller *controller)
{
return new Button(*this);
m_textView->setController(controller);
Element::setController(controller);
}
vector<m2::AnyRectD> 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<double, 3>()));
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);
}
}

View file

@ -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<TextView> m_textView;
string m_text;
mutable vector<m2::AnyRectD> 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<double, 3, 3> const & m) const;
vector<m2::AnyRectD> const & boundRects() const;
void draw(yg::gl::OverlayRenderer * r, math::Matrix<double, 3, 3> const & m) const;

View file

@ -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.

View file

@ -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<double, 3, 3> const & m) const
{
return new TextView(*this);
}
void TextView::draw(yg::gl::OverlayRenderer *r, const math::Matrix<double, 3, 3> &m)
void TextView::draw(yg::gl::OverlayRenderer *r, math::Matrix<double, 3, 3> const & m) const
{
checkDirtyDrawing();
m_elem->draw(r, m);

View file

@ -35,9 +35,8 @@ namespace gui
void setText(string const & text);
string const & text() const;
yg::OverlayElement * clone(math::Matrix<double, 3, 3> const & m) const;
vector<m2::AnyRectD> const & boundRects() const;
void draw(yg::gl::OverlayRenderer * r, math::Matrix<double, 3, 3> const & m);
void draw(yg::gl::OverlayRenderer * r, math::Matrix<double, 3, 3> const & m) const;
bool onTapStarted(m2::PointD const & pt);
bool onTapMoved(m2::PointD const & pt);

View file

@ -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";