Escher: Adding a GUI toolkit

Change-Id: I9c78cc1afdd35968110484c3e3782c866b88c671
This commit is contained in:
Romain Goyet
2016-04-12 18:47:36 +02:00
parent 33226cc3a9
commit a21f201cc0
7 changed files with 146 additions and 0 deletions

View File

@@ -59,6 +59,7 @@ endif
include ion/Makefile
include kandinsky/Makefile
include poincare/Makefile
include escher/Makefile
include app/Makefile
include quiz/Makefile # Quiz should be included at the end

6
escher/Makefile Normal file
View File

@@ -0,0 +1,6 @@
SFLAGS += -Iescher/include
objs += $(addprefix escher/src/,\
solid_color_view.o\
view.o\
)

7
escher/include/escher.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef ESCHER_H
#define ESCHER_H
#include <escher/solid_color_view.h>
#include <escher/view.h>
#endif

View File

@@ -0,0 +1,14 @@
#ifndef ESCHER_SOLID_COLOR_VIEW_H
#define ESCHER_SOLID_COLOR_VIEW_H
#include <escher/view.h>
class SolidColorView : public View {
public:
SolidColorView(KDRect frame, KDColor color);
void drawRect(KDRect rect) override;
private:
KDColor m_color;
};
#endif

View File

@@ -0,0 +1,31 @@
#ifndef ESCHER_VIEW_H
#define ESCHER_VIEW_H
extern "C" {
#include <stdint.h>
#include <kandinsky.h>
}
class View {
public:
View(KDRect frame);
~View();
void draw();
virtual void drawRect(KDRect rect);
void addSubview(View * subview);
void removeFromSuperview();
void setFrame(KDRect frame);
protected:
KDRect bounds();
private:
void setOriginAndDrawRect(KDRect rect);
KDPoint absoluteOrigin();
//TODO: We may want a dynamic size at some point
static constexpr uint8_t k_maxNumberOfSubviews = 4;
KDRect m_frame;
View * m_superview;
View * m_subviews[k_maxNumberOfSubviews];
uint8_t m_numberOfSubviews;
};
#endif

View File

@@ -0,0 +1,11 @@
#include <escher/solid_color_view.h>
SolidColorView::SolidColorView(KDRect frame, KDColor color) :
View(frame),
m_color(color)
{
}
void SolidColorView::drawRect(KDRect rect) {
KDFillRect(rect, m_color);
}

76
escher/src/view.cpp Normal file
View File

@@ -0,0 +1,76 @@
extern "C" {
#include <assert.h>
}
#include <escher/view.h>
View::View(KDRect frame) :
m_frame(frame),
m_superview(nullptr),
m_numberOfSubviews(0)
{
}
View::~View() {
}
void View::drawRect(KDRect rect) {
}
void View::draw() {
setOriginAndDrawRect(m_frame);
for (uint8_t i=0; i<m_numberOfSubviews; i++) {
View * subview = m_subviews[i];
subview->draw();
}
}
void View::addSubview(View * subview) {
assert(m_numberOfSubviews < k_maxNumberOfSubviews);
subview->m_superview = this;
m_subviews[m_numberOfSubviews++] = subview;
}
void View::removeFromSuperview() {
assert(m_superview != nullptr);
assert(false); // FIXME: Unimplemented
}
void View::setFrame(KDRect frame) {
// TODO: Return if frame is equal to m_frame
if (m_superview != nullptr) {
// We're moving this view
// So let's ask its superview to redraw itself where that view previously was
m_superview->setOriginAndDrawRect(m_frame);
}
m_frame = frame;
if (m_superview != nullptr) {
// We've been moved
// Draw ourself at the new location
setOriginAndDrawRect(bounds());
}
}
KDRect View::bounds() {
KDRect bounds = m_frame;
bounds.x = 0;
bounds.y = 0;
return bounds;
}
void View::setOriginAndDrawRect(KDRect rect) {
KDSetOrigin(absoluteOrigin());
this->drawRect(rect);
}
KDPoint View::absoluteOrigin() {
if (m_superview == nullptr) {
return m_frame.origin;
} else {
KDPoint parentOrigin = m_superview->absoluteOrigin();
KDPoint myOrigin = m_frame.origin;
KDPoint result;
result.x = parentOrigin.x + myOrigin.x;
result.y = parentOrigin.y + myOrigin.y;
return result;
}
}