Skip to content

Commit

Permalink
Image UI widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Sep 21, 2023
1 parent d333fb1 commit c7c67d2
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/planet096.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <planet096_app.h>
#include <planet096_colors.h>
#include <planet096_scene.h>
#include <planet096_ui_image.h>
#include <planet096_ui_progress_bar.h>
#include <planet096_ui_scrollable_text.h>
#include <planet096_ui_text.h>
Expand Down
20 changes: 20 additions & 0 deletions src/planet096_bitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PLANET096_BITMAP_H
#define PLANET096_BITMAP_H

#include <Arduino.h>

typedef struct _Planet096Bitmap {
uint8_t *bitmap;
uint8_t width, height;
} Planet096Bitmap;

static inline Planet096Bitmap createBitmap(uint8_t *bitmap, uint8_t width, uint8_t height) {
Planet096Bitmap bitmap_image;
bitmap_image.bitmap = bitmap;
bitmap_image.width = width;
bitmap_image.height = height;

return bitmap_image;
}

#endif
21 changes: 20 additions & 1 deletion src/planet096_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <planet096_app.h>
#include <planet096_scene.h>
#include <planet096_ui_image.h>
#include <planet096_ui_progress_bar.h>
#include <planet096_ui_scrollable_text.h>
#include <planet096_ui_text.h>
Expand Down Expand Up @@ -279,8 +280,12 @@ void Planet096Scene::renderWidget() {
if(this->main_widget.scrollable_text_ui != nullptr &&
this->main_widget.scrollable_text_ui->isUpdated())
this->renderScrollaleTextWidget(this->main_widget.scrollable_text_ui);
if(this->main_widget.progress_bar_ui != nullptr)
if(this->main_widget.progress_bar_ui != nullptr &&
this->main_widget.progress_bar_ui->isUpdated())
this->renderProgressBarWidget(this->main_widget.progress_bar_ui);
if(this->main_widget.image_ui != nullptr &&
this->main_widget.image_ui->isUpdated())
this->renderImageWidget(this->main_widget.image_ui);
}

void Planet096Scene::render() {
Expand Down Expand Up @@ -447,4 +452,18 @@ void Planet096Scene::renderProgressBarWidget(Planet096ProgressBar* progressBarUI

this->display.display();
progressBarUI->hasRendered();
}

void Planet096Scene::renderImageWidget(Planet096Image* imageUI) {
this->getDisplay()->drawBitmap(
imageUI->getX(),
imageUI->getY(),
imageUI->getBitmap().bitmap,
imageUI->getBitmap().width,
imageUI->getBitmap().height,
imageUI->getColor()
);
this->getDisplay()->display();

imageUI->hasRendered();
}
1 change: 1 addition & 0 deletions src/planet096_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Planet096Scene {
void renderTextWidget(Planet096Text* textUI);
void renderScrollaleTextWidget(Planet096ScrollableText* scrollableTextUI);
void renderProgressBarWidget(Planet096ProgressBar* progressBarUI);
void renderImageWidget(Planet096Image* imageUI);
};

#endif
64 changes: 64 additions & 0 deletions src/planet096_ui_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <planet096_scene.h>
#include <planet096_ui_image.h>

Planet096Image::Planet096Image():
scene(nullptr) { }

Planet096Image::Planet096Image(Planet096Scene *scene):
scene(scene) { }

Planet096Image::Planet096Image(Planet096Scene *scene, Planet096Bitmap bitmap):
scene(scene),
bitmap(bitmap) { }

void Planet096Image::setX(uint8_t x) {
this->x = x;
this->invalidate();
}

uint8_t Planet096Image::getX() {
return this->x;
}

void Planet096Image::setY(uint8_t y) {
this->y = y;
this->invalidate();
}

uint8_t Planet096Image::getY() {
return this->y;
}

void Planet096Image::setColor(Planet096Color color) {
this->color = color;
this->invalidate();
}

uint8_t Planet096Image::getColor() {
return this->color;
}

void Planet096Image::setBitmap(Planet096Bitmap bitmap) {
this->bitmap = bitmap;
this->invalidate();
}

Planet096Bitmap Planet096Image::getBitmap() {
return this->bitmap;
}

bool Planet096Image::isUpdated() {
bool is_updated = this->is_updated;
this->is_updated = !is_updated;

return is_updated;
}

void Planet096Image::hasRendered() {
this->has_rendered = true;
}

void Planet096Image::invalidate() {
if(this->has_rendered)
this->scene->renderWidget();
}
44 changes: 44 additions & 0 deletions src/planet096_ui_image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef PLANET096_UI_IMAGE_H
#define PLANET096_UI_IMAGE_H

#include <Arduino.h>
#include <planet096_bitmap.h>
#include <planet096_colors.h>
#include <planet096_scene.h>

class Planet096Scene;

class Planet096Image {
public:
Planet096Image();
Planet096Image(Planet096Scene *scene);
Planet096Image(Planet096Scene *scene, Planet096Bitmap bitmap);

void setX(uint8_t x);
uint8_t getX();

void setY(uint8_t y);
uint8_t getY();

void setColor(Planet096Color color);
uint8_t getColor();

void setBitmap(Planet096Bitmap bitmap);
Planet096Bitmap getBitmap();

bool isUpdated();
void hasRendered();

private:
uint8_t x = 0, y = 0;
Planet096Color color = WHITE;
bool has_rendered = false,
is_updated = true;

Planet096Bitmap bitmap;
Planet096Scene *scene;

void invalidate();
};

#endif
16 changes: 16 additions & 0 deletions src/planet096_widget.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#ifndef PLANET096_WIDGET_H
#define PLANET096_WIDGET_H

#include <planet096_ui_image.h>
#include <planet096_ui_progress_bar.h>
#include <planet096_ui_scrollable_text.h>
#include <planet096_ui_text.h>

typedef struct _Planet096Widget {
Planet096Image* image_ui;
Planet096ProgressBar* progress_bar_ui;
Planet096ScrollableText* scrollable_text_ui;
Planet096Text* text_ui;
} Planet096Widget;

static Planet096Widget createWidgets() {
Planet096Widget widget;
widget.image_ui = nullptr;
widget.progress_bar_ui = nullptr;
widget.scrollable_text_ui = nullptr;
widget.text_ui = nullptr;
Expand All @@ -22,6 +25,7 @@ static Planet096Widget createWidgets() {

static Planet096Widget createWidget(Planet096Text* textUI) {
Planet096Widget widget;
widget.image_ui = nullptr;
widget.progress_bar_ui = nullptr;
widget.scrollable_text_ui = nullptr;
widget.text_ui = textUI;
Expand All @@ -31,6 +35,7 @@ static Planet096Widget createWidget(Planet096Text* textUI) {

static Planet096Widget createWidget(Planet096ScrollableText* scrollableTextUI) {
Planet096Widget widget;
widget.image_ui = nullptr;
widget.progress_bar_ui = nullptr;
widget.scrollable_text_ui = scrollableTextUI;
widget.text_ui = nullptr;
Expand All @@ -40,11 +45,22 @@ static Planet096Widget createWidget(Planet096ScrollableText* scrollableTextUI) {

static Planet096Widget createWidget(Planet096ProgressBar* progressBarUI) {
Planet096Widget widget;
widget.image_ui = nullptr;
widget.progress_bar_ui = progressBarUI;
widget.scrollable_text_ui = nullptr;
widget.text_ui = nullptr;

return widget;
}

static Planet096Widget createWidget(Planet096Image* imageUI) {
Planet096Widget widget;
widget.image_ui = imageUI;
widget.progress_bar_ui = nullptr;
widget.scrollable_text_ui = nullptr;
widget.text_ui = nullptr;

return widget;
}

#endif

0 comments on commit c7c67d2

Please sign in to comment.