diff --git a/examples/HelloGrid/README.md b/examples/HelloGrid/README.md new file mode 100644 index 0000000..2b0f612 --- /dev/null +++ b/examples/HelloGrid/README.md @@ -0,0 +1,19 @@ +# HelloGrid Example + +This example demonstrates the usage of the GridElement class which arranges its children in configurable grid patterns. + +## Features Shown + +- Creating a GridElement with specified columns and rows +- Setting spacing between grid cells +- Adding multiple child elements to the grid +- Automatic positioning of children in a grid layout +- Different colored elements to visualize the grid structure + +## How It Works + +The GridElement automatically distributes its children across a grid defined by: +- Number of columns and rows +- Horizontal and vertical spacing between elements + +Each child element is positioned in row-major order, filling the grid from left to right and top to bottom. \ No newline at end of file diff --git a/examples/HelloGrid/main.cpp b/examples/HelloGrid/main.cpp new file mode 100644 index 0000000..5ab7dfb --- /dev/null +++ b/examples/HelloGrid/main.cpp @@ -0,0 +1,61 @@ +import Crafter.Event; +import Crafter.Graphics; +import std; +using namespace Crafter; + +int main() { + WindowWayland window(1280, 720, "Hello Grid!"); + + // Create a GridElement with 3 columns and 2 rows + GridElement grid( + 3, // columns + 2, // rows + FractionalToMapped(0.1), // spacingX + FractionalToMapped(0.1), // spacingY + FractionalToMapped(0), // anchorX + FractionalToMapped(0), // anchorY + FractionalToMapped(1), // relativeWidth + FractionalToMapped(1), // relativeHeight + FractionalToMapped(0), // anchorOffsetX + FractionalToMapped(0), // anchorOffsetY + 0, // z + false // ignoreScaling + ); + + for (int i = 0; i < 6; i++) { + RenderingElementScaling* rendering = new RenderingElementScaling( + OpaqueType::FullyOpaque, + 1, + 1, + FractionalToMapped(0.5), // anchorX + FractionalToMapped(0.5), // anchorY + FractionalToMapped(1.0), // relativeSizeX (will be overridden by grid) + FractionalToMapped(1.0), // relativeSizeY (will be overridden by grid) + FractionalToMapped(0.0), // anchorOffsetX + FractionalToMapped(0.0), // anchorOffsetY + 0, // z + false // ignoreScaling + ); + + // // Set different colors for each element + switch (i % 6) { + case 0: rendering->buffer = {{255, 0, 0, 255}}; break; // Red + case 1: rendering->buffer = {{0, 255, 0, 255}}; break; // Green + case 2: rendering->buffer = {{0, 0, 255, 255}}; break; // Blue + case 3: rendering->buffer = {{255, 255, 0, 255}}; break; // Yellow + case 4: rendering->buffer = {{255, 0, 255, 255}}; break; // Magenta + case 5: rendering->buffer = {{0, 255, 255, 255}}; break; // Cyan + } + + grid.children.push_back(rendering); + } + + // Add the grid to the window + window.elements.push_back(&grid); + + // Update positions to arrange children in grid + grid.UpdatePosition(window); + + window.Render(); + window.StartSync(); +} \ No newline at end of file diff --git a/examples/HelloGrid/project.json b/examples/HelloGrid/project.json new file mode 100644 index 0000000..70e3d7a --- /dev/null +++ b/examples/HelloGrid/project.json @@ -0,0 +1,15 @@ +{ + "name": "crafter-graphics", + "configurations": [ + { + "name": "executable", + "implementations": ["main"], + "dependencies": [ + { + "path":"../../project.json", + "configuration":"lib-wayland" + } + ] + } + ] +} diff --git a/implementations/Crafter.Graphics-GridElement.cpp b/implementations/Crafter.Graphics-GridElement.cpp index cbcb24b..84727f7 100644 --- a/implementations/Crafter.Graphics-GridElement.cpp +++ b/implementations/Crafter.Graphics-GridElement.cpp @@ -48,7 +48,7 @@ void GridElement::SetSpacing(std::int_fast32_t spacingX, std::int_fast32_t spaci } void GridElement::UpdatePosition(Window& window) { - Transform::UpdatePosition(window); + window.ScaleElement(*this); // Calculate grid dimensions std::int_fast32_t totalWidth = 0; @@ -84,8 +84,7 @@ void GridElement::UpdatePosition(Window& window) { } void GridElement::UpdatePosition(Window& window, Transform& parent) { - Transform::UpdatePosition(window, parent); - + window.ScaleElement(*this, parent); // Calculate grid dimensions std::int_fast32_t totalWidth = 0; std::int_fast32_t totalHeight = 0;