diff --git a/examples/HelloUI/main.cpp b/examples/HelloUI/main.cpp index b6f1f0b..0741907 100644 --- a/examples/HelloUI/main.cpp +++ b/examples/HelloUI/main.cpp @@ -6,7 +6,7 @@ using namespace Crafter; int main() { WindowWayland window(1280, 720, "Hello Input!"); - UiElementBufferMouseBuffer* element = new UiElementBufferMouseBuffer( + UiElement* element = new UiElement( 2, //bufferWidth: the width of this elements pixel buffer 1, //bufferHeight: the height of this elements pixel buffer FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor @@ -22,8 +22,9 @@ int main() { window.elements.push_back(element); window.mouseElements.push_back(element); + element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; element->UpdatePosition(window); - window.ScaleMouse(element->UiElementMouse::transform, element->UiElement::transform); + window.ScaleMouse(element->mouseTransform, element->transform); EventListener clickListener(&element->onMouseLeftClick, [element, &window](MousePoint point){ // Print the coordinates where the user clicked relative to the element's top left corner. @@ -35,11 +36,9 @@ int main() { std::cout << std::format("Clicked on Fraction X:{} Y:{}!", MappedToFractionalBoundless(point.x), MappedToFractionalBoundless(point.y)) << std::endl; // Screen space - std::cout << std::format("Clicked on Screen X:{} Y:{}!\n", MappedToPixelBoundless(point.x, MappedToPixelBoundless(element->UiElementMouse::transform.scaled.width, window.width)), MappedToPixelBoundless(element->UiElementMouse::transform.scaled.height, window.height)) << std::endl; + std::cout << std::format("Clicked on Screen X:{} Y:{}!\n", MappedToPixelBoundless(point.x, MappedToPixelBoundless(element->mouseTransform.scaled.width, window.width)), MappedToPixelBoundless(point.y, window.height)) << std::endl; }); - - element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; window.Render(); window.StartSync(); } diff --git a/implementations/Crafter.Graphics-UiElement.cpp b/implementations/Crafter.Graphics-UiElement.cpp index 7f539eb..5a38262 100644 --- a/implementations/Crafter.Graphics-UiElement.cpp +++ b/implementations/Crafter.Graphics-UiElement.cpp @@ -18,10 +18,15 @@ 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; import :Types; +import :Font; import std; using namespace Crafter; @@ -30,14 +35,122 @@ Transform::Transform(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std:: } -UiElement::UiElement(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(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { +UiElement::UiElement(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(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouseTransform(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) { } -UiElementMouse::UiElementMouse(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(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { +UiElement::UiElement(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) : transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), mouseTransform(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) { } -UiElementBuffer::UiElementBuffer(std::uint_fast32_t width, std::uint_fast32_t height) : width(width), height(height) { - +UiElement::UiElement(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(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouseTransform(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) { + RenderImage(imagePath); +} + +UiElement::UiElement(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(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), mouseTransform(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) { + RenderText(text, size, pixel, font); +} + +UiElement::UiElement(Transform transform) : transform(transform), mouseTransform(FractionalToMapped(0), FractionalToMapped(0), FractionalToMapped(1), FractionalToMapped(1), FractionalToMapped(0), FractionalToMapped(0), 0, false) { + +} + +void UiElement::ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height) { + this->bufferWidth = width; + this->bufferHeight = height; + buffer.resize(width * height); +} + +void UiElement::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 UiElement::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 UiElement::UpdatePosition(Window& window) { + window.ScaleElement(transform); + scaled.resize(transform.scaled.width * transform.scaled.height); + CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); + for(UiElement* child : children) { + child->UpdatePosition(window, *this); + } +} + +void UiElement::UpdatePosition(Window& window, UiElement& parent) { + window.ScaleElement(transform, parent.transform); + scaled.resize(transform.scaled.width * transform.scaled.height); + CopyNearestNeighbour(scaled.data(),transform.scaled.width, transform.scaled.height); + for(UiElement* child : children) { + UpdatePosition(window, *child); + } +} + +void UiElement::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]; + } + } } \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp b/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp deleted file mode 100644 index e1eafdf..0000000 --- a/implementations/Crafter.Graphics-UiElementBufferBuffer.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* -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:UiElementBufferBuffer_impl; -import :UiElement; -import :Window; -import std; - -using namespace Crafter; - -UiElementBufferBuffer::UiElementBufferBuffer(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) : UiElement(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - -} - -UiElementBufferBuffer::UiElementBufferBuffer(std::uint_fast32_t width, std::uint_fast32_t height, 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) : UiElement(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementBufferBufferBase(width, height){ - -} - -void UiElementBufferBuffer::UpdatePosition(WindowFramebuffer& window) { - window.ScaleElement(transform); - scaled.resize(transform.scaled.width*transform.scaled.height); - CopyNearestNeighbour(scaled.data(), transform.scaled.width, transform.scaled.height); - for(UiElement* child : children) { - child->UpdatePosition(window, *this); - } -} - -void UiElementBufferBuffer::UpdatePosition(WindowFramebuffer& window, UiElement& parent) { - window.ScaleElement(transform, parent.transform); - scaled.resize(transform.scaled.width*transform.scaled.height); - CopyNearestNeighbour(scaled.data(),transform.scaled.width, transform.scaled.height); - for(UiElement* child : children) { - UpdatePosition(window, *child); - } -} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp b/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp deleted file mode 100644 index 2cc63b4..0000000 --- a/implementations/Crafter.Graphics-UiElementBufferBufferBase.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* -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 -*/ - -module Crafter.Graphics:UiElementBufferBufferBase_impl; -import :UiElement; -import std; - -using namespace Crafter; - -UiElementBufferBufferBase::UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height) : UiElementBuffer(width, height), buffer(width*height) { - -} -void UiElementBufferBufferBase::Create(std::uint_fast32_t width, std::uint_fast32_t height) { - this->width = width; - this->height = height; - buffer.resize(width * height); -} -void UiElementBufferBufferBase::Resize(std::uint_fast32_t width, std::uint_fast32_t height) { - this->width = width; - this->height = height; - buffer.resize(width*height); -} -void UiElementBufferBufferBase::Resize(std::uint_fast32_t width, std::uint_fast32_t height, std::uint_fast32_t offsetX, std::uint_fast32_t offsetY) { - -} -void UiElementBufferBufferBase::ResizeNearestNeighbour(std::uint_fast32_t width, std::uint_fast32_t height) { - -} -void UiElementBufferBufferBase::ResizeBicubic(std::uint_fast32_t width, std::uint_fast32_t height) { - -} -void UiElementBufferBufferBase::Destroy() { - buffer.clear(); - width = 0; - height = 0; -} - -void UiElementBufferBufferBase::Copy(Pixel_BU8_GU8_RU8_AU8* dst) const { - std::memcpy(dst, buffer.data(), width*height*sizeof(Pixel_BU8_GU8_RU8_AU8)); -} - -void UiElementBufferBufferBase::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 * height / dstHeight; - for (std::uint_fast32_t x = 0; x < dstWidth; x++) { - std::uint_fast32_t srcX = x * width / dstWidth; - dst[y * dstWidth + x] = buffer[srcY * width + srcX]; - } - } -} - -void UiElementBufferBufferBase::CopyBicubic(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const { - -} -void UiElementBufferBufferBase::Write(Pixel_BU8_GU8_RU8_AU8* pixels) { - std::memcpy(buffer.data(), pixels, width*height*sizeof(Pixel_BU8_GU8_RU8_AU8)); -} -void UiElementBufferBufferBase::Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) { - buffer[y * width + x] = pixel; -} -Pixel_BU8_GU8_RU8_AU8 UiElementBufferBufferBase::Read(std::uint_fast32_t x, std::uint_fast32_t y) const { - return buffer[y * width + x]; -} -const Pixel_BU8_GU8_RU8_AU8* UiElementBufferBufferBase::Read() const { - return buffer.data(); -} -Pixel_BU8_GU8_RU8_AU8* UiElementBufferBufferBase::Get() { - return buffer.data(); -} -void UiElementBufferBufferBase::Store() { - -} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp b/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp deleted file mode 100644 index a8405aa..0000000 --- a/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* -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:UiElementBufferMouseBuffer_impl; -import :UiElement; -import std; - -using namespace Crafter; - -UiElementBufferMouseBuffer::UiElementBufferMouseBuffer(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) : UiElementBufferBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementMouse() { - -} - -UiElementBufferMouseBuffer::UiElementBufferMouseBuffer(std::uint_fast32_t width, std::uint_fast32_t height, 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) : UiElementBufferBuffer(width, height, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementMouse() { - -} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementImageBuffer.cpp b/implementations/Crafter.Graphics-UiElementImageBuffer.cpp deleted file mode 100644 index 2bcc1de..0000000 --- a/implementations/Crafter.Graphics-UiElementImageBuffer.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* -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" -module Crafter.Graphics:UiElementImage_impl; -import :UiElement; -import std; - -using namespace Crafter; - - -UiElementImageBuffer::UiElementImageBuffer(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) : UiElementBufferBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - -} - -UiElementImageBuffer::UiElementImageBuffer(const std::string_view path, 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) : UiElementBufferBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - Load(path); -} - -void UiElementImageBuffer::Load(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); - - Create(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]; - } - } -} - -UiElementImageMouseBuffer::UiElementImageMouseBuffer(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) : UiElementImageBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementMouse() { - -} - -UiElementImageMouseBuffer::UiElementImageMouseBuffer(const std::string_view path, 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) : UiElementImageBuffer(path, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementMouse() { - -} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementTextBuffer.cpp b/implementations/Crafter.Graphics-UiElementTextBuffer.cpp deleted file mode 100644 index 5a8ff4f..0000000 --- a/implementations/Crafter.Graphics-UiElementTextBuffer.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* -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; -#include "../lib/stb_truetype.h" -module Crafter.Graphics:UiElementTextBuffer_impl; -import :UiElement; -import :Font; -import std; - -using namespace Crafter; - - -UiElementTextBuffer::UiElementTextBuffer(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) : UiElementBufferBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - -} - -UiElementTextBuffer::UiElementTextBuffer(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, 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) : UiElementBufferBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - Render(text, size, color, font); -} - -void UiElementTextBuffer::Render(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); - } - - Create(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); - } - } -} - - -UiElementTextMouseBuffer::UiElementTextMouseBuffer(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) : UiElementTextBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementMouse() { - -} - -UiElementTextMouseBuffer::UiElementTextMouseBuffer(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, 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) : UiElementTextBuffer(text, size, color, font, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementMouse() { - -} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-Window.cpp b/implementations/Crafter.Graphics-Window.cpp index 3ca506b..34f9d58 100644 --- a/implementations/Crafter.Graphics-Window.cpp +++ b/implementations/Crafter.Graphics-Window.cpp @@ -20,10 +20,34 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA module Crafter.Graphics:Window_impl; import :Window; +import :UiElement; import std; using namespace Crafter; Window::Window(std::uint_fast32_t width, std::uint_fast32_t height) : width(width), height(height) { -} \ No newline at end of file +} + +void Window::ScaleElement(Transform& element) { + element.scaled.width = MappedToPixel(element.relativeWidth, width); + element.scaled.height = MappedToPixel(element.relativeHeight, height); + element.scaled.x = MappedToPixel(element.anchorX, width) - MappedToPixel(element.anchorOffsetX, element.scaled.width); + element.scaled.y = MappedToPixel(element.anchorY, height) - MappedToPixel(element.anchorOffsetY, element.scaled.height); +} + +void Window::ScaleElement(Transform& element, Transform& parent) { + element.scaled.width = MappedToPixel(element.relativeWidth, parent.scaled.width); + element.scaled.height = MappedToPixel(element.relativeHeight, parent.scaled.height); + element.scaled.x = MappedToPixel(element.anchorX, parent.scaled.width) - MappedToPixel(element.anchorOffsetX, element.scaled.width) + parent.scaled.x; + element.scaled.y = MappedToPixel(element.anchorY, parent.scaled.height) - MappedToPixel(element.anchorOffsetY, element.scaled.height) + parent.scaled.y; +} + +void Window::ScaleMouse(Transform& element, Transform& parent) { + std::int_fast32_t boundlessWidth = PixelToMappedBoundless(parent.scaled.width, width); + std::int_fast32_t boundlessHeight = PixelToMappedBoundless(parent.scaled.height, height); + element.scaled.width = BoundToBoundless(MappedToPixel(element.relativeWidth, PixelToMapped(parent.scaled.width, width))); + element.scaled.height = BoundToBoundless(MappedToPixel(element.relativeHeight, PixelToMapped(parent.scaled.height, height))); + element.scaled.x = MappedToPixelBoundless(element.anchorX, boundlessWidth) - MappedToPixelBoundless(element.anchorOffsetX, element.scaled.width) + PixelToMappedBoundless(parent.scaled.x, width); + element.scaled.y = MappedToPixelBoundless(element.anchorY, boundlessHeight) - MappedToPixelBoundless(element.anchorOffsetY, element.scaled.height) + PixelToMappedBoundless(parent.scaled.y, height); +} diff --git a/implementations/Crafter.Graphics-Window_wayland.cpp b/implementations/Crafter.Graphics-Window_wayland.cpp index 2336c97..9bb5c32 100644 --- a/implementations/Crafter.Graphics-Window_wayland.cpp +++ b/implementations/Crafter.Graphics-Window_wayland.cpp @@ -51,30 +51,6 @@ WindowFramebuffer::WindowFramebuffer(std::uint_fast32_t width, std::uint_fast32 } - -void WindowFramebuffer::ScaleElement(Transform& element) { - element.scaled.width = MappedToPixel(element.relativeWidth, width); - element.scaled.height = MappedToPixel(element.relativeHeight, height); - element.scaled.x = MappedToPixel(element.anchorX, width) - MappedToPixel(element.anchorOffsetX, element.scaled.width); - element.scaled.y = MappedToPixel(element.anchorY, height) - MappedToPixel(element.anchorOffsetY, element.scaled.height); -} - -void WindowFramebuffer::ScaleElement(Transform& element, Transform& parent) { - element.scaled.width = MappedToPixel(element.relativeWidth, parent.scaled.width); - element.scaled.height = MappedToPixel(element.relativeHeight, parent.scaled.height); - element.scaled.x = MappedToPixel(element.anchorX, parent.scaled.width) - MappedToPixel(element.anchorOffsetX, element.scaled.width) + parent.scaled.x; - element.scaled.y = MappedToPixel(element.anchorY, parent.scaled.height) - MappedToPixel(element.anchorOffsetY, element.scaled.height) + parent.scaled.y; -} - -void WindowFramebuffer::ScaleMouse(Transform& element, Transform& parent) { - std::int_fast32_t boundlessWidth = PixelToMappedBoundless(parent.scaled.width, width); - std::int_fast32_t boundlessHeight = PixelToMappedBoundless(parent.scaled.height, height); - element.scaled.width = BoundToBoundless(MappedToPixel(element.relativeWidth, PixelToMapped(parent.scaled.width, width))); - element.scaled.height = BoundToBoundless(MappedToPixel(element.relativeHeight, PixelToMapped(parent.scaled.height, height))); - element.scaled.x = MappedToPixelBoundless(element.anchorX, boundlessWidth) - MappedToPixelBoundless(element.anchorOffsetX, element.scaled.width) + PixelToMappedBoundless(parent.scaled.x, width); - element.scaled.y = MappedToPixelBoundless(element.anchorY, boundlessHeight) - MappedToPixelBoundless(element.anchorOffsetY, element.scaled.height) + PixelToMappedBoundless(parent.scaled.y, height); -} - WindowWayland::WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height) : WindowFramebuffer(width, height) { display = wl_display_connect(NULL); if (display == NULL) { @@ -159,7 +135,7 @@ void WindowWayland::StartSync() { -void RenderElement(UiElementBufferBuffer* element, WindowWayland* window) { +void RenderElement(UiElement* element, WindowWayland* window) { for (std::int_fast32_t x = element->transform.scaled.x; x - element->transform.scaled.x < element->transform.scaled.width; x++) { for (std::int_fast32_t y = element->transform.scaled.y; y - element->transform.scaled.y < element->transform.scaled.height; y++) { if (x >= 0 && x < window->width && y >= 0 && y < window->height) { @@ -183,20 +159,20 @@ void RenderElement(UiElementBufferBuffer* element, WindowWayland* window) { } std::sort(element->children.begin(), element->children.end(), [](UiElement* a, UiElement* b){ return a->transform.z < b->transform.z; }); for(UiElement* child : element->children) { - RenderElement(static_cast(child), window); + RenderElement(child, window); } } void WindowWayland::Render() { - std::sort(elements.begin(), elements.end(), [](UiElementBufferBuffer* a, UiElementBufferBuffer* b){ return a->transform.z < b->transform.z; }); + std::sort(elements.begin(), elements.end(), [](UiElement* a, UiElement* b){ return a->transform.z < b->transform.z; }); - for (std::int_fast32_t x = 0; x < width; x++) { - for (std::int_fast32_t y = 0; y - height; y++) { + for (std::uint_fast32_t x = 0; x < width; x++) { + for (std::uint_fast32_t y = 0; y - height; y++) { framebuffer[y * width + x] = {0,0,0,0}; } } - for(UiElementBufferBuffer* element : elements) { + for(UiElement* element : elements) { RenderElement(element, this); } @@ -264,7 +240,7 @@ void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, ui cb = wl_surface_frame(window->surface); wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window); auto startUpdate = std::chrono::high_resolution_clock::now(); - window->onUpdate.Invoke({start, start-window->lastFrameEnd}); + window->onUpdate.Invoke({start, start-window->lastFrameBegin}); auto endUpdate = std::chrono::high_resolution_clock::now(); auto startRender = std::chrono::high_resolution_clock::now(); window->Render(); @@ -275,7 +251,7 @@ void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, ui framEnd = std::chrono::high_resolution_clock::now(); - window->lastFrameEnd = start; + window->lastFrameBegin = start; } void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) { @@ -284,17 +260,17 @@ void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std:: if(state == WL_POINTER_BUTTON_STATE_PRESSED) { window->mouseLeftHeld = true; window->onMouseLeftClick.Invoke(window->currentMousePos); - for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->transform.scaled.x && window->currentMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->currentMousePos.y > element->transform.scaled.y && window->currentMousePos.y < element->transform.scaled.y+element->transform.scaled.height) { - element->onMouseLeftClick.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); + for(UiElement* element : window->mouseElements) { + if(window->currentMousePos.x >= element->mouseTransform.scaled.x && window->currentMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->currentMousePos.y > element->mouseTransform.scaled.y && window->currentMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height) { + element->onMouseLeftClick.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); } } } else { window->mouseLeftHeld = false; window->onMouseLeftRelease.Invoke(window->currentMousePos); - for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->transform.scaled.x && window->currentMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->currentMousePos.y > element->transform.scaled.y && window->currentMousePos.y < element->transform.scaled.y+element->transform.scaled.height) { - element->onMouseLeftRelease.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); + for(UiElement* element : window->mouseElements) { + if(window->currentMousePos.x >= element->mouseTransform.scaled.x && window->currentMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->currentMousePos.y > element->mouseTransform.scaled.y && window->currentMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height) { + element->onMouseLeftRelease.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); } } } @@ -302,17 +278,17 @@ void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std:: if(state == WL_POINTER_BUTTON_STATE_PRESSED) { window->mouseRightHeld = true; window->onMouseRightClick.Invoke(window->currentMousePos); - for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->transform.scaled.x && window->currentMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->currentMousePos.y > element->transform.scaled.y && window->currentMousePos.y < element->transform.scaled.y+element->transform.scaled.height) { - element->onMouseRightClick.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); + for(UiElement* element : window->mouseElements) { + if(window->currentMousePos.x >= element->mouseTransform.scaled.x && window->currentMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->currentMousePos.y > element->mouseTransform.scaled.y && window->currentMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height) { + element->onMouseRightClick.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); } } } else { window->mouseRightHeld = true; window->onMouseRightRelease.Invoke(window->currentMousePos); - for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->transform.scaled.x && window->currentMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->currentMousePos.y > element->transform.scaled.y && window->currentMousePos.y < element->transform.scaled.y+element->transform.scaled.height) { - element->onMouseRightRelease.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); + for(UiElement* element : window->mouseElements) { + if(window->currentMousePos.x >= element->mouseTransform.scaled.x && window->currentMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->currentMousePos.y > element->mouseTransform.scaled.y && window->currentMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height) { + element->onMouseRightRelease.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); } } } @@ -326,14 +302,14 @@ void WindowWayland::PointerListenerHandleMotion(void* data, wl_pointer* wl_point window->currentMousePos = pos; window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y}; window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); - for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->transform.scaled.x && window->currentMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->currentMousePos.y > element->transform.scaled.y && window->currentMousePos.y < element->transform.scaled.y+element->transform.scaled.height) { - element->onMouseMove.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); - if(!(window->lastMousePos.x >= element->transform.scaled.x && window->lastMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->lastMousePos.y > element->transform.scaled.y && window->lastMousePos.y < element->transform.scaled.y+element->transform.scaled.height)) { - element->onMouseEnter.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); + for(UiElement* element : window->mouseElements) { + if(window->currentMousePos.x >= element->mouseTransform.scaled.x && window->currentMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->currentMousePos.y > element->mouseTransform.scaled.y && window->currentMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height) { + element->onMouseMove.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); + if(!(window->lastMousePos.x >= element->mouseTransform.scaled.x && window->lastMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->lastMousePos.y > element->mouseTransform.scaled.y && window->lastMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height)) { + element->onMouseEnter.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); } - } else if(window->lastMousePos.x >= element->transform.scaled.x && window->lastMousePos.x <= element->transform.scaled.x+element->transform.scaled.width && window->lastMousePos.y > element->transform.scaled.y && window->lastMousePos.y < element->transform.scaled.y+element->transform.scaled.height) { - element->onMouseLeave.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->transform.scaled.x) / element->transform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->transform.scaled.y) / element->transform.scaled.height)}); + } else if(window->lastMousePos.x >= element->mouseTransform.scaled.x && window->lastMousePos.x <= element->mouseTransform.scaled.x+element->mouseTransform.scaled.width && window->lastMousePos.y > element->mouseTransform.scaled.y && window->lastMousePos.y < element->mouseTransform.scaled.y+element->mouseTransform.scaled.height) { + element->onMouseLeave.Invoke({FractionalToMappedBoundless(static_cast(window->currentMousePos.x - element->mouseTransform.scaled.x) / element->mouseTransform.scaled.width), FractionalToMappedBoundless(static_cast(window->currentMousePos.y - element->mouseTransform.scaled.y) / element->mouseTransform.scaled.height)}); } } } diff --git a/interfaces/Crafter.Graphics-UiElement.cppm b/interfaces/Crafter.Graphics-UiElement.cppm index 1166dd8..8dbe014 100644 --- a/interfaces/Crafter.Graphics-UiElement.cppm +++ b/interfaces/Crafter.Graphics-UiElement.cppm @@ -37,22 +37,12 @@ export namespace Crafter { Transform(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 WindowFramebuffer; + class Window; + class Font; class UiElement { public: Transform transform; - std::vector children; - - UiElement(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); - UiElement(UiElement&) = delete; - UiElement& operator=(UiElement&) = delete; - virtual void UpdatePosition(WindowFramebuffer& window) = 0; - virtual void UpdatePosition(WindowFramebuffer& window, UiElement& parent) = 0; - }; - - class UiElementMouse { - public: - Transform transform; + Transform mouseTransform; Event onMouseMove; Event onMouseEnter; Event onMouseLeave; @@ -62,104 +52,24 @@ export namespace Crafter { Event onMouseLeftHold; Event onMouseRightRelease; Event onMouseLeftRelease; - UiElementMouse(std::int_fast32_t anchorX = FractionalToMapped(0), std::int_fast32_t anchorY = FractionalToMapped(0), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0), std::int_fast32_t anchorOffsetY = FractionalToMapped(0), std::int_fast32_t z = 0, bool ignoreScaling = false); - }; - - class UiElementBuffer { - public: - std::uint_fast32_t width; - std::uint_fast32_t height; - UiElementBuffer() = default; - UiElementBuffer(std::uint_fast32_t width, std::uint_fast32_t height); - virtual void Create(std::uint_fast32_t width, std::uint_fast32_t height) = 0; - virtual void Resize(std::uint_fast32_t width, std::uint_fast32_t height) = 0; - virtual void Resize(std::uint_fast32_t width, std::uint_fast32_t height, std::uint_fast32_t offsetX, std::uint_fast32_t offsetY) = 0; - virtual void ResizeNearestNeighbour(std::uint_fast32_t width, std::uint_fast32_t height) = 0; - virtual void ResizeBicubic(std::uint_fast32_t width, std::uint_fast32_t height) = 0; - virtual void Destroy() = 0; - virtual void Copy(Pixel_BU8_GU8_RU8_AU8* dst) const = 0; - virtual void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const = 0; - virtual void CopyBicubic(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const = 0; - virtual void Write(Pixel_BU8_GU8_RU8_AU8* pixels) = 0; - virtual void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) = 0; - virtual Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const = 0; - virtual const Pixel_BU8_GU8_RU8_AU8* Read() const = 0; - virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0; - virtual void Store() = 0; - }; - - class UiElementBufferBufferBase : public UiElementBuffer { - public: + std::vector children; std::vector buffer; std::vector scaled; - UiElementBufferBufferBase() = default; - UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height); - void Create(std::uint_fast32_t width, std::uint_fast32_t height) override; - void Resize(std::uint_fast32_t width, std::uint_fast32_t height) override; - void Resize(std::uint_fast32_t width, std::uint_fast32_t height, std::uint_fast32_t offsetX, std::uint_fast32_t offsetY) override; - void ResizeNearestNeighbour(std::uint_fast32_t width, std::uint_fast32_t height) override; - void ResizeBicubic(std::uint_fast32_t width, std::uint_fast32_t height) override; - void Destroy() override; - void Copy(Pixel_BU8_GU8_RU8_AU8* dst) const override; - void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const override; - void CopyBicubic(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const override; - void Write(Pixel_BU8_GU8_RU8_AU8* pixels) override; - void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) override; - Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const override; - const Pixel_BU8_GU8_RU8_AU8* Read() const override; - Pixel_BU8_GU8_RU8_AU8* Get() override; - void Store() override; - }; + std::uint_fast32_t bufferWidth; + std::uint_fast32_t bufferHeight; - class UiElementBufferBuffer : public UiElement, public UiElementBufferBufferBase { - public: - UiElementBufferBuffer(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); - UiElementBufferBuffer(std::uint_fast32_t width, std::uint_fast32_t height, 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); - void UpdatePosition(WindowFramebuffer& window) override; - void UpdatePosition(WindowFramebuffer& window, UiElement& parent) override; - }; - - - class Font; - class UiElementText { - public: - virtual void Render(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font) = 0; - }; - - class UiElementTextBuffer: public UiElementText, public UiElementBufferBuffer { - public: - UiElementTextBuffer(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); - UiElementTextBuffer(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); - void Render(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font) override; - }; - - class UiElementTextMouseBuffer: public UiElementTextBuffer, public UiElementMouse { - public: - UiElementTextMouseBuffer(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); - UiElementTextMouseBuffer(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); - }; - - class UiElementImage { - public: - virtual void Load(const std::string_view path) = 0; - }; - - class UiElementImageBuffer : public UiElementImage, public UiElementBufferBuffer { - public: - UiElementImageBuffer(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); - UiElementImageBuffer(const std::string_view path, 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); - void Load(const std::string_view path) override; - }; - - class UiElementImageMouseBuffer : public UiElementImageBuffer, public UiElementMouse { - public: - UiElementImageMouseBuffer(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); - UiElementImageMouseBuffer(const std::string_view path, 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 UiElementBufferMouseBuffer : public UiElementBufferBuffer, public UiElementMouse { - public: - UiElementBufferMouseBuffer(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); - UiElementBufferMouseBuffer(std::uint_fast32_t width, std::uint_fast32_t height, 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); + UiElement(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); + UiElement(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); + UiElement(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); + UiElement(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); + UiElement(Transform transform); + UiElement(UiElement&) = delete; + UiElement& operator=(UiElement&) = delete; + void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height); + void UpdatePosition(Window& window); + void UpdatePosition(Window& window, UiElement& parent); + 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); }; } \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-Window.cppm b/interfaces/Crafter.Graphics-Window.cppm index 5c17dd0..5fb1e2a 100644 --- a/interfaces/Crafter.Graphics-Window.cppm +++ b/interfaces/Crafter.Graphics-Window.cppm @@ -45,11 +45,12 @@ import Crafter.Event; export namespace Crafter { class UiElement; + class Transform; class Window { public: std::uint_fast32_t width; std::uint_fast32_t height; - std::chrono::time_point lastFrameEnd; + std::chrono::time_point lastFrameBegin; Event onClose; Event onUpdate; bool open = true; @@ -63,6 +64,9 @@ export namespace Crafter { virtual void StartSync() = 0; virtual void StartUpdate() = 0; virtual void StopUpdate() = 0; + void ScaleElement(Transform& element, Transform& parent); + void ScaleElement(Transform& element); + void ScaleMouse(Transform& element, Transform& parent); }; class WindowKeyboard { @@ -76,7 +80,6 @@ export namespace Crafter { Event onAnyKeyUp; }; - class UiElementMouse; class WindowMouse { public: Event onMouseRightClick; @@ -94,7 +97,7 @@ export namespace Crafter { MousePoint mouseDelta; bool mouseLeftHeld = false; bool mouseRightHeld = false; - std::vector mouseElements; + std::vector mouseElements; }; class WindowTitle { @@ -115,9 +118,6 @@ export namespace Crafter { virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0; virtual void Store() = 0; virtual void Render() = 0; - void ScaleElement(Transform& element, Transform& parent); - void ScaleElement(Transform& element); - void ScaleMouse(Transform& element, Transform& parent); }; #ifdef CRAFTER_GRAPHICS_WAYLAND @@ -125,7 +125,7 @@ export namespace Crafter { class WindowWayland final : public WindowKeyboard, public WindowMouse, public WindowFramebuffer, public WindowTitle { public: Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr; - std::vector elements; + std::vector elements; WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height); WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height, const std::string_view title); ~WindowWayland(); diff --git a/project.json b/project.json index 04e4936..ff181ce 100644 --- a/project.json +++ b/project.json @@ -3,7 +3,7 @@ "configurations": [ { "name": "base", - "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-UiElement", "implementations/Crafter.Graphics-UiElementBufferBuffer", "implementations/Crafter.Graphics-UiElementBufferBufferBase", "implementations/Crafter.Graphics-UiElementImageBuffer", "implementations/Crafter.Graphics-UiElementBufferMouseBuffer", "implementations/Crafter.Graphics-UiElementTextBuffer", "implementations/Crafter.Graphics-Window"], + "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"], "type": "library" },