From 84824a504f7a913abc067a22e8c10dc97f103270 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 3 Feb 2016 15:48:50 -0800 Subject: [PATCH] Add UI Image and ImageButton classes. --- firmware/common/ui_widget.cpp | 93 +++++++++++++++++++++++++++++++++++ firmware/common/ui_widget.hpp | 37 ++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/firmware/common/ui_widget.cpp b/firmware/common/ui_widget.cpp index 6a184576e..bc1883603 100644 --- a/firmware/common/ui_widget.cpp +++ b/firmware/common/ui_widget.cpp @@ -420,6 +420,99 @@ bool Button::on_touch(const TouchEvent event) { #endif } +/* Image *****************************************************************/ + +Image::Image( +) : Image { { }, nullptr, Color::white(), Color::black() } +{ +} + +Image::Image( + const Rect parent_rect, + const Bitmap* bitmap, + const Color foreground, + const Color background +) : Widget { parent_rect }, + bitmap_ { bitmap }, + foreground_ { foreground }, + background_ { background } +{ +} + +void Image::set_bitmap(const Bitmap* bitmap) { + bitmap_ = bitmap; + set_dirty(); +} + +void Image::set_foreground(const Color color) { + foreground_ = color; + set_dirty(); +} + +void Image::set_background(const Color color) { + background_ = color; + set_dirty(); +} + +void Image::paint(Painter& painter) { + if( bitmap_ ) { + // Code also handles ImageButton behavior. + const bool selected = (has_focus() || flags.highlighted); + painter.draw_bitmap( + screen_pos(), + *bitmap_, + selected ? background_ : foreground_, + selected ? foreground_ : background_ + ); + } +} + +/* ImageButton ***********************************************************/ + +// TODO: Virtually all this code is duplicated from Button. Base class? + +ImageButton::ImageButton( + const Rect parent_rect, + const Bitmap* bitmap, + const Color foreground, + const Color background +) : Image { parent_rect, bitmap, foreground, background } +{ + flags.focusable = true; +} + +bool ImageButton::on_key(const KeyEvent key) { + if( key == KeyEvent::Select ) { + if( on_select ) { + on_select(*this); + return true; + } + } + + return false; +} + +bool ImageButton::on_touch(const TouchEvent event) { + switch(event.type) { + case TouchEvent::Type::Start: + flags.highlighted = true; + set_dirty(); + return true; + + + case TouchEvent::Type::End: + flags.highlighted = false; + set_dirty(); + if( on_select ) { + on_select(*this); + } + return true; + + default: + return false; + } +} + /* OptionsField **********************************************************/ OptionsField::OptionsField( diff --git a/firmware/common/ui_widget.hpp b/firmware/common/ui_widget.hpp index ddecaae26..80d3de46d 100644 --- a/firmware/common/ui_widget.hpp +++ b/firmware/common/ui_widget.hpp @@ -218,6 +218,43 @@ private: std::string text_; }; +class Image : public Widget { +public: + Image(); + Image( + const Rect parent_rect, + const Bitmap* bitmap, + const Color foreground, + const Color background + ); + + void set_bitmap(const Bitmap* bitmap); + void set_foreground(const Color color); + void set_background(const Color color); + + void paint(Painter& painter) override; + +private: + const Bitmap* bitmap_; + Color foreground_; + Color background_; +}; + +class ImageButton : public Image { +public: + std::function on_select; + + ImageButton( + const Rect parent_rect, + const Bitmap* bitmap, + const Color foreground, + const Color background + ); + + bool on_key(const KeyEvent key) override; + bool on_touch(const TouchEvent event) override; +}; + class OptionsField : public Widget { public: using name_t = std::string;