grid
This commit is contained in:
parent
70f05160c5
commit
d7a45e436c
4 changed files with 172 additions and 2 deletions
120
implementations/Crafter.Graphics-GridElement.cpp
Normal file
120
implementations/Crafter.Graphics-GridElement.cpp
Normal file
|
|
@ -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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
49
interfaces/Crafter.Graphics-GridElement.cppm
Normal file
49
interfaces/Crafter.Graphics-GridElement.cppm
Normal file
|
|
@ -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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -26,6 +26,7 @@ export import :RenderingElement;
|
||||||
export import :MouseElement;
|
export import :MouseElement;
|
||||||
export import :ImageElement;
|
export import :ImageElement;
|
||||||
export import :TextElement;
|
export import :TextElement;
|
||||||
|
export import :GridElement;
|
||||||
export import :Types;
|
export import :Types;
|
||||||
export import :Font;
|
export import :Font;
|
||||||
export import :Shm;
|
export import :Shm;
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "base",
|
"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"],
|
"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": ["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"
|
"type": "library"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue