merged uielement

This commit is contained in:
Jorijn van der Graaf 2025-11-25 18:52:32 +01:00
commit 02a6a64c56
12 changed files with 200 additions and 506 deletions

View file

@ -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<UiElement*> 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<MouseMoveEvent> onMouseMove;
Event<MouseMoveEvent> onMouseEnter;
Event<MouseMoveEvent> onMouseLeave;
@ -62,104 +52,24 @@ export namespace Crafter {
Event<MousePoint> onMouseLeftHold;
Event<MousePoint> onMouseRightRelease;
Event<MousePoint> 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<UiElement*> children;
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
std::vector<Pixel_BU8_GU8_RU8_AU8> 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);
};
}

View file

@ -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<std::chrono::high_resolution_clock> lastFrameEnd;
std::chrono::time_point<std::chrono::high_resolution_clock> lastFrameBegin;
Event<void> onClose;
Event<FrameTime> 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<char> onAnyKeyUp;
};
class UiElementMouse;
class WindowMouse {
public:
Event<MousePoint> onMouseRightClick;
@ -94,7 +97,7 @@ export namespace Crafter {
MousePoint mouseDelta;
bool mouseLeftHeld = false;
bool mouseRightHeld = false;
std::vector<UiElementMouse*> mouseElements;
std::vector<UiElement*> 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<UiElementBufferBuffer*> elements;
std::vector<UiElement*> 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();