From 280acfd227384ef3a15829983920f79ad09237ac Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 8 Dec 2015 14:16:36 -0800 Subject: [PATCH] First commit of AIS data scroller. It's weird and buggy right now, but I think I can make it work sensibly. --- firmware/application/app_ais.cpp | 42 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/firmware/application/app_ais.cpp b/firmware/application/app_ais.cpp index 5c387e5bd..b39608798 100644 --- a/firmware/application/app_ais.cpp +++ b/firmware/application/app_ais.cpp @@ -432,23 +432,35 @@ void AISView::paint(Painter& painter) { const auto& s = style(); Rect target_rect { r.pos, { r.width(), s.font.line_height() }}; - bool found_selected_item = false; - for(const auto entry : recent) { - const auto next_y = target_rect.pos.y + target_rect.height(); - const auto last_visible_entry = (next_y >= r.bottom()); + const size_t visible_item_count = r.height() / s.font.line_height(); + auto selected = selected_entry(); + if( selected == std::end(recent) ) { + selected = std::begin(recent); + } + + RecentEntries::iterator start = selected; + RecentEntries::iterator end = selected; + size_t i = 0; + + // Move start iterator toward first entry. + while( (start != std::begin(recent)) && (i < visible_item_count / 2) ) { + std::advance(start, -1); + i++; + } + + // Move end iterator toward last entry. + while( (end != std::end(recent)) && (i < visible_item_count) ) { + std::advance(end, 1); + i++; + } + + for(auto p = start; p != end; p++) { + const auto& entry = *p; const auto is_selected_key = (selected_key == entry.mmsi); - found_selected_item |= is_selected_key; - - if( !last_visible_entry || (last_visible_entry && found_selected_item) ) { - const auto& draw_style = (has_focus && is_selected_key) ? s.invert() : s; - draw_entry(entry, target_rect, painter, draw_style); - target_rect.pos.y += target_rect.height(); - } - - if( last_visible_entry && found_selected_item ) { - break; - } + const auto& draw_style = (has_focus && is_selected_key) ? s.invert() : s; + draw_entry(entry, target_rect, painter, draw_style); + target_rect.pos.y += target_rect.height(); } }