/* Crafter®.Graphics Copyright (C) 2026 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:Transform2D_impl; import :Transform2D; import :Rendertarget; import :Types; import :Font; import std; using namespace Crafter; Anchor2D::Anchor2D(float x, float y, float width, float height, float offsetX, float offsetY, std::int32_t z, bool maintainAspectRatio): x(x), y(y), width(width), height(height), offsetX(offsetX), offsetY(offsetY), z(z), maintainAspectRatio(maintainAspectRatio) { } Transform2D::Transform2D(Anchor2D anchor) : anchor(anchor) { } void Transform2D::UpdatePosition(Rendertarget& window) { ScaleElement(window.transform); for(Transform2D* child : children) { child->UpdatePosition(window, *this); } } void Transform2D::UpdatePosition(Rendertarget& window, Transform2D& parent) { ScaleElement(parent); for(Transform2D* child : children) { child->UpdatePosition(window, *this); } } void Transform2D::ScaleElement(Transform2D& parent) { if(anchor.maintainAspectRatio) { if(parent.scaled.size.x > parent.scaled.size.y) { scaled.size.x = anchor.width * parent.scaled.size.y; scaled.size.y = anchor.height * parent.scaled.size.y; } else { scaled.size.x = anchor.width * parent.scaled.size.x; scaled.size.y = anchor.height * parent.scaled.size.x; } } else { scaled.size.x = anchor.width * parent.scaled.size.x; scaled.size.y = anchor.height * parent.scaled.size.y; } scaled.position.x = parent.scaled.position.x + (anchor.x * parent.scaled.size.x - anchor.offsetX * scaled.size.x); scaled.position.y = parent.scaled.position.y + (anchor.y * parent.scaled.size.y - anchor.offsetY * scaled.size.y); }