2016-08-06 02:49:45 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
#include "ui_widget.hpp"
|
|
|
|
#include "ui_painter.hpp"
|
2016-02-05 11:40:14 -05:00
|
|
|
#include "portapack.hpp"
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
#include "string_format.hpp"
|
|
|
|
|
2016-02-05 11:40:14 -05:00
|
|
|
using namespace portapack;
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
namespace ui {
|
|
|
|
|
2015-08-15 00:17:23 -04:00
|
|
|
static bool ui_dirty = true;
|
|
|
|
|
|
|
|
void dirty_set() {
|
|
|
|
ui_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dirty_clear() {
|
|
|
|
ui_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_dirty() {
|
|
|
|
return ui_dirty;
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* Widget ****************************************************************/
|
|
|
|
|
2016-02-07 14:24:25 -05:00
|
|
|
const std::vector<Widget*> Widget::no_children { };
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
Point Widget::screen_pos() {
|
2016-11-28 14:25:27 -05:00
|
|
|
return screen_rect().location();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Size Widget::size() const {
|
2016-12-06 12:28:48 -05:00
|
|
|
return _parent_rect.size();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:05:11 -04:00
|
|
|
Rect Widget::screen_rect() const {
|
2016-12-06 12:28:48 -05:00
|
|
|
return parent() ? (parent_rect() + parent()->screen_pos()) : parent_rect();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect Widget::parent_rect() const {
|
|
|
|
return _parent_rect;
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_parent_rect(const Rect new_parent_rect) {
|
2016-12-06 12:28:48 -05:00
|
|
|
_parent_rect = new_parent_rect;
|
2015-07-08 11:39:24 -04:00
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::parent() const {
|
|
|
|
return parent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_parent(Widget* const widget) {
|
2016-09-05 18:04:28 -04:00
|
|
|
if( widget == parent_ ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
if( parent_ && !widget ) {
|
|
|
|
// We have a parent, but are losing it. Update visible status.
|
2016-09-05 18:04:28 -04:00
|
|
|
dirty_overlapping_children_in_rect(screen_rect());
|
2015-07-08 11:39:24 -04:00
|
|
|
visible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_ = widget;
|
2016-09-05 18:04:28 -04:00
|
|
|
|
|
|
|
set_dirty();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_dirty() {
|
|
|
|
flags.dirty = true;
|
2015-08-15 00:17:23 -04:00
|
|
|
dirty_set();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::dirty() const {
|
|
|
|
return flags.dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_clean() {
|
|
|
|
flags.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::hidden(bool hide) {
|
|
|
|
if( hide != flags.hidden ) {
|
|
|
|
flags.hidden = hide;
|
|
|
|
|
|
|
|
// If parent is hidden, either of these is a no-op.
|
|
|
|
if( hide ) {
|
2016-01-31 03:34:24 -05:00
|
|
|
// TODO: Instead of dirtying parent entirely, dirty only children
|
|
|
|
// that overlap with this widget.
|
2016-12-06 12:28:48 -05:00
|
|
|
parent()->dirty_overlapping_children_in_rect(parent_rect());
|
2015-07-08 11:39:24 -04:00
|
|
|
/* TODO: Notify self and all non-hidden children that they're
|
|
|
|
* now effectively hidden?
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
set_dirty();
|
|
|
|
/* TODO: Notify self and all non-hidden children that they're
|
|
|
|
* now effectively shown?
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::focus() {
|
2015-11-09 14:56:40 -05:00
|
|
|
context().focus_manager().set_focus_widget(this);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::on_focus() {
|
|
|
|
//set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::blur() {
|
2015-11-09 14:56:40 -05:00
|
|
|
context().focus_manager().set_focus_widget(nullptr);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::on_blur() {
|
|
|
|
//set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::focusable() const {
|
|
|
|
return flags.focusable;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:32:38 -05:00
|
|
|
void Widget::set_focusable(const bool value) {
|
|
|
|
flags.focusable = value;
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::has_focus() {
|
2015-11-09 14:56:40 -05:00
|
|
|
return (context().focus_manager().focus_widget() == this);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::on_key(const KeyEvent event) {
|
|
|
|
(void)event;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::on_encoder(const EncoderEvent event) {
|
|
|
|
(void)event;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::on_touch(const TouchEvent event) {
|
|
|
|
(void)event;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-07 14:24:25 -05:00
|
|
|
const std::vector<Widget*>& Widget::children() const {
|
|
|
|
return no_children;
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Context& Widget::context() const {
|
|
|
|
return parent()->context();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_style(const Style* new_style) {
|
|
|
|
if( new_style != style_ ) {
|
|
|
|
style_ = new_style;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Style& Widget::style() const {
|
|
|
|
return style_ ? *style_ : parent()->style();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::visible(bool v) {
|
|
|
|
if( v != flags.visible ) {
|
|
|
|
flags.visible = v;
|
|
|
|
|
|
|
|
/* TODO: This on_show/on_hide implementation seems inelegant.
|
|
|
|
* But I need *some* way to take/configure resources when
|
|
|
|
* a widget becomes visible, and reverse the process when the
|
|
|
|
* widget becomes invisible, whether the widget (or parent) is
|
|
|
|
* hidden, or the widget (or parent) is removed from the tree.
|
|
|
|
*/
|
|
|
|
if( v ) {
|
|
|
|
on_show();
|
|
|
|
} else {
|
|
|
|
on_hide();
|
|
|
|
|
|
|
|
// Set all children invisible too.
|
|
|
|
for(const auto child : children()) {
|
|
|
|
child->visible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:32:38 -05:00
|
|
|
bool Widget::highlighted() const {
|
|
|
|
return flags.highlighted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_highlighted(const bool value) {
|
|
|
|
flags.highlighted = value;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
void Widget::dirty_overlapping_children_in_rect(const Rect& child_rect) {
|
|
|
|
for(auto child : children()) {
|
2016-12-06 12:28:48 -05:00
|
|
|
if( !child_rect.intersect(child->parent_rect()).is_empty() ) {
|
2016-01-31 03:34:24 -05:00
|
|
|
child->set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* View ******************************************************************/
|
|
|
|
|
|
|
|
void View::paint(Painter& painter) {
|
|
|
|
painter.fill_rectangle(
|
|
|
|
screen_rect(),
|
|
|
|
style().background
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void View::add_child(Widget* const widget) {
|
2015-10-16 22:34:22 -04:00
|
|
|
if( widget ) {
|
|
|
|
if( widget->parent() == nullptr ) {
|
|
|
|
widget->set_parent(this);
|
|
|
|
children_.push_back(widget);
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:53:04 -04:00
|
|
|
void View::add_children(const std::initializer_list<Widget*> children) {
|
|
|
|
children_.insert(std::end(children_), children);
|
2015-07-08 11:39:24 -04:00
|
|
|
for(auto child : children) {
|
2016-09-05 17:53:04 -04:00
|
|
|
child->set_parent(this);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void View::remove_child(Widget* const widget) {
|
2015-10-16 22:34:22 -04:00
|
|
|
if( widget ) {
|
|
|
|
children_.erase(std::remove(children_.begin(), children_.end(), widget), children_.end());
|
|
|
|
widget->set_parent(nullptr);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
void View::remove_children(const std::vector<Widget*>& children) {
|
|
|
|
for(auto child : children) {
|
|
|
|
remove_child(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 14:24:25 -05:00
|
|
|
const std::vector<Widget*>& View::children() const {
|
2015-07-08 11:39:24 -04:00
|
|
|
return children_;
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
std::string View::title() const {
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* Rectangle *************************************************************/
|
|
|
|
|
2016-05-25 14:32:06 -04:00
|
|
|
Rectangle::Rectangle(
|
|
|
|
Color c
|
|
|
|
) : Widget { },
|
|
|
|
color { c }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
Rectangle::Rectangle(
|
|
|
|
Rect parent_rect,
|
|
|
|
Color c
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
color { c }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void Rectangle::set_color(const Color c) {
|
|
|
|
color = c;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:52:51 -04:00
|
|
|
void Rectangle::set_outline(const bool outline) {
|
|
|
|
_outline = outline;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void Rectangle::paint(Painter& painter) {
|
2016-07-28 22:52:51 -04:00
|
|
|
if (!_outline) {
|
|
|
|
painter.fill_rectangle(
|
|
|
|
screen_rect(),
|
|
|
|
color
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
painter.draw_rectangle(
|
|
|
|
screen_rect(),
|
|
|
|
color
|
|
|
|
);
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Text ******************************************************************/
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
Text::Text(
|
|
|
|
Rect parent_rect,
|
|
|
|
std::string text
|
|
|
|
) : Widget { parent_rect },
|
2015-12-17 01:35:26 -05:00
|
|
|
text { text }
|
2016-01-31 03:34:24 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Text::Text(
|
|
|
|
Rect parent_rect
|
|
|
|
) : Text { parent_rect, { } }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void Text::set(const std::string value) {
|
|
|
|
text = value;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::paint(Painter& painter) {
|
2016-01-31 03:34:24 -05:00
|
|
|
const auto rect = screen_rect();
|
2016-02-04 04:27:53 -05:00
|
|
|
const auto s = style();
|
2016-01-31 03:34:24 -05:00
|
|
|
|
|
|
|
painter.fill_rectangle(rect, s.background);
|
2016-01-31 02:37:08 -05:00
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.draw_string(
|
2016-11-28 14:25:27 -05:00
|
|
|
rect.location(),
|
2016-02-04 04:27:53 -05:00
|
|
|
s,
|
2015-07-08 11:39:24 -04:00
|
|
|
text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-11 23:05:21 -05:00
|
|
|
/* Labels ****************************************************************/
|
|
|
|
|
|
|
|
Labels::Labels(
|
2017-02-12 02:23:31 -05:00
|
|
|
std::initializer_list<Label> labels
|
2017-02-11 23:05:21 -05:00
|
|
|
) : labels_ { labels }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 02:23:31 -05:00
|
|
|
void Labels::set_labels(std::initializer_list<Label> labels) {
|
2017-02-11 23:05:21 -05:00
|
|
|
labels_ = labels;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Labels::paint(Painter& painter) {
|
2017-02-12 02:23:31 -05:00
|
|
|
for (auto &label : labels_) {
|
|
|
|
painter.draw_string(
|
|
|
|
label.pos + screen_pos(),
|
|
|
|
style().font,
|
|
|
|
label.color,
|
|
|
|
style().background,
|
|
|
|
label.text
|
|
|
|
);
|
|
|
|
}
|
2017-02-11 23:05:21 -05:00
|
|
|
}
|
|
|
|
|
2016-05-11 17:57:22 -04:00
|
|
|
/* BigFrequency **********************************************************/
|
|
|
|
|
|
|
|
BigFrequency::BigFrequency(
|
|
|
|
Rect parent_rect,
|
|
|
|
rf::Frequency frequency
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
_frequency { frequency }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BigFrequency::set(const rf::Frequency frequency) {
|
|
|
|
_frequency = frequency;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BigFrequency::paint(Painter& painter) {
|
2017-03-13 00:09:21 -04:00
|
|
|
uint32_t i, digit_def;
|
2016-05-11 17:57:22 -04:00
|
|
|
char digits[7];
|
|
|
|
char digit;
|
|
|
|
Coord digit_x, digit_y;
|
2016-05-16 06:02:45 -04:00
|
|
|
ui::Color segment_color;
|
2016-05-11 17:57:22 -04:00
|
|
|
|
|
|
|
const auto rect = screen_rect();
|
|
|
|
|
|
|
|
// Erase
|
2017-01-15 22:45:44 -05:00
|
|
|
painter.fill_rectangle({{0, rect.location().y()}, {240, 52}}, ui::Color::black());
|
2016-05-11 17:57:22 -04:00
|
|
|
|
|
|
|
if (!_frequency) {
|
2016-05-16 06:02:45 -04:00
|
|
|
for (i = 0; i < 7; i++) // ----.------
|
2016-05-11 17:57:22 -04:00
|
|
|
digits[i] = 10;
|
|
|
|
} else {
|
|
|
|
_frequency /= 1000; // GMMM.KKKuuu
|
|
|
|
|
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
digits[6 - i] = _frequency % 10;
|
|
|
|
_frequency /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove leading zeros
|
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
if (!digits[i])
|
|
|
|
digits[i] = 16; // "Don't draw" code
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 06:02:45 -04:00
|
|
|
|
|
|
|
segment_color = style().foreground;
|
2016-05-11 17:57:22 -04:00
|
|
|
|
|
|
|
// Draw
|
2017-01-15 22:45:44 -05:00
|
|
|
digit_x = rect.location().x(); // 7 * 32 + 8 = 232 (4 px margins)
|
2016-05-11 17:57:22 -04:00
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
digit = digits[i];
|
2017-01-15 22:45:44 -05:00
|
|
|
digit_y = rect.location().y();
|
2016-05-11 17:57:22 -04:00
|
|
|
if (digit < 16) {
|
2017-03-13 00:09:21 -04:00
|
|
|
digit_def = segment_font[(uint8_t)digit];
|
2016-07-27 23:25:33 -04:00
|
|
|
if (digit_def & 0x01) painter.fill_rectangle({{digit_x + 4, digit_y}, {20, 4}}, segment_color);
|
|
|
|
if (digit_def & 0x02) painter.fill_rectangle({{digit_x + 24, digit_y + 4}, {4, 20}}, segment_color);
|
|
|
|
if (digit_def & 0x04) painter.fill_rectangle({{digit_x + 24, digit_y + 28}, {4, 20}}, segment_color);
|
|
|
|
if (digit_def & 0x08) painter.fill_rectangle({{digit_x + 4, digit_y + 48}, {20, 4}}, segment_color);
|
|
|
|
if (digit_def & 0x10) painter.fill_rectangle({{digit_x, digit_y + 28}, {4, 20}}, segment_color);
|
|
|
|
if (digit_def & 0x20) painter.fill_rectangle({{digit_x, digit_y + 4}, {4, 20}}, segment_color);
|
|
|
|
if (digit_def & 0x40) painter.fill_rectangle({{digit_x + 4, digit_y + 24}, {20, 4}}, segment_color);
|
2016-05-11 17:57:22 -04:00
|
|
|
}
|
|
|
|
if (i == 3) {
|
|
|
|
// Dot
|
2016-05-16 06:02:45 -04:00
|
|
|
painter.fill_rectangle({{digit_x + 34, digit_y + 48}, {4, 4}}, segment_color);
|
2016-05-11 17:57:22 -04:00
|
|
|
digit_x += 40;
|
|
|
|
} else {
|
|
|
|
digit_x += 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:42:20 -04:00
|
|
|
/* ProgressBar ***********************************************************/
|
|
|
|
|
|
|
|
ProgressBar::ProgressBar(
|
|
|
|
Rect parent_rect
|
|
|
|
) : Widget { parent_rect }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:11:24 -04:00
|
|
|
void ProgressBar::set_max(const uint32_t max) {
|
2016-07-27 18:08:05 -04:00
|
|
|
_value = 0;
|
|
|
|
_max = max;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:11:24 -04:00
|
|
|
void ProgressBar::set_value(const uint32_t value) {
|
2016-07-27 18:08:05 -04:00
|
|
|
if (value > _max)
|
|
|
|
_value = _max;
|
2016-05-09 14:42:20 -04:00
|
|
|
else
|
|
|
|
_value = value;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressBar::paint(Painter& painter) {
|
2017-01-16 08:36:28 -05:00
|
|
|
int v_scaled;
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
const auto sr = screen_rect();
|
2016-05-09 14:42:20 -04:00
|
|
|
const auto s = style();
|
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
v_scaled = (sr.size().width() * _value) / _max;
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
painter.fill_rectangle({sr.location(), {v_scaled, sr.size().height()}}, style().foreground);
|
|
|
|
painter.fill_rectangle({{sr.location().x() + v_scaled, sr.location().y()}, {sr.size().width() - v_scaled, sr.size().height()}}, s.background);
|
2016-05-09 14:42:20 -04:00
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
painter.draw_rectangle(sr, s.foreground);
|
2016-05-09 14:42:20 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 22:52:51 -04:00
|
|
|
/* Console ***************************************************************/
|
|
|
|
|
|
|
|
Console::Console(
|
|
|
|
Rect parent_rect
|
|
|
|
) : Widget { parent_rect }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::clear() {
|
|
|
|
display.fill_rectangle(
|
|
|
|
screen_rect(),
|
|
|
|
Color::black()
|
|
|
|
);
|
|
|
|
pos = { 0, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::write(std::string message) {
|
|
|
|
if (visible) {
|
|
|
|
const Style& s = style();
|
|
|
|
const Font& font = s.font;
|
|
|
|
const auto rect = screen_rect();
|
|
|
|
for(const auto c : message) {
|
|
|
|
if( c == '\n' ) {
|
|
|
|
crlf();
|
|
|
|
} else {
|
|
|
|
const auto glyph = font.glyph(c);
|
|
|
|
const auto advance = glyph.advance();
|
2017-01-15 22:45:44 -05:00
|
|
|
if( (pos.x() + advance.x()) > rect.width() ) {
|
2016-07-28 22:52:51 -04:00
|
|
|
crlf();
|
|
|
|
}
|
|
|
|
const Point pos_glyph {
|
2017-01-15 22:45:44 -05:00
|
|
|
rect.left() + pos.x(),
|
|
|
|
display.scroll_area_y(pos.y())
|
2016-07-28 22:52:51 -04:00
|
|
|
};
|
|
|
|
display.draw_glyph(pos_glyph, glyph, s.foreground, s.background);
|
2017-01-15 22:45:44 -05:00
|
|
|
pos += { advance.x(), 0 };
|
2016-07-28 22:52:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer = message;
|
|
|
|
} else {
|
|
|
|
buffer += message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::writeln(std::string message) {
|
|
|
|
write(message);
|
|
|
|
crlf();
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
void Console::paint(Painter&) {
|
2016-07-28 22:52:51 -04:00
|
|
|
write(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::on_show() {
|
|
|
|
const auto screen_r = screen_rect();
|
|
|
|
display.scroll_set_area(screen_r.top(), screen_r.bottom());
|
2017-01-16 08:36:28 -05:00
|
|
|
display.scroll_set_position(0);
|
2016-07-28 22:52:51 -04:00
|
|
|
clear();
|
|
|
|
visible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::on_hide() {
|
|
|
|
/* TODO: Clear region to eliminate brief flash of content at un-shifted
|
|
|
|
* position?
|
|
|
|
*/
|
|
|
|
display.scroll_disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::crlf() {
|
|
|
|
const Style& s = style();
|
|
|
|
const auto sr = screen_rect();
|
|
|
|
const auto line_height = s.font.line_height();
|
2017-01-15 22:45:44 -05:00
|
|
|
pos = { 0, pos.y() + line_height };
|
|
|
|
const int32_t y_excess = pos.y() + line_height - sr.height();
|
2016-07-28 22:52:51 -04:00
|
|
|
if( y_excess > 0 ) {
|
|
|
|
display.scroll(-y_excess);
|
2017-01-15 22:45:44 -05:00
|
|
|
pos = { pos.x(), pos.y() - y_excess };
|
2016-07-28 22:52:51 -04:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
const Rect dirty { sr.left(), display.scroll_area_y(pos.y()), sr.width(), line_height };
|
2016-07-28 22:52:51 -04:00
|
|
|
display.fill_rectangle(dirty, s.background);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
/* Checkbox **************************************************************/
|
|
|
|
|
2016-02-05 11:40:14 -05:00
|
|
|
Checkbox::Checkbox(
|
|
|
|
Point parent_pos,
|
|
|
|
size_t length,
|
2017-01-16 08:36:28 -05:00
|
|
|
std::string text,
|
|
|
|
bool small
|
|
|
|
) : Widget { },
|
|
|
|
text_ { text },
|
|
|
|
small_ { small }
|
2016-02-05 11:40:14 -05:00
|
|
|
{
|
2017-01-16 08:36:28 -05:00
|
|
|
if (!small_)
|
|
|
|
set_parent_rect({ parent_pos, { static_cast<ui::Dim>((8 * length) + 24), 24 } });
|
|
|
|
else
|
|
|
|
set_parent_rect({ parent_pos, { static_cast<ui::Dim>((8 * length) + 16), 16 } });
|
|
|
|
|
2016-04-21 16:12:51 -04:00
|
|
|
set_focusable(true);
|
2016-02-05 11:40:14 -05:00
|
|
|
}
|
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
void Checkbox::set_text(const std::string value) {
|
|
|
|
text_ = value;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
bool Checkbox::set_value(const bool value) {
|
2016-02-04 04:27:53 -05:00
|
|
|
value_ = value;
|
2017-01-16 08:36:28 -05:00
|
|
|
set_dirty();
|
2016-12-09 12:21:47 -05:00
|
|
|
|
|
|
|
if( on_select ) {
|
2017-01-17 09:27:37 -05:00
|
|
|
on_select(*this, value_);
|
2016-12-09 12:21:47 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-02-04 04:27:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Checkbox::value() const {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Checkbox::paint(Painter& painter) {
|
|
|
|
const auto r = screen_rect();
|
|
|
|
|
2016-04-21 16:12:51 -04:00
|
|
|
const auto paint_style = (has_focus() || highlighted()) ? style().invert() : style();
|
2016-02-04 04:27:53 -05:00
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
const auto x = r.location().x();
|
|
|
|
const auto y = r.location().y();
|
2016-02-04 04:27:53 -05:00
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
const auto label_r = paint_style.font.size_of(text_);
|
2016-02-04 04:27:53 -05:00
|
|
|
|
2017-01-16 08:36:28 -05:00
|
|
|
if (!small_) {
|
|
|
|
painter.draw_rectangle({ { r.location() }, { 24, 24 } }, style().foreground);
|
|
|
|
|
|
|
|
painter.fill_rectangle({ x + 1, y + 1, 24 - 2, 24 - 2 }, style().background);
|
|
|
|
|
|
|
|
// Highlight
|
|
|
|
painter.draw_rectangle({ x + 2, y + 2, 24 - 4, 24 - 4 }, paint_style.background);
|
|
|
|
|
|
|
|
if (value_ == true) {
|
|
|
|
// Check
|
|
|
|
portapack::display.draw_line( {x + 2, y + 14}, {x + 6, y + 18}, ui::Color::green());
|
|
|
|
portapack::display.draw_line( {x + 6, y + 18}, {x + 20, y + 4}, ui::Color::green());
|
|
|
|
} else {
|
|
|
|
// Cross
|
|
|
|
portapack::display.draw_line( {x + 1, y + 1}, {x + 24 - 2, y + 24 - 2}, ui::Color::red());
|
|
|
|
portapack::display.draw_line( {x + 24 - 2, y + 1}, {x + 1, y + 24 - 2}, ui::Color::red());
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.draw_string(
|
|
|
|
{
|
|
|
|
static_cast<Coord>(x + 24 + 4),
|
|
|
|
static_cast<Coord>(y + (24 - label_r.height()) / 2)
|
|
|
|
},
|
|
|
|
paint_style,
|
|
|
|
text_
|
|
|
|
);
|
2016-02-04 04:27:53 -05:00
|
|
|
} else {
|
2017-01-16 08:36:28 -05:00
|
|
|
painter.draw_rectangle({ { r.location() }, { 16, 16 } }, style().foreground);
|
|
|
|
|
|
|
|
painter.fill_rectangle({ x + 1, y + 1, 16 - 2, 16 - 2 }, style().background);
|
|
|
|
|
|
|
|
// Highlight
|
|
|
|
painter.draw_rectangle({ x + 1, y + 1, 16 - 2, 16 - 2 }, paint_style.background);
|
|
|
|
|
|
|
|
if (value_ == true) {
|
|
|
|
// Check
|
|
|
|
portapack::display.draw_line( {x + 2, y + 8}, {x + 6, y + 12}, ui::Color::green());
|
|
|
|
portapack::display.draw_line( {x + 6, y + 12}, {x + 13, y + 5}, ui::Color::green());
|
|
|
|
} else {
|
|
|
|
// Cross
|
|
|
|
portapack::display.draw_line( {x + 1, y + 1}, {x + 16 - 2, y + 16 - 2}, ui::Color::red());
|
|
|
|
portapack::display.draw_line( {x + 16 - 2, y + 1}, {x + 1, y + 16 - 2}, ui::Color::red());
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.draw_string(
|
|
|
|
{
|
|
|
|
static_cast<Coord>(x + 16),
|
|
|
|
static_cast<Coord>(y + (16 - label_r.height()) / 2)
|
|
|
|
},
|
|
|
|
paint_style,
|
|
|
|
text_
|
|
|
|
);
|
2016-02-04 04:27:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Checkbox::on_key(const KeyEvent key) {
|
2016-12-09 12:21:47 -05:00
|
|
|
if( key == KeyEvent::Select )
|
|
|
|
return set_value(not value_);
|
2016-02-04 04:27:53 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Checkbox::on_touch(const TouchEvent event) {
|
|
|
|
switch(event.type) {
|
|
|
|
case TouchEvent::Type::Start:
|
2016-04-21 16:12:51 -04:00
|
|
|
set_highlighted(true);
|
2016-02-04 04:27:53 -05:00
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TouchEvent::Type::End:
|
2016-04-21 16:12:51 -04:00
|
|
|
set_highlighted(false);
|
2016-02-04 04:27:53 -05:00
|
|
|
value_ = not value_;
|
|
|
|
set_dirty();
|
|
|
|
if( on_select ) {
|
2017-01-17 09:27:37 -05:00
|
|
|
on_select(*this, value_);
|
2016-02-04 04:27:53 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* Button ****************************************************************/
|
|
|
|
|
2016-02-05 11:40:14 -05:00
|
|
|
Button::Button(
|
2016-01-31 03:34:24 -05:00
|
|
|
Rect parent_rect,
|
|
|
|
std::string text
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
text_ { text }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
void Button::set_text(const std::string value) {
|
2015-09-10 14:36:39 -04:00
|
|
|
text_ = value;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
std::string Button::text() const {
|
|
|
|
return text_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::paint(Painter& painter) {
|
2016-12-08 20:35:50 -05:00
|
|
|
Color bg, fg;
|
2015-07-08 11:39:24 -04:00
|
|
|
const auto r = screen_rect();
|
|
|
|
|
2016-12-08 20:35:50 -05:00
|
|
|
if (has_focus() || highlighted()) {
|
|
|
|
bg = style().foreground;
|
|
|
|
fg = Color::black();
|
|
|
|
} else {
|
|
|
|
bg = Color::grey();
|
|
|
|
fg = style().foreground;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Style paint_style = { style().font, bg, fg };
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
painter.draw_rectangle({r.location(), {r.size().width(), 1}}, Color::light_grey());
|
|
|
|
painter.draw_rectangle({r.location().x(), r.location().y() + r.size().height() - 1, r.size().width(), 1}, Color::dark_grey());
|
|
|
|
painter.draw_rectangle({r.location().x() + r.size().width() - 1, r.location().y(), 1, r.size().height()}, Color::dark_grey());
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
painter.fill_rectangle(
|
2017-01-15 22:45:44 -05:00
|
|
|
{ r.location().x(), r.location().y() + 1, r.size().width() - 1, r.size().height() - 2 },
|
2015-07-08 11:39:24 -04:00
|
|
|
paint_style.background
|
|
|
|
);
|
|
|
|
|
2016-05-29 06:06:47 -04:00
|
|
|
const auto label_r = paint_style.font.size_of(text_);
|
|
|
|
painter.draw_string(
|
2017-01-15 22:45:44 -05:00
|
|
|
{ r.location().x() + (r.size().width() - label_r.width()) / 2, r.location().y() + (r.size().height() - label_r.height()) / 2 },
|
2016-05-29 06:06:47 -04:00
|
|
|
paint_style,
|
|
|
|
text_
|
|
|
|
);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 02:11:24 -04:00
|
|
|
void Button::on_focus() {
|
|
|
|
if( on_highlight )
|
|
|
|
on_highlight(*this);
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
bool Button::on_key(const KeyEvent key) {
|
|
|
|
if( key == KeyEvent::Select ) {
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-05 11:40:14 -05:00
|
|
|
} else {
|
|
|
|
if( on_dir ) {
|
2016-12-09 12:21:47 -05:00
|
|
|
return on_dir(*this, key);
|
2016-02-05 11:40:14 -05:00
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::on_touch(const TouchEvent event) {
|
|
|
|
switch(event.type) {
|
|
|
|
case TouchEvent::Type::Start:
|
2016-02-07 13:32:38 -05:00
|
|
|
set_highlighted(true);
|
2015-07-08 11:39:24 -04:00
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
case TouchEvent::Type::End:
|
2016-02-07 13:32:38 -05:00
|
|
|
set_highlighted(false);
|
2015-07-08 11:39:24 -04:00
|
|
|
set_dirty();
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
switch(event.type) {
|
|
|
|
case TouchEvent::Type::Start:
|
|
|
|
flags.highlighted = true;
|
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TouchEvent::Type::Move:
|
|
|
|
{
|
|
|
|
const bool new_highlighted = screen_rect().contains(event.point);
|
|
|
|
if( flags.highlighted != new_highlighted ) {
|
|
|
|
flags.highlighted = new_highlighted;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case TouchEvent::Type::End:
|
|
|
|
if( flags.highlighted ) {
|
|
|
|
flags.highlighted = false;
|
|
|
|
set_dirty();
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
|
2016-07-27 23:25:33 -04:00
|
|
|
void Image::invert_colors() {
|
|
|
|
Color temp;
|
|
|
|
|
|
|
|
temp = background_;
|
|
|
|
background_ = foreground_;
|
|
|
|
foreground_ = temp;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
void Image::paint(Painter& painter) {
|
|
|
|
if( bitmap_ ) {
|
|
|
|
// Code also handles ImageButton behavior.
|
2016-02-07 13:32:38 -05:00
|
|
|
const bool selected = (has_focus() || highlighted());
|
2016-02-04 04:27:53 -05:00
|
|
|
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 }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2016-02-04 04:27:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2016-02-07 13:32:38 -05:00
|
|
|
set_highlighted(true);
|
2016-02-04 04:27:53 -05:00
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
|
2016-02-03 18:48:50 -05:00
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
case TouchEvent::Type::End:
|
2016-02-07 13:32:38 -05:00
|
|
|
set_highlighted(false);
|
2016-02-04 04:27:53 -05:00
|
|
|
set_dirty();
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 00:48:04 -04:00
|
|
|
/* ImageOptionsField *****************************************************/
|
|
|
|
|
|
|
|
ImageOptionsField::ImageOptionsField(
|
|
|
|
Rect parent_rect,
|
|
|
|
options_t options
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
options { options }
|
|
|
|
{
|
|
|
|
set_focusable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ImageOptionsField::selected_index() const {
|
|
|
|
return selected_index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ImageOptionsField::selected_index_value() const {
|
|
|
|
return options[selected_index_].second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageOptionsField::set_selected_index(const size_t new_index) {
|
2016-05-29 06:06:47 -04:00
|
|
|
if ( new_index < options.size() ) {
|
|
|
|
if ( new_index != selected_index() ) {
|
2016-05-27 00:48:04 -04:00
|
|
|
selected_index_ = new_index;
|
2016-05-29 06:06:47 -04:00
|
|
|
if ( on_change ) {
|
2016-05-27 00:48:04 -04:00
|
|
|
on_change(selected_index(), options[selected_index()].second);
|
|
|
|
}
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageOptionsField::set_by_value(value_t v) {
|
|
|
|
size_t new_index { 0 };
|
|
|
|
for(const auto& option : options) {
|
|
|
|
if( option.second == v ) {
|
|
|
|
set_selected_index(new_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageOptionsField::set_options(options_t new_options) {
|
|
|
|
options = new_options;
|
|
|
|
set_by_value(0);
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageOptionsField::paint(Painter& painter) {
|
|
|
|
const auto paint_style = has_focus() ? style().invert() : style();
|
|
|
|
|
|
|
|
if( selected_index() < options.size() ) {
|
|
|
|
const auto bmp_ptr = options[selected_index()].first;
|
2017-01-15 22:45:44 -05:00
|
|
|
painter.fill_rectangle({screen_rect().location(), {screen_rect().size().width() + 4, screen_rect().size().height() + 4}}, ui::Color::black());
|
|
|
|
painter.draw_rectangle({screen_rect().location(), {screen_rect().size().width() + 4, screen_rect().size().height() + 4}}, paint_style.background);
|
|
|
|
portapack::display.drawBMP({screen_pos().x() + 2, screen_pos().y() + 1}, bmp_ptr, true);
|
2016-05-27 00:48:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageOptionsField::on_focus() {
|
|
|
|
if( on_show_options ) {
|
|
|
|
on_show_options();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageOptionsField::on_encoder(const EncoderEvent delta) {
|
|
|
|
set_selected_index(selected_index() + delta);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageOptionsField::on_touch(const TouchEvent event) {
|
|
|
|
if( event.type == TouchEvent::Type::Start ) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* OptionsField **********************************************************/
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
OptionsField::OptionsField(
|
|
|
|
Point parent_pos,
|
2016-11-29 13:13:56 -05:00
|
|
|
int length,
|
2016-01-31 03:34:24 -05:00
|
|
|
options_t options
|
2016-11-29 13:13:56 -05:00
|
|
|
) : Widget { { parent_pos, { 8 * length, 16 } } },
|
2016-01-31 03:34:24 -05:00
|
|
|
length_ { length },
|
|
|
|
options { options }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
size_t OptionsField::selected_index() const {
|
|
|
|
return selected_index_;
|
|
|
|
}
|
|
|
|
|
2016-05-13 03:34:15 -04:00
|
|
|
size_t OptionsField::selected_index_value() const {
|
|
|
|
return options[selected_index_].second;
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void OptionsField::set_selected_index(const size_t new_index) {
|
|
|
|
if( new_index < options.size() ) {
|
|
|
|
if( new_index != selected_index() ) {
|
|
|
|
selected_index_ = new_index;
|
|
|
|
if( on_change ) {
|
|
|
|
on_change(selected_index(), options[selected_index()].second);
|
|
|
|
}
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionsField::set_by_value(value_t v) {
|
|
|
|
size_t new_index { 0 };
|
|
|
|
for(const auto& option : options) {
|
2016-01-31 03:34:24 -05:00
|
|
|
if( option.second == v ) {
|
2015-07-08 11:39:24 -04:00
|
|
|
set_selected_index(new_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 11:40:14 -05:00
|
|
|
void OptionsField::set_options(options_t new_options) {
|
|
|
|
options = new_options;
|
|
|
|
set_by_value(0);
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void OptionsField::paint(Painter& painter) {
|
|
|
|
const auto paint_style = has_focus() ? style().invert() : style();
|
2016-05-29 06:06:47 -04:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
painter.fill_rectangle({screen_rect().location(), {(int)length_ * 8, 16}}, ui::Color::black());
|
2016-05-29 06:06:47 -04:00
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
if( selected_index() < options.size() ) {
|
|
|
|
const auto text = options[selected_index()].first;
|
|
|
|
painter.draw_string(
|
|
|
|
screen_pos(),
|
|
|
|
paint_style,
|
|
|
|
text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 04:27:53 -05:00
|
|
|
void OptionsField::on_focus() {
|
|
|
|
if( on_show_options ) {
|
|
|
|
on_show_options();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
bool OptionsField::on_encoder(const EncoderEvent delta) {
|
|
|
|
set_selected_index(selected_index() + delta);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OptionsField::on_touch(const TouchEvent event) {
|
|
|
|
if( event.type == TouchEvent::Type::Start ) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NumberField ***********************************************************/
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
NumberField::NumberField(
|
|
|
|
Point parent_pos,
|
2016-11-29 13:13:56 -05:00
|
|
|
int length,
|
2016-01-31 03:34:24 -05:00
|
|
|
range_t range,
|
|
|
|
int32_t step,
|
|
|
|
char fill_char
|
2016-11-29 13:13:56 -05:00
|
|
|
) : Widget { { parent_pos, { 8 * length, 16 } } },
|
2016-01-31 03:34:24 -05:00
|
|
|
range { range },
|
|
|
|
step { step },
|
|
|
|
length_ { length },
|
|
|
|
fill_char { fill_char }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
int32_t NumberField::value() const {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
2016-08-02 22:53:51 -04:00
|
|
|
void NumberField::set_value(int32_t new_value, bool trigger_change) {
|
2015-07-08 11:39:24 -04:00
|
|
|
new_value = clip_value(new_value);
|
|
|
|
|
|
|
|
if( new_value != value() ) {
|
|
|
|
value_ = new_value;
|
2016-08-02 22:53:51 -04:00
|
|
|
if( on_change && trigger_change ) {
|
2015-07-08 11:39:24 -04:00
|
|
|
on_change(value_);
|
|
|
|
}
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 22:53:51 -04:00
|
|
|
void NumberField::set_range(const int32_t min, const int32_t max) {
|
|
|
|
range.first = min;
|
|
|
|
range.second = max;
|
|
|
|
set_value(value(), false);
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void NumberField::paint(Painter& painter) {
|
|
|
|
const auto text = to_string_dec_int(value_, length_, fill_char);
|
|
|
|
|
|
|
|
const auto paint_style = has_focus() ? style().invert() : style();
|
|
|
|
|
|
|
|
painter.draw_string(
|
|
|
|
screen_pos(),
|
|
|
|
paint_style,
|
|
|
|
text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-25 10:21:27 -04:00
|
|
|
bool NumberField::on_key(const KeyEvent key) {
|
2016-08-01 14:06:17 -04:00
|
|
|
if( key == KeyEvent::Select ) {
|
2016-07-25 10:21:27 -04:00
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-01 14:06:17 -04:00
|
|
|
}
|
2016-07-25 10:21:27 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
bool NumberField::on_encoder(const EncoderEvent delta) {
|
|
|
|
set_value(value() + (delta * step));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NumberField::on_touch(const TouchEvent event) {
|
|
|
|
if( event.type == TouchEvent::Type::Start ) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t NumberField::clip_value(int32_t value) {
|
|
|
|
if( value > range.second ) {
|
|
|
|
value = range.second;
|
|
|
|
}
|
|
|
|
if( value < range.first ) {
|
|
|
|
value = range.first;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-08-06 02:49:45 -04:00
|
|
|
/* SymField **************************************************************/
|
|
|
|
|
2016-12-05 06:56:41 -05:00
|
|
|
SymField::SymField(
|
|
|
|
Point parent_pos,
|
|
|
|
size_t length,
|
2017-01-31 19:21:13 -05:00
|
|
|
symfield_type type
|
2016-12-05 06:56:41 -05:00
|
|
|
) : Widget { { parent_pos, { static_cast<ui::Dim>(8 * length), 16 } } },
|
|
|
|
length_ { length },
|
2017-01-31 19:21:13 -05:00
|
|
|
type_ { type }
|
2016-12-05 06:56:41 -05:00
|
|
|
{
|
2017-01-31 19:21:13 -05:00
|
|
|
uint32_t c;
|
2016-12-05 06:56:41 -05:00
|
|
|
|
2017-01-31 19:21:13 -05:00
|
|
|
// Auto-init
|
|
|
|
if (type == SYMFIELD_OCT) {
|
|
|
|
for (c = 0; c < length; c++)
|
|
|
|
set_symbol_list(c, "01234567");
|
|
|
|
} else if (type == SYMFIELD_DEC) {
|
|
|
|
for (c = 0; c < length; c++)
|
|
|
|
set_symbol_list(c, "0123456789");
|
|
|
|
} else if (type == SYMFIELD_HEX) {
|
|
|
|
for (c = 0; c < length; c++)
|
|
|
|
set_symbol_list(c, "0123456789ABCDEF");
|
|
|
|
}
|
2016-12-05 06:56:41 -05:00
|
|
|
|
|
|
|
set_focusable(true);
|
|
|
|
}
|
|
|
|
|
2017-02-07 12:48:17 -05:00
|
|
|
uint32_t SymField::value_dec_u32() {
|
|
|
|
uint32_t c, mul = 1;
|
|
|
|
uint32_t v = 0;
|
|
|
|
|
|
|
|
if (type_ == SYMFIELD_DEC) {
|
|
|
|
for (c = 0; c < length_; c++) {
|
|
|
|
v += values_[(length_ - 1 - c)] * mul;
|
|
|
|
mul *= 10;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-05 06:56:41 -05:00
|
|
|
uint64_t SymField::value_hex_u64() {
|
2017-01-31 19:21:13 -05:00
|
|
|
uint32_t c;
|
2016-12-05 06:56:41 -05:00
|
|
|
uint64_t v = 0;
|
|
|
|
|
2017-01-31 19:21:13 -05:00
|
|
|
if (type_ != SYMFIELD_DEF) {
|
2016-12-05 06:56:41 -05:00
|
|
|
for (c = 0; c < length_; c++)
|
|
|
|
v += values_[c] << (4 * (length_ - 1 - c));
|
|
|
|
return v;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-13 00:09:21 -04:00
|
|
|
uint32_t SymField::get_sym(const uint32_t index) {
|
2016-09-26 21:06:14 -04:00
|
|
|
if (index >= length_) return 0;
|
|
|
|
|
2016-08-06 02:49:45 -04:00
|
|
|
return values_[index];
|
|
|
|
}
|
|
|
|
|
2017-03-13 00:09:21 -04:00
|
|
|
void SymField::set_sym(const uint32_t index, const uint32_t new_value) {
|
2016-09-26 21:06:14 -04:00
|
|
|
if (index >= length_) return;
|
|
|
|
|
|
|
|
uint32_t clipped_value = clip_value(index, new_value);
|
2016-08-06 02:49:45 -04:00
|
|
|
|
2016-09-26 21:06:14 -04:00
|
|
|
if (clipped_value != values_[index]) {
|
|
|
|
values_[index] = clipped_value;
|
2016-08-06 02:49:45 -04:00
|
|
|
if( on_change ) {
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymField::set_length(const uint32_t new_length) {
|
2016-09-26 21:06:14 -04:00
|
|
|
if ((new_length <= 32) && (new_length != length_)) {
|
2016-08-06 02:49:45 -04:00
|
|
|
prev_length_ = length_;
|
|
|
|
length_ = new_length;
|
2016-09-26 21:06:14 -04:00
|
|
|
|
|
|
|
// Clip eventual garbage from previous shorter word
|
|
|
|
for (size_t n = 0; n < length_; n++)
|
2017-03-13 00:09:21 -04:00
|
|
|
set_sym(n, values_[n]);
|
2016-09-26 21:06:14 -04:00
|
|
|
|
2016-08-06 02:49:45 -04:00
|
|
|
erase_prev_ = true;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:06:14 -04:00
|
|
|
void SymField::set_symbol_list(const uint32_t index, const std::string symbol_list) {
|
|
|
|
if (index >= length_) return;
|
2016-08-06 02:49:45 -04:00
|
|
|
|
2016-09-26 21:06:14 -04:00
|
|
|
symbol_list_[index] = symbol_list;
|
|
|
|
|
|
|
|
// Re-clip symbol's value
|
2017-03-13 00:09:21 -04:00
|
|
|
set_sym(index, values_[index]);
|
2016-08-06 02:49:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymField::paint(Painter& painter) {
|
|
|
|
Point pt_draw = screen_pos();
|
|
|
|
|
|
|
|
if (erase_prev_) {
|
2016-09-26 21:06:14 -04:00
|
|
|
painter.fill_rectangle( { pt_draw, { (int)prev_length_ * 8, 16 } }, Color::black() );
|
2016-08-06 02:49:45 -04:00
|
|
|
erase_prev_ = false;
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:06:14 -04:00
|
|
|
for (size_t n = 0; n < length_; n++) {
|
|
|
|
const auto text = symbol_list_[n].substr(values_[n], 1);
|
2016-08-06 02:49:45 -04:00
|
|
|
|
|
|
|
const auto paint_style = (has_focus() && (n == selected_)) ? style().invert() : style();
|
|
|
|
|
|
|
|
painter.draw_string(
|
|
|
|
pt_draw,
|
|
|
|
paint_style,
|
|
|
|
text
|
|
|
|
);
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
pt_draw += { 8, 0 };
|
2016-08-06 02:49:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymField::on_key(const KeyEvent key) {
|
|
|
|
switch (key) {
|
|
|
|
case KeyEvent::Select:
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyEvent::Left:
|
|
|
|
if (selected_ > 0) {
|
|
|
|
selected_--;
|
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyEvent::Right:
|
|
|
|
if (selected_ < (length_ - 1)) {
|
|
|
|
selected_++;
|
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymField::on_encoder(const EncoderEvent delta) {
|
2016-09-26 21:06:14 -04:00
|
|
|
int32_t new_value = (int)values_[selected_] + delta;
|
|
|
|
|
|
|
|
if (new_value >= 0)
|
2017-03-13 00:09:21 -04:00
|
|
|
set_sym(selected_, values_[selected_] + delta);
|
2016-09-26 21:06:14 -04:00
|
|
|
|
2016-08-06 02:49:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SymField::on_touch(const TouchEvent event) {
|
2016-09-26 21:06:14 -04:00
|
|
|
if (event.type == TouchEvent::Type::Start) {
|
2016-08-06 02:49:45 -04:00
|
|
|
focus();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:06:14 -04:00
|
|
|
int32_t SymField::clip_value(const uint32_t index, const uint32_t value) {
|
|
|
|
size_t symbol_count = symbol_list_[index].length() - 1;
|
|
|
|
|
|
|
|
if (value > symbol_count)
|
|
|
|
return symbol_count;
|
|
|
|
else
|
|
|
|
return value;
|
2016-08-06 02:49:45 -04:00
|
|
|
}
|
|
|
|
|
2017-01-08 21:45:29 -05:00
|
|
|
/* Waveform **************************************************************/
|
|
|
|
|
|
|
|
Waveform::Waveform(
|
|
|
|
Rect parent_rect,
|
|
|
|
int8_t * data,
|
|
|
|
uint32_t length,
|
|
|
|
uint32_t offset,
|
2017-01-17 02:00:42 -05:00
|
|
|
bool digital,
|
2017-01-08 21:45:29 -05:00
|
|
|
Color color
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
data_ { data },
|
|
|
|
length_ { length },
|
|
|
|
offset_ { offset },
|
2017-01-17 02:00:42 -05:00
|
|
|
digital_ { digital },
|
2017-01-08 21:45:29 -05:00
|
|
|
color_ { color }
|
|
|
|
{
|
|
|
|
//set_focusable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Waveform::set_offset(const uint32_t new_offset) {
|
|
|
|
if (new_offset != offset_) {
|
|
|
|
offset_ = new_offset;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Waveform::set_length(const uint32_t new_length) {
|
|
|
|
if (new_length != length_) {
|
|
|
|
length_ = new_length;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Waveform::paint(Painter& painter) {
|
|
|
|
uint32_t n, point_count;
|
2017-01-15 22:45:44 -05:00
|
|
|
Coord y, y_offset = screen_rect().location().y();
|
|
|
|
Coord prev_x = screen_rect().location().x(), prev_y;
|
2017-01-08 21:45:29 -05:00
|
|
|
float x, x_inc;
|
2017-01-15 22:45:44 -05:00
|
|
|
Dim h = screen_rect().size().height();
|
2017-01-17 02:00:42 -05:00
|
|
|
int8_t * data_start = data_ + offset_;
|
2017-01-08 21:45:29 -05:00
|
|
|
|
|
|
|
// Clear
|
|
|
|
painter.fill_rectangle(screen_rect(), Color::black());
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
x_inc = (float)screen_rect().size().width() / length_;
|
2017-01-08 21:45:29 -05:00
|
|
|
point_count = length_;
|
|
|
|
const float y_scale = (float)(h - 1) / 256; // TODO: Make variable
|
|
|
|
|
|
|
|
if (!point_count) return;
|
|
|
|
|
|
|
|
|
2017-01-17 02:00:42 -05:00
|
|
|
if (digital_) {
|
|
|
|
// Digital waveform: each value is an horizontal line
|
|
|
|
x = 0;
|
|
|
|
h--;
|
|
|
|
for (n = 0; n < point_count; n++) {
|
|
|
|
y = *(data_start++) ? h : 0;
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
if (y != prev_y)
|
2017-03-13 00:09:21 -04:00
|
|
|
painter.draw_vline( {(Coord)x, y_offset}, h, color_);
|
2017-01-17 02:00:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 00:09:21 -04:00
|
|
|
painter.draw_hline( {(Coord)x, y_offset + y}, ceil(x_inc), color_);
|
2017-01-17 02:00:42 -05:00
|
|
|
|
|
|
|
prev_y = y;
|
|
|
|
x += x_inc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Analog waveform: each value is a point's Y coordinate
|
|
|
|
x = prev_x + x_inc;
|
|
|
|
h = h / 2;
|
|
|
|
prev_y = y_offset + h - (*(data_start++) * y_scale);
|
|
|
|
for (n = 1; n < point_count; n++) {
|
|
|
|
y = y_offset + h - (*(data_start++) * y_scale);
|
|
|
|
display.draw_line( {prev_x, prev_y}, {(Coord)x, y}, color_);
|
|
|
|
|
|
|
|
prev_x = x;
|
|
|
|
prev_y = y;
|
|
|
|
x += x_inc;
|
|
|
|
}
|
2017-01-08 21:45:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 00:09:21 -04:00
|
|
|
|
|
|
|
/* VuMeter **************************************************************/
|
|
|
|
|
|
|
|
VuMeter::VuMeter(
|
|
|
|
Rect parent_rect,
|
|
|
|
uint32_t LEDs
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
LEDs_ { LEDs },
|
|
|
|
height { parent_rect.size().height() }
|
|
|
|
{
|
|
|
|
//set_focusable(false);
|
|
|
|
LED_height = height / LEDs;
|
|
|
|
split = 256 / LEDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VuMeter::set_value(const uint8_t new_value) {
|
|
|
|
value_ = new_value;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VuMeter::set_mark(const uint8_t new_mark) {
|
|
|
|
if (new_mark != mark) {
|
|
|
|
mark = new_mark;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VuMeter::paint(Painter& painter) {
|
|
|
|
Point pos = screen_rect().location();
|
|
|
|
Dim width = screen_rect().size().width() - 4;
|
|
|
|
|
|
|
|
if (value_ != prev_value) {
|
|
|
|
uint32_t bar;
|
|
|
|
Color color;
|
|
|
|
bool lit = false;
|
|
|
|
uint32_t bar_level = LEDs_ - ((value_ + 1) / split);
|
|
|
|
// Draw LEDs
|
|
|
|
for (bar = 0; bar < LEDs_; bar++) {
|
|
|
|
if (bar >= bar_level)
|
|
|
|
lit = true;
|
|
|
|
|
|
|
|
if (bar == 0)
|
|
|
|
color = lit ? Color::red() : Color::dark_red();
|
|
|
|
else if (bar == 1)
|
|
|
|
color = lit ? Color::orange() : Color::dark_orange();
|
|
|
|
else if ((bar == 2) || (bar == 3))
|
|
|
|
color = lit ? Color::yellow() : Color::dark_yellow();
|
|
|
|
else
|
|
|
|
color = lit ? Color::green() : Color::dark_green();
|
|
|
|
|
|
|
|
painter.fill_rectangle({ pos.x(), pos.y() + (Coord)(bar * LED_height), width, (Coord)LED_height - 2 }, color);
|
|
|
|
}
|
|
|
|
prev_value = value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update max level
|
|
|
|
if (value_ > max) {
|
|
|
|
max = value_;
|
|
|
|
hold_timer = 30; // 0.5s @ 60Hz
|
|
|
|
} else {
|
|
|
|
if (hold_timer) {
|
|
|
|
hold_timer--;
|
|
|
|
} else {
|
|
|
|
if (max) max--; // Let it drop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw max level
|
|
|
|
if (max != prev_max) {
|
|
|
|
painter.draw_hline({ pos.x() + width, pos.y() + height - (height * prev_max) / 256 }, 8, Color::black());
|
|
|
|
painter.draw_hline({ pos.x() + width, pos.y() + height - (height * max) / 256 }, 8, Color::white());
|
|
|
|
if (prev_max == mark)
|
|
|
|
prev_mark = 0; // Force mark refresh
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw mark
|
|
|
|
if ((mark != prev_mark) && (mark)) {
|
|
|
|
painter.draw_hline({ pos.x() + width, pos.y() + height - (height * prev_mark) / 256 }, 8, Color::black());
|
|
|
|
painter.draw_hline({ pos.x() + width, pos.y() + height - (height * mark) / 256 }, 8, Color::grey());
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_max = max;
|
|
|
|
prev_mark = mark;
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
} /* namespace ui */
|