From d7a45e436c51b1d694a53c2da23c8cbc857ada11 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Thu, 27 Nov 2025 00:27:13 +0100 Subject: [PATCH] grid --- .../Crafter.Graphics-GridElement.cpp | 120 ++++++++++++++++++ interfaces/Crafter.Graphics-GridElement.cppm | 49 +++++++ interfaces/Crafter.Graphics.cppm | 1 + project.json | 4 +- 4 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 implementations/Crafter.Graphics-GridElement.cpp create mode 100644 interfaces/Crafter.Graphics-GridElement.cppm diff --git a/implementations/Crafter.Graphics-GridElement.cpp b/implementations/Crafter.Graphics-GridElement.cpp new file mode 100644 index 0000000..cbcb24b --- /dev/null +++ b/implementations/Crafter.Graphics-GridElement.cpp @@ -0,0 +1,120 @@ +/* +Crafter®.Graphics +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module Crafter.Graphics:GridElement_impl; +import :GridElement; +import :Window; +import :Types; +import std; + +using namespace Crafter; + +GridElement::GridElement(std::uint_fast32_t columns, std::uint_fast32_t rows, + std::int_fast32_t spacingX, std::int_fast32_t spacingY, + std::int_fast32_t anchorX, std::int_fast32_t anchorY, + std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, + std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, + std::int_fast32_t z, bool ignoreScaling) + : Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), + columns(columns), rows(rows), spacingX(spacingX), spacingY(spacingY) { + +} + +void GridElement::SetGridSize(std::uint_fast32_t columns, std::uint_fast32_t rows) { + this->columns = columns; + this->rows = rows; +} + +void GridElement::SetSpacing(std::int_fast32_t spacingX, std::int_fast32_t spacingY) { + this->spacingX = spacingX; + this->spacingY = spacingY; +} + +void GridElement::UpdatePosition(Window& window) { + Transform::UpdatePosition(window); + + // Calculate grid dimensions + std::int_fast32_t totalWidth = 0; + std::int_fast32_t totalHeight = 0; + + if (columns > 0 && rows > 0) { + // Calculate the space available for children based on grid size + std::int_fast32_t cellWidth = (relativeWidth - (columns - 1) * spacingX) / columns; + std::int_fast32_t cellHeight = (relativeHeight - (rows - 1) * spacingY) / rows; + + // Position each child in the grid + std::size_t childIndex = 0; + for (std::uint_fast32_t row = 0; row < rows && childIndex < children.size(); ++row) { + for (std::uint_fast32_t col = 0; col < columns && childIndex < children.size(); ++col) { + Transform* child = children[childIndex]; + + // Calculate position for this child + std::int_fast32_t childX = anchorOffsetX + (cellWidth * col) + (spacingX * col); + std::int_fast32_t childY = anchorOffsetY + (cellHeight * row) + (spacingY * row); + + // Apply relative positioning + child->anchorX = childX; + child->anchorY = childY; + child->relativeWidth = cellWidth; + child->relativeHeight = cellHeight; + + // Update child position + child->UpdatePosition(window, *this); + childIndex++; + } + } + } +} + +void GridElement::UpdatePosition(Window& window, Transform& parent) { + Transform::UpdatePosition(window, parent); + + // Calculate grid dimensions + std::int_fast32_t totalWidth = 0; + std::int_fast32_t totalHeight = 0; + + if (columns > 0 && rows > 0) { + // Calculate the space available for children based on grid size + std::int_fast32_t cellWidth = (relativeWidth - (columns - 1) * spacingX) / columns; + std::int_fast32_t cellHeight = (relativeHeight - (rows - 1) * spacingY) / rows; + + // Position each child in the grid + std::size_t childIndex = 0; + for (std::uint_fast32_t row = 0; row < rows && childIndex < children.size(); ++row) { + for (std::uint_fast32_t col = 0; col < columns && childIndex < children.size(); ++col) { + Transform* child = children[childIndex]; + + // Calculate position for this child + std::int_fast32_t childX = anchorOffsetX + (cellWidth * col) + (spacingX * col); + std::int_fast32_t childY = anchorOffsetY + (cellHeight * row) + (spacingY * row); + + // Apply relative positioning + child->anchorX = childX; + child->anchorY = childY; + child->relativeWidth = cellWidth; + child->relativeHeight = cellHeight; + + // Update child position + child->UpdatePosition(window, *this); + childIndex++; + } + } + } +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-GridElement.cppm b/interfaces/Crafter.Graphics-GridElement.cppm new file mode 100644 index 0000000..9663f32 --- /dev/null +++ b/interfaces/Crafter.Graphics-GridElement.cppm @@ -0,0 +1,49 @@ +/* +Crafter®.Graphics +Copyright (C) 2025 Catcrafts® +catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Graphics:GridElement; +import std; +import :Transform; +import :Types; + +export namespace Crafter { + class GridElement : public Transform { + private: + std::uint_fast32_t columns; + std::uint_fast32_t rows; + std::int_fast32_t spacingX; + std::int_fast32_t spacingY; + + public: + GridElement(std::uint_fast32_t columns = 1, std::uint_fast32_t rows = 1, + std::int_fast32_t spacingX = 0, std::int_fast32_t spacingY = 0, + std::int_fast32_t anchorX = FractionalToMapped(0.5), + std::int_fast32_t anchorY = FractionalToMapped(0.5), + std::uint_fast32_t relativeWidth = FractionalToMapped(1), + std::uint_fast32_t relativeHeight = FractionalToMapped(1), + std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), + std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), + std::int_fast32_t z = 0, bool ignoreScaling = false); + + void SetGridSize(std::uint_fast32_t columns, std::uint_fast32_t rows); + void SetSpacing(std::int_fast32_t spacingX, std::int_fast32_t spacingY); + void UpdatePosition(Window& window) override; + void UpdatePosition(Window& window, Transform& parent) override; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics.cppm b/interfaces/Crafter.Graphics.cppm index 2eedfaa..9b64da2 100644 --- a/interfaces/Crafter.Graphics.cppm +++ b/interfaces/Crafter.Graphics.cppm @@ -26,6 +26,7 @@ export import :RenderingElement; export import :MouseElement; export import :ImageElement; export import :TextElement; +export import :GridElement; export import :Types; export import :Font; export import :Shm; diff --git a/project.json b/project.json index 22464e8..b730015 100644 --- a/project.json +++ b/project.json @@ -3,8 +3,8 @@ "configurations": [ { "name": "base", - "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-Window", "implementations/Crafter.Graphics-RenderingElement", "implementations/Crafter.Graphics-TextElement", "implementations/Crafter.Graphics-ImageElement", "implementations/Crafter.Graphics-MouseElement", "implementations/Crafter.Graphics-Transform"], - "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-RenderingElement", "interfaces/Crafter.Graphics-TextElement", "interfaces/Crafter.Graphics-ImageElement", "interfaces/Crafter.Graphics-MouseElement", "interfaces/Crafter.Graphics-Transform"], + "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-Window", "implementations/Crafter.Graphics-RenderingElement", "implementations/Crafter.Graphics-TextElement", "implementations/Crafter.Graphics-ImageElement", "implementations/Crafter.Graphics-MouseElement", "implementations/Crafter.Graphics-Transform", "implementations/Crafter.Graphics-GridElement"], + "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-RenderingElement", "interfaces/Crafter.Graphics-TextElement", "interfaces/Crafter.Graphics-ImageElement", "interfaces/Crafter.Graphics-MouseElement", "interfaces/Crafter.Graphics-Transform", "interfaces/Crafter.Graphics-GridElement"], "type": "library" }, {