2016-01-18 00:42:15 -05: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __RECENT_ENTRIES_H__
|
|
|
|
#define __RECENT_ENTRIES_H__
|
|
|
|
|
|
|
|
#include "ui_widget.hpp"
|
2016-01-26 20:25:51 -05:00
|
|
|
#include "ui_font_fixed_8x16.hpp"
|
2016-01-18 00:42:15 -05:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <list>
|
|
|
|
#include <utility>
|
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2016-09-03 19:38:44 -04:00
|
|
|
template<class Entry>
|
2016-09-04 01:53:44 -04:00
|
|
|
using RecentEntries = std::list<Entry>;
|
2016-01-18 00:42:15 -05:00
|
|
|
|
2016-09-03 21:26:48 -04:00
|
|
|
template<typename ContainerType, typename Key>
|
|
|
|
typename ContainerType::const_iterator find(const ContainerType& entries, const Key key) {
|
|
|
|
return std::find_if(
|
|
|
|
std::begin(entries), std::end(entries),
|
|
|
|
[key](typename ContainerType::const_reference e) { return e.key() == key; }
|
|
|
|
);
|
|
|
|
}
|
2016-01-18 00:42:15 -05:00
|
|
|
|
2016-09-03 21:26:48 -04:00
|
|
|
template<typename ContainerType>
|
|
|
|
static void truncate_entries(ContainerType& entries, const size_t entries_max = 64) {
|
|
|
|
while(entries.size() > entries_max) {
|
|
|
|
entries.pop_back();
|
2016-01-18 00:42:15 -05:00
|
|
|
}
|
2016-09-03 21:26:48 -04:00
|
|
|
}
|
2016-01-18 00:42:15 -05:00
|
|
|
|
2016-09-03 21:26:48 -04:00
|
|
|
template<typename ContainerType, typename Key>
|
|
|
|
typename ContainerType::reference on_packet(ContainerType& entries, const Key key) {
|
|
|
|
auto matching_recent = find(entries, key);
|
|
|
|
if( matching_recent != std::end(entries) ) {
|
|
|
|
// Found within. Move to front of list, increment counter.
|
|
|
|
entries.push_front(*matching_recent);
|
|
|
|
entries.erase(matching_recent);
|
|
|
|
} else {
|
|
|
|
entries.emplace_front(key);
|
|
|
|
truncate_entries(entries);
|
2016-01-18 00:42:15 -05:00
|
|
|
}
|
2016-09-03 21:26:48 -04:00
|
|
|
|
|
|
|
return entries.front();
|
|
|
|
}
|
2016-01-18 00:42:15 -05:00
|
|
|
|
2016-09-03 20:09:03 -04:00
|
|
|
template<typename ContainerType>
|
|
|
|
static std::pair<typename ContainerType::const_iterator, typename ContainerType::const_iterator> range_around(
|
|
|
|
const ContainerType& entries,
|
|
|
|
typename ContainerType::const_iterator item,
|
|
|
|
const size_t count
|
|
|
|
) {
|
|
|
|
auto start = item;
|
|
|
|
auto end = item;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
// Move start iterator toward first entry.
|
|
|
|
while( (start != std::begin(entries)) && (i < count / 2) ) {
|
|
|
|
std::advance(start, -1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move end iterator toward last entry.
|
|
|
|
while( (end != std::end(entries)) && (i < count) ) {
|
|
|
|
std::advance(end, 1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { start, end };
|
|
|
|
}
|
|
|
|
|
2016-01-18 00:42:15 -05:00
|
|
|
namespace ui {
|
|
|
|
|
2016-09-03 01:22:30 -04:00
|
|
|
using RecentEntriesColumn = std::pair<std::string, size_t>;
|
|
|
|
|
2016-09-05 15:34:41 -04:00
|
|
|
class RecentEntriesColumns {
|
2016-01-18 00:42:15 -05:00
|
|
|
public:
|
2016-09-05 15:34:41 -04:00
|
|
|
using ContainerType = std::vector<RecentEntriesColumn>;
|
2016-01-18 00:42:15 -05:00
|
|
|
|
2016-09-05 15:34:41 -04:00
|
|
|
RecentEntriesColumns(
|
|
|
|
const std::initializer_list<RecentEntriesColumn> columns
|
2016-09-05 19:49:44 -04:00
|
|
|
);
|
2016-09-05 15:34:41 -04:00
|
|
|
|
|
|
|
ContainerType::const_iterator begin() const { return std::begin(_columns); }
|
|
|
|
ContainerType::const_iterator end() const { return std::end(_columns); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ContainerType _columns;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RecentEntriesHeader : public Widget {
|
|
|
|
public:
|
|
|
|
RecentEntriesHeader(
|
|
|
|
const RecentEntriesColumns& columns
|
2016-09-05 19:49:44 -04:00
|
|
|
);
|
2016-09-03 01:22:30 -04:00
|
|
|
|
2016-09-05 19:49:44 -04:00
|
|
|
void paint(Painter& painter) override;
|
2016-09-05 15:09:29 -04:00
|
|
|
|
|
|
|
private:
|
2016-09-05 15:34:41 -04:00
|
|
|
const RecentEntriesColumns& _columns;
|
2016-09-05 15:09:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class Entries>
|
|
|
|
class RecentEntriesTable : public Widget {
|
|
|
|
public:
|
|
|
|
using Entry = typename Entries::value_type;
|
|
|
|
|
|
|
|
std::function<void(const Entry& entry)> on_select;
|
|
|
|
|
|
|
|
RecentEntriesTable(
|
|
|
|
Entries& recent
|
|
|
|
) : recent { recent }
|
|
|
|
{
|
|
|
|
set_focusable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void paint(Painter& painter) override {
|
|
|
|
const auto r = screen_rect();
|
|
|
|
const auto& s = style();
|
|
|
|
|
|
|
|
Rect target_rect { r.pos, { r.width(), s.font.line_height() }};
|
|
|
|
const size_t visible_item_count = r.height() / s.font.line_height();
|
2016-01-26 20:25:51 -05:00
|
|
|
|
2016-09-03 21:26:48 -04:00
|
|
|
auto selected = find(recent, selected_key);
|
2016-01-18 00:42:15 -05:00
|
|
|
if( selected == std::end(recent) ) {
|
|
|
|
selected = std::begin(recent);
|
|
|
|
}
|
|
|
|
|
2016-09-03 20:09:03 -04:00
|
|
|
auto range = range_around(recent, selected, visible_item_count);
|
2016-01-18 00:42:15 -05:00
|
|
|
|
|
|
|
for(auto p = range.first; p != range.second; p++) {
|
|
|
|
const auto& entry = *p;
|
|
|
|
const auto is_selected_key = (selected_key == entry.key());
|
2016-09-03 01:44:40 -04:00
|
|
|
const auto item_style = (has_focus() && is_selected_key) ? s.invert() : s;
|
|
|
|
draw(entry, target_rect, painter, item_style);
|
2016-01-18 00:42:15 -05:00
|
|
|
target_rect.pos.y += target_rect.height();
|
|
|
|
}
|
2016-01-18 00:56:06 -05:00
|
|
|
|
|
|
|
painter.fill_rectangle(
|
|
|
|
{ target_rect.left(), target_rect.top(), target_rect.width(), r.bottom() - target_rect.top() },
|
|
|
|
style().background
|
|
|
|
);
|
2016-01-18 00:42:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool on_encoder(const EncoderEvent event) override {
|
|
|
|
advance(event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool on_key(const ui::KeyEvent event) override {
|
|
|
|
if( event == ui::KeyEvent::Select ) {
|
|
|
|
if( on_select ) {
|
2016-09-03 21:26:48 -04:00
|
|
|
const auto selected = find(recent, selected_key);
|
2016-01-18 00:42:15 -05:00
|
|
|
if( selected != std::end(recent) ) {
|
|
|
|
on_select(*selected);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-27 13:49:17 -05:00
|
|
|
void on_focus() override {
|
|
|
|
advance(0);
|
|
|
|
}
|
|
|
|
|
2016-01-18 00:42:15 -05:00
|
|
|
private:
|
|
|
|
Entries& recent;
|
|
|
|
|
|
|
|
using EntryKey = typename Entry::Key;
|
2016-01-27 13:49:17 -05:00
|
|
|
EntryKey selected_key = Entry::invalid_key;
|
2016-01-18 00:42:15 -05:00
|
|
|
|
|
|
|
void advance(const int32_t amount) {
|
2016-09-03 21:26:48 -04:00
|
|
|
auto selected = find(recent, selected_key);
|
2016-01-18 00:42:15 -05:00
|
|
|
if( selected == std::end(recent) ) {
|
|
|
|
if( recent.empty() ) {
|
|
|
|
selected_key = Entry::invalid_key;
|
|
|
|
} else {
|
|
|
|
selected_key = recent.front().key();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if( amount < 0 ) {
|
|
|
|
if( selected != std::begin(recent) ) {
|
|
|
|
std::advance(selected, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( amount > 0 ) {
|
|
|
|
std::advance(selected, 1);
|
|
|
|
if( selected == std::end(recent) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selected_key = selected->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:09:29 -04:00
|
|
|
void draw(
|
|
|
|
const Entry& entry,
|
2016-01-26 20:25:51 -05:00
|
|
|
const Rect& target_rect,
|
|
|
|
Painter& painter,
|
|
|
|
const Style& style
|
2016-09-05 15:09:29 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Entries>
|
|
|
|
class RecentEntriesView : public View {
|
|
|
|
public:
|
|
|
|
using Entry = typename Entries::value_type;
|
|
|
|
|
|
|
|
std::function<void(const Entry& entry)> on_select;
|
|
|
|
|
|
|
|
RecentEntriesView(
|
2016-09-05 15:34:41 -04:00
|
|
|
const RecentEntriesColumns& columns,
|
2016-09-05 15:09:29 -04:00
|
|
|
Entries& recent
|
2016-09-05 15:34:41 -04:00
|
|
|
) : _header { columns },
|
|
|
|
_table { recent }
|
2016-09-05 15:09:29 -04:00
|
|
|
{
|
2016-09-05 17:53:04 -04:00
|
|
|
add_children({
|
2016-09-05 15:09:29 -04:00
|
|
|
&_header,
|
|
|
|
&_table,
|
2016-09-05 17:53:04 -04:00
|
|
|
});
|
2016-09-05 15:09:29 -04:00
|
|
|
|
|
|
|
_table.on_select = [this](const Entry& entry) { if( this->on_select ) { this->on_select(entry); } };
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_parent_rect(const Rect new_parent_rect) override {
|
|
|
|
constexpr Dim scale_height = 16;
|
|
|
|
|
|
|
|
View::set_parent_rect(new_parent_rect);
|
|
|
|
_header.set_parent_rect({ 0, 0, new_parent_rect.width(), scale_height });
|
|
|
|
_table.set_parent_rect({
|
|
|
|
0, scale_height,
|
|
|
|
new_parent_rect.width(),
|
|
|
|
new_parent_rect.height() - scale_height
|
|
|
|
});
|
2016-09-03 01:22:30 -04:00
|
|
|
}
|
2016-01-26 20:25:51 -05:00
|
|
|
|
2016-09-05 15:09:29 -04:00
|
|
|
void paint(Painter&) override {
|
|
|
|
// Children completely cover this View, do not paint.
|
|
|
|
// TODO: What happens here shouldn't matter if I do proper damage detection!
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_focus() override {
|
|
|
|
_table.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RecentEntriesHeader _header;
|
|
|
|
RecentEntriesTable<Entries> _table;
|
2016-01-18 00:42:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace ui */
|
|
|
|
|
|
|
|
#endif/*__RECENT_ENTRIES_H__*/
|