diff --git a/src/dialogxml/widgets/stack.cpp b/src/dialogxml/widgets/stack.cpp index 56b8cb71..f41783d1 100644 --- a/src/dialogxml/widgets/stack.cpp +++ b/src/dialogxml/widgets/stack.cpp @@ -13,6 +13,7 @@ #include "message.hpp" #include "pict.hpp" #include "scrollbar.hpp" +#include "mathutil.hpp" #include bool cStack::hasChild(std::string id) const { @@ -84,6 +85,17 @@ bool cStack::setPage(size_t n) { return !failed; } +void cStack::doSelectPage(int dir, bool loop) { + curPage += dir; + if(loop){ + if(curPage < 0) curPage += nPages; + else if(curPage >= nPages) curPage -= nPages; + }else{ + curPage = minmax(0, nPages - 1, curPage); + } + setPage(curPage); +} + size_t cStack::getPage() const { return curPage; } diff --git a/src/dialogxml/widgets/stack.hpp b/src/dialogxml/widgets/stack.hpp index d8c645ce..53e8175b 100644 --- a/src/dialogxml/widgets/stack.hpp +++ b/src/dialogxml/widgets/stack.hpp @@ -57,6 +57,10 @@ public: /// @param The new page number /// @return false if the page could not be changed, usually due to a focus handler bool setPage(size_t n); + /// Page forward or backward in the stack + /// @param dir Usually -1 or 1 + /// @param loop Beyond the first and last page, loop to the other side + void doSelectPage(int dir, bool loop = true); /// Get the current page the stack is displaying. /// @return The current page number size_t getPage() const; diff --git a/src/game/boe.dlgutil.cpp b/src/game/boe.dlgutil.cpp index 8c155fa1..fa64a81d 100644 --- a/src/game/boe.dlgutil.cpp +++ b/src/game/boe.dlgutil.cpp @@ -1645,11 +1645,7 @@ class cChooseScenario { bool doSelectPage(int dir) { auto& stk = dynamic_cast(me["list"]); - int curPage = stk.getPage(), nPages = stk.getPageCount(); - curPage += dir; - if(curPage < 0) curPage += nPages; - else if(curPage >= nPages) curPage -= nPages; - stk.setPage(curPage); + stk.doSelectPage(dir, true); return true; }