From c3cd4f1691b440cb30741c071d53ca32942ef8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sat, 27 May 2023 20:38:33 +0200 Subject: [PATCH] Make element search scrollable But this is very buggy, ToolButtons don't lose their hover state when they get scrolled out from under the cursor and the scroll bar ignores clicks if they land on a component under it. --- .../elementsearch/ElementSearchActivity.cpp | 18 ++++++++++-------- src/gui/elementsearch/ElementSearchActivity.h | 2 ++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/gui/elementsearch/ElementSearchActivity.cpp b/src/gui/elementsearch/ElementSearchActivity.cpp index 474c16b5e..90084ec64 100644 --- a/src/gui/elementsearch/ElementSearchActivity.cpp +++ b/src/gui/elementsearch/ElementSearchActivity.cpp @@ -6,6 +6,7 @@ #include #include "gui/interface/Textbox.h" +#include "gui/interface/ScrollPanel.h" #include "gui/interface/Label.h" #include "gui/game/Tool.h" #include "gui/game/Menu.h" @@ -52,6 +53,9 @@ ElementSearchActivity::ElementSearchActivity(GameController * gameController, st AddComponent(okButton); AddComponent(closeButton); + scrollPanel = new ui::ScrollPanel(searchField->Position + Vec2{ 1, searchField->Size.Y+9 }, { searchField->Size.X - 2, Size.Y-(searchField->Position.Y+searchField->Size.Y+6)-23 }); + AddComponent(scrollPanel); + searchTools(""); } @@ -59,12 +63,12 @@ void ElementSearchActivity::searchTools(String query) { firstResult = NULL; for (auto &toolButton : toolButtons) { - RemoveComponent(toolButton); + scrollPanel->RemoveChild(toolButton); delete toolButton; } toolButtons.clear(); - ui::Point viewPosition = searchField->Position + ui::Point(2+0, searchField->Size.Y+2+8); + ui::Point viewPosition = { 1, 1 }; ui::Point current = ui::Point(0, 0); String queryLower = query.ToLower(); @@ -178,7 +182,7 @@ void ElementSearchActivity::searchTools(String query) } toolButtons.push_back(tempButton); - AddComponent(tempButton); + scrollPanel->AddChild(tempButton); current.X += 31; @@ -186,10 +190,9 @@ void ElementSearchActivity::searchTools(String query) current.X = 0; current.Y += 19; } - - if(current.Y + viewPosition.Y + 18 > Size.Y-23) - break; } + + scrollPanel->InnerSize = ui::Point(scrollPanel->Size.X, current.Y + 1); } void ElementSearchActivity::SetActiveTool(int selectionState, Tool * tool) @@ -216,8 +219,7 @@ void ElementSearchActivity::OnDraw() g->DrawRect(RectSized(Position, Size), 0xFFFFFF_rgb); g->BlendRect( - RectSized(Position + searchField->Position + Vec2{ 0, searchField->Size.Y+8 }, - { searchField->Size.X, Size.Y-(searchField->Position.Y+searchField->Size.Y+8)-23 }), + RectSized(Position + scrollPanel->Position - Vec2{ 1, 1 }, scrollPanel->Size + Vec2{ 2, 2 }), 0xFFFFFF_rgb .WithAlpha(180)); if (toolTipPresence && toolTip.length()) { diff --git a/src/gui/elementsearch/ElementSearchActivity.h b/src/gui/elementsearch/ElementSearchActivity.h index 049698f02..004f67abf 100644 --- a/src/gui/elementsearch/ElementSearchActivity.h +++ b/src/gui/elementsearch/ElementSearchActivity.h @@ -10,6 +10,7 @@ class GameController; namespace ui { + class ScrollPanel; class Textbox; } @@ -20,6 +21,7 @@ class ElementSearchActivity: public WindowActivity std::vector tools; ui::Textbox * searchField; std::vector toolButtons; + ui::ScrollPanel *scrollPanel = nullptr; String toolTip; int toolTipPresence; bool shiftPressed;