Skip to content

Lobby Overhaul #2977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions libraries/ZWidget/include/zwidget/widgets/listview/listview.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ class ListView : public Widget
public:
ListView(Widget* parent = nullptr);

void AddItem(const std::string& text);
void SetColumnWidths(const std::vector<double>& widths);
void AddItem(const std::string& text, int index = -1, int column = 0);
void UpdateItem(const std::string& text, int index, int column = 0);
void RemoveItem(int index = -1);
size_t GetItemAmount() const { return items.size(); }
size_t GetColumnAmount() const { return columnwidths.size(); }
int GetSelectedItem() const { return selectedItem; }
void SetSelectedItem(int index);
void ScrollToItem(int index);
Expand All @@ -34,6 +39,7 @@ class ListView : public Widget

Scrollbar* scrollbar = nullptr;

std::vector<std::string> items;
std::vector<std::vector<std::string>> items;
std::vector<double> columnwidths;
int selectedItem = 0;
};
92 changes: 88 additions & 4 deletions libraries/ZWidget/src/widgets/listview/listview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,90 @@ ListView::ListView(Widget* parent) : Widget(parent)

scrollbar = new Scrollbar(this);
scrollbar->FuncScroll = [=]() { OnScrollbarScroll(); };

SetColumnWidths({ 0.0 });
}

void ListView::SetColumnWidths(const std::vector<double>& widths)
{
columnwidths = widths;

bool updated = false;
const size_t newWidth = columnwidths.size();
for (std::vector<std::string>& column : items)
{
while (column.size() < newWidth)
{
updated = true;
column.push_back("");
}
while (column.size() > newWidth)
{
updated = true;
column.erase(column.end());
}
}

if (updated)
Update();
}

void ListView::AddItem(const std::string& text, int index, int column)
{
if (column < 0 || column >= columnwidths.size())
return;

std::vector<std::string> newEntry;
for (size_t i = 0u; i < columnwidths.size(); ++i)
newEntry.push_back("");

newEntry[column] = text;
if (index >= 0)
{
if (index >= items.size())
{
newEntry[column] = "";
while (items.size() < index)
items.push_back(newEntry);

newEntry[column] = text;
items.push_back(newEntry);
}
else
{
items.insert(items.begin() + index, newEntry);
}
}
else
{
items.push_back(newEntry);
}
scrollbar->SetRanges(GetHeight(), items.size() * 20.0);
Update();
}

void ListView::AddItem(const std::string& text)
void ListView::UpdateItem(const std::string& text, int index, int column)
{
items.push_back(text);
if (index < 0 || index >= items.size() || column < 0 || column >= columnwidths.size())
return;

items[index][column] = text;
Update();
}

void ListView::RemoveItem(int index)
{
if (!items.size() || index >= items.size())
return;

if (index < 0)
index = items.size() - 1;

if (selectedItem == index)
SetSelectedItem(0);

items.erase(items.begin() + index);
scrollbar->SetRanges(GetHeight(), items.size() * 20.0);
Update();
}

Expand Down Expand Up @@ -68,7 +147,7 @@ void ListView::OnPaint(Canvas* canvas)
double h = 20.0;

int index = 0;
for (const std::string& item : items)
for (const std::vector<std::string>& item : items)
{
double itemY = y;
if (itemY + h >= 0.0 && itemY < GetHeight())
Expand All @@ -77,7 +156,12 @@ void ListView::OnPaint(Canvas* canvas)
{
canvas->fillRect(Rect::xywh(x - 2.0, itemY, w, h), Colorf::fromRgba8(100, 100, 100));
}
canvas->drawText(Point(x, y + 15.0), Colorf::fromRgba8(255, 255, 255), item);
double cx = x;
for (size_t entry = 0u; entry < item.size(); ++entry)
{
canvas->drawText(Point(cx, y + 15.0), Colorf::fromRgba8(255, 255, 255), item[entry]);
cx += columnwidths[entry];
}
}
y += h;
index++;
Expand Down
Loading