This commit is contained in:
Jorijn van der Graaf 2025-11-27 00:38:47 +01:00
commit 7bbb107c5e
4 changed files with 97 additions and 3 deletions

View file

@ -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.

View file

@ -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();
}

View file

@ -0,0 +1,15 @@
{
"name": "crafter-graphics",
"configurations": [
{
"name": "executable",
"implementations": ["main"],
"dependencies": [
{
"path":"../../project.json",
"configuration":"lib-wayland"
}
]
}
]
}

View file

@ -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;