From 721ff8f42f112593606d75d6f4ef46784f67d530 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Mon, 24 Nov 2025 01:47:49 +0100 Subject: [PATCH] wayland rewrite complete --- examples/HelloInput/main.cpp | 4 +- examples/HelloUI/main.cpp | 15 ++- .../Crafter.Graphics-UiElement.cpp | 40 ++++---- ...er.Graphics-UiElementBufferMouseBuffer.cpp | 4 +- .../Crafter.Graphics-Window_wayland.cpp | 92 ++++++++++--------- interfaces/Crafter.Graphics-Types.cppm | 39 ++++++-- interfaces/Crafter.Graphics-UiElement.cppm | 21 +++-- interfaces/Crafter.Graphics-Window.cppm | 6 +- 8 files changed, 134 insertions(+), 87 deletions(-) diff --git a/examples/HelloInput/main.cpp b/examples/HelloInput/main.cpp index a4ad99e..770e51b 100644 --- a/examples/HelloInput/main.cpp +++ b/examples/HelloInput/main.cpp @@ -9,8 +9,8 @@ int main() { // Listen for left mouse click events on the window // The callback receives the MousePoint struct containing the click coordinates in float pixels from the top left corner EventListener clickListener(&window.onMouseLeftClick, [&window](MousePoint point){ - // Print the coordinates where the user clicked - std::cout << std::format("Clicked on X:{} Y:{}!", MappedToPixel(point.x, window.width), MappedToPixel(point.y, window.height)) << std::endl; + // Print the coordinates where the user clicked, we recieve the point in mapped space so we must convert it to pixels first + std::cout << std::format("Clicked on X:{} Y:{}!", MappedToPixelBoundless(point.x, window.width), MappedToPixelBoundless(point.y, window.height)) << std::endl; }); // Listen specifically for the 'a' key being pressed down diff --git a/examples/HelloUI/main.cpp b/examples/HelloUI/main.cpp index d91a35d..b6f1f0b 100644 --- a/examples/HelloUI/main.cpp +++ b/examples/HelloUI/main.cpp @@ -20,13 +20,24 @@ int main() { ); window.elements.push_back(element); + window.mouseElements.push_back(element); element->UpdatePosition(window); + window.ScaleMouse(element->UiElementMouse::transform, element->UiElement::transform); - EventListener clickListener(&element.onMouseLeftClick, [](MousePoint point){ + EventListener clickListener(&element->onMouseLeftClick, [element, &window](MousePoint point){ // Print the coordinates where the user clicked relative to the element's top left corner. - std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl; + + //Mapped space + std::cout << std::format("Clicked on Mapped X:{} Y:{}!", point.x, point.y) << std::endl; + + // Fraction space + 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; }); + element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; window.Render(); diff --git a/implementations/Crafter.Graphics-UiElement.cpp b/implementations/Crafter.Graphics-UiElement.cpp index 5f65a57..d6114f6 100644 --- a/implementations/Crafter.Graphics-UiElement.cpp +++ b/implementations/Crafter.Graphics-UiElement.cpp @@ -26,7 +26,15 @@ import std; using namespace Crafter; -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) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), ignoreScaling(ignoreScaling) { +Transform::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, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), ignoreScaling(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) { + +} + +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) { } @@ -34,22 +42,16 @@ UiElementBuffer::UiElementBuffer(std::uint_fast32_t width, std::uint_fast32_t he } -// void UiElement::UpdatePosition(Window& window) { -// // scaled.width = relativeWidth; -// // scaled.height = relativeHeight; -// // scaled.x = anchorX - (anchorOffsetX * scaled.width); -// // scaled.y = anchorY - (anchorOffsetY * scaled.height); -// // for(UiElement* child : children) { -// // UpdatePosition(*child); -// // } -// } +void UiElement::UpdatePosition(WindowFramebuffer& window) { + window.ScaleElement(transform); + for(UiElement* child : children) { + UpdatePosition(window, *child); + } +} -// void UiElement::UpdatePosition(UiElement& parent) { -// // scaled.width = parent.scaled.width / relativeWidth; -// // scaled.height = parent.scaled.height / relativeHeight; -// // scaled.x = (parent.scaled.width / anchorX) - (anchorOffsetX * scaled.width); -// // scaled.y = (parent.scaled.height / anchorY) - (anchorOffsetY * scaled.height); -// // for(UiElement* child : children) { -// // UpdatePosition(*child); -// // } -// } \ No newline at end of file +void UiElement::UpdatePosition(WindowFramebuffer& window, UiElement& parent) { + window.ScaleElement(transform, parent.transform); + for(UiElement* child : children) { + UpdatePosition(window, *child); + } +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp b/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp index 4b75b0f..a8405aa 100644 --- a/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp +++ b/implementations/Crafter.Graphics-UiElementBufferMouseBuffer.cpp @@ -24,10 +24,10 @@ 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) { +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) { +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-Window_wayland.cpp b/implementations/Crafter.Graphics-Window_wayland.cpp index 743c691..622c8ec 100644 --- a/implementations/Crafter.Graphics-Window_wayland.cpp +++ b/implementations/Crafter.Graphics-Window_wayland.cpp @@ -51,6 +51,30 @@ 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) { @@ -135,15 +159,15 @@ void WindowWayland::StartSync() { -void RenderElement(UiElementBufferBuffer* element, ScaleData data , WindowWayland* window) { - std::vector scaled(data.width*data.height); - element->CopyNearestNeighbour(scaled.data(), data.width, data.height); +void RenderElement(UiElementBufferBuffer* element, WindowWayland* window) { + std::vector scaled(element->transform.scaled.width*element->transform.scaled.height); + element->CopyNearestNeighbour(scaled.data(), element->transform.scaled.width, element->transform.scaled.height); - for (std::int32_t x = data.x; x - data.x < data.width; x++) { - for (std::int32_t y = data.y; y - data.y < data.height; y++) { + for (std::int32_t x = element->transform.scaled.x; x - element->transform.scaled.x < element->transform.scaled.width; x++) { + for (std::int32_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) { Pixel_BU8_GU8_RU8_AU8& dst = window->framebuffer[y * window->width + x]; - const Pixel_BU8_GU8_RU8_AU8& src = scaled[(y - data.y) * data.width + (x - data.x)]; + const Pixel_BU8_GU8_RU8_AU8& src = scaled[(y - element->transform.scaled.y) * element->transform.scaled.width + (x - element->transform.scaled.x)]; float srcA = src.a / 255.0f; float dstA = dst.a / 255.0f; @@ -158,35 +182,17 @@ void RenderElement(UiElementBufferBuffer* element, ScaleData data , WindowWaylan } } } - std::sort(element->children.begin(), element->children.end(), [](UiElement* a, UiElement* b){ return a->z < b->z; }); + std::sort(element->children.begin(), element->children.end(), [](UiElement* a, UiElement* b){ return a->transform.z < b->transform.z; }); for(UiElement* child : element->children) { - std::int32_t scaledWidth = MappedToPixel(element->relativeWidth, data.width); - std::int32_t scaledHeight = MappedToPixel(element->relativeHeight, data.height); - std::int32_t scaledX = MappedToPixel(element->anchorX, data.width) - MappedToPixel(element->anchorOffsetX, scaledWidth) - data.x; - std::int32_t scaledY = MappedToPixel(element->anchorY, data.height) - MappedToPixel(element->anchorOffsetY, scaledHeight) - data.y; - RenderElement(element, { - scaledX, - scaledY, - scaledWidth, - scaledHeight - }, window); + RenderElement(element, window); } } void WindowWayland::Render() { - std::sort(elements.begin(), elements.end(), [](UiElementBufferBuffer* a, UiElementBufferBuffer* b){ return a->z < b->z; }); + std::sort(elements.begin(), elements.end(), [](UiElementBufferBuffer* a, UiElementBufferBuffer* b){ return a->transform.z < b->transform.z; }); for(UiElementBufferBuffer* element : elements) { - std::int32_t scaledWidth = MappedToPixel(element->relativeWidth, width); - std::int32_t scaledHeight = MappedToPixel(element->relativeHeight, height); - std::int32_t scaledX = MappedToPixel(element->anchorX, width) - MappedToPixel(element->anchorOffsetX, scaledWidth); - std::int32_t scaledY = MappedToPixel(element->anchorY, height) - MappedToPixel(element->anchorOffsetY, scaledHeight); - RenderElement(element, { - scaledX, - scaledY, - scaledWidth, - scaledHeight - }, this); + RenderElement(element, this); } wl_surface_attach(surface, buffer, 0, 0); @@ -260,16 +266,16 @@ void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std:: window->mouseLeftHeld = true; window->onMouseLeftClick.Invoke(window->currentMousePos); for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) { - element->onMouseLeftClick.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); + 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)}); } } } else { window->mouseLeftHeld = false; window->onMouseLeftRelease.Invoke(window->currentMousePos); for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) { - element->onMouseLeftRelease.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); + 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)}); } } } @@ -278,16 +284,16 @@ void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std:: window->mouseRightHeld = true; window->onMouseRightClick.Invoke(window->currentMousePos); for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) { - element->onMouseRightClick.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); + 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)}); } } } else { window->mouseRightHeld = true; window->onMouseRightRelease.Invoke(window->currentMousePos); for(UiElementMouse* element : window->mouseElements) { - if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) { - element->onMouseRightRelease.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); + 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)}); } } } @@ -296,19 +302,19 @@ void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std:: void WindowWayland::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) { WindowWayland* window = reinterpret_cast(data); - MousePoint pos = {PixelToMapped(wl_fixed_to_double(surface_x), window->width), PixelToMapped(wl_fixed_to_double(surface_y), window->height)}; + MousePoint pos = {FractionalToMappedBoundless(wl_fixed_to_double(surface_x) / window->width), FractionalToMappedBoundless(wl_fixed_to_double(surface_y) / window->height)}; window->lastMousePos = window->currentMousePos; 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->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) { - element->onMouseMove.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); - if(!(window->lastMousePos.x >= element->clickArea.x && window->lastMousePos.x <= element->clickArea.x+element->clickArea.width && window->lastMousePos.y > element->clickArea.y && window->lastMousePos.y < element->clickArea.y+element->clickArea.height)) { - element->onMouseEnter.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); + 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)}); } - } else if(window->lastMousePos.x >= element->clickArea.x && window->lastMousePos.x <= element->clickArea.x+element->clickArea.width && window->lastMousePos.y > element->clickArea.y && window->lastMousePos.y < element->clickArea.y+element->clickArea.height) { - element->onMouseLeave.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y}); + } 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)}); } } } diff --git a/interfaces/Crafter.Graphics-Types.cppm b/interfaces/Crafter.Graphics-Types.cppm index 9a739af..6c1dc24 100644 --- a/interfaces/Crafter.Graphics-Types.cppm +++ b/interfaces/Crafter.Graphics-Types.cppm @@ -23,8 +23,8 @@ import std; namespace Crafter { export struct MousePoint { - std::uint_fast32_t x; - std::uint_fast32_t y; + std::int_fast32_t x; + std::int_fast32_t y; }; export struct MouseMoveEvent { @@ -101,22 +101,45 @@ namespace Crafter { export constexpr std::int_fast32_t SCALE = std::numeric_limits::max() / BOUND; export constexpr double SCALEDOUBLE = static_cast(std::numeric_limits::max()) / BOUND; + export constexpr std::int_fast32_t SCALEBOUNDLESS = std::numeric_limits::max(); + export constexpr double SCALEDOUBLEBOUNDLESS = static_cast(std::numeric_limits::max()); + export constexpr std::int_fast32_t FractionalToMapped(double f) { return std::int_fast32_t(f * SCALEDOUBLE); } - export constexpr std::int_fast32_t MappedToPixel(std::int_fast32_t mapped, std::int_fast32_t width) { - return mapped / (SCALE / width); - } - export constexpr double MappedToFractional(std::int_fast32_t mapped) { return static_cast(mapped) / SCALEDOUBLE; } + export constexpr std::int_fast32_t MappedToPixel(std::int_fast32_t mapped, std::int_fast32_t width) { + return mapped / (SCALE / width); + } + export constexpr std::int_fast32_t PixelToMapped(std::int_fast32_t pixel, std::int_fast32_t width) { return pixel * (SCALE / width); } + export constexpr std::int_fast32_t BoundToBoundless(std::int_fast32_t bound) { + return bound * BOUND; + } + + export constexpr std::int_fast32_t FractionalToMappedBoundless(double f) { + return std::int_fast32_t(f * SCALEDOUBLEBOUNDLESS); + } + + export constexpr double MappedToFractionalBoundless(std::int_fast32_t mapped) { + return static_cast(mapped) / SCALEDOUBLEBOUNDLESS; + } + + export constexpr std::int_fast32_t MappedToPixelBoundless(std::int_fast32_t mapped, std::int_fast32_t width) { + return mapped / (SCALEBOUNDLESS / width); + } + + export constexpr std::int_fast32_t PixelToMappedBoundless(std::int_fast32_t pixel, std::int_fast32_t width) { + return pixel * (SCALEBOUNDLESS / width); + } + // export constexpr double bound = 10; // export constexpr std::uint_fast32_t FractionalToMapped(double fractional) { @@ -166,8 +189,4 @@ namespace Crafter { // return static_cast(t * static_cast(size)); // } - - export constexpr std::uint_fast32_t PixelToMapped(double pixel, std::uint_fast32_t size) { - return static_cast((pixel / size) * static_cast((std::numeric_limits::max() / 10))); - } } diff --git a/interfaces/Crafter.Graphics-UiElement.cppm b/interfaces/Crafter.Graphics-UiElement.cppm index 40da4f7..e8fbaa8 100644 --- a/interfaces/Crafter.Graphics-UiElement.cppm +++ b/interfaces/Crafter.Graphics-UiElement.cppm @@ -23,9 +23,8 @@ import Crafter.Event; import :Types; export namespace Crafter { - class Window; - class UiElement { - public: + class Transform { + public: std::int_fast32_t z; std::int_fast32_t anchorX; std::int_fast32_t anchorY; @@ -34,19 +33,26 @@ export namespace Crafter { std::uint_fast32_t relativeWidth; std::uint_fast32_t relativeHeight; bool ignoreScaling; - std::vector children; ScaleData scaled; + 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 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; - // void UpdatePosition(Window& window); - // void UpdatePosition(UiElement& parent); + void UpdatePosition(WindowFramebuffer& window); + void UpdatePosition(WindowFramebuffer& window, UiElement& parent); }; class UiElementMouse { public: - ScaleData clickArea; + Transform transform; Event onMouseMove; Event onMouseEnter; Event onMouseLeave; @@ -56,6 +62,7 @@ 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 { diff --git a/interfaces/Crafter.Graphics-Window.cppm b/interfaces/Crafter.Graphics-Window.cppm index 1a97d9d..e7f03c5 100644 --- a/interfaces/Crafter.Graphics-Window.cppm +++ b/interfaces/Crafter.Graphics-Window.cppm @@ -59,7 +59,6 @@ export namespace Crafter { virtual void StartSync() = 0; virtual void StartUpdate() = 0; virtual void StopUpdate() = 0; - ScaleData ScaleElement(const UiElement& element); }; class WindowKeyboard { @@ -99,6 +98,7 @@ export namespace Crafter { virtual void SetTitle(const std::string_view title) = 0; }; + class Transform; class WindowFramebuffer { public: std::uint_fast32_t width; @@ -113,7 +113,9 @@ export namespace Crafter { virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0; virtual void Store() = 0; virtual void Render() = 0; - ScaleData ScaleElementAbsolute(const UiElement& element); + void ScaleElement(Transform& element, Transform& parent); + void ScaleElement(Transform& element); + void ScaleMouse(Transform& element, Transform& parent); }; #ifdef CRAFTER_GRAPHICS_WAYLAND