[escher] Create a class button

Change-Id: I6236925a4d8720d4e6e1c39483cf904d39d2031b
This commit is contained in:
Émilie Feral
2016-10-12 11:16:30 +02:00
parent c7d5998660
commit 6b16752574
4 changed files with 87 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ SFLAGS += -Iescher/include
objs += $(addprefix escher/src/,\
app.o\
childless_view.o\
button.o\
container.o\
header_view_controller.o\
image_view.o\

View File

@@ -2,6 +2,7 @@
#define ESCHER_H
#include <escher/app.h>
#include <escher/button.h>
#include <escher/container.h>
#include <escher/header_view_controller.h>
#include <escher/image.h>

View File

@@ -0,0 +1,27 @@
#ifndef ESCHER_BUTTON_H
#define ESCHER_BUTTON_H
#include <escher/view.h>
#include <escher/responder.h>
#include <escher/text_view.h>
#include <escher/invocation.h>
class Button : public View, public Responder {
public:
Button(Responder * parentResponder);
void drawRect(KDContext * ctx, KDRect rect) const override;
bool handleEvent(Ion::Events::Event event) override;
void setText(const char * textBody);
void setInvocation(Invocation invocation);
void setBackgroundColor(KDColor backgroundColor);
KDSize textSize();
private:
int numberOfSubviews() const override;
View * subviewAtIndex(int index) override;
void layoutSubviews() override;
TextView m_textView;
Invocation m_invocation;
KDColor m_backgroundColor;
};
#endif

58
escher/src/button.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include <escher/button.h>
#include <assert.h>
Button::Button(Responder * parentResponder) :
Responder(parentResponder),
m_textView(),
m_invocation(Invocation(nullptr, nullptr)),
m_backgroundColor(KDColorWhite)
{
m_textView.setBackgroundColor(KDColorWhite);
m_textView.setTextColor(KDColorBlack);
m_textView.setAlignment(0.5f, 0.5f);
}
void Button::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(rect, m_backgroundColor);
}
int Button::numberOfSubviews() const {
return 1;
}
View * Button::subviewAtIndex(int index) {
assert(index == 0);
return &m_textView;
}
void Button::layoutSubviews() {
m_textView.setFrame(bounds());
}
bool Button::handleEvent(Ion::Events::Event event) {
switch (event) {
case Ion::Events::Event::ENTER:
m_invocation.perform(this);
return true;
default:
return false;
}
}
void Button::setText(const char * textBody) {
m_textView.setText(textBody);
}
void Button::setInvocation(Invocation invocation) {
m_invocation = invocation;
}
void Button::setBackgroundColor(KDColor backgroundColor) {
m_backgroundColor = backgroundColor;
m_textView.setBackgroundColor(backgroundColor);
markRectAsDirty(bounds());
}
KDSize Button::textSize() {
return m_textView.textSize();
}