mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-08-14 01:15:38 -04:00
Formatted code (#1007)
* Updated style * Updated files * fixed new line * Updated spacing * File fix WIP * Updated to clang 13 * updated comment style * Removed old comment code
This commit is contained in:
parent
7aca7ce74d
commit
033c4e9a5b
599 changed files with 70746 additions and 66896 deletions
|
@ -33,62 +33,60 @@
|
|||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
template<class Entry>
|
||||
template <class Entry>
|
||||
using RecentEntries = std::list<Entry>;
|
||||
|
||||
template<typename ContainerType, typename Key>
|
||||
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; }
|
||||
);
|
||||
return std::find_if(
|
||||
std::begin(entries), std::end(entries),
|
||||
[key](typename ContainerType::const_reference e) { return e.key() == key; });
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
template <typename ContainerType>
|
||||
static void truncate_entries(ContainerType& entries, const size_t entries_max = 64) {
|
||||
while(entries.size() > entries_max) {
|
||||
entries.pop_back();
|
||||
}
|
||||
while (entries.size() > entries_max) {
|
||||
entries.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename ContainerType, typename Key>
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
return entries.front();
|
||||
return entries.front();
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
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;
|
||||
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 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++;
|
||||
}
|
||||
// Move end iterator toward last entry.
|
||||
while ((end != std::end(entries)) && (i < count)) {
|
||||
std::advance(end, 1);
|
||||
i++;
|
||||
}
|
||||
|
||||
return { start, end };
|
||||
return {start, end};
|
||||
}
|
||||
|
||||
namespace ui {
|
||||
|
@ -96,201 +94,189 @@ namespace ui {
|
|||
using RecentEntriesColumn = std::pair<std::string, size_t>;
|
||||
|
||||
class RecentEntriesColumns {
|
||||
public:
|
||||
using ContainerType = std::vector<RecentEntriesColumn>;
|
||||
public:
|
||||
using ContainerType = std::vector<RecentEntriesColumn>;
|
||||
|
||||
RecentEntriesColumns(
|
||||
const std::initializer_list<RecentEntriesColumn> columns
|
||||
);
|
||||
RecentEntriesColumns(
|
||||
const std::initializer_list<RecentEntriesColumn> columns);
|
||||
|
||||
ContainerType::const_iterator begin() const { return std::begin(_columns); }
|
||||
ContainerType::const_iterator end() const { return std::end(_columns); }
|
||||
ContainerType::const_iterator begin() const { return std::begin(_columns); }
|
||||
ContainerType::const_iterator end() const { return std::end(_columns); }
|
||||
|
||||
private:
|
||||
const ContainerType _columns;
|
||||
private:
|
||||
const ContainerType _columns;
|
||||
};
|
||||
|
||||
class RecentEntriesHeader : public Widget {
|
||||
public:
|
||||
RecentEntriesHeader(
|
||||
const RecentEntriesColumns& columns
|
||||
);
|
||||
public:
|
||||
RecentEntriesHeader(
|
||||
const RecentEntriesColumns& columns);
|
||||
|
||||
void paint(Painter& painter) override;
|
||||
void paint(Painter& painter) override;
|
||||
|
||||
private:
|
||||
const RecentEntriesColumns& _columns;
|
||||
private:
|
||||
const RecentEntriesColumns& _columns;
|
||||
};
|
||||
|
||||
template<class Entries>
|
||||
template <class Entries>
|
||||
class RecentEntriesTable : public Widget {
|
||||
public:
|
||||
using Entry = typename Entries::value_type;
|
||||
public:
|
||||
using Entry = typename Entries::value_type;
|
||||
|
||||
std::function<void(const Entry& entry)> on_select { };
|
||||
std::function<void(const Entry& entry)> on_select{};
|
||||
|
||||
RecentEntriesTable(
|
||||
Entries& recent
|
||||
) : recent { recent }
|
||||
{
|
||||
set_focusable(true);
|
||||
}
|
||||
RecentEntriesTable(
|
||||
Entries& recent)
|
||||
: recent{recent} {
|
||||
set_focusable(true);
|
||||
}
|
||||
|
||||
void paint(Painter& painter) override {
|
||||
const auto r = screen_rect();
|
||||
const auto& s = style();
|
||||
void paint(Painter& painter) override {
|
||||
const auto r = screen_rect();
|
||||
const auto& s = style();
|
||||
|
||||
Rect target_rect { r.location(), { r.width(), s.font.line_height() }};
|
||||
const size_t visible_item_count = r.height() / s.font.line_height();
|
||||
Rect target_rect{r.location(), {r.width(), s.font.line_height()}};
|
||||
const size_t visible_item_count = r.height() / s.font.line_height();
|
||||
|
||||
auto selected = find(recent, selected_key);
|
||||
if( selected == std::end(recent) ) {
|
||||
selected = std::begin(recent);
|
||||
}
|
||||
auto selected = find(recent, selected_key);
|
||||
if (selected == std::end(recent)) {
|
||||
selected = std::begin(recent);
|
||||
}
|
||||
|
||||
auto range = range_around(recent, selected, visible_item_count);
|
||||
auto range = range_around(recent, selected, visible_item_count);
|
||||
|
||||
for(auto p = range.first; p != range.second; p++) {
|
||||
const auto& entry = *p;
|
||||
const auto is_selected_key = (selected_key == entry.key());
|
||||
const auto item_style = (has_focus() && is_selected_key) ? s.invert() : s;
|
||||
draw(entry, target_rect, painter, item_style);
|
||||
target_rect += { 0, target_rect.height() };
|
||||
}
|
||||
for (auto p = range.first; p != range.second; p++) {
|
||||
const auto& entry = *p;
|
||||
const auto is_selected_key = (selected_key == entry.key());
|
||||
const auto item_style = (has_focus() && is_selected_key) ? s.invert() : s;
|
||||
draw(entry, target_rect, painter, item_style);
|
||||
target_rect += {0, target_rect.height()};
|
||||
}
|
||||
|
||||
painter.fill_rectangle(
|
||||
{ target_rect.left(), target_rect.top(), target_rect.width(), r.bottom() - target_rect.top() },
|
||||
style().background
|
||||
);
|
||||
}
|
||||
painter.fill_rectangle(
|
||||
{target_rect.left(), target_rect.top(), target_rect.width(), r.bottom() - target_rect.top()},
|
||||
style().background);
|
||||
}
|
||||
|
||||
bool on_encoder(const EncoderEvent event) override {
|
||||
advance(event);
|
||||
return true;
|
||||
}
|
||||
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 ) {
|
||||
const auto selected = find(recent, selected_key);
|
||||
if( selected != std::end(recent) ) {
|
||||
on_select(*selected);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( event == ui::KeyEvent::Up ) {
|
||||
if(selected_key == recent.front().key()){
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
advance(-1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if( event == ui::KeyEvent::Down ) {
|
||||
if( selected_key == recent.back().key()) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
advance(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool on_key(const ui::KeyEvent event) override {
|
||||
if (event == ui::KeyEvent::Select) {
|
||||
if (on_select) {
|
||||
const auto selected = find(recent, selected_key);
|
||||
if (selected != std::end(recent)) {
|
||||
on_select(*selected);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (event == ui::KeyEvent::Up) {
|
||||
if (selected_key == recent.front().key()) {
|
||||
return false;
|
||||
} else {
|
||||
advance(-1);
|
||||
return true;
|
||||
}
|
||||
} else if (event == ui::KeyEvent::Down) {
|
||||
if (selected_key == recent.back().key()) {
|
||||
return false;
|
||||
} else {
|
||||
advance(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void on_focus() override {
|
||||
advance(0);
|
||||
}
|
||||
void on_focus() override {
|
||||
advance(0);
|
||||
}
|
||||
|
||||
private:
|
||||
Entries& recent;
|
||||
|
||||
using EntryKey = typename Entry::Key;
|
||||
EntryKey selected_key = Entry::invalid_key;
|
||||
private:
|
||||
Entries& recent;
|
||||
|
||||
void advance(const int32_t amount) {
|
||||
auto selected = find(recent, selected_key);
|
||||
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();
|
||||
}
|
||||
using EntryKey = typename Entry::Key;
|
||||
EntryKey selected_key = Entry::invalid_key;
|
||||
|
||||
set_dirty();
|
||||
}
|
||||
void advance(const int32_t amount) {
|
||||
auto selected = find(recent, selected_key);
|
||||
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();
|
||||
}
|
||||
|
||||
void draw(
|
||||
const Entry& entry,
|
||||
const Rect& target_rect,
|
||||
Painter& painter,
|
||||
const Style& style
|
||||
);
|
||||
set_dirty();
|
||||
}
|
||||
|
||||
void draw(
|
||||
const Entry& entry,
|
||||
const Rect& target_rect,
|
||||
Painter& painter,
|
||||
const Style& style);
|
||||
};
|
||||
|
||||
template<class Entries>
|
||||
template <class Entries>
|
||||
class RecentEntriesView : public View {
|
||||
public:
|
||||
using Entry = typename Entries::value_type;
|
||||
public:
|
||||
using Entry = typename Entries::value_type;
|
||||
|
||||
std::function<void(const Entry& entry)> on_select { };
|
||||
std::function<void(const Entry& entry)> on_select{};
|
||||
|
||||
RecentEntriesView(
|
||||
const RecentEntriesColumns& columns,
|
||||
Entries& recent
|
||||
) : _header { columns },
|
||||
_table { recent }
|
||||
{
|
||||
add_children({
|
||||
&_header,
|
||||
&_table,
|
||||
});
|
||||
RecentEntriesView(
|
||||
const RecentEntriesColumns& columns,
|
||||
Entries& recent)
|
||||
: _header{columns},
|
||||
_table{recent} {
|
||||
add_children({
|
||||
&_header,
|
||||
&_table,
|
||||
});
|
||||
|
||||
_table.on_select = [this](const Entry& entry) { if( this->on_select ) { this->on_select(entry); } };
|
||||
}
|
||||
_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;
|
||||
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
|
||||
});
|
||||
}
|
||||
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});
|
||||
}
|
||||
|
||||
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 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 focus() override {
|
||||
_table.focus();
|
||||
}
|
||||
void focus() override {
|
||||
_table.focus();
|
||||
}
|
||||
|
||||
private:
|
||||
RecentEntriesHeader _header;
|
||||
RecentEntriesTable<Entries> _table;
|
||||
private:
|
||||
RecentEntriesHeader _header;
|
||||
RecentEntriesTable<Entries> _table;
|
||||
};
|
||||
|
||||
} /* namespace ui */
|
||||
|
||||
#endif/*__RECENT_ENTRIES_H__*/
|
||||
#endif /*__RECENT_ENTRIES_H__*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue