diff --git a/CGame_Event.cpp b/CGame_Event.cpp index 2767017..9523fee 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, Window->getRect()) + && (!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/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/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..58a0f97 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 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 void draw(Position parentOrigin) const; diff --git a/CIO/CSelectBox.cpp b/CIO/CSelectBox.cpp index a8cc946..275e79d 100644 --- a/CIO/CSelectBox.cpp +++ b/CIO/CSelectBox.cpp @@ -9,7 +9,11 @@ #include "../globals.h" #include "CButton.h" #include "CFont.h" +#include "CollisionDetection.h" #include +#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 +21,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 +82,168 @@ 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(); +} + +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 newScrollPos = std::clamp(scrollPos_ + lineDelta, 0, getMaxScrollPos()); + 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 +{ + 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 +{ + // 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 +{ + return std::clamp(sliderPos, 0, getMaxSliderPos()); +} + +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; + scrollBy(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 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; 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; - // left button is pressed - if(button.button == SDL_BUTTON_LEFT) + // 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, btn.getRect()); }; + + 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); - } + // Above/below the slider: scroll up/down by half a page + const int halfPage = std::max(1, getPageSize() / 2); + scrollBy(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)) + scrollBy(-1); + else if(scrollDownButtonPressed_ && isOnButton(*ScrollDownButton)) + scrollBy(1); + scrollUpButtonPressed_ = scrollDownButtonPressed_ = false; + } + + if(isPointInRect(mousePos, Rect(Position(0, 0), size_))) + setEntriesMouseData(button); + if(scrollbar) + { ScrollUpButton->setMouseData(button); ScrollDownButton->setMouseData(button); } @@ -195,8 +254,12 @@ 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); + // Re-clamp the scroll position to the new page size (a taller box may not stay scrolled as far down) + scrollBy(0); } } @@ -227,14 +290,28 @@ 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(); + // 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 7b149d5..e987fae 100644 --- a/CIO/CSelectBox.h +++ b/CIO/CSelectBox.h @@ -18,6 +18,13 @@ 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; + /// Minimum height of the scrollbar slider, so it stays grabbable with many entries + static constexpr int minSliderHeight = scrollButtonSize; + std::vector> Entries; Position pos_; Extent size_; @@ -27,7 +34,34 @@ 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; + /// 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); public: CSelectBox(Position pos, Extent size, FontSize fontsize = FontSize::Large, FontColor text_color = FontColor::Yellow, @@ -36,6 +70,9 @@ 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); + /// 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; } 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;