From 0535f8273b94783acab7d26615dfe88d6b5c60c7 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Sun, 10 Aug 2014 10:09:42 +0200 Subject: [PATCH] Rewrote the manual paging code Nebuleon spotted a bug in TextManualDialog where the unsigned value 'pages[page].text.size() - rowsPerPage' could wrap around at 0 for short chapters. I decided to change a bit more than just fixing the bug though. --- src/textdialog.cpp | 15 +++++++-------- src/textmanualdialog.cpp | 19 +++++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/textdialog.cpp b/src/textdialog.cpp index ae26730..8ca2e3b 100644 --- a/src/textdialog.cpp +++ b/src/textdialog.cpp @@ -23,6 +23,8 @@ #include "gmenu2x.h" #include "utilities.h" +#include + using namespace std; TextDialog::TextDialog(GMenu2X *gmenu2x, const string &title, const string &description, const string &icon, const string &text) @@ -79,6 +81,8 @@ void TextDialog::exec() { unsigned int contentY, contentHeight; tie(contentY, contentHeight) = gmenu2x->getContentArea(); const unsigned rowsPerPage = contentHeight / fontHeight; + const unsigned maxFirstRow = + text.size() < rowsPerPage ? 0 : text.size() - rowsPerPage; contentY += (contentHeight % fontHeight) / 2; unsigned firstRow = 0; @@ -94,19 +98,14 @@ void TextDialog::exec() { if (firstRow > 0) firstRow--; break; case InputManager::DOWN: - if (firstRow + rowsPerPage < text.size()) firstRow++; + if (firstRow < maxFirstRow) firstRow++; break; case InputManager::ALTLEFT: - if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1; + if (firstRow >= rowsPerPage - 1) firstRow -= rowsPerPage - 1; else firstRow = 0; break; case InputManager::ALTRIGHT: - if (firstRow + rowsPerPage*2 -1 < text.size()) { - firstRow += rowsPerPage-1; - } else { - firstRow = text.size() < rowsPerPage ? - 0 : text.size() - rowsPerPage; - } + firstRow = min(firstRow + rowsPerPage - 1, maxFirstRow); break; case InputManager::SETTINGS: case InputManager::CANCEL: diff --git a/src/textmanualdialog.cpp b/src/textmanualdialog.cpp index a198018..0632c0c 100644 --- a/src/textmanualdialog.cpp +++ b/src/textmanualdialog.cpp @@ -68,9 +68,6 @@ TextManualDialog::TextManualDialog(GMenu2X *gmenu2x, const string &title, const } void TextManualDialog::exec() { - bool close = false; - uint page=0; - Surface bg(gmenu2x->bg); //link icon @@ -89,13 +86,17 @@ void TextManualDialog::exec() { bg.convertToDisplayFormat(); - uint firstRow = 0, rowsPerPage = 180/gmenu2x->font->getLineSpacing(); stringstream ss; ss << pages.size(); string spagecount; ss >> spagecount; string pageStatus; + const unsigned rowsPerPage = 180 / gmenu2x->font->getLineSpacing(); + + unsigned page = 0, firstRow = 0; + bool close = false; + while (!close) { Surface& s = *gmenu2x->s; @@ -111,12 +112,15 @@ void TextManualDialog::exec() { s.flip(); + const unsigned maxFirstRow = pages[page].text.size() < rowsPerPage + ? 0 : pages[page].text.size() - rowsPerPage; + switch(gmenu2x->input.waitForPressedButton()) { case InputManager::UP: if (firstRow > 0) firstRow--; break; case InputManager::DOWN: - if (firstRow + rowsPerPage < pages[page].text.size()) firstRow++; + if (firstRow < maxFirstRow) firstRow++; break; case InputManager::LEFT: if (page > 0) { @@ -131,12 +135,11 @@ void TextManualDialog::exec() { } break; case InputManager::ALTLEFT: - if (firstRow >= rowsPerPage-1) firstRow -= rowsPerPage-1; + if (firstRow >= rowsPerPage - 1) firstRow -= rowsPerPage - 1; else firstRow = 0; break; case InputManager::ALTRIGHT: - if (firstRow + rowsPerPage*2 -1 < pages[page].text.size()) firstRow += rowsPerPage-1; - else firstRow = max(0ul, (unsigned long) (pages[page].text.size() - rowsPerPage)); + firstRow = min(firstRow + rowsPerPage - 1, maxFirstRow); break; case InputManager::CANCEL: case InputManager::SETTINGS: