[escher] Create a class pointer text view inheriting

from text view

Change-Id: I10dd24ed9404e634e90f2bc7fecb6441f7a93550
This commit is contained in:
Émilie Feral
2016-10-14 14:32:16 +02:00
committed by Romain Goyet
parent d14107b2bc
commit 0a6d822046
11 changed files with 68 additions and 34 deletions

View File

@@ -19,7 +19,7 @@ public:
private:
static constexpr KDCoordinate k_iconSize = 32;
ImageView m_iconView;
TextView m_nameView;
PointerTextView m_nameView;
bool m_visible;
bool m_active;
};

View File

@@ -14,6 +14,7 @@ objs += $(addprefix escher/src/,\
list_view.o\
metric.o\
palette.o\
pointer_text_view.o\
responder.o\
scroll_view.o\
scroll_view_indicator.o\

View File

@@ -14,6 +14,7 @@
#include <escher/list_view.h>
#include <escher/metric.h>
#include <escher/palette.h>
#include <escher/pointer_text_view.h>
#include <escher/responder.h>
#include <escher/scroll_view.h>
#include <escher/scroll_view_indicator.h>

View File

@@ -3,7 +3,7 @@
#include <escher/view.h>
#include <escher/responder.h>
#include <escher/text_view.h>
#include <escher/pointer_text_view.h>
#include <escher/invocation.h>
class Button : public View, public Responder {
@@ -19,7 +19,7 @@ private:
int numberOfSubviews() const override;
View * subviewAtIndex(int index) override;
void layoutSubviews() override;
TextView m_textView;
PointerTextView m_pointerTextView;
Invocation m_invocation;
KDColor m_backgroundColor;
};

View File

@@ -0,0 +1,18 @@
#ifndef ESCHER_POINTER_TEXT_VIEW_H
#define ESCHER_POINTER_TEXT_VIEW_H
#include <escher/text_view.h>
class PointerTextView : public TextView {
public:
PointerTextView();
PointerTextView(const char * text, float horizontalAlignment, float verticalAlignment,
KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite);
void setText(const char * text);
protected:
const char * text() const override;
private:
const char * m_textPointer;
};
#endif

View File

@@ -2,7 +2,7 @@
#define ESCHER_TABLE_VIEW_CELL_H
#include <escher/view.h>
#include <escher/text_view.h>
#include <escher/pointer_text_view.h>
#include <escher/palette.h>
#include <escher/metric.h>
@@ -10,7 +10,7 @@
class TableViewCell : public View {
public:
TableViewCell(char * label = nullptr);
TextView * textView();
PointerTextView * textView();
virtual View * contentView() const;
bool isHighlighted() const;
void setHighlighted(bool highlight);
@@ -22,7 +22,7 @@ public:
private:
constexpr static KDCoordinate k_separatorThickness = 1;
bool m_highlighted;
TextView m_textView;
PointerTextView m_pointerTextView;
};
#endif

View File

@@ -10,21 +10,18 @@ public:
// alignment = 0 -> align left or top
// alignment = 0.5 -> align center
// alignment = 1.0 -> align right or bottom
TextView(const char * text,
float horizontalAlignment, float verticalAlignment,
KDColor textColor = KDColorBlack, KDColor backgroundColor = KDColorWhite);
TextView(float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor);
void drawRect(KDContext * ctx, KDRect rect) const override;
void setText(const char * text);
void setBackgroundColor(KDColor backgroundColor);
void setTextColor(KDColor textColor);
void setAlignment(float horizontalAlignment, float verticalAlignment);
KDSize minimalSizeForOptimalDisplay() override;
protected:
virtual const char * text() const = 0;
#if ESCHER_VIEW_LOGGING
const char * className() const override;
#endif
private:
const char * m_text;
float m_horizontalAlignment;
float m_verticalAlignment;
KDColor m_textColor;

View File

@@ -3,7 +3,7 @@
Button::Button(Responder * parentResponder, const char * textBody, Invocation invocation) :
Responder(parentResponder),
m_textView(TextView(textBody, 0.5f, 0.5f)),
m_pointerTextView(PointerTextView(textBody, 0.5f, 0.5f)),
m_invocation(invocation),
m_backgroundColor(KDColorWhite)
{
@@ -19,11 +19,11 @@ int Button::numberOfSubviews() const {
View * Button::subviewAtIndex(int index) {
assert(index == 0);
return &m_textView;
return &m_pointerTextView;
}
void Button::layoutSubviews() {
m_textView.setFrame(bounds());
m_pointerTextView.setFrame(bounds());
}
bool Button::handleEvent(Ion::Events::Event event) {
@@ -38,11 +38,11 @@ bool Button::handleEvent(Ion::Events::Event event) {
void Button::setBackgroundColor(KDColor backgroundColor) {
m_backgroundColor = backgroundColor;
m_textView.setBackgroundColor(backgroundColor);
m_pointerTextView.setBackgroundColor(backgroundColor);
markRectAsDirty(bounds());
}
KDSize Button::minimalSizeForOptimalDisplay() {
KDSize textSize = m_textView.minimalSizeForOptimalDisplay();
KDSize textSize = m_pointerTextView.minimalSizeForOptimalDisplay();
return KDSize(textSize.width() + k_horizontalMargin, textSize.height() + k_verticalMargin);
}

View File

@@ -0,0 +1,23 @@
#include <escher/pointer_text_view.h>
PointerTextView::PointerTextView() :
TextView(),
m_textPointer(nullptr)
{
}
PointerTextView::PointerTextView(const char * text, float horizontalAlignment, float verticalAlignment,
KDColor textColor, KDColor backgroundColor) :
TextView(horizontalAlignment, verticalAlignment, textColor, backgroundColor),
m_textPointer(text)
{
}
const char * PointerTextView::text() const {
return m_textPointer;
}
void PointerTextView::setText(const char * text) {
m_textPointer = text;
markRectAsDirty(bounds());
}

View File

@@ -6,7 +6,7 @@ constexpr KDCoordinate TableViewCell::k_separatorThickness;
TableViewCell::TableViewCell(char * label) :
View(),
m_highlighted(false),
m_textView(TextView(label, 0, 0.5, KDColorBlack, Palette::CellBackgroundColor))
m_pointerTextView(PointerTextView(label, 0, 0.5, KDColorBlack, Palette::CellBackgroundColor))
{
}
@@ -19,7 +19,7 @@ int TableViewCell::numberOfSubviews() const {
View * TableViewCell::subviewAtIndex(int index) {
if (index == 0) {
return &m_textView;
return &m_pointerTextView;
}
assert(numberOfSubviews() == 2 && index == 1);
return contentView();
@@ -28,15 +28,15 @@ View * TableViewCell::subviewAtIndex(int index) {
void TableViewCell::layoutSubviews() {
KDCoordinate width = bounds().width();
KDCoordinate height = bounds().height();
m_textView.setFrame(KDRect(k_separatorThickness, k_separatorThickness, 3*width/4 - 2*k_separatorThickness, height - 2*k_separatorThickness));
m_pointerTextView.setFrame(KDRect(k_separatorThickness, k_separatorThickness, 3*width/4 - 2*k_separatorThickness, height - 2*k_separatorThickness));
View * content = contentView();
if (content) {
content->setFrame(KDRect(k_separatorThickness + 3*width/4, k_separatorThickness, width/4-2*k_separatorThickness, height-2*k_separatorThickness));
}
}
TextView * TableViewCell::textView() {
return &m_textView;
PointerTextView * TableViewCell::textView() {
return &m_pointerTextView;
}
View * TableViewCell::contentView() const {
@@ -50,7 +50,7 @@ bool TableViewCell::isHighlighted() const {
void TableViewCell::setHighlighted(bool highlight) {
m_highlighted = highlight;
KDColor backgroundColor = highlight? Palette::FocusCellBackgroundColor : Palette::CellBackgroundColor;
m_textView.setBackgroundColor(backgroundColor);
m_pointerTextView.setBackgroundColor(backgroundColor);
markRectAsDirty(bounds());
}

View File

@@ -1,13 +1,12 @@
#include <escher/text_view.h>
TextView::TextView() : TextView(nullptr, 0.0f, 0.0f, KDColorBlack, KDColorWhite)
TextView::TextView() : TextView(0.0f, 0.0f, KDColorBlack, KDColorWhite)
{
}
TextView::TextView(const char * text, float horizontalAlignment, float verticalAlignment,
TextView::TextView(float horizontalAlignment, float verticalAlignment,
KDColor textColor, KDColor backgroundColor) :
ChildlessView(),
m_text(text),
m_horizontalAlignment(horizontalAlignment),
m_verticalAlignment(verticalAlignment),
m_textColor(textColor),
@@ -15,11 +14,6 @@ TextView::TextView(const char * text, float horizontalAlignment, float verticalA
{
}
void TextView::setText(const char * text) {
m_text = text;
markRectAsDirty(bounds());
}
void TextView::setBackgroundColor(KDColor backgroundColor) {
m_backgroundColor = backgroundColor;
markRectAsDirty(bounds());
@@ -37,19 +31,19 @@ void TextView::setAlignment(float horizontalAlignment, float verticalAlignment)
}
KDSize TextView::minimalSizeForOptimalDisplay() {
return KDText::stringSize(m_text);
return KDText::stringSize(text());
}
void TextView::drawRect(KDContext * ctx, KDRect rect) const {
if (m_text == nullptr) {
if (text() == nullptr) {
return;
}
KDSize textSize = KDText::stringSize(m_text);
KDSize textSize = KDText::stringSize(text());
KDPoint origin = {
(KDCoordinate)(m_horizontalAlignment*(m_frame.width() - textSize.width())),
(KDCoordinate)(m_verticalAlignment*(m_frame.height() - textSize.height()))
};
ctx->drawString(m_text, origin, m_textColor, m_backgroundColor);
ctx->drawString(text(), origin, m_textColor, m_backgroundColor);
}
#if ESCHER_VIEW_LOGGING