2015-07-08 15:39:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
2016-07-30 03:27:28 +00:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2015-07-08 15:39:24 +00:00
|
|
|
*
|
|
|
|
* 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_menu.hpp"
|
2017-01-16 03:45:44 +00:00
|
|
|
#include "rtc_time.hpp"
|
2015-07-08 15:39:24 +00:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2016-07-30 03:27:28 +00:00
|
|
|
/* MenuItemView **********************************************************/
|
2015-07-08 15:39:24 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
void MenuItemView::set_item(MenuItem* item_) {
|
|
|
|
item = item_;
|
|
|
|
}
|
|
|
|
|
2015-07-08 15:39:24 +00:00
|
|
|
void MenuItemView::highlight() {
|
2016-02-07 18:32:38 +00:00
|
|
|
set_highlighted(true);
|
|
|
|
set_dirty();
|
2015-07-08 15:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuItemView::unhighlight() {
|
2016-02-07 18:32:38 +00:00
|
|
|
set_highlighted(false);
|
|
|
|
set_dirty();
|
2015-07-08 15:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuItemView::paint(Painter& painter) {
|
2017-01-30 13:10:30 +00:00
|
|
|
Coord offset_x;
|
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
if (!item) return;
|
|
|
|
|
2015-07-08 15:39:24 +00:00
|
|
|
const auto r = screen_rect();
|
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
const auto paint_style = (highlighted() && (parent()->has_focus() || keep_highlight)) ? style().invert() : style();
|
2015-07-08 15:39:24 +00:00
|
|
|
|
|
|
|
const auto font_height = paint_style.font.line_height();
|
2016-12-09 17:21:47 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
ui::Color final_item_color = (highlighted() && (parent()->has_focus() || keep_highlight)) ? paint_style.foreground : item->color;
|
|
|
|
ui::Color final_bg_color = (highlighted() && (parent()->has_focus() || keep_highlight)) ? item->color : paint_style.background;
|
2016-12-09 17:21:47 +00:00
|
|
|
|
|
|
|
if (final_item_color.v == final_bg_color.v) final_item_color = paint_style.foreground;
|
2015-07-08 15:39:24 +00:00
|
|
|
|
|
|
|
painter.fill_rectangle(
|
|
|
|
r,
|
2016-12-09 17:21:47 +00:00
|
|
|
final_bg_color
|
2015-07-08 15:39:24 +00:00
|
|
|
);
|
2016-12-26 19:33:38 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
if (item->bitmap) {
|
2016-12-26 19:33:38 +00:00
|
|
|
painter.draw_bitmap(
|
2017-01-16 03:45:44 +00:00
|
|
|
{ r.location().x() + 4, r.location().y() + 4 },
|
2017-12-08 18:58:46 +00:00
|
|
|
*item->bitmap,
|
2016-12-26 19:33:38 +00:00
|
|
|
final_item_color,
|
|
|
|
final_bg_color
|
|
|
|
);
|
2017-01-30 13:10:30 +00:00
|
|
|
offset_x = 26;
|
|
|
|
} else
|
|
|
|
offset_x = 8;
|
2016-04-28 12:59:14 +00:00
|
|
|
|
|
|
|
Style text_style {
|
|
|
|
.font = paint_style.font,
|
2016-12-09 17:21:47 +00:00
|
|
|
.background = final_bg_color,
|
2016-04-28 12:59:14 +00:00
|
|
|
.foreground = final_item_color
|
|
|
|
};
|
2016-04-21 20:12:51 +00:00
|
|
|
|
2015-07-08 15:39:24 +00:00
|
|
|
painter.draw_string(
|
2017-01-30 13:10:30 +00:00
|
|
|
{ r.location().x() + offset_x, r.location().y() + (r.size().height() - font_height) / 2 },
|
2016-04-28 12:59:14 +00:00
|
|
|
text_style,
|
2017-12-08 18:58:46 +00:00
|
|
|
item->text
|
2015-07-08 15:39:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MenuView **************************************************************/
|
|
|
|
|
2016-12-26 00:31:38 +00:00
|
|
|
MenuView::MenuView(
|
2017-02-03 15:10:27 +00:00
|
|
|
Rect new_parent_rect,
|
2016-12-26 00:31:38 +00:00
|
|
|
bool keep_highlight
|
2017-12-08 18:58:46 +00:00
|
|
|
) : keep_highlight { keep_highlight }
|
2016-12-26 00:31:38 +00:00
|
|
|
{
|
2017-09-02 07:28:29 +00:00
|
|
|
set_parent_rect(new_parent_rect);
|
2017-02-03 15:10:27 +00:00
|
|
|
|
2016-07-30 03:27:28 +00:00
|
|
|
set_focusable(true);
|
|
|
|
|
2017-01-16 03:45:44 +00:00
|
|
|
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
|
2016-07-30 03:27:28 +00:00
|
|
|
this->on_tick_second();
|
|
|
|
};
|
|
|
|
|
2016-12-26 00:31:38 +00:00
|
|
|
add_child(&arrow_more);
|
2016-07-30 03:27:28 +00:00
|
|
|
arrow_more.set_focusable(false);
|
2016-12-09 17:21:47 +00:00
|
|
|
arrow_more.set_foreground(Color::black());
|
2016-07-30 03:27:28 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 15:39:24 +00:00
|
|
|
MenuView::~MenuView() {
|
2017-01-16 03:45:44 +00:00
|
|
|
rtc_time::signal_tick_second -= signal_token_tick_second;
|
2017-12-08 18:58:46 +00:00
|
|
|
|
|
|
|
for (auto item : menu_item_views) {
|
2017-06-22 08:08:37 +00:00
|
|
|
delete item;
|
|
|
|
}
|
2015-07-08 15:39:24 +00:00
|
|
|
}
|
2017-09-02 07:28:29 +00:00
|
|
|
|
|
|
|
void MenuView::set_parent_rect(const Rect new_parent_rect) {
|
|
|
|
View::set_parent_rect(new_parent_rect);
|
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
displayed_max = (parent_rect().size().height() / item_height);
|
|
|
|
arrow_more.set_parent_rect( { 228, (Coord)(displayed_max * item_height), 8, 8 } );
|
|
|
|
|
|
|
|
// TODO: Clean this up :(
|
|
|
|
if (menu_item_views.size()) {
|
|
|
|
|
|
|
|
for (auto item : menu_item_views) {
|
|
|
|
remove_child(item);
|
|
|
|
delete item;
|
|
|
|
}
|
2017-12-10 03:34:11 +00:00
|
|
|
|
|
|
|
menu_item_views.clear();
|
2017-12-08 18:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t c = 0; c < displayed_max; c++) {
|
|
|
|
auto item = new MenuItemView { keep_highlight };
|
|
|
|
menu_item_views.push_back(item);
|
|
|
|
add_child(item);
|
|
|
|
|
2017-12-08 21:46:16 +00:00
|
|
|
Coord y_pos = c * item_height;
|
2017-12-08 18:58:46 +00:00
|
|
|
item->set_parent_rect({
|
|
|
|
{ 0, y_pos },
|
|
|
|
{ size().width(), (Coord)item_height }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
update_items();
|
2017-09-02 07:28:29 +00:00
|
|
|
}
|
2015-07-08 15:39:24 +00:00
|
|
|
|
2016-07-30 03:27:28 +00:00
|
|
|
void MenuView::on_tick_second() {
|
2017-12-08 18:58:46 +00:00
|
|
|
if (more && blink)
|
2016-07-30 03:27:28 +00:00
|
|
|
arrow_more.set_foreground(Color::white());
|
|
|
|
else
|
|
|
|
arrow_more.set_foreground(Color::black());
|
2016-12-26 00:31:38 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
blink = !blink;
|
2016-07-30 03:27:28 +00:00
|
|
|
|
|
|
|
arrow_more.set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-12-26 00:31:38 +00:00
|
|
|
void MenuView::clear() {
|
2017-12-08 18:58:46 +00:00
|
|
|
for (auto item : menu_item_views) {
|
|
|
|
item->set_item(nullptr);
|
2017-02-03 15:10:27 +00:00
|
|
|
}
|
2017-06-22 08:08:37 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
menu_items.clear();
|
2016-12-26 00:31:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:08:37 +00:00
|
|
|
void MenuView::add_item(MenuItem new_item) {
|
2017-12-08 18:58:46 +00:00
|
|
|
menu_items.push_back(new_item);
|
2017-06-22 08:08:37 +00:00
|
|
|
|
2016-07-30 03:27:28 +00:00
|
|
|
update_items();
|
|
|
|
}
|
2015-07-08 15:39:24 +00:00
|
|
|
|
2017-06-22 08:08:37 +00:00
|
|
|
void MenuView::add_items(std::initializer_list<MenuItem> new_items) {
|
|
|
|
for (auto item : new_items) {
|
2016-09-05 22:30:45 +00:00
|
|
|
add_item(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 03:27:28 +00:00
|
|
|
void MenuView::update_items() {
|
2015-07-08 15:39:24 +00:00
|
|
|
size_t i = 0;
|
2016-07-30 03:27:28 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
if (menu_items.size() > displayed_max + offset) {
|
|
|
|
more = true;
|
|
|
|
blink = true;
|
2016-12-26 00:31:38 +00:00
|
|
|
} else
|
2017-12-08 18:58:46 +00:00
|
|
|
more = false;
|
2016-07-30 03:27:28 +00:00
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
for (auto item : menu_item_views) {
|
2017-12-08 21:46:16 +00:00
|
|
|
if (i >= menu_items.size()) break;
|
2017-12-08 18:58:46 +00:00
|
|
|
|
|
|
|
// Assign item data to MenuItemViews according to offset
|
|
|
|
item->set_item(&menu_items[i + offset]);
|
|
|
|
item->set_dirty();
|
|
|
|
|
|
|
|
if (highlighted_item == (i + offset)) {
|
|
|
|
item->highlight();
|
|
|
|
} else
|
|
|
|
item->unhighlight();
|
|
|
|
|
2015-07-08 15:39:24 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuItemView* MenuView::item_view(size_t index) const {
|
2017-12-08 18:58:46 +00:00
|
|
|
return menu_item_views[index];
|
2015-07-08 15:39:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 00:31:38 +00:00
|
|
|
bool MenuView::set_highlighted(int32_t new_value) {
|
2017-12-08 18:58:46 +00:00
|
|
|
int32_t item_count = (int32_t)menu_items.size();
|
2017-02-03 15:10:27 +00:00
|
|
|
|
2016-12-26 00:31:38 +00:00
|
|
|
if (new_value < 0)
|
2015-07-08 15:39:24 +00:00
|
|
|
return false;
|
2016-07-30 03:27:28 +00:00
|
|
|
|
2016-12-26 00:31:38 +00:00
|
|
|
if (new_value >= item_count)
|
|
|
|
new_value = item_count - 1;
|
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
if (((uint32_t)new_value > offset) && ((new_value - offset) >= displayed_max)) {
|
2016-07-30 03:27:28 +00:00
|
|
|
// Shift MenuView up
|
2017-12-10 03:34:11 +00:00
|
|
|
highlighted_item = new_value;
|
2017-12-08 18:58:46 +00:00
|
|
|
offset = new_value - displayed_max + 1;
|
2016-07-30 03:27:28 +00:00
|
|
|
update_items();
|
2017-12-08 18:58:46 +00:00
|
|
|
} else if ((uint32_t)new_value < offset) {
|
2016-07-30 03:27:28 +00:00
|
|
|
// Shift MenuView down
|
2017-12-10 03:34:11 +00:00
|
|
|
highlighted_item = new_value;
|
2017-12-08 18:58:46 +00:00
|
|
|
offset = new_value;
|
2016-07-30 03:27:28 +00:00
|
|
|
update_items();
|
2017-12-08 18:58:46 +00:00
|
|
|
} else {
|
|
|
|
// Just update highlight
|
|
|
|
item_view(highlighted_item - offset)->unhighlight();
|
|
|
|
highlighted_item = new_value;
|
|
|
|
item_view(highlighted_item - offset)->highlight();
|
2016-07-30 03:27:28 +00:00
|
|
|
}
|
2015-07-08 15:39:24 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:58:46 +00:00
|
|
|
uint32_t MenuView::highlighted_index() {
|
|
|
|
return highlighted_item;
|
|
|
|
}
|
|
|
|
|
2015-07-08 15:39:24 +00:00
|
|
|
void MenuView::on_focus() {
|
2017-12-10 03:34:11 +00:00
|
|
|
item_view(highlighted_item - offset)->highlight();
|
2015-07-08 15:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuView::on_blur() {
|
2017-12-10 03:34:11 +00:00
|
|
|
if (!keep_highlight)
|
|
|
|
item_view(highlighted_item - offset)->unhighlight();
|
2015-07-08 15:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuView::on_key(const KeyEvent key) {
|
|
|
|
switch(key) {
|
|
|
|
case KeyEvent::Up:
|
2017-12-08 18:58:46 +00:00
|
|
|
return set_highlighted(highlighted_item - 1);
|
2015-07-08 15:39:24 +00:00
|
|
|
|
|
|
|
case KeyEvent::Down:
|
2017-12-08 18:58:46 +00:00
|
|
|
return set_highlighted(highlighted_item + 1);
|
2015-07-08 15:39:24 +00:00
|
|
|
|
|
|
|
case KeyEvent::Select:
|
|
|
|
case KeyEvent::Right:
|
2017-12-10 03:34:11 +00:00
|
|
|
if( menu_items[highlighted_item].on_select ) {
|
|
|
|
menu_items[highlighted_item].on_select();
|
|
|
|
}
|
2015-07-08 15:39:24 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case KeyEvent::Left:
|
|
|
|
if( on_left ) {
|
|
|
|
on_left();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuView::on_encoder(const EncoderEvent event) {
|
2017-12-08 18:58:46 +00:00
|
|
|
set_highlighted(highlighted_item + event);
|
2015-07-08 15:39:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: This could be handled by default behavior, if the UI stack were to
|
|
|
|
* transmit consumable events from the top of the hit-stack down, and each
|
|
|
|
* MenuItem could respond to a touch and update its parent MenuView.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
bool MenuView::on_touch(const TouchEvent event) {
|
|
|
|
size_t i = 0;
|
|
|
|
for(const auto child : children_) {
|
|
|
|
if( child->screen_rect().contains(event.point) ) {
|
|
|
|
return set_highlighted(i);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} /* namespace ui */
|