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

67 lines
No EOL
2.4 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, Anchor anchor) : Transform(anchor), columns(columns), rows(rows), spacingX(spacingX), spacingY(spacingY) {
}
void GridElement::UpdatePositionScaled(Window& window) {
std::int_fast32_t cellWidth = SCALE / columns;
std::int_fast32_t cellHeight = SCALE / 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);
std::int_fast32_t childY = (cellHeight * row) + (spacingY * row);
// 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);
}