Added some scroll area and grid fixes

This commit is contained in:
Yousef Khadadeh 2026-04-19 10:03:57 +03:00
parent 886fd313a4
commit 0853df4d2a
7 changed files with 224 additions and 23 deletions

View file

@ -7,9 +7,11 @@ namespace GUIStuff {
GridScrollArea::GridScrollArea(GUIManager& gui): Element(gui) {}
void GridScrollArea::layout(const Clay_ElementId& id, const Options& options) {
void GridScrollArea::layout(const Clay_ElementId& id, const Options& o) {
using namespace ElementHelpers;
options = o;
ScrollBarManyEntriesOptions opts;
opts.clipHorizontal = true;
opts.entryHeight = options.entryHeight;
@ -18,29 +20,36 @@ void GridScrollArea::layout(const Clay_ElementId& id, const Options& options) {
if(extraCallback) extraCallback(contParams);
};
size_t entriesPerRow = static_cast<size_t>(rowWidth / options.entryMaximumWidth) + 1;
size_t rowCount = options.entryCount / entriesPerRow;
size_t rowCount = (options.entryCount + entriesPerRow - 1) / entriesPerRow;
float entryWidth = rowWidth == 0.0f ? 100.0f : (rowWidth / entriesPerRow);
opts.entryCount = rowCount;
opts.elementContent = [&] (size_t rowIndex) {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(options.entryHeight)},
.childAlignment = { .x = options.childAlignmentX}
gui.new_id("grid row", [&] {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(options.entryHeight)},
.childAlignment = { .x = options.childAlignmentX}
}
}) {
size_t startIndex = rowIndex * entriesPerRow;
size_t endIndex = std::min(rowIndex * entriesPerRow + entriesPerRow, o.entryCount);
for(size_t i = startIndex; i < endIndex; i++) {
gui.new_id(static_cast<int64_t>(i), [&] {
CLAY_AUTO_ID({
.layout = {.sizing = {.width = CLAY_SIZING_FIXED(entryWidth), .height = CLAY_SIZING_FIXED(options.entryHeight)}}
}) {
options.elementContent(i);
}
});
}
}
}) {
for(size_t i = 0; i < entriesPerRow; i++) {
size_t entryNum = rowIndex * entriesPerRow + i;
gui.new_id(static_cast<int64_t>(i), [&] {
CLAY_AUTO_ID({
.layout = {.sizing = {.width = CLAY_SIZING_FIXED(entryWidth), .height = CLAY_SIZING_FIXED(options.entryHeight)}}
}) {
options.elementContent(entryNum);
}
});
}
}
});
};
scroll_area_many_entries(gui, "scroll many area", opts);
CLAY(id, {
.layout = {.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}}
}) {
scroll_area_many_entries(gui, "scroll many area", opts);
}
}
}