From 886fd313a436d894bae4878206ae64aaae7f6907 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Sat, 18 Apr 2026 20:51:45 +0300 Subject: [PATCH] Added grid scroll area (has issues with many elements) --- CMakeLists.txt | 1 + src/GUIStuff/Elements/GridScrollArea.cpp | 46 ++++++++++++++++++++++++ src/GUIStuff/Elements/GridScrollArea.hpp | 26 ++++++++++++++ src/MainProgram.cpp | 2 +- src/Screens/FileSelectScreen.cpp | 31 ++++++++++++++-- 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/GUIStuff/Elements/GridScrollArea.cpp create mode 100644 src/GUIStuff/Elements/GridScrollArea.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c6325a..812114c 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ set(sources "src/main.cpp" "src/GUIStuff/Elements/ScrollArea.cpp" "src/GUIStuff/Elements/PositionAdjustingPopupMenu.cpp" "src/GUIStuff/Elements/ColorPickerShaders.cpp" + "src/GUIStuff/Elements/GridScrollArea.cpp" "src/GUIStuff/ElementHelpers/PopupHelpers.cpp" "src/GUIStuff/ElementHelpers/ButtonHelpers.cpp" "src/GUIStuff/ElementHelpers/LayoutHelpers.cpp" diff --git a/src/GUIStuff/Elements/GridScrollArea.cpp b/src/GUIStuff/Elements/GridScrollArea.cpp new file mode 100644 index 0000000..4b7b8c8 --- /dev/null +++ b/src/GUIStuff/Elements/GridScrollArea.cpp @@ -0,0 +1,46 @@ +#include "GridScrollArea.hpp" +#include "../ElementHelpers/ScrollAreaHelpers.hpp" +#include "Helpers/ConvertVec.hpp" +#include "../GUIManager.hpp" + +namespace GUIStuff { + +GridScrollArea::GridScrollArea(GUIManager& gui): Element(gui) {} + +void GridScrollArea::layout(const Clay_ElementId& id, const Options& options) { + using namespace ElementHelpers; + + ScrollBarManyEntriesOptions opts; + opts.clipHorizontal = true; + opts.entryHeight = options.entryHeight; + opts.innerContentExtraCallback = [&, extraCallback = options.innerContentExtraCallback] (const ScrollArea::InnerContentParameters& contParams) { + rowWidth = std::fabs(contParams.containerDimensions.x()); + if(extraCallback) extraCallback(contParams); + }; + size_t entriesPerRow = static_cast(rowWidth / options.entryMaximumWidth) + 1; + size_t rowCount = options.entryCount / 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} + } + }) { + for(size_t i = 0; i < entriesPerRow; i++) { + size_t entryNum = rowIndex * entriesPerRow + i; + gui.new_id(static_cast(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); +} + +} diff --git a/src/GUIStuff/Elements/GridScrollArea.hpp b/src/GUIStuff/Elements/GridScrollArea.hpp new file mode 100644 index 0000000..5cab388 --- /dev/null +++ b/src/GUIStuff/Elements/GridScrollArea.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "Element.hpp" +#include "Helpers/ConvertVec.hpp" +#include "ScrollArea.hpp" + +namespace GUIStuff { + +class GridScrollArea : public Element { + public: + GridScrollArea(GUIManager& gui); + + struct Options { + float entryMaximumWidth; + Clay_LayoutAlignmentX childAlignmentX = CLAY_ALIGN_X_LEFT; + float entryHeight; + size_t entryCount; + std::function elementContent; + std::function innerContentExtraCallback; + }; + + void layout(const Clay_ElementId& id, const Options& options); + private: + float rowWidth = 0.0f; +}; + +} diff --git a/src/MainProgram.cpp b/src/MainProgram.cpp index 26754cf..ff9dcf9 100755 --- a/src/MainProgram.cpp +++ b/src/MainProgram.cpp @@ -42,7 +42,7 @@ MainProgram::MainProgram(): fonts(std::make_shared()), g(*this) { - screen = std::make_unique(*this); + screen = std::make_unique(*this); Logger::get().add_log("WORLDFATAL", [&](const std::string& text) { *logFile << "[WORLDFATAL] " << text << std::endl; std::cout << "[WORLDFATAL] " << text << std::endl; diff --git a/src/Screens/FileSelectScreen.cpp b/src/Screens/FileSelectScreen.cpp index 30f037b..bda7f02 100644 --- a/src/Screens/FileSelectScreen.cpp +++ b/src/Screens/FileSelectScreen.cpp @@ -2,16 +2,43 @@ #include "../MainProgram.hpp" #include "../GUIHolder.hpp" #include "../GUIStuff/ElementHelpers/TextLabelHelpers.hpp" +#include "Helpers/ConvertVec.hpp" +#include "../GUIStuff/Elements/GridScrollArea.hpp" +#include "../GUIStuff/ElementHelpers/ButtonHelpers.hpp" FileSelectScreen::FileSelectScreen(MainProgram& m): Screen(m) {} void FileSelectScreen::gui_layout_run() { using namespace GUIStuff; using namespace ElementHelpers; + auto& gui = main.g.gui; + CLAY_AUTO_ID({ - .layout = {} + .layout = { + .sizing = {.width = CLAY_SIZING_FIXED(gui.io.windowSize.x()), .height = CLAY_SIZING_FIXED(gui.io.windowSize.y())}, + .childGap = 15, + .childAlignment = { .x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_TOP }, + .layoutDirection = CLAY_TOP_TO_BOTTOM, + }, }) { - text_label(main.g.gui, "File select screen"); + text_label(gui, "Test grid"); + gui.element("Test grid", GridScrollArea::Options{ + .entryMaximumWidth = 100.0f, + .childAlignmentX = CLAY_ALIGN_X_LEFT, + .entryHeight = 100.0f, + .entryCount = 700, + .elementContent = [&](size_t i) { + CLAY_AUTO_ID({ + .layout = { + .sizing = { .width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}, + .padding = CLAY_PADDING_ALL(2), + .childAlignment = { .x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER }, + } + }) { + text_button_sized(gui, "button", "Square " + std::to_string(i), CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0)); + } + } + }); } }