Crafter.Graphics/implementations/Crafter.Graphics-UiElement.cpp

211 lines
11 KiB
C++
Raw Normal View History

2025-05-07 19:21:51 +02: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
*/
2025-11-25 18:52:32 +01:00
module;
#define STB_IMAGE_IMPLEMENTATION
#include "../lib/stb_image.h"
#include "../lib/stb_truetype.h"
2025-11-16 15:32:11 +01:00
module Crafter.Graphics:UiElement_impl;
import :UiElement;
2025-11-23 04:04:53 +01:00
import :Window;
import :Types;
2025-11-25 18:52:32 +01:00
import :Font;
2025-11-16 18:52:52 +01:00
import std;
2025-04-16 00:43:33 +02:00
using namespace Crafter;
2025-11-25 19:43:40 +01:00
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) {
2025-11-24 01:47:49 +01:00
}
2025-11-25 19:43:40 +01:00
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) {
2025-11-24 01:47:49 +01:00
}
2025-11-25 19:43:40 +01:00
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) {
2025-11-17 00:44:45 +01:00
2025-11-23 04:04:53 +01:00
}
2025-11-25 19:43:40 +01:00
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) {
2025-11-25 18:52:32 +01:00
RenderImage(imagePath);
}
2025-11-25 19:43:40 +01:00
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) {
2025-11-25 18:52:32 +01:00
RenderText(text, size, pixel, font);
}
2025-11-25 19:43:40 +01:00
RenderingElement::RenderingElement(Transform transform) : transform(transform) {
2025-11-25 18:52:32 +01:00
}
2025-11-25 19:43:40 +01:00
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) {
2025-11-25 18:52:32 +01:00
this->bufferWidth = width;
this->bufferHeight = height;
buffer.resize(width * height);
}
2025-11-25 19:43:40 +01:00
void RenderingElement::RenderImage(const std::string_view path) {
2025-11-25 18:52:32 +01:00
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];
}
}
}
2025-11-25 19:43:40 +01:00
void RenderingElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font) {
2025-11-25 18:52:32 +01:00
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<unsigned char> 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);
}
}
}
2025-11-25 19:43:40 +01:00
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) {
2025-11-25 18:52:32 +01:00
window.ScaleElement(transform);
scaled.resize(transform.scaled.width * transform.scaled.height);
CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height);
2025-11-25 19:43:40 +01:00
for(Transform* child : transform.children) {
reinterpret_cast<RenderingElement*>(child->element)->UpdatePosition(window, transform);
2025-11-25 18:52:32 +01:00
}
}
2025-11-25 19:43:40 +01:00
void RenderingElement::UpdatePosition(Window& window, Transform& parent) {
window.ScaleElement(transform, parent);
2025-11-25 18:52:32 +01:00
scaled.resize(transform.scaled.width * transform.scaled.height);
2025-11-25 19:43:40 +01:00
CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height);
for(Transform* child : transform.children) {
reinterpret_cast<RenderingElement*>(child->element)->UpdatePosition(window, transform);
2025-11-25 18:52:32 +01:00
}
}
2025-11-25 19:43:40 +01:00
// 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<MouseElement*>(child->element)->UpdatePosition(window, transform);
}
}
void MouseElement::UpdatePosition(Window& window, Transform& parent) {
window.ScaleMouse(transform, parent);
for(Transform* child : transform.children) {
reinterpret_cast<MouseElement*>(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) {
}
RenderingMouseElement::RenderingMouseElement(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) : rendering(bufferWidth, bufferHeight, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouse(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) {
}
RenderingMouseElement::RenderingMouseElement(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) : rendering(imagePath, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouse(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) {
}
RenderingMouseElement::RenderingMouseElement(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) : rendering(text, size, pixel, font, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouse(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) {
}
RenderingMouseElement::RenderingMouseElement(Transform transform, Transform mouseTransform) : rendering(transform), mouse(mouseTransform) {
}
void RenderingMouseElement::UpdatePosition(Window& window) {
rendering.UpdatePosition(window);
mouse.UpdatePosition(window, rendering.transform);
2025-11-24 01:47:49 +01:00
}