From fb55b1cd91cfa425b407de319edfeed31004b5a8 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Sat, 26 Oct 2013 15:57:46 -0500 Subject: [PATCH] Limit OptionListComponent to 5 entries on screen. Can now hold up/down to scroll OptionListComponent. --- src/components/OptionListComponent.h | 82 ++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 18 deletions(-) diff --git a/src/components/OptionListComponent.h b/src/components/OptionListComponent.h index d28d5b63e..f81d248ea 100644 --- a/src/components/OptionListComponent.h +++ b/src/components/OptionListComponent.h @@ -167,7 +167,7 @@ private: { public: OptionListPopup(Window* window, OptionListComponent& optList) : GuiComponent(window), - mOptList(optList), mBox(window, ":/textbox.png"), mCursor(0), mScrollOffset(0) + mOptList(optList), mBox(window, ":/textbox.png"), mCursor(0), mScrollOffset(0), mCursorTimer(0) { //find global position GuiComponent* p = &mOptList; @@ -185,7 +185,11 @@ private: std::shared_ptr font = mOptList.getFont(); - unsigned int renderCount = mTextCaches.size() - mScrollOffset; + unsigned int renderCount = getPageSize(); + if(renderCount + mScrollOffset > mTextCaches.size()) + renderCount = mTextCaches.size() - mScrollOffset; + + unsigned int renderTo = mScrollOffset + renderCount; float height = (float)renderCount * font->getHeight(); trans.translate(Eigen::Vector3f(0, -height / 2 + font->getHeight() * 0.5f, 0)); @@ -196,7 +200,7 @@ private: Renderer::setMatrix(trans); Renderer::drawRect(0, 0, (int)getSize().x(), (int)height, 0xFFFFFFFF); - for(unsigned int i = mScrollOffset; i < renderCount; i++) + for(unsigned int i = mScrollOffset; i < renderTo; i++) { Renderer::setMatrix(trans); @@ -232,29 +236,64 @@ private: return true; } - if(mOptList.mEntries.size() > 1) + if(config->isMappedTo("up", input)) { - if(config->isMappedTo("up", input)) - { - if(mCursor > 0) - mCursor--; - - return true; - } - if(config->isMappedTo("down", input)) - { - if(mCursor < mOptList.mEntries.size() - 1) - mCursor++; - - return true; - } + mCursorDir = -1; + mCursorTimer = -350; + moveCursor(); + return true; } + if(config->isMappedTo("down", input)) + { + mCursorDir = 1; + mCursorTimer = -350; + moveCursor(); + return true; + } + }else{ + if(config->isMappedTo("up", input) || config->isMappedTo("down", input)) + mCursorDir = 0; } return GuiComponent::input(config, input); } + + void update(int deltaTime) + { + if(mCursorDir != 0) + { + mCursorTimer += deltaTime; + while(mCursorTimer >= 100) + { + moveCursor(); + mCursorTimer -= 100; + } + } + } private: + void moveCursor() + { + if(mOptList.mEntries.size() == 0) + return; + + if(mCursorDir < 0) //scroll up + { + if(mCursor > 0) + mCursor--; + + if(mCursor < mScrollOffset) + mScrollOffset--; + }else if(mCursorDir > 0) //scroll down + { + if(mCursor < mOptList.mEntries.size() - 1) + mCursor++; + + if(mCursor - mScrollOffset >= getPageSize()) + mScrollOffset++; + } + } + void close() { delete this; @@ -277,10 +316,17 @@ private: } } + unsigned int getPageSize() + { + return 5; + } + OptionListComponent& mOptList; NinePatchComponent mBox; unsigned int mCursor; + int mCursorDir; + int mCursorTimer; unsigned int mScrollOffset; std::vector mTextCaches; };