From 3e486b7f8432090b47d91f7d041c59fc34b20a16 Mon Sep 17 00:00:00 2001 From: Andreas K Date: Thu, 19 Nov 2026 10:35:46 +0100 Subject: [PATCH 1/2] Add a scrollbar and mouse wheel scrolling to select boxes - Draw a scrollbar track with a draggable slider between the scroll buttons - Scroll by whole lines and only draw/click the visible entries - Show the scroll buttons and scrollbar only when needed - Make the whole row of an entry clickable - Scroll select boxes with the mouse wheel: the wheel goes to the topmost window under the cursor, otherwise to the active menus --- CGame_Event.cpp | 23 ++++ CIO/CControlContainer.cpp | 6 + CIO/CControlContainer.h | 1 + CIO/CFont.cpp | 5 +- CIO/CFont.h | 4 +- CIO/CSelectBox.cpp | 278 +++++++++++++++++++++++--------------- CIO/CSelectBox.h | 29 +++- CIO/CWindow.cpp | 6 + CIO/CWindow.h | 1 + 9 files changed, 240 insertions(+), 113 deletions(-) diff --git a/CGame_Event.cpp b/CGame_Event.cpp index 2767017..b818076 100644 --- a/CGame_Event.cpp +++ b/CGame_Event.cpp @@ -380,6 +380,29 @@ void CGame::EventHandling(SDL_Event* Event) break; } + case SDL_MOUSEWHEEL: + { + CWindow* wndAtCursor = nullptr; + for(auto& Window : Windows) + { + if(!Window->isWaste() && isPointInRect(Cursor.pos, Rect(Window->getPos(), Window->getSize())) + && (!wndAtCursor || Window->getPriority() > wndAtCursor->getPriority())) + wndAtCursor = Window.get(); + } + if(wndAtCursor) + { + // Positions of the controls are relative to the window + wndAtCursor->setMouseWheel(Event->wheel, Cursor.pos - wndAtCursor->getPos()); + break; + } + for(auto& Menu : Menus) + { + if(Menu->isActive() && !Menu->isWaste()) + Menu->setMouseWheel(Event->wheel, Cursor.pos); + } + break; + } + case SDL_WINDOWEVENT: { if(Event->window.event == SDL_WINDOWEVENT_RESIZED) diff --git a/CIO/CControlContainer.cpp b/CIO/CControlContainer.cpp index 9a53aee..3c8b33c 100644 --- a/CIO/CControlContainer.cpp +++ b/CIO/CControlContainer.cpp @@ -61,6 +61,12 @@ void CControlContainer::setMouseData(const SDL_MouseButtonEvent button) } } +void CControlContainer::setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos) +{ + for(const auto& selectbox : selectboxes) + selectbox->setMouseWheel(wheel, mousePos); +} + void CControlContainer::setKeyboardData(const SDL_KeyboardEvent& key) { for(const auto& textfield : textfields) diff --git a/CIO/CControlContainer.h b/CIO/CControlContainer.h index 2f945dd..8426a3a 100644 --- a/CIO/CControlContainer.h +++ b/CIO/CControlContainer.h @@ -75,6 +75,7 @@ class CControlContainer virtual void setMouseData(SDL_MouseMotionEvent motion); virtual void setMouseData(SDL_MouseButtonEvent button); void setKeyboardData(const SDL_KeyboardEvent& key); + virtual void setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos); void setWaste() { waste = true; } bool isWaste() const { return waste; } // Methods diff --git a/CIO/CFont.cpp b/CIO/CFont.cpp index dd91ebd..8924eac 100644 --- a/CIO/CFont.cpp +++ b/CIO/CFont.cpp @@ -40,16 +40,15 @@ void CFont::setText(std::string text) size_ = Extent(getTextWidth(string_, fontsize_), getLineHeight(fontsize_)); } -void CFont::setMouseData(SDL_MouseButtonEvent button) +void CFont::setMouseData(SDL_MouseButtonEvent button, const Rect& clickArea) { if(!callback) return; // left button is pressed if(button.button == SDL_BUTTON_LEFT) { - if(isPointInRect(button.x, button.y, Rect(pos_, size_))) + if(isPointInRect(button.x, button.y, clickArea)) { - // if mouse button is pressed ON the text if(button.state == SDL_PRESSED && getColor() == initialColor_) { const auto tmpInitialColor = initialColor_; diff --git a/CIO/CFont.h b/CIO/CFont.h index 2448235..c2198c1 100644 --- a/CIO/CFont.h +++ b/CIO/CFont.h @@ -45,7 +45,9 @@ class CFont callback = nullptr; clickedParam = 0; } - void setMouseData(SDL_MouseButtonEvent button); + void setMouseData(SDL_MouseButtonEvent button) { setMouseData(button, Rect(pos_, size_)); } + /// Handle a click with a custom clickable area (e.g. a whole row), in the same coordinates as the position + void setMouseData(SDL_MouseButtonEvent button, const Rect& clickArea); /// Draw this font's text at the given absolute position void draw(Position parentOrigin) const; diff --git a/CIO/CSelectBox.cpp b/CIO/CSelectBox.cpp index a8cc946..b758012 100644 --- a/CIO/CSelectBox.cpp +++ b/CIO/CSelectBox.cpp @@ -9,7 +9,10 @@ #include "../globals.h" #include "CButton.h" #include "CFont.h" +#include "CollisionDetection.h" #include +#include +#include CSelectBox::CSelectBox(Position pos, Extent size, FontSize fontsize, FontColor text_color, int bg_color) : pos_(pos), size_(size), fontsize(fontsize), text_color(text_color) @@ -17,22 +20,24 @@ CSelectBox::CSelectBox(Position pos, Extent size, FontSize fontsize, FontColor t setColor(bg_color); // button position is relative to the selectbox - ScrollUpButton = std::make_unique(nullptr, 0, Position(static_cast(size_.x) - 1 - 20, 0), - Extent(20, 20), BUTTON_GREY, nullptr, PICTURE_SMALL_ARROW_UP); - ScrollDownButton = std::make_unique( - nullptr, 0, Position(static_cast(size_.x) - 1 - 20, static_cast(size_.y) - 1 - 20), Extent(20, 20), - BUTTON_GREY, nullptr, PICTURE_SMALL_ARROW_DOWN); + const int scrollButtonX = static_cast(size_.x) - 1 - scrollButtonSize; + const Extent scrollButtonExtent(scrollButtonSize, scrollButtonSize); + ScrollUpButton = std::make_unique(nullptr, 0, Position(scrollButtonX, 0), scrollButtonExtent, BUTTON_GREY, + nullptr, PICTURE_SMALL_ARROW_UP); + ScrollDownButton = + std::make_unique(nullptr, 0, Position(scrollButtonX, static_cast(size_.y) - 1 - scrollButtonSize), + scrollButtonExtent, BUTTON_GREY, nullptr, PICTURE_SMALL_ARROW_DOWN); } void CSelectBox::addOption(const std::string& string, std::function callback, int param) { - // explanation: row_height = row_separator + fontsize - const unsigned row_height = getLineHeight(fontsize); + // The entries are moved when scrolling, so place the new one relative to the current scroll position + const int index = static_cast(Entries.size()); + const int y = textPadding + (index - scrollPos_) * static_cast(getLineHeight(fontsize)); - auto Entry = std::make_unique(string, Position(10, last_text_pos_y), fontsize, FontColor::Yellow); + auto Entry = std::make_unique(string, Position(textPadding, y), fontsize, FontColor::Yellow); Entry->setCallback(std::move(callback), param); Entries.emplace_back(std::move(Entry)); - last_text_pos_y += row_height; } void CSelectBox::setColor(int color) @@ -76,115 +81,157 @@ void CSelectBox::setColor(int color) } } +int CSelectBox::getPageSize() const +{ + return std::max(1, (static_cast(size_.y) - textPadding) / static_cast(getLineHeight(fontsize))); +} + +bool CSelectBox::isEntryVisible(int index) const +{ + return index >= scrollPos_ && index < scrollPos_ + getPageSize(); +} + +void CSelectBox::scroll(int lines) +{ + const auto rowHeight = static_cast(getLineHeight(fontsize)); + const int maxScrollPos = std::max(0, static_cast(Entries.size()) - getPageSize()); + const int newScrollPos = std::clamp(scrollPos_ + lines, 0, maxScrollPos); + const int offset = (scrollPos_ - newScrollPos) * rowHeight; + scrollPos_ = newScrollPos; + for(auto& entry : Entries) + entry->setPos(Position(entry->getPos().x, entry->getPos().y + offset)); +} + +bool CSelectBox::hasScrollbar() const +{ + return static_cast(Entries.size()) > getPageSize(); +} + +Rect CSelectBox::getScrollTrack() const +{ + const int height = std::max(0, static_cast(size_.y) - 1 - 2 * scrollButtonSize); + return Rect(Position(static_cast(size_.x) - 1 - scrollButtonSize, scrollButtonSize), + Extent(scrollButtonSize, height)); +} + +int CSelectBox::getSliderHeight() const +{ + const int range = static_cast(Entries.size()); + return std::max(1, static_cast(getScrollTrack().getSize().y) * getPageSize() / range); +} + +int CSelectBox::getSliderPosFromScroll() const +{ + const int range = static_cast(Entries.size()); + const int trackHeight = static_cast(getScrollTrack().getSize().y); + if(scrollPos_ + getPageSize() >= range) + return trackHeight - getSliderHeight(); + return trackHeight * scrollPos_ / range; +} + +int CSelectBox::clampSliderPos(int sliderPos) const +{ + const int maxSliderPos = static_cast(getScrollTrack().getSize().y) - getSliderHeight(); + return std::clamp(sliderPos, 0, std::max(0, maxSliderPos)); +} + +void CSelectBox::setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos) +{ + if(!isPointInRect(mousePos, Rect(pos_, size_))) + return; + const int lines = (wheel.direction == SDL_MOUSEWHEEL_FLIPPED) ? wheel.y : -wheel.y; + scroll(lines); +} + void CSelectBox::setMouseData(SDL_MouseMotionEvent motion) { - // IMPORTANT: we use the left upper corner of the selectbox as (x,y)=(0,0), so we have to manipulate - // the motion-structure before give it to the buttons: x_absolute - x_selectbox, y_absolute - y_selectbox + // Make the coordinates relative to the select box, like the positions of the scroll buttons and entries motion.x -= pos_.x; motion.y -= pos_.y; + // The release of the button might have been missed, e.g. when it happened outside of the parent window + if((motion.state & SDL_BUTTON_LMASK) == 0) + sliderDragging_ = false; + if(sliderDragging_) + { + const int dy = motion.y - dragStartMouseY_; + sliderPos_ = clampSliderPos(dragStartSliderPos_ + dy); + // Derive the scroll position from the distance moved since grabbing the slider. Converting the slider position + // back and forth would lose a line due to rounding as soon as the slider is grabbed. + const int trackHeight = std::max(1, static_cast(getScrollTrack().getSize().y)); + const auto lines = + static_cast(std::lround(static_cast(dy) * static_cast(Entries.size()) / trackHeight)); + scroll(dragStartScrollPos_ + lines - scrollPos_); + } + if(!hasScrollbar()) + return; ScrollUpButton->setMouseData(motion); ScrollDownButton->setMouseData(motion); } +void CSelectBox::setEntriesMouseData(const SDL_MouseButtonEvent& button) +{ + // The whole row is clickable: From the left border to the scrollbar or right border + const int rowWidth = hasScrollbar() ? getScrollTrack().left : static_cast(size_.x); + const Extent rowSize(static_cast(std::max(0, rowWidth)), getLineHeight(fontsize)); + for(int i = 0; i < static_cast(Entries.size()); i++) + { + // Hidden entries get an empty area: They can't be clicked but still lose their highlight on a click elsewhere + const Extent clickSize = isEntryVisible(i) ? rowSize : Extent(0, 0); + Entries[i]->setMouseData(button, Rect(Position(0, Entries[i]->getPos().y), clickSize)); + } +} + void CSelectBox::setMouseData(SDL_MouseButtonEvent button) { - bool manipulated = false; - static bool scroll_up_button_marked = false; - static bool scroll_down_button_marked = false; + if(button.button != SDL_BUTTON_LEFT) + return; + + // Make the coordinates relative to the select box, like the positions of the scroll buttons and entries + button.x -= pos_.x; + button.y -= pos_.y; + const Position mousePos(button.x, button.y); + const bool scrollbar = hasScrollbar(); + const auto isOnButton = [mousePos](const CButton& btn) { + return isPointInRect(mousePos, Rect(btn.getPos(), btn.getSize())); + }; - // left button is pressed - if(button.button == SDL_BUTTON_LEFT) + if(button.state == SDL_PRESSED) { - // if mouse button is pressed ON the selectbox - if(button.state == SDL_PRESSED) + if(scrollbar && isPointInRect(mousePos, getScrollTrack())) { - if((button.x >= pos_.x) && (button.x < pos_.x + static_cast(size_.x)) && (button.y >= pos_.y) - && (button.y < pos_.y + static_cast(size_.y))) + const int sliderPos = getSliderPosFromScroll(); + const int sliderHeight = getSliderHeight(); + const int y = mousePos.y - getScrollTrack().top; + if(y >= sliderPos && y < sliderPos + sliderHeight) { - // scroll up button - if((button.x > pos_.x + static_cast(size_.x) - 20) && (button.y < pos_.y + 20)) - { - scroll_up_button_marked = true; - } - // scroll down button - else if((button.x > pos_.x + static_cast(size_.x) - 20) - && (button.y > pos_.y + static_cast(size_.y) - 20)) - { - scroll_down_button_marked = true; - } - - // IMPORTANT: we use the left upper corner of the selectbox as (x,y)=(0,0), so we have to manipulate - // the motion-structure before give it to buttons and entries: x_absolute - x_selectbox, - // y_absolute - y_selectbox - button.x -= pos_.x; - button.y -= pos_.y; - manipulated = true; - - for(auto& entry : Entries) - { - entry->setMouseData(button); - } - } - } else if(button.state == SDL_RELEASED) - { - if((button.x >= pos_.x) && (button.x < pos_.x + static_cast(size_.x)) && (button.y >= pos_.y) - && (button.y < pos_.y + static_cast(size_.y))) + sliderDragging_ = true; + sliderPos_ = dragStartSliderPos_ = sliderPos; + dragStartScrollPos_ = scrollPos_; + dragStartMouseY_ = mousePos.y; + } else { - // scroll up button - if(scroll_up_button_marked) - { - if((button.x > pos_.x + static_cast(size_.x) - 20) && (button.y < pos_.y + 20)) - { - // test if first entry is on the most upper position - if(!Entries.empty() && Entries.front()->getPos().y < 10) - { - for(auto& entry : Entries) - { - entry->setPos(Position(entry->getPos().x, entry->getPos().y + 10)); - } - } - } - } - // scroll down button - else if(scroll_down_button_marked) - { - if((button.x > pos_.x + static_cast(size_.x) - 20) - && (button.y > pos_.y + static_cast(size_.y) - 20)) - { - // test if last entry is on the most lower position - if(!Entries.empty() && Entries.back()->getPos().y > static_cast(size_.y) - 10) - { - for(auto& entry : Entries) - { - entry->setPos(Position(entry->getPos().x, entry->getPos().y - 10)); - } - } - } - } - - // IMPORTANT: we use the left upper corner of the selectbox as (x,y)=(0,0), so we have to manipulate - // the motion-structure before give it to buttons and entries: x_absolute - x_selectbox, - // y_absolute - y_selectbox - button.x -= pos_.x; - button.y -= pos_.y; - manipulated = true; - - for(auto& entry : Entries) - { - entry->setMouseData(button); - } + // Clicked above/below the slider -> Move half a slider height, i.e. half a page, up/down + const int halfPage = std::max(1, getPageSize() / 2); + scroll(y < sliderPos ? -halfPage : halfPage); } - scroll_up_button_marked = false; - scroll_down_button_marked = false; - } - // IMPORTANT: we use the left upper corner of the selectbox as (x,y)=(0,0), so we have to manipulate - // the motion-structure before give it to buttons and entries: x_absolute - x_selectbox, y_absolute - - // y_selectbox - if(!manipulated) - { - button.x -= pos_.x; - button.y -= pos_.y; + return; } + scrollUpButtonPressed_ = scrollbar && isOnButton(*ScrollUpButton); + scrollDownButtonPressed_ = scrollbar && isOnButton(*ScrollDownButton); + } else + { + sliderDragging_ = false; + if(scrollUpButtonPressed_ && isOnButton(*ScrollUpButton)) + scroll(-1); + else if(scrollDownButtonPressed_ && isOnButton(*ScrollDownButton)) + scroll(1); + scrollUpButtonPressed_ = scrollDownButtonPressed_ = false; + } + + if(isPointInRect(mousePos, Rect(Position(0, 0), size_))) + setEntriesMouseData(button); + if(scrollbar) + { ScrollUpButton->setMouseData(button); ScrollDownButton->setMouseData(button); } @@ -195,8 +242,13 @@ void CSelectBox::setSize(Extent size) if(size_ != size) { size_ = size; - // update scroll down button position - ScrollDownButton->setY(size_.y - 1 - 20); + const int scrollButtonX = static_cast(size_.x) - 1 - scrollButtonSize; + ScrollUpButton->setX(scrollButtonX); + ScrollDownButton->setX(scrollButtonX); + ScrollDownButton->setY(static_cast(size_.y) - 1 - scrollButtonSize); + // scroll() keeps the scroll position in the valid range, so scrolling by 0 lines only corrects it for the new + // number of visible entries. E.g. a taller box can't stay scrolled as far down, else rows would stay empty. + scroll(0); } } @@ -227,14 +279,24 @@ void CSelectBox::draw(Position parentOrigin) static_cast(size_.y)); // Draw entries - for(const auto& entry : Entries) + for(int i = 0; i < static_cast(Entries.size()); i++) { - entry->draw(absPos); + if(isEntryVisible(i)) + Entries[i]->draw(absPos); } glDisable(GL_SCISSOR_TEST); - // Draw scroll buttons (on top, within the select box) - ScrollUpButton->draw(absPos); - ScrollDownButton->draw(absPos); + if(hasScrollbar()) + { + const Rect track = getScrollTrack(); + drawButtonBox(Rect(absPos + track.getOrigin(), track.getSize()), true, BUTTON_GREY_BACKGROUND, + BUTTON_GREY_DARK); + const int sliderPos = sliderDragging_ ? sliderPos_ : getSliderPosFromScroll(); + drawButtonBox(Rect(absPos + track.getOrigin() + Position(0, sliderPos), + Extent(track.getSize().x, static_cast(getSliderHeight()))), + false, BUTTON_GREY_BACKGROUND, BUTTON_GREY_DARK); + ScrollUpButton->draw(absPos); + ScrollDownButton->draw(absPos); + } } diff --git a/CIO/CSelectBox.h b/CIO/CSelectBox.h index 7b149d5..ed53ddc 100644 --- a/CIO/CSelectBox.h +++ b/CIO/CSelectBox.h @@ -18,6 +18,11 @@ class CSelectBox friend class CDebug; private: + /// Width and height of the scroll buttons, also the width of the scrollbar + static constexpr int scrollButtonSize = 20; + /// Distance of the entries from the left and top border + static constexpr int textPadding = 10; + std::vector> Entries; Position pos_; Extent size_; @@ -27,7 +32,27 @@ class CSelectBox FontColor text_color; std::unique_ptr ScrollUpButton; std::unique_ptr ScrollDownButton; - Uint16 last_text_pos_y = 10; + bool scrollUpButtonPressed_ = false; + bool scrollDownButtonPressed_ = false; + int scrollPos_ = 0; ///< Index of the first visible entry + // Scrollbar slider between the scroll buttons + bool sliderDragging_ = false; + int sliderPos_ = 0; ///< Position of the slider in the track while dragging + int dragStartSliderPos_ = 0; + int dragStartScrollPos_ = 0; + int dragStartMouseY_ = 0; + + /// Number of fully visible entries + int getPageSize() const; + bool isEntryVisible(int index) const; + bool hasScrollbar() const; + /// Area between the scroll buttons, relative to the select box + Rect getScrollTrack() const; + int getSliderHeight() const; + int getSliderPosFromScroll() const; + int clampSliderPos(int sliderPos) const; + /// Pass the event (relative to the select box) to the entries, only the visible ones are clickable + void setEntriesMouseData(const SDL_MouseButtonEvent& button); public: CSelectBox(Position pos, Extent size, FontSize fontsize = FontSize::Large, FontColor text_color = FontColor::Yellow, @@ -36,6 +61,8 @@ class CSelectBox Extent getSize() const { return size_; } void setMouseData(SDL_MouseButtonEvent button); void setMouseData(SDL_MouseMotionEvent motion); + void setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos); + void scroll(int lines); void draw(Position parentOrigin); void setColor(int color); void setTextColor(FontColor color) { text_color = color; } diff --git a/CIO/CWindow.cpp b/CIO/CWindow.cpp index 0c3f6ff..8ee8e05 100644 --- a/CIO/CWindow.cpp +++ b/CIO/CWindow.cpp @@ -145,6 +145,12 @@ void CWindow::setMouseData(SDL_MouseMotionEvent motion) } } +void CWindow::setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos) +{ + if(!minimized) + CControlContainer::setMouseWheel(wheel, mousePos); +} + void CWindow::setMouseData(SDL_MouseButtonEvent button) { // at first check if the right mouse button was pressed, cause in this case we will close the window diff --git a/CIO/CWindow.h b/CIO/CWindow.h index eb0ed8b..50695ee 100644 --- a/CIO/CWindow.h +++ b/CIO/CWindow.h @@ -61,6 +61,7 @@ class CWindow : public CControlContainer void setTitle(const char* title); void setMouseData(SDL_MouseMotionEvent motion) final; void setMouseData(SDL_MouseButtonEvent button) final; + void setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos) final; void setActive() { active = true; From 9950b322adbe9d70a7b292a4f9de113044dff50f Mon Sep 17 00:00:00 2001 From: Andreas K Date: Thu, 19 Nov 2026 16:59:56 +0100 Subject: [PATCH 2/2] Address review feedback for select box scrolling - Give the scrollbar slider a minimum height of one scroll button, capped at the track height, and map the scroll range onto the slider range so the slider reaches both ends of the track exactly - Don't draw the track and slider if the track is shorter than the minimum slider height, drawButtonBox would underflow below 4px - Rename CSelectBox::scroll to scrollBy and document it - Add CButton::getRect and use getRect for buttons and windows - Clarify some comments and document the slider helpers --- CGame_Event.cpp | 2 +- CIO/CButton.h | 1 + CIO/CFont.h | 2 +- CIO/CSelectBox.cpp | 81 +++++++++++++++++++++++++++------------------- CIO/CSelectBox.h | 12 ++++++- 5 files changed, 62 insertions(+), 36 deletions(-) diff --git a/CGame_Event.cpp b/CGame_Event.cpp index b818076..9523fee 100644 --- a/CGame_Event.cpp +++ b/CGame_Event.cpp @@ -385,7 +385,7 @@ void CGame::EventHandling(SDL_Event* Event) CWindow* wndAtCursor = nullptr; for(auto& Window : Windows) { - if(!Window->isWaste() && isPointInRect(Cursor.pos, Rect(Window->getPos(), Window->getSize())) + if(!Window->isWaste() && isPointInRect(Cursor.pos, Window->getRect()) && (!wndAtCursor || Window->getPriority() > wndAtCursor->getPriority())) wndAtCursor = Window.get(); } diff --git a/CIO/CButton.h b/CIO/CButton.h index 27fe79d..76bb2be 100644 --- a/CIO/CButton.h +++ b/CIO/CButton.h @@ -35,6 +35,7 @@ class CButton int getY() const { return pos_.y; }; Position getPos() const { return pos_; } Extent getSize() const { return size_; }; + Rect getRect() const { return Rect(pos_, size_); } void setX(int x) { pos_.x = x; }; void setY(int y) { pos_.y = y; }; void setButtonPicture(int picture); diff --git a/CIO/CFont.h b/CIO/CFont.h index c2198c1..58a0f97 100644 --- a/CIO/CFont.h +++ b/CIO/CFont.h @@ -46,7 +46,7 @@ class CFont clickedParam = 0; } void setMouseData(SDL_MouseButtonEvent button) { setMouseData(button, Rect(pos_, size_)); } - /// Handle a click with a custom clickable area (e.g. a whole row), in the same coordinates as the position + /// Handle a click in a custom clickable area (e.g. a full row), in the same coordinate system as pos_ void setMouseData(SDL_MouseButtonEvent button, const Rect& clickArea); /// Draw this font's text at the given absolute position diff --git a/CIO/CSelectBox.cpp b/CIO/CSelectBox.cpp index b758012..275e79d 100644 --- a/CIO/CSelectBox.cpp +++ b/CIO/CSelectBox.cpp @@ -12,6 +12,7 @@ #include "CollisionDetection.h" #include #include +#include #include CSelectBox::CSelectBox(Position pos, Extent size, FontSize fontsize, FontColor text_color, int bg_color) @@ -91,11 +92,15 @@ bool CSelectBox::isEntryVisible(int index) const return index >= scrollPos_ && index < scrollPos_ + getPageSize(); } -void CSelectBox::scroll(int lines) +int CSelectBox::getMaxScrollPos() const +{ + return std::max(0, static_cast(Entries.size()) - getPageSize()); +} + +void CSelectBox::scrollBy(int lineDelta) { const auto rowHeight = static_cast(getLineHeight(fontsize)); - const int maxScrollPos = std::max(0, static_cast(Entries.size()) - getPageSize()); - const int newScrollPos = std::clamp(scrollPos_ + lines, 0, maxScrollPos); + const int newScrollPos = std::clamp(scrollPos_ + lineDelta, 0, getMaxScrollPos()); const int offset = (scrollPos_ - newScrollPos) * rowHeight; scrollPos_ = newScrollPos; for(auto& entry : Entries) @@ -116,23 +121,29 @@ Rect CSelectBox::getScrollTrack() const int CSelectBox::getSliderHeight() const { - const int range = static_cast(Entries.size()); - return std::max(1, static_cast(getScrollTrack().getSize().y) * getPageSize() / range); + assert(!Entries.empty()); + const int trackHeight = static_cast(getScrollTrack().getSize().y); + const int proportionalHeight = trackHeight * getPageSize() / static_cast(Entries.size()); + return std::min(trackHeight, std::max(minSliderHeight, proportionalHeight)); +} + +int CSelectBox::getMaxSliderPos() const +{ + return std::max(0, static_cast(getScrollTrack().getSize().y) - getSliderHeight()); } int CSelectBox::getSliderPosFromScroll() const { - const int range = static_cast(Entries.size()); - const int trackHeight = static_cast(getScrollTrack().getSize().y); - if(scrollPos_ + getPageSize() >= range) - return trackHeight - getSliderHeight(); - return trackHeight * scrollPos_ / range; + // Map the scroll range onto the slider range, so the slider reaches both ends of the track exactly + const int maxScrollPos = getMaxScrollPos(); + if(maxScrollPos == 0) + return 0; + return getMaxSliderPos() * scrollPos_ / maxScrollPos; } int CSelectBox::clampSliderPos(int sliderPos) const { - const int maxSliderPos = static_cast(getScrollTrack().getSize().y) - getSliderHeight(); - return std::clamp(sliderPos, 0, std::max(0, maxSliderPos)); + return std::clamp(sliderPos, 0, getMaxSliderPos()); } void CSelectBox::setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos) @@ -140,7 +151,7 @@ void CSelectBox::setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mouseP if(!isPointInRect(mousePos, Rect(pos_, size_))) return; const int lines = (wheel.direction == SDL_MOUSEWHEEL_FLIPPED) ? wheel.y : -wheel.y; - scroll(lines); + scrollBy(lines); } void CSelectBox::setMouseData(SDL_MouseMotionEvent motion) @@ -157,10 +168,13 @@ void CSelectBox::setMouseData(SDL_MouseMotionEvent motion) sliderPos_ = clampSliderPos(dragStartSliderPos_ + dy); // Derive the scroll position from the distance moved since grabbing the slider. Converting the slider position // back and forth would lose a line due to rounding as soon as the slider is grabbed. - const int trackHeight = std::max(1, static_cast(getScrollTrack().getSize().y)); - const auto lines = - static_cast(std::lround(static_cast(dy) * static_cast(Entries.size()) / trackHeight)); - scroll(dragStartScrollPos_ + lines - scrollPos_); + const int maxSliderPos = getMaxSliderPos(); + if(maxSliderPos > 0) + { + const auto lines = static_cast( + std::lround(static_cast(dy) * static_cast(getMaxScrollPos()) / maxSliderPos)); + scrollBy(dragStartScrollPos_ + lines - scrollPos_); + } } if(!hasScrollbar()) return; @@ -191,9 +205,7 @@ void CSelectBox::setMouseData(SDL_MouseButtonEvent button) button.y -= pos_.y; const Position mousePos(button.x, button.y); const bool scrollbar = hasScrollbar(); - const auto isOnButton = [mousePos](const CButton& btn) { - return isPointInRect(mousePos, Rect(btn.getPos(), btn.getSize())); - }; + const auto isOnButton = [mousePos](const CButton& btn) { return isPointInRect(mousePos, btn.getRect()); }; if(button.state == SDL_PRESSED) { @@ -210,9 +222,9 @@ void CSelectBox::setMouseData(SDL_MouseButtonEvent button) dragStartMouseY_ = mousePos.y; } else { - // Clicked above/below the slider -> Move half a slider height, i.e. half a page, up/down + // Above/below the slider: scroll up/down by half a page const int halfPage = std::max(1, getPageSize() / 2); - scroll(y < sliderPos ? -halfPage : halfPage); + scrollBy(y < sliderPos ? -halfPage : halfPage); } return; } @@ -222,9 +234,9 @@ void CSelectBox::setMouseData(SDL_MouseButtonEvent button) { sliderDragging_ = false; if(scrollUpButtonPressed_ && isOnButton(*ScrollUpButton)) - scroll(-1); + scrollBy(-1); else if(scrollDownButtonPressed_ && isOnButton(*ScrollDownButton)) - scroll(1); + scrollBy(1); scrollUpButtonPressed_ = scrollDownButtonPressed_ = false; } @@ -246,9 +258,8 @@ void CSelectBox::setSize(Extent size) ScrollUpButton->setX(scrollButtonX); ScrollDownButton->setX(scrollButtonX); ScrollDownButton->setY(static_cast(size_.y) - 1 - scrollButtonSize); - // scroll() keeps the scroll position in the valid range, so scrolling by 0 lines only corrects it for the new - // number of visible entries. E.g. a taller box can't stay scrolled as far down, else rows would stay empty. - scroll(0); + // Re-clamp the scroll position to the new page size (a taller box may not stay scrolled as far down) + scrollBy(0); } } @@ -290,12 +301,16 @@ void CSelectBox::draw(Position parentOrigin) if(hasScrollbar()) { const Rect track = getScrollTrack(); - drawButtonBox(Rect(absPos + track.getOrigin(), track.getSize()), true, BUTTON_GREY_BACKGROUND, - BUTTON_GREY_DARK); - const int sliderPos = sliderDragging_ ? sliderPos_ : getSliderPosFromScroll(); - drawButtonBox(Rect(absPos + track.getOrigin() + Position(0, sliderPos), - Extent(track.getSize().x, static_cast(getSliderHeight()))), - false, BUTTON_GREY_BACKGROUND, BUTTON_GREY_DARK); + // Skip drawing the track and the slider if the track is shorter than the minimum slider height + if(static_cast(track.getSize().y) >= minSliderHeight) + { + drawButtonBox(Rect(absPos + track.getOrigin(), track.getSize()), true, BUTTON_GREY_BACKGROUND, + BUTTON_GREY_DARK); + const int sliderPos = sliderDragging_ ? sliderPos_ : getSliderPosFromScroll(); + drawButtonBox(Rect(absPos + track.getOrigin() + Position(0, sliderPos), + Extent(track.getSize().x, static_cast(getSliderHeight()))), + false, BUTTON_GREY_BACKGROUND, BUTTON_GREY_DARK); + } ScrollUpButton->draw(absPos); ScrollDownButton->draw(absPos); } diff --git a/CIO/CSelectBox.h b/CIO/CSelectBox.h index ed53ddc..e987fae 100644 --- a/CIO/CSelectBox.h +++ b/CIO/CSelectBox.h @@ -22,6 +22,8 @@ class CSelectBox static constexpr int scrollButtonSize = 20; /// Distance of the entries from the left and top border static constexpr int textPadding = 10; + /// Minimum height of the scrollbar slider, so it stays grabbable with many entries + static constexpr int minSliderHeight = scrollButtonSize; std::vector> Entries; Position pos_; @@ -45,11 +47,18 @@ class CSelectBox /// Number of fully visible entries int getPageSize() const; bool isEntryVisible(int index) const; + /// Largest valid scroll position, i.e. the index of the first visible entry when scrolled to the bottom + int getMaxScrollPos() const; bool hasScrollbar() const; /// Area between the scroll buttons, relative to the select box Rect getScrollTrack() const; + /// Height of the slider: Proportional to the visible part of the entries, but at least minSliderHeight int getSliderHeight() const; + /// Slider offset (from the track top) when the slider is at the bottom of the track + int getMaxSliderPos() const; + /// Slider offset (from the track top) for the current scroll position. int getSliderPosFromScroll() const; + /// Clamp a slider offset (from the track top) so the slider stays within the track int clampSliderPos(int sliderPos) const; /// Pass the event (relative to the select box) to the entries, only the visible ones are clickable void setEntriesMouseData(const SDL_MouseButtonEvent& button); @@ -62,7 +71,8 @@ class CSelectBox void setMouseData(SDL_MouseButtonEvent button); void setMouseData(SDL_MouseMotionEvent motion); void setMouseWheel(const SDL_MouseWheelEvent& wheel, Position mousePos); - void scroll(int lines); + /// Scroll by lineDelta entries (negative = up), clamped to the valid range + void scrollBy(int lineDelta); void draw(Position parentOrigin); void setColor(int color); void setTextColor(FontColor color) { text_color = color; }