From 9bbefbc0212e5c6338a496d4298edccb3475f1f2 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Tue, 25 Nov 2025 19:54:03 +0100 Subject: [PATCH] split --- .../Crafter.Graphics-MouseElement.cpp | 50 ++++++ .../Crafter.Graphics-RenderingElement.cpp | 156 ++++++++++++++++++ .../Crafter.Graphics-Transform.cpp | 32 ++++ .../Crafter.Graphics-UiElement.cpp | 154 ----------------- interfaces/Crafter.Graphics-MouseElement.cppm | 48 ++++++ .../Crafter.Graphics-RenderingElement.cppm | 52 ++++++ interfaces/Crafter.Graphics-Transform.cppm | 40 +++++ interfaces/Crafter.Graphics-UiElement.cppm | 70 +------- interfaces/Crafter.Graphics.cppm | 3 + project.json | 4 +- 10 files changed, 386 insertions(+), 223 deletions(-) create mode 100644 implementations/Crafter.Graphics-MouseElement.cpp create mode 100644 implementations/Crafter.Graphics-RenderingElement.cpp create mode 100644 implementations/Crafter.Graphics-Transform.cpp create mode 100644 interfaces/Crafter.Graphics-MouseElement.cppm create mode 100644 interfaces/Crafter.Graphics-RenderingElement.cppm create mode 100644 interfaces/Crafter.Graphics-Transform.cppm diff --git a/implementations/Crafter.Graphics-MouseElement.cpp b/implementations/Crafter.Graphics-MouseElement.cpp new file mode 100644 index 0000000..bde1010 --- /dev/null +++ b/implementations/Crafter.Graphics-MouseElement.cpp @@ -0,0 +1,50 @@ +/* +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:MouseElement_impl; +import :MouseElement; +import :Window; +import :Types; +import :Font; +import std; + +using namespace Crafter; + +MouseElement::MouseElement(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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { + +} + +MouseElement::MouseElement(Transform transform) : transform(transform) { + +} + +void MouseElement::UpdatePosition(Window& window) { + window.ScaleMouse(transform); + for(Transform* child : transform.children) { + reinterpret_cast(child->element)->UpdatePosition(window, transform); + } +} + +void MouseElement::UpdatePosition(Window& window, Transform& parent) { + window.ScaleMouse(transform, parent); + for(Transform* child : transform.children) { + reinterpret_cast(child->element)->UpdatePosition(window, transform); + } +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-RenderingElement.cpp b/implementations/Crafter.Graphics-RenderingElement.cpp new file mode 100644 index 0000000..49b1c26 --- /dev/null +++ b/implementations/Crafter.Graphics-RenderingElement.cpp @@ -0,0 +1,156 @@ +/* +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; +#define STB_IMAGE_IMPLEMENTATION +#include "../lib/stb_image.h" +#include "../lib/stb_truetype.h" +module Crafter.Graphics:RenderingElement_impl; +import :RenderingElement; +import :Window; +import :Types; +import :Font; +import std; + +using namespace Crafter; + +RenderingElement::RenderingElement(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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { + +} + +RenderingElement::RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, 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) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), transform(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { + +} + +RenderingElement::RenderingElement(const std::string_view imagePath, 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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { + RenderImage(imagePath); +} + +RenderingElement::RenderingElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, 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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { + RenderText(text, size, pixel, font); +} + +RenderingElement::RenderingElement(Transform transform) : transform(transform) { + +} + +RenderingElement::RenderingElement(Transform transform, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight) : transform(transform), bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight) { + +} + +void RenderingElement::ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height) { + this->bufferWidth = width; + this->bufferHeight = height; + buffer.resize(width * height); +} + +void RenderingElement::RenderImage(const std::string_view path) { + std::filesystem::path abs = std::filesystem::absolute(path); + int xSize; + int ySize; + unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4); + + ResizeBuffer(xSize, ySize); + + for(std::uint_fast32_t x = 0; x < xSize; x++) { + for(std::uint_fast32_t y = 0; y < ySize; y++) { + std::uint_fast32_t idx = (x*ySize+y)*4; + buffer[x*ySize+y].r = bgData[idx]; + buffer[x*ySize+y].g = bgData[idx+1]; + buffer[x*ySize+y].b = bgData[idx+2]; + buffer[x*ySize+y].a = bgData[idx+3]; + } + } +} + +void RenderingElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font) { + buffer.clear(); + + float scale = stbtt_ScaleForPixelHeight(&font.font, size); + + int baseline = (int)(font.ascent * scale); + + std::uint_fast32_t bufferWidth = 0; + for (const char c : text) { + int advance, lsb; + stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb); + bufferWidth += (int)(advance * scale); + } + + ResizeBuffer(bufferWidth, (font.ascent - font.descent) * scale); + + int x = 0; + for (std::uint_fast32_t i = 0; i < text.size(); i++) { + int codepoint = text[i]; + + int ax; + int lsb; + stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb); + + int c_x1, c_y1, c_x2, c_y2; + stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2); + + int w = c_x2 - c_x1; + int h = c_y2 - c_y1; + + std::vector bitmap(w * h); + stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint); + + for (int j = 0; j < h; j++) { + for (int i = 0; i < w; i++) { + buffer[(baseline + j + c_y1) * bufferWidth + (x + i + c_x1)] = {color.r, color.g, color.b, bitmap[j * w + i]}; + } + } + + x += (int)(ax * scale); + + if (i + 1 < text.size()) { + x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, text[i+1] * scale); + } + } +} + +void RenderingElement::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const { + for (std::uint_fast32_t y = 0; y < dstHeight; y++) { + std::uint_fast32_t srcY = y * bufferHeight / dstHeight; + for (std::uint_fast32_t x = 0; x < dstWidth; x++) { + std::uint_fast32_t srcX = x * bufferWidth / dstWidth; + dst[y * dstWidth + x] = buffer[srcY * bufferWidth + srcX]; + } + } +} + +void RenderingElement::UpdatePosition(Window& window) { + window.ScaleElement(transform); + scaled.resize(transform.scaled.width * transform.scaled.height); + CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); + for(Transform* child : transform.children) { + reinterpret_cast(child->element)->UpdatePosition(window, transform); + } +} + +void RenderingElement::UpdatePosition(Window& window, Transform& parent) { + window.ScaleElement(transform, parent); + scaled.resize(transform.scaled.width * transform.scaled.height); + CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); + for(Transform* child : transform.children) { + reinterpret_cast(child->element)->UpdatePosition(window, transform); + } +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-Transform.cpp b/implementations/Crafter.Graphics-Transform.cpp new file mode 100644 index 0000000..5044607 --- /dev/null +++ b/implementations/Crafter.Graphics-Transform.cpp @@ -0,0 +1,32 @@ +/* +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:UiElement_impl; +import :UiElement; +import :Window; +import :Types; +import :Font; +import std; + +using namespace Crafter; + +Transform::Transform(void* element, 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) : element(element), anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), ignoreScaling(ignoreScaling) { + +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElement.cpp b/implementations/Crafter.Graphics-UiElement.cpp index e4ca310..686b184 100644 --- a/implementations/Crafter.Graphics-UiElement.cpp +++ b/implementations/Crafter.Graphics-UiElement.cpp @@ -18,10 +18,6 @@ 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; -#define STB_IMAGE_IMPLEMENTATION -#include "../lib/stb_image.h" -#include "../lib/stb_truetype.h" module Crafter.Graphics:UiElement_impl; import :UiElement; import :Window; @@ -35,155 +31,6 @@ Transform::Transform(void* element, std::int_fast32_t anchorX, std::int_fast32_t } -RenderingElement::RenderingElement(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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - -} - -RenderingElement::RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, 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) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), transform(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - -} - -RenderingElement::RenderingElement(const std::string_view imagePath, 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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - RenderImage(imagePath); -} - -RenderingElement::RenderingElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, 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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - RenderText(text, size, pixel, font); -} - -RenderingElement::RenderingElement(Transform transform) : transform(transform) { - -} - -RenderingElement::RenderingElement(Transform transform, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight) : transform(transform), bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight) { - -} - -void RenderingElement::ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height) { - this->bufferWidth = width; - this->bufferHeight = height; - buffer.resize(width * height); -} - -void RenderingElement::RenderImage(const std::string_view path) { - std::filesystem::path abs = std::filesystem::absolute(path); - int xSize; - int ySize; - unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4); - - ResizeBuffer(xSize, ySize); - - for(std::uint_fast32_t x = 0; x < xSize; x++) { - for(std::uint_fast32_t y = 0; y < ySize; y++) { - std::uint_fast32_t idx = (x*ySize+y)*4; - buffer[x*ySize+y].r = bgData[idx]; - buffer[x*ySize+y].g = bgData[idx+1]; - buffer[x*ySize+y].b = bgData[idx+2]; - buffer[x*ySize+y].a = bgData[idx+3]; - } - } -} - -void RenderingElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font) { - buffer.clear(); - - float scale = stbtt_ScaleForPixelHeight(&font.font, size); - - int baseline = (int)(font.ascent * scale); - - std::uint_fast32_t bufferWidth = 0; - for (const char c : text) { - int advance, lsb; - stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb); - bufferWidth += (int)(advance * scale); - } - - ResizeBuffer(bufferWidth, (font.ascent - font.descent) * scale); - - int x = 0; - for (std::uint_fast32_t i = 0; i < text.size(); i++) { - int codepoint = text[i]; - - int ax; - int lsb; - stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb); - - int c_x1, c_y1, c_x2, c_y2; - stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2); - - int w = c_x2 - c_x1; - int h = c_y2 - c_y1; - - std::vector bitmap(w * h); - stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint); - - for (int j = 0; j < h; j++) { - for (int i = 0; i < w; i++) { - buffer[(baseline + j + c_y1) * bufferWidth + (x + i + c_x1)] = {color.r, color.g, color.b, bitmap[j * w + i]}; - } - } - - x += (int)(ax * scale); - - if (i + 1 < text.size()) { - x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, text[i+1] * scale); - } - } -} - -void RenderingElement::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const { - for (std::uint_fast32_t y = 0; y < dstHeight; y++) { - std::uint_fast32_t srcY = y * bufferHeight / dstHeight; - for (std::uint_fast32_t x = 0; x < dstWidth; x++) { - std::uint_fast32_t srcX = x * bufferWidth / dstWidth; - dst[y * dstWidth + x] = buffer[srcY * bufferWidth + srcX]; - } - } -} - -void RenderingElement::UpdatePosition(Window& window) { - window.ScaleElement(transform); - scaled.resize(transform.scaled.width * transform.scaled.height); - CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); - for(Transform* child : transform.children) { - reinterpret_cast(child->element)->UpdatePosition(window, transform); - } -} - -void RenderingElement::UpdatePosition(Window& window, Transform& parent) { - window.ScaleElement(transform, parent); - scaled.resize(transform.scaled.width * transform.scaled.height); - CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); - for(Transform* child : transform.children) { - reinterpret_cast(child->element)->UpdatePosition(window, transform); - } -} - - -// MouseElement implementation -MouseElement::MouseElement(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(this, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - -} - -MouseElement::MouseElement(Transform transform) : transform(transform) { - -} - -void MouseElement::UpdatePosition(Window& window) { - window.ScaleMouse(transform); - for(Transform* child : transform.children) { - reinterpret_cast(child->element)->UpdatePosition(window, transform); - } -} - -void MouseElement::UpdatePosition(Window& window, Transform& parent) { - window.ScaleMouse(transform, parent); - for(Transform* child : transform.children) { - reinterpret_cast(child->element)->UpdatePosition(window, transform); - } -} - - RenderingMouseElement::RenderingMouseElement(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) : rendering(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouse(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) { } @@ -204,7 +51,6 @@ RenderingMouseElement::RenderingMouseElement(Transform transform, Transform mous } - void RenderingMouseElement::UpdatePosition(Window& window) { rendering.UpdatePosition(window); mouse.UpdatePosition(window, rendering.transform); diff --git a/interfaces/Crafter.Graphics-MouseElement.cppm b/interfaces/Crafter.Graphics-MouseElement.cppm new file mode 100644 index 0000000..be37fb3 --- /dev/null +++ b/interfaces/Crafter.Graphics-MouseElement.cppm @@ -0,0 +1,48 @@ +/* +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:MouseElement; +import std; +import Crafter.Event; +import :Types; +import :Transform; + +export namespace Crafter { + class Window; + class MouseElement { + public: + Transform transform; + Event onMouseMove; + Event onMouseEnter; + Event onMouseLeave; + Event onMouseRightClick; + Event onMouseLeftClick; + Event onMouseRightHold; + Event onMouseLeftHold; + Event onMouseRightRelease; + Event onMouseLeftRelease; + + MouseElement(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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + MouseElement(Transform transform); + MouseElement(MouseElement&) = delete; + MouseElement& operator=(MouseElement&) = delete; + void UpdatePosition(Window& window); + void UpdatePosition(Window& window, Transform& parent); + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-RenderingElement.cppm b/interfaces/Crafter.Graphics-RenderingElement.cppm new file mode 100644 index 0000000..c3921fe --- /dev/null +++ b/interfaces/Crafter.Graphics-RenderingElement.cppm @@ -0,0 +1,52 @@ +/* +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:RenderingElement; +import std; +import :Transform; +import :Types; + +export namespace Crafter { + class Window; + class Font; + class RenderingElement { + public: + Transform transform; + std::vector buffer; + std::vector scaled; + std::uint_fast32_t bufferWidth; + std::uint_fast32_t bufferHeight; + + RenderingElement(Transform transform); + RenderingElement(Transform transform, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight); + RenderingElement(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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + RenderingElement(const std::string_view imagePath, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + RenderingElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + RenderingElement(RenderingElement&) = delete; + RenderingElement& operator=(RenderingElement&) = delete; + + void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height); + void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const; + void RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font); + void RenderImage(const std::string_view path); + void UpdatePosition(Window& window); + void UpdatePosition(Window& window, Transform& parent); + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-Transform.cppm b/interfaces/Crafter.Graphics-Transform.cppm new file mode 100644 index 0000000..fe52820 --- /dev/null +++ b/interfaces/Crafter.Graphics-Transform.cppm @@ -0,0 +1,40 @@ +/* +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:Transform; +import std; +import :Types; + +export namespace Crafter { + class Transform { + public: + void* element; + std::int_fast32_t z; + std::int_fast32_t anchorX; + std::int_fast32_t anchorY; + std::int_fast32_t anchorOffsetX; + std::int_fast32_t anchorOffsetY; + std::uint_fast32_t relativeWidth; + std::uint_fast32_t relativeHeight; + bool ignoreScaling; + ScaleData scaled; + std::vector children; + Transform(void* element, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-UiElement.cppm b/interfaces/Crafter.Graphics-UiElement.cppm index 1b11dec..7f700bd 100644 --- a/interfaces/Crafter.Graphics-UiElement.cppm +++ b/interfaces/Crafter.Graphics-UiElement.cppm @@ -21,75 +21,11 @@ export module Crafter.Graphics:UiElement; import std; import Crafter.Event; import :Types; +import :Transform; +import :RenderingElement; +import :MouseElement; export namespace Crafter { - class Transform { - public: - void* element; - std::int_fast32_t z; - std::int_fast32_t anchorX; - std::int_fast32_t anchorY; - std::int_fast32_t anchorOffsetX; - std::int_fast32_t anchorOffsetY; - std::uint_fast32_t relativeWidth; - std::uint_fast32_t relativeHeight; - bool ignoreScaling; - ScaleData scaled; - std::vector children; - Transform(void* element, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); - }; - - class Window; - class Font; - - // Base class for rendering elements - class RenderingElement { - public: - Transform transform; - std::vector buffer; - std::vector scaled; - std::uint_fast32_t bufferWidth; - std::uint_fast32_t bufferHeight; - - RenderingElement(Transform transform); - RenderingElement(Transform transform, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight); - RenderingElement(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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); - RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); - RenderingElement(const std::string_view imagePath, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); - RenderingElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, 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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); - RenderingElement(RenderingElement&) = delete; - RenderingElement& operator=(RenderingElement&) = delete; - - void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height); - void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const; - void RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font); - void RenderImage(const std::string_view path); - void UpdatePosition(Window& window); - void UpdatePosition(Window& window, Transform& parent); - }; - - // Base class for Mouse elements - class MouseElement { - public: - Transform transform; - Event onMouseMove; - Event onMouseEnter; - Event onMouseLeave; - Event onMouseRightClick; - Event onMouseLeftClick; - Event onMouseRightHold; - Event onMouseLeftHold; - Event onMouseRightRelease; - Event onMouseLeftRelease; - - MouseElement(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 = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); - MouseElement(Transform transform); - MouseElement(MouseElement&) = delete; - MouseElement& operator=(MouseElement&) = delete; - void UpdatePosition(Window& window); - void UpdatePosition(Window& window, Transform& parent); - }; - // Combined UI element that can have both rendering and interaction class RenderingMouseElement { public: diff --git a/interfaces/Crafter.Graphics.cppm b/interfaces/Crafter.Graphics.cppm index 9a0e40f..98e5d7f 100644 --- a/interfaces/Crafter.Graphics.cppm +++ b/interfaces/Crafter.Graphics.cppm @@ -21,6 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA export module Crafter.Graphics; export import :Window; +export import :Transform; +export import :RenderingElement; +export import :MouseElement; export import :UiElement; export import :Types; export import :Font; diff --git a/project.json b/project.json index ff181ce..d361a97 100644 --- a/project.json +++ b/project.json @@ -3,8 +3,8 @@ "configurations": [ { "name": "base", - "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-UiElement", "implementations/Crafter.Graphics-Window"], - "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-UiElement", "interfaces/Crafter.Graphics-Animation"], + "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-UiElement", "implementations/Crafter.Graphics-Window", "implementations/Crafter.Graphics-RenderingElement", "implementations/Crafter.Graphics-MouseElement", "implementations/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-UiElement", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-RenderingElement", "interfaces/Crafter.Graphics-MouseElement", "interfaces/Crafter.Graphics-Transform"], "type": "library" }, {