Crafter.Graphics/implementations/Crafter.Graphics-GridElement.cpp
2025-12-29 18:56:06 +01:00

67 lines
No EOL
2.6 KiB
C++

/*
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 paddingX, std::int_fast32_t paddingY, Anchor anchor) : Transform(anchor), columns(columns), rows(rows), spacingX(spacingX), spacingY(spacingY), paddingX(paddingX), paddingY(paddingY) {
}
void GridElement::UpdatePositionScaled(Window& window) {
std::int_fast32_t cellWidth = (SCALE - (paddingX * 2) - (spacingX * (columns - 1))) / columns;
std::int_fast32_t cellHeight = (SCALE - (paddingY * 2) - (spacingY * (rows - 1))) / rows;
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 = (cellWidth * col) + (spacingX * col) + paddingX;
std::int_fast32_t childY = (cellHeight * row) + (spacingY * row) + paddingY;
// Apply relative positioning
child->anchor.x = childX;
child->anchor.y = childY;
child->anchor.width = cellWidth;
child->anchor.height = cellHeight;
// Update child position
child->UpdatePosition(window, *this);
childIndex++;
}
}
}
void GridElement::UpdatePosition(Window& window) {
window.ScaleElement(*this);
UpdatePositionScaled(window);
}
void GridElement::UpdatePosition(Window& window, Transform& parent) {
window.ScaleElement(*this, parent);
UpdatePositionScaled(window);
}