From a505cc464eb2ae0462c36843787fbbba2149ab78 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 22 Jun 2023 23:49:38 +0300 Subject: [PATCH] Fixed slider's behavior, especially for mobile systems --- client/gui/InterfaceObjectConfigurable.cpp | 2 +- client/lobby/OptionsTab.cpp | 2 +- client/lobby/SelectionTab.cpp | 6 +++++- client/widgets/ObjectLists.cpp | 12 ++++++++++-- client/widgets/Slider.cpp | 12 ++++++++++-- client/widgets/Slider.h | 3 ++- client/widgets/TextControls.cpp | 4 +++- client/windows/CQuestLog.cpp | 3 ++- client/windows/CTradeWindow.cpp | 4 ++-- client/windows/CreaturePurchaseCard.cpp | 2 +- client/windows/GUIClasses.cpp | 4 ++-- lib/Rect.h | 5 +++++ 12 files changed, 44 insertions(+), 15 deletions(-) diff --git a/client/gui/InterfaceObjectConfigurable.cpp b/client/gui/InterfaceObjectConfigurable.cpp index 8e4dfd15c..1dcda2d00 100644 --- a/client/gui/InterfaceObjectConfigurable.cpp +++ b/client/gui/InterfaceObjectConfigurable.cpp @@ -453,7 +453,7 @@ std::shared_ptr InterfaceObjectConfigurable::buildSlider(const JsonNode auto value = config["selected"].Integer(); bool horizontal = config["orientation"].String() == "horizontal"; const auto & result = - std::make_shared(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style); + std::make_shared(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style); if (!config["scrollBounds"].isNull()) { diff --git a/client/lobby/OptionsTab.cpp b/client/lobby/OptionsTab.cpp index 69544f96b..28389fd62 100644 --- a/client/lobby/OptionsTab.cpp +++ b/client/lobby/OptionsTab.cpp @@ -48,7 +48,7 @@ OptionsTab::OptionsTab() : humanPlayers(0) labelStartingBonus = std::make_shared(Rect(315, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[520]); if(SEL->screenType == ESelectionScreen::newGame || SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::scenarioInfo) { - sliderTurnDuration = std::make_shared(Point(55, 551), 194, std::bind(&IServerAPI::setTurnLength, CSH, _1), 1, (int)GameConstants::POSSIBLE_TURNTIME.size(), (int)GameConstants::POSSIBLE_TURNTIME.size(), true, CSlider::BLUE); + sliderTurnDuration = std::make_shared(Point(55, 551), 194, std::bind(&IServerAPI::setTurnLength, CSH, _1), 1, (int)GameConstants::POSSIBLE_TURNTIME.size(), (int)GameConstants::POSSIBLE_TURNTIME.size(), Orientation::HORIZONTAL, CSlider::BLUE); sliderTurnDuration->setScrollBounds(Rect(-3, -25, 337, 43)); sliderTurnDuration->setPanningStep(20); labelPlayerTurnDuration = std::make_shared(222, 538, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[521]); diff --git a/client/lobby/SelectionTab.cpp b/client/lobby/SelectionTab.cpp index 04f4319dd..6845ae6cb 100644 --- a/client/lobby/SelectionTab.cpp +++ b/client/lobby/SelectionTab.cpp @@ -205,8 +205,12 @@ SelectionTab::SelectionTab(ESelectionScreen Type) listItems.push_back(std::make_shared(Point(30, 129 + i * 25), iconsMapFormats, iconsVictoryCondition, iconsLossCondition)); labelTabTitle = std::make_shared(205, 28, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, tabTitle); - slider = std::make_shared(Point(372, 86), tabType != ESelectionScreen::saveGame ? 480 : 430, std::bind(&SelectionTab::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, false, CSlider::BLUE); + slider = std::make_shared(Point(372, 86), tabType != ESelectionScreen::saveGame ? 480 : 430, std::bind(&SelectionTab::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, Orientation::VERTICAL, CSlider::BLUE); slider->setPanningStep(24); + + // create scroll bounds that encompass all area in this UI element to the left of slider (including area of slider itself) + // entire screen can't be used in here since map description might also have slider + slider->setScrollBounds(Rect(pos.x - slider->pos.x, 0, slider->pos.x + slider->pos.w - pos.x, slider->pos.h )); filter(0); } diff --git a/client/widgets/ObjectLists.cpp b/client/widgets/ObjectLists.cpp index a30f6152b..e9f873464 100644 --- a/client/widgets/ObjectLists.cpp +++ b/client/widgets/ObjectLists.cpp @@ -92,8 +92,16 @@ CListBox::CListBox(CreateFunc create, Point Pos, Point ItemOffset, size_t Visibl if(Slider & 1) { OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); - slider = std::make_shared(SliderPos.topLeft(), SliderPos.w, std::bind(&CListBox::moveToPos, this, _1), - (int)VisibleSize, (int)TotalSize, (int)InitialPos, Slider & 2, Slider & 4 ? CSlider::BLUE : CSlider::BROWN); + slider = std::make_shared( + SliderPos.topLeft(), + SliderPos.w, + std::bind(&CListBox::moveToPos, this, _1), + (int)VisibleSize, + (int)TotalSize, + (int)InitialPos, + Slider & 2 ? Orientation::HORIZONTAL : Orientation::VERTICAL, + Slider & 4 ? CSlider::BLUE : CSlider::BROWN + ); slider->setPanningStep(itemOffset.x + itemOffset.y); } diff --git a/client/widgets/Slider.cpp b/client/widgets/Slider.cpp index da36764bd..8035d1739 100644 --- a/client/widgets/Slider.cpp +++ b/client/widgets/Slider.cpp @@ -40,6 +40,14 @@ void CSlider::mouseDragged(const Point & cursorPosition, const Point & lastUpdat } } +void CSlider::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) +{ + if (getOrientation() == Orientation::VERTICAL) + Scrollable::gesturePanning(initialPosition, currentPosition, lastUpdateDistance); + else + mouseDragged(currentPosition, lastUpdateDistance); +} + void CSlider::setScrollBounds(const Rect & bounds ) { scrollBounds = bounds; @@ -151,8 +159,8 @@ bool CSlider::receiveEvent(const Point &position, int eventType) const return testTarget.isInside(position); } -CSlider::CSlider(Point position, int totalw, std::function Moved, int Capacity, int Amount, int Value, bool Horizontal, CSlider::EStyle style) - : Scrollable(LCLICK | DRAG, position, Horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL ), +CSlider::CSlider(Point position, int totalw, std::function Moved, int Capacity, int Amount, int Value, Orientation orientation, CSlider::EStyle style) + : Scrollable(LCLICK | DRAG, position, orientation ), capacity(Capacity), amount(Amount), value(Value), diff --git a/client/widgets/Slider.h b/client/widgets/Slider.h index 906b9cfac..413f185d3 100644 --- a/client/widgets/Slider.h +++ b/client/widgets/Slider.h @@ -71,6 +71,7 @@ public: void keyPressed(EShortcut key) override; void clickLeft(tribool down, bool previousState) override; void mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance) override; + void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override; void showAll(Canvas & to) override; /// @param position coordinates of slider @@ -80,6 +81,6 @@ public: /// @param Amount total amount of elements, including not visible /// @param Value starting position CSlider(Point position, int length, std::function Moved, int Capacity, int Amount, - int Value=0, bool Horizontal=true, EStyle style = BROWN); + int Value, Orientation orientation, EStyle style = BROWN); ~CSlider(); }; diff --git a/client/widgets/TextControls.cpp b/client/widgets/TextControls.cpp index 159671f0d..f3089be80 100644 --- a/client/widgets/TextControls.cpp +++ b/client/widgets/TextControls.cpp @@ -349,8 +349,10 @@ void CTextBox::setText(const std::string & text) OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE); slider = std::make_shared(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1), - label->pos.h, label->textSize.y, 0, false, CSlider::EStyle(sliderStyle)); + label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle)); slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight()); + slider->setPanningStep(1); + slider->setScrollBounds(pos - slider->pos.topLeft()); } } diff --git a/client/windows/CQuestLog.cpp b/client/windows/CQuestLog.cpp index 8bf701f9f..eb7f2b7f2 100644 --- a/client/windows/CQuestLog.cpp +++ b/client/windows/CQuestLog.cpp @@ -134,7 +134,8 @@ CQuestLog::CQuestLog (const std::vector & Quests) // Both button and lable are shifted to -2px by x and y to not make them actually look like they're on same line with quests list and ok button hideCompleteButton = std::make_shared(Point(10, 396), "sysopchk.def", CButton::tooltipLocalized("vcmi.questLog.hideComplete"), std::bind(&CQuestLog::toggleComplete, this, _1)); hideCompleteLabel = std::make_shared(46, 398, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.questLog.hideComplete.hover")); - slider = std::make_shared(Point(166, 195), 191, std::bind(&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, 0, false, CSlider::BROWN); + slider = std::make_shared(Point(166, 195), 191, std::bind(&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, 0, 0, Orientation::VERTICAL, CSlider::BROWN); + slider->setPanningStep(32); recreateLabelList(); recreateQuestList(0); diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index 93d7694d0..a16fdd2b5 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -716,7 +716,7 @@ CMarketplaceWindow::CMarketplaceWindow(const IMarket * Market, const CGHeroInsta if(sliderNeeded) { - slider = std::make_shared(Point(231, 490),137, std::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0); + slider = std::make_shared(Point(231, 490), 137, std::bind(&CMarketplaceWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL); max = std::make_shared(Point(229, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[596], [&](){ setMax(); }); max->block(true); } @@ -1117,7 +1117,7 @@ CAltarWindow::CAltarWindow(const IMarket * Market, const CGHeroInstance * Hero, //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW); - slider = std::make_shared(Point(231,481),137,std::bind(&CAltarWindow::sliderMoved,this,_1),0,0); + slider = std::make_shared(Point(231, 481), 137, std::bind(&CAltarWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL); max = std::make_shared(Point(147, 520), "IRCBTNS.DEF", CGI->generaltexth->zelp[578], std::bind(&CSlider::scrollToMax, slider)); sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0); diff --git a/client/windows/CreaturePurchaseCard.cpp b/client/windows/CreaturePurchaseCard.cpp index f00f35010..4cc765ca7 100644 --- a/client/windows/CreaturePurchaseCard.cpp +++ b/client/windows/CreaturePurchaseCard.cpp @@ -75,7 +75,7 @@ void CreaturePurchaseCard::updateAmountInfo(int value) void CreaturePurchaseCard::initSlider() { - slider = std::make_shared(Point(pos.x, pos.y + 158), 102, std::bind(&CreaturePurchaseCard::sliderMoved, this , _1), 0, maxAmount, 0); + slider = std::make_shared(Point(pos.x, pos.y + 158), 102, std::bind(&CreaturePurchaseCard::sliderMoved, this, _1), 0, maxAmount, 0, Orientation::HORIZONTAL); } void CreaturePurchaseCard::initCostBox() diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 1f4e4d793..c30569268 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -215,7 +215,7 @@ CRecruitmentWindow::CRecruitmentWindow(const CGDwelling * Dwelling, int Level, c statusbar = CGStatusBar::create(std::make_shared(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26)); - slider = std::make_shared(Point(176,279),135,std::bind(&CRecruitmentWindow::sliderMoved,this, _1),0,0,0,true); + slider = std::make_shared(Point(176, 279), 135, std::bind(&CRecruitmentWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL); maxButton = std::make_shared(Point(134, 313), "IRCBTNS.DEF", CGI->generaltexth->zelp[553], std::bind(&CSlider::scrollToMax, slider), EShortcut::RECRUITMENT_MAX); buyButton = std::make_shared(Point(212, 313), "IBY6432.DEF", CGI->generaltexth->zelp[554], std::bind(&CRecruitmentWindow::buy, this), EShortcut::GLOBAL_ACCEPT); @@ -338,7 +338,7 @@ CSplitWindow::CSplitWindow(const CCreature * creature, std::function(20, 54, creature, true, false); animRight = std::make_shared(177, 54,creature, true, false); - slider = std::make_shared(Point(21, 194), 257, std::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPosition, rightAmount - rightMin, true); + slider = std::make_shared(Point(21, 194), 257, std::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPosition, rightAmount - rightMin, Orientation::HORIZONTAL); std::string titleStr = CGI->generaltexth->allTexts[256]; boost::algorithm::replace_first(titleStr,"%s", creature->getNamePluralTranslated()); diff --git a/lib/Rect.h b/lib/Rect.h index 94eb220fd..981f62693 100644 --- a/lib/Rect.h +++ b/lib/Rect.h @@ -109,6 +109,11 @@ public: return Rect(x+p.x,y+p.y,w,h); } + Rect operator-(const Point &p) const + { + return Rect(x-p.x,y-p.y,w,h); + } + Rect& operator=(const Rect &p) { x = p.x;