[escher] Clean TextView

This commit is contained in:
Léa Saviot
2019-01-23 14:53:23 +01:00
committed by Émilie Feral
parent 0fdf6665ad
commit 03446bc9d1
2 changed files with 16 additions and 25 deletions

View File

@@ -4,13 +4,20 @@
#include <escher/view.h>
#include <kandinsky/color.h>
/* alignment = 0 -> align left or top
* alignment = 0.5 -> align center
* alignment = 1.0 -> align right or bottom */
class TextView : public View {
public:
// alignment = 0 -> align left or top
// alignment = 0.5 -> align center
// alignment = 1.0 -> align right or bottom
TextView(const KDFont * font = KDFont::LargeFont, float horizontalAlignment = 0.0f, float verticalAlignment = 0.0f,
KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite);
TextView(const KDFont * font = KDFont::LargeFont, float horizontalAlignment = 0.0f, float verticalAlignment = 0.0f, KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite) :
View(),
m_font(font),
m_horizontalAlignment(horizontalAlignment),
m_verticalAlignment(verticalAlignment),
m_textColor(textColor),
m_backgroundColor(backgroundColor)
{}
void drawRect(KDContext * ctx, KDRect rect) const override;
void setBackgroundColor(KDColor backgroundColor);
void setTextColor(KDColor textColor);
@@ -22,7 +29,7 @@ public:
void setFont(const KDFont * font);
protected:
#if ESCHER_VIEW_LOGGING
const char * className() const override;
const char * className() const override { return "TextView"; }
#endif
const KDFont * m_font;
float m_horizontalAlignment;

View File

@@ -1,16 +1,5 @@
#include <escher/text_view.h>
TextView::TextView(const KDFont * font, float horizontalAlignment, float verticalAlignment,
KDColor textColor, KDColor backgroundColor) :
View(),
m_font(font),
m_horizontalAlignment(horizontalAlignment),
m_verticalAlignment(verticalAlignment),
m_textColor(textColor),
m_backgroundColor(backgroundColor)
{
}
void TextView::setBackgroundColor(KDColor backgroundColor) {
m_backgroundColor = backgroundColor;
markRectAsDirty(bounds());
@@ -44,14 +33,9 @@ void TextView::drawRect(KDContext * ctx, KDRect rect) const {
return;
}
KDSize textSize = m_font->stringSize(text());
KDPoint origin(m_horizontalAlignment*(m_frame.width() - textSize.width()),
m_verticalAlignment*(m_frame.height() - textSize.height()));
KDPoint origin(
m_horizontalAlignment * (m_frame.width() - textSize.width()),
m_verticalAlignment * (m_frame.height() - textSize.height()));
ctx->fillRect(bounds(), m_backgroundColor);
ctx->drawString(text(), origin, m_font, m_textColor, m_backgroundColor);
}
#if ESCHER_VIEW_LOGGING
const char * TextView::className() const {
return "TextView";
}
#endif