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.
This commit is contained in:
Tamás Bálint Misius 2023-05-27 20:38:33 +02:00
parent e6e36a6b7c
commit c3cd4f1691
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
2 changed files with 12 additions and 8 deletions

View File

@ -6,6 +6,7 @@
#include <SDL.h> #include <SDL.h>
#include "gui/interface/Textbox.h" #include "gui/interface/Textbox.h"
#include "gui/interface/ScrollPanel.h"
#include "gui/interface/Label.h" #include "gui/interface/Label.h"
#include "gui/game/Tool.h" #include "gui/game/Tool.h"
#include "gui/game/Menu.h" #include "gui/game/Menu.h"
@ -52,6 +53,9 @@ ElementSearchActivity::ElementSearchActivity(GameController * gameController, st
AddComponent(okButton); AddComponent(okButton);
AddComponent(closeButton); 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(""); searchTools("");
} }
@ -59,12 +63,12 @@ void ElementSearchActivity::searchTools(String query)
{ {
firstResult = NULL; firstResult = NULL;
for (auto &toolButton : toolButtons) { for (auto &toolButton : toolButtons) {
RemoveComponent(toolButton); scrollPanel->RemoveChild(toolButton);
delete toolButton; delete toolButton;
} }
toolButtons.clear(); 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); ui::Point current = ui::Point(0, 0);
String queryLower = query.ToLower(); String queryLower = query.ToLower();
@ -178,7 +182,7 @@ void ElementSearchActivity::searchTools(String query)
} }
toolButtons.push_back(tempButton); toolButtons.push_back(tempButton);
AddComponent(tempButton); scrollPanel->AddChild(tempButton);
current.X += 31; current.X += 31;
@ -186,10 +190,9 @@ void ElementSearchActivity::searchTools(String query)
current.X = 0; current.X = 0;
current.Y += 19; 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) void ElementSearchActivity::SetActiveTool(int selectionState, Tool * tool)
@ -216,8 +219,7 @@ void ElementSearchActivity::OnDraw()
g->DrawRect(RectSized(Position, Size), 0xFFFFFF_rgb); g->DrawRect(RectSized(Position, Size), 0xFFFFFF_rgb);
g->BlendRect( g->BlendRect(
RectSized(Position + searchField->Position + Vec2{ 0, searchField->Size.Y+8 }, RectSized(Position + scrollPanel->Position - Vec2{ 1, 1 }, scrollPanel->Size + Vec2{ 2, 2 }),
{ searchField->Size.X, Size.Y-(searchField->Position.Y+searchField->Size.Y+8)-23 }),
0xFFFFFF_rgb .WithAlpha(180)); 0xFFFFFF_rgb .WithAlpha(180));
if (toolTipPresence && toolTip.length()) if (toolTipPresence && toolTip.length())
{ {

View File

@ -10,6 +10,7 @@ class GameController;
namespace ui namespace ui
{ {
class ScrollPanel;
class Textbox; class Textbox;
} }
@ -20,6 +21,7 @@ class ElementSearchActivity: public WindowActivity
std::vector<Tool*> tools; std::vector<Tool*> tools;
ui::Textbox * searchField; ui::Textbox * searchField;
std::vector<ToolButton*> toolButtons; std::vector<ToolButton*> toolButtons;
ui::ScrollPanel *scrollPanel = nullptr;
String toolTip; String toolTip;
int toolTipPresence; int toolTipPresence;
bool shiftPressed; bool shiftPressed;