2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_widget.hpp"
|
|
|
|
#include "ui_painter.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2015-12-02 16:38:17 -05:00
|
|
|
#include "string_format.hpp"
|
|
|
|
|
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-02-07 13:55:55 -05:00
|
|
|
return screen_rect().pos;
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Size Widget::size() const {
|
|
|
|
return parent_rect.size;
|
|
|
|
}
|
|
|
|
|
2016-05-08 17:59:26 -04:00
|
|
|
Rect Widget::screen_rect() const {
|
2015-07-08 11:39:24 -04:00
|
|
|
return parent() ? (parent_rect + parent()->screen_pos()) : parent_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_parent_rect(const Rect new_parent_rect) {
|
|
|
|
parent_rect = new_parent_rect;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Widget::parent() const {
|
|
|
|
return parent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_parent(Widget* const widget) {
|
|
|
|
if( parent_ && !widget ) {
|
|
|
|
// We have a parent, but are losing it. Update visible status.
|
|
|
|
visible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_ = widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 00:47:10 -05:00
|
|
|
// TODO: Instead of dirtying parent entirely, dirty only children
|
|
|
|
// that overlap with this widget.
|
|
|
|
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-28 00:47:10 -05:00
|
|
|
void Widget::dirty_overlapping_children_in_rect(const Rect& child_rect) {
|
|
|
|
for(auto child : children()) {
|
|
|
|
if( !child_rect.intersect(child->parent_rect).is_empty() ) {
|
|
|
|
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);
|
|
|
|
widget->set_dirty();
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void View::add_children(const std::vector<Widget*>& children) {
|
|
|
|
for(auto child : children) {
|
|
|
|
add_child(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2016-02-05 13:44:09 -05:00
|
|
|
dirty_overlapping_children_in_rect(widget->screen_rect());
|
2015-10-16 22:34:22 -04:00
|
|
|
widget->set_parent(nullptr);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-26 16:01:16 -05:00
|
|
|
std::string View::title() const {
|
|
|
|
return "";
|
|
|
|
};
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* Rectangle *************************************************************/
|
|
|
|
|
2015-12-17 01:35:26 -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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rectangle::paint(Painter& painter) {
|
|
|
|
painter.fill_rectangle(
|
|
|
|
screen_rect(),
|
|
|
|
color
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Text ******************************************************************/
|
|
|
|
|
2015-12-17 01:35:26 -05:00
|
|
|
Text::Text(
|
|
|
|
Rect parent_rect,
|
|
|
|
std::string text
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
text { text }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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-26 16:00:36 -05:00
|
|
|
const auto rect = screen_rect();
|
|
|
|
const auto s = style();
|
|
|
|
|
|
|
|
painter.fill_rectangle(rect, s.background);
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
painter.draw_string(
|
2016-01-26 16:00:36 -05:00
|
|
|
rect.pos,
|
|
|
|
s,
|
2015-07-08 11:39:24 -04:00
|
|
|
text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Button ****************************************************************/
|
|
|
|
|
2015-12-17 01:35:26 -05:00
|
|
|
Button::Button(
|
|
|
|
Rect parent_rect,
|
|
|
|
std::string text
|
|
|
|
) : Widget { parent_rect },
|
|
|
|
text_ { text }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2015-12-17 01:35:26 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void Button::set_text(const std::string value) {
|
|
|
|
text_ = value;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Button::text() const {
|
|
|
|
return text_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::paint(Painter& painter) {
|
|
|
|
const auto r = screen_rect();
|
|
|
|
|
2016-02-07 13:32:38 -05:00
|
|
|
const auto paint_style = (has_focus() || highlighted()) ? style().invert() : style();
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
painter.draw_rectangle(r, style().foreground);
|
|
|
|
|
|
|
|
painter.fill_rectangle(
|
2016-01-23 20:02:16 -05:00
|
|
|
{ r.pos.x + 1, r.pos.y + 1, r.size.w - 2, r.size.h - 2 },
|
2015-07-08 11:39:24 -04:00
|
|
|
paint_style.background
|
|
|
|
);
|
|
|
|
|
|
|
|
const auto label_r = paint_style.font.size_of(text_);
|
|
|
|
painter.draw_string(
|
2016-01-23 20:02:16 -05:00
|
|
|
{ r.pos.x + (r.size.w - label_r.w) / 2, r.pos.y + (r.size.h - label_r.h) / 2 },
|
2015-07-08 11:39:24 -04:00
|
|
|
paint_style,
|
|
|
|
text_
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::on_key(const KeyEvent key) {
|
|
|
|
if( key == KeyEvent::Select ) {
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 18:48:50 -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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 18:48:50 -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-03 18:48:50 -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-03 18:48:50 -05:00
|
|
|
set_dirty();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
case TouchEvent::Type::End:
|
2016-02-07 13:32:38 -05:00
|
|
|
set_highlighted(false);
|
2016-02-03 18:48:50 -05:00
|
|
|
set_dirty();
|
|
|
|
if( on_select ) {
|
|
|
|
on_select(*this);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
/* OptionsField **********************************************************/
|
|
|
|
|
2015-12-17 01:35:26 -05:00
|
|
|
OptionsField::OptionsField(
|
|
|
|
Point parent_pos,
|
|
|
|
size_t length,
|
|
|
|
options_t options
|
|
|
|
) : Widget { { parent_pos, { static_cast<ui::Dim>(8 * length), 16 } } },
|
|
|
|
length_ { length },
|
|
|
|
options { options }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2015-12-17 01:35:26 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
size_t OptionsField::selected_index() const {
|
|
|
|
return selected_index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-12-03 23:53:14 -05:00
|
|
|
if( option.second == v ) {
|
2015-07-08 11:39:24 -04:00
|
|
|
set_selected_index(new_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OptionsField::paint(Painter& painter) {
|
|
|
|
const auto paint_style = has_focus() ? style().invert() : style();
|
|
|
|
|
|
|
|
if( selected_index() < options.size() ) {
|
|
|
|
const auto text = options[selected_index()].first;
|
|
|
|
painter.draw_string(
|
|
|
|
screen_pos(),
|
|
|
|
paint_style,
|
|
|
|
text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 16:41:11 -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 ***********************************************************/
|
|
|
|
|
2015-12-17 01:35:26 -05:00
|
|
|
NumberField::NumberField(
|
|
|
|
Point parent_pos,
|
|
|
|
size_t length,
|
|
|
|
range_t range,
|
|
|
|
int32_t step,
|
|
|
|
char fill_char
|
|
|
|
) : Widget { { parent_pos, { static_cast<ui::Dim>(8 * length), 16 } } },
|
|
|
|
range { range },
|
|
|
|
step { step },
|
|
|
|
length_ { length },
|
|
|
|
fill_char { fill_char }
|
|
|
|
{
|
2016-02-07 13:32:38 -05:00
|
|
|
set_focusable(true);
|
2015-12-17 01:35:26 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
int32_t NumberField::value() const {
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberField::set_value(int32_t new_value) {
|
|
|
|
new_value = clip_value(new_value);
|
|
|
|
|
|
|
|
if( new_value != value() ) {
|
|
|
|
value_ = new_value;
|
|
|
|
if( on_change ) {
|
|
|
|
on_change(value_);
|
|
|
|
}
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|