Crafter.Graphics/implementations/Crafter.Graphics-GridElement.cpp

119 lines
4.8 KiB
C++
Raw Permalink Normal View History

2025-11-27 00:27:13 +01:00
/*
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) {
2025-11-27 00:38:47 +01:00
window.ScaleElement(*this);
2025-11-27 00:27:13 +01:00
// 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) {
2025-11-27 00:38:47 +01:00
window.ScaleElement(*this, parent);
2025-11-27 00:27:13 +01:00
// 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++;
}
}
}
}