rendering element rewrite
This commit is contained in:
parent
4c286e1fd8
commit
3d40256bde
22 changed files with 799 additions and 795 deletions
|
|
@ -31,18 +31,8 @@ export namespace Crafter {
|
|||
std::int_fast32_t spacingY;
|
||||
|
||||
public:
|
||||
GridElement(std::uint_fast32_t columns = 1, std::uint_fast32_t rows = 1,
|
||||
std::int_fast32_t spacingX = 0, std::int_fast32_t spacingY = 0,
|
||||
std::int_fast32_t anchorX = FractionalToMapped(0.5),
|
||||
std::int_fast32_t anchorY = FractionalToMapped(0.5),
|
||||
std::uint_fast32_t relativeWidth = FractionalToMapped(1),
|
||||
std::uint_fast32_t relativeHeight = FractionalToMapped(1),
|
||||
std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5),
|
||||
std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5),
|
||||
std::int_fast32_t z = 0);
|
||||
|
||||
void SetGridSize(std::uint_fast32_t columns, std::uint_fast32_t rows);
|
||||
void SetSpacing(std::int_fast32_t spacingX, std::int_fast32_t spacingY);
|
||||
GridElement(std::uint_fast32_t columns, std::uint_fast32_t rows, std::int_fast32_t spacingX, std::int_fast32_t spacingY, Anchor anchor);
|
||||
void UpdatePositionScaled(Window& window);
|
||||
void UpdatePosition(Window& window) override;
|
||||
void UpdatePosition(Window& window, Transform& parent) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 version 3.0 as published by the Free Software Foundation;
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
export module Crafter.Graphics:ImageElement;
|
||||
import std;
|
||||
import :RenderingElement;
|
||||
import :Types;
|
||||
|
||||
export namespace Crafter {
|
||||
class ImageElement : public RenderingElementScaling {
|
||||
public:
|
||||
ImageElement();
|
||||
ImageElement(const std::string_view imagePath, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), 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 RenderImage(const std::string_view path);
|
||||
};
|
||||
}
|
||||
|
|
@ -38,10 +38,8 @@ export namespace Crafter {
|
|||
Event<MousePoint> onMouseRightRelease;
|
||||
Event<MousePoint> onMouseLeftRelease;
|
||||
|
||||
MouseElement(WindowMouse& window, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0);
|
||||
MouseElement(std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0);
|
||||
MouseElement(MouseElement&) = delete;
|
||||
MouseElement& operator=(MouseElement&) = delete;
|
||||
MouseElement(Anchor anchor);
|
||||
MouseElement(Anchor anchor, WindowMouse& window);
|
||||
void UpdatePosition(Window& window) override;
|
||||
void UpdatePosition(Window& window, Transform& parent) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -17,10 +17,14 @@ 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"
|
||||
export module Crafter.Graphics:RenderingElement;
|
||||
import std;
|
||||
import :Transform;
|
||||
import :Types;
|
||||
import :Window;
|
||||
|
||||
export namespace Crafter {
|
||||
enum class OpaqueType {
|
||||
|
|
@ -29,66 +33,355 @@ export namespace Crafter {
|
|||
Transparent // Color blending is used
|
||||
};
|
||||
|
||||
class Window;
|
||||
class RenderingElement : public Transform {
|
||||
public:
|
||||
std::vector<Pixel_BU8_GU8_RU8_AU8> bufferScaled;
|
||||
OpaqueType opaque = OpaqueType::Transparent;
|
||||
|
||||
RenderingElement(OpaqueType opaque = OpaqueType::Transparent);
|
||||
RenderingElement(OpaqueType opaque, 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);
|
||||
RenderingElement(RenderingElement&) = delete;
|
||||
RenderingElement& operator=(RenderingElement&) = delete;
|
||||
};
|
||||
|
||||
class RenderingElementPreScaled : public RenderingElement {
|
||||
public:
|
||||
RenderingElementPreScaled(OpaqueType opaque = OpaqueType::Transparent);
|
||||
RenderingElementPreScaled(OpaqueType opaque, 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);
|
||||
RenderingElementPreScaled(RenderingElementPreScaled&) = delete;
|
||||
RenderingElementPreScaled& operator=(RenderingElementPreScaled&) = delete;
|
||||
|
||||
void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const;
|
||||
void UpdatePosition(Window& window) override;
|
||||
void UpdatePosition(Window& window, Transform& parent) override;
|
||||
};
|
||||
|
||||
class RenderingElementScaling: public RenderingElement {
|
||||
public:
|
||||
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
||||
struct RenderElementScalingOwning {
|
||||
std::vector<Pixel_BU8_GU8_RU8_AU8> scalingBuffer;
|
||||
std::uint_fast32_t bufferWidth;
|
||||
std::uint_fast32_t bufferHeight;
|
||||
|
||||
RenderingElementScaling(OpaqueType opaque = OpaqueType::Transparent);
|
||||
RenderingElementScaling(OpaqueType opaque, 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);
|
||||
RenderingElementScaling(OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0);
|
||||
RenderingElementScaling(RenderingElementScaling&) = delete;
|
||||
RenderingElementScaling& operator=(RenderingElementScaling&) = delete;
|
||||
|
||||
void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height);
|
||||
void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const;
|
||||
void UpdatePosition(Window& window) override;
|
||||
void UpdatePosition(Window& window, Transform& parent) override;
|
||||
bool bufferUpdated = true;
|
||||
RenderElementScalingOwning() = default;
|
||||
RenderElementScalingOwning(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight) : scalingBuffer(bufferWidth*bufferHeight), bufferWidth(bufferWidth), bufferHeight(bufferHeight) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class RenderingElementScalingRotating2D : public RenderingElement {
|
||||
public:
|
||||
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
||||
struct RenderElementScalingNonOwning {
|
||||
Pixel_BU8_GU8_RU8_AU8* scalingBuffer;
|
||||
std::uint_fast32_t bufferWidth;
|
||||
std::uint_fast32_t bufferHeight;
|
||||
bool bufferUpdated = true;
|
||||
RenderElementScalingNonOwning() = default;
|
||||
RenderElementScalingNonOwning(Pixel_BU8_GU8_RU8_AU8* scalingBuffer, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight) : scalingBuffer(scalingBuffer), bufferWidth(bufferWidth), bufferHeight(bufferHeight) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
struct RenderElementRotating {
|
||||
std::uint_fast32_t rotation;
|
||||
bool rotationUpdated = true;
|
||||
RenderElementRotating() = default;
|
||||
RenderElementRotating(std::uint_fast32_t rotation) : rotation(rotation) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct EmptyScalingBase {};
|
||||
struct EmptyRotatingBase {};
|
||||
|
||||
template<bool Scaling, bool Owning>
|
||||
using ScalingBase =
|
||||
std::conditional_t<
|
||||
Scaling,
|
||||
std::conditional_t<Owning,
|
||||
RenderElementScalingOwning,
|
||||
RenderElementScalingNonOwning>,
|
||||
EmptyScalingBase
|
||||
>;
|
||||
|
||||
template<bool Rotating>
|
||||
using RotatingBase =
|
||||
std::conditional_t<
|
||||
Rotating,
|
||||
RenderElementRotating,
|
||||
EmptyRotatingBase
|
||||
>;
|
||||
|
||||
class RenderingElementBase : public Transform {
|
||||
public:
|
||||
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
||||
OpaqueType opaque;
|
||||
RenderingElementBase(Anchor anchor, OpaqueType opaque) : Transform(anchor), opaque(opaque) {
|
||||
scaled.width = 0;
|
||||
}
|
||||
};
|
||||
|
||||
template<bool Scaling, bool Owning, bool Rotating> requires ((!Rotating || Scaling) && (!Owning || Scaling))
|
||||
class RenderingElement : public RenderingElementBase, public ScalingBase<Scaling, Owning>, public RotatingBase<Rotating> {
|
||||
public:
|
||||
RenderingElement() = default;
|
||||
RenderingElement(Anchor anchor, OpaqueType opaque) : RenderingElementBase(anchor, opaque) {
|
||||
|
||||
}
|
||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint_fast32_t rotation) requires(Rotating) : RenderingElementBase(anchor, opaque), RotatingBase<Rotating>(rotation) {
|
||||
|
||||
}
|
||||
|
||||
RenderingElement(Anchor anchor, const std::string_view imagePath) : Transform(anchor) {
|
||||
LoadImage(imagePath);
|
||||
}
|
||||
RenderingElement(Anchor anchor, const std::string_view imagePath, std::uint_fast32_t rotation) requires(Rotating) : Transform(anchor), RotatingBase<Rotating>(rotation) {
|
||||
LoadImage(imagePath);
|
||||
}
|
||||
|
||||
RenderingElement(Anchor anchor, const std::string_view imagePath, OpaqueType opaque) : RenderingElementBase(anchor, opaque) {
|
||||
LoadImageNoOpaqueCheck(imagePath);
|
||||
}
|
||||
RenderingElement(Anchor anchor, const std::string_view imagePath, OpaqueType opaque, std::uint_fast32_t rotation) requires(Rotating) : RenderingElementBase(anchor, opaque), RotatingBase<Rotating>(rotation) {
|
||||
LoadImageNoOpaqueCheck(imagePath);
|
||||
}
|
||||
|
||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, Pixel_BU8_GU8_RU8_AU8* scalingBuffer) requires(Scaling && !Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight, scalingBuffer) {
|
||||
|
||||
}
|
||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, Pixel_BU8_GU8_RU8_AU8* scalingBuffer, std::uint_fast32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight, scalingBuffer), RotatingBase<Rotating>(rotation) {
|
||||
|
||||
}
|
||||
|
||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight) requires(Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) {
|
||||
|
||||
}
|
||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::uint_fast32_t rotation) requires(Owning && Rotating) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) , RotatingBase<Rotating>(rotation) {
|
||||
|
||||
}
|
||||
|
||||
RenderingElement(RenderingElement&) = delete;
|
||||
RenderingElement& operator=(RenderingElement&) = delete;
|
||||
|
||||
void ScaleNearestNeighbor() requires(Scaling) {
|
||||
for (std::uint_fast32_t y = 0; y < scaled.height; y++) {
|
||||
std::uint_fast32_t srcY = y * ScalingBase<true, Owning>::bufferHeight / scaled.height;
|
||||
for (std::uint_fast32_t x = 0; x < scaled.width; x++) {
|
||||
std::uint_fast32_t srcX = x * ScalingBase<true, Owning>::bufferWidth / scaled.width;
|
||||
buffer[y * scaled.width + x] = ScalingBase<true, Owning>::scalingBuffer[srcY * ScalingBase<true, Owning>::bufferWidth + srcX];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScaleRotating() requires(Scaling) {
|
||||
const double rad = (static_cast<double>(RotatingBase<true>::rotation) / static_cast<double>(std::numeric_limits<std::uint_fast32_t>::max())) * 2.0 * std::numbers::pi;
|
||||
|
||||
const std::uint_fast32_t dstWidth = scaled.width;
|
||||
const std::uint_fast32_t dstHeight = scaled.height;
|
||||
|
||||
const double c2 = std::abs(std::cos(rad));
|
||||
const double s2 = std::abs(std::sin(rad));
|
||||
|
||||
const double rotatedWidth = dstWidth * c2 + dstHeight * s2;
|
||||
const double rotatedHeight = dstWidth * s2 + dstHeight * c2;
|
||||
|
||||
const std::uint_fast32_t diffX = static_cast<std::uint_fast32_t>(std::ceil((rotatedWidth - dstWidth) * 0.5));
|
||||
const std::uint_fast32_t diffY = static_cast<std::uint_fast32_t>(std::ceil((rotatedHeight - dstHeight) * 0.5));
|
||||
|
||||
scaled.width += diffX + diffX;
|
||||
scaled.height += diffY + diffY;
|
||||
|
||||
scaled.x -= diffX;
|
||||
scaled.y -= diffY;
|
||||
|
||||
ScalingBase<true, Owning>::scalingBuffer.clear();
|
||||
ScalingBase<true, Owning>::scalingBuffer.resize(scaled.width * scaled.height);
|
||||
|
||||
// Destination center
|
||||
const double dstCx = (static_cast<double>(scaled.width) - 1.0) * 0.5;
|
||||
const double dstCy = (static_cast<double>(scaled.height) - 1.0) * 0.5;
|
||||
|
||||
// Source center
|
||||
const double srcCx = (static_cast<double>(ScalingBase<true, Owning>::bufferWidth) - 1.0) * 0.5;
|
||||
const double srcCy = (static_cast<double>(ScalingBase<true, Owning>::bufferHeight) - 1.0) * 0.5;
|
||||
|
||||
const double c = std::cos(rad);
|
||||
const double s = std::sin(rad);
|
||||
|
||||
// Scale factors (destination → source)
|
||||
const double scaleX = static_cast<double>(ScalingBase<true, Owning>::bufferWidth) / dstWidth;
|
||||
const double scaleY = static_cast<double>(ScalingBase<true, Owning>::bufferHeight) / dstHeight;
|
||||
|
||||
for (std::uint_fast32_t yB = 0; yB < scaled.height; ++yB) {
|
||||
for (std::uint_fast32_t xB = 0; xB < scaled.width; ++xB) {
|
||||
|
||||
// ---- Destination pixel relative to center ----
|
||||
const double dx = (static_cast<double>(xB) - dstCx) * scaleX;
|
||||
const double dy = (static_cast<double>(yB) - dstCy) * scaleY;
|
||||
|
||||
// ---- Inverse rotation ----
|
||||
const double sx = (c * dx - s * dy) + srcCx;
|
||||
const double sy = (s * dx + c * dy) + srcCy;
|
||||
|
||||
// ---- Nearest neighbour sampling ----
|
||||
const std::int_fast32_t srcX = static_cast<std::int_fast32_t>(std::round(sx));
|
||||
const std::int_fast32_t srcY = static_cast<std::int_fast32_t>(std::round(sy));
|
||||
|
||||
if (srcX >= 0 && srcX < ScalingBase<true, Owning>::bufferWidth && srcY >= 0 && srcY < ScalingBase<true, Owning>::bufferHeight) {
|
||||
buffer[yB * scaled.width + xB] = ScalingBase<true, Owning>::scalingBuffer[srcY * ScalingBase<true, Owning>::bufferWidth + srcX];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RenderingElementScalingRotating2D(OpaqueType opaque = OpaqueType::Transparent);
|
||||
RenderingElementScalingRotating2D(OpaqueType opaque, 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);
|
||||
RenderingElementScalingRotating2D(OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::uint_fast32_t rotationAngle = 0, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0);
|
||||
RenderingElementScalingRotating2D(RenderingElementScalingRotating2D&) = delete;
|
||||
RenderingElementScalingRotating2D& operator=(RenderingElementScalingRotating2D&) = delete;
|
||||
|
||||
void UpdateRotation(std::uint_fast32_t newRotation);
|
||||
void ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height);
|
||||
void UpdateScaledBuffer(ScaleData& data);
|
||||
void UpdatePosition(Window& window) override;
|
||||
void UpdatePosition(Window& window, Transform& parent) override;
|
||||
void UpdatePosition(Window& window, ScaleData oldScale) {
|
||||
if constexpr(Scaling && !Rotating) {
|
||||
if(oldScale.width != scaled.width || oldScale.height != scaled.height) {
|
||||
buffer.resize(scaled.width * scaled.height);
|
||||
ScaleNearestNeighbor();
|
||||
window.AddDirtyRect(oldScale);
|
||||
window.AddDirtyRect(scaled);
|
||||
} else if(oldScale.x != scaled.x || oldScale.y != scaled.y) {
|
||||
window.AddDirtyRect(oldScale);
|
||||
window.AddDirtyRect(scaled);
|
||||
if(ScalingBase<true, Owning>::bufferUpdated) {
|
||||
ScaleNearestNeighbor();
|
||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||
}
|
||||
} else if(ScalingBase<true, Owning>::bufferUpdated) {
|
||||
ScaleNearestNeighbor();
|
||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||
}
|
||||
} else if constexpr(Rotating) {
|
||||
if(oldScale.width != scaled.width || oldScale.height != scaled.height) {
|
||||
buffer.resize(scaled.width * scaled.height);
|
||||
ScaleNearestNeighbor();
|
||||
window.AddDirtyRect(oldScale);
|
||||
window.AddDirtyRect(scaled);
|
||||
} else if(oldScale.x != scaled.x || oldScale.y != scaled.y) {
|
||||
window.AddDirtyRect(oldScale);
|
||||
window.AddDirtyRect(scaled);
|
||||
if(ScalingBase<true, Owning>::bufferUpdated) {
|
||||
ScaleRotating();
|
||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||
RotatingBase<true>::rotationUpdated = false;
|
||||
}
|
||||
} else if(ScalingBase<true, Owning>::bufferUpdated || RotatingBase<true>::rotationUpdated) {
|
||||
ScaleRotating();
|
||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||
RotatingBase<true>::rotationUpdated = false;
|
||||
}
|
||||
} else {
|
||||
if(oldScale.width != scaled.width || oldScale.height != scaled.height) {
|
||||
buffer.resize(scaled.width * scaled.height);
|
||||
window.AddDirtyRect(oldScale);
|
||||
window.AddDirtyRect(scaled);
|
||||
}
|
||||
if(oldScale.x != scaled.x || oldScale.y != scaled.y) {
|
||||
window.AddDirtyRect(oldScale);
|
||||
window.AddDirtyRect(scaled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePosition(Window& window) override {
|
||||
ScaleData oldScale = scaled;
|
||||
window.ScaleElement(*this);
|
||||
UpdatePosition(window, oldScale);
|
||||
for(Transform* child : children) {
|
||||
child->UpdatePosition(window, *this);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdatePosition(Window& window, Transform& parent) override {
|
||||
ScaleData oldScale = scaled;
|
||||
window.ScaleElement(*this, parent);
|
||||
UpdatePosition(window, oldScale);
|
||||
for(Transform* child : children) {
|
||||
child->UpdatePosition(window, *this);
|
||||
}
|
||||
}
|
||||
|
||||
void LoadImage(const std::string_view imagePath) {
|
||||
std::filesystem::path abs = std::filesystem::absolute(imagePath);
|
||||
int xSize;
|
||||
int ySize;
|
||||
unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4);
|
||||
|
||||
if constexpr(Scaling && !Owning) {
|
||||
ScalingBase<true, false>::bufferUpdated = true;
|
||||
} else if constexpr(Scaling && Owning) {
|
||||
ScalingBase<true, true>::bufferWidth = xSize;
|
||||
ScalingBase<true, true>::bufferHeight = ySize;
|
||||
ScalingBase<true, true>::bufferUpdated = true;
|
||||
} else {
|
||||
buffer.resize(xSize*ySize);
|
||||
}
|
||||
|
||||
opaque = OpaqueType::FullyOpaque;
|
||||
|
||||
if constexpr(Scaling) {
|
||||
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;
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].r = bgData[idx];
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].g = bgData[idx+1];
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].b = bgData[idx+2];
|
||||
ScalingBase<true, Owning>::calingBuffer[x*ySize+y].a = bgData[idx+3];
|
||||
}
|
||||
}
|
||||
|
||||
for(std::uint_fast32_t i = 0; i < xSize*ySize; i++) {
|
||||
if(ScalingBase<true, Owning>::scalingBuffer[i].a != 255) {
|
||||
opaque = OpaqueType::SemiOpaque;
|
||||
for(std::uint_fast32_t i2 = 0; i2 < xSize*ySize; i2++) {
|
||||
if(ScalingBase<true, Owning>::scalingBuffer[i2].a != 0 && ScalingBase<true, Owning>::scalingBuffer[i2].a != 255) {
|
||||
opaque = OpaqueType::Transparent;
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
for(std::uint_fast32_t i = 0; i < xSize*ySize; i++) {
|
||||
if(buffer[i].a != 255) {
|
||||
opaque = OpaqueType::SemiOpaque;
|
||||
for(std::uint_fast32_t i2 = 0; i2 < xSize*ySize; i2++) {
|
||||
if(buffer[i2].a != 0 && buffer[i2].a != 255) {
|
||||
opaque = OpaqueType::Transparent;
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void LoadImageNoOpaqueCheck(const std::string_view imagePath) {
|
||||
std::filesystem::path abs = std::filesystem::absolute(imagePath);
|
||||
int xSize;
|
||||
int ySize;
|
||||
unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4);
|
||||
|
||||
|
||||
if constexpr(Scaling && !Owning) {
|
||||
ScalingBase<true, false>::bufferUpdated = true;
|
||||
} else if constexpr(Scaling && Owning) {
|
||||
ScalingBase<true, true>::bufferWidth = xSize;
|
||||
ScalingBase<true, true>::bufferHeight = ySize;
|
||||
ScalingBase<true, true>::bufferUpdated = true;
|
||||
} else {
|
||||
buffer.resize(xSize*ySize);
|
||||
}
|
||||
|
||||
|
||||
if constexpr(Scaling) {
|
||||
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;
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].r = bgData[idx];
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].g = bgData[idx+1];
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].b = bgData[idx+2];
|
||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].a = bgData[idx+3];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -41,11 +41,11 @@ export namespace Crafter {
|
|||
Wrap // Wrap text to multiple lines
|
||||
};
|
||||
|
||||
class TextElement : public RenderingElementPreScaled {
|
||||
class TextElement : public RenderingElement<false, false, false> {
|
||||
private:
|
||||
void RenderWrappedLine(const std::string_view line, float scale, int baseline, std::uint_fast32_t startY, Pixel_BU8_GU8_RU8_AU8 color, Font& font, TextAlignment alignment);
|
||||
public:
|
||||
TextElement(std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0);
|
||||
TextElement(Anchor anchor);
|
||||
void RenderText(Window& window, const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 pixel, Font& font, TextAlignment alignment = TextAlignment::Left, VerticalTextAlignment verticalAlignment = VerticalTextAlignment::Top, TextOverflowMode overflowMode = TextOverflowMode::Clip);
|
||||
};
|
||||
}
|
||||
|
|
@ -23,19 +23,25 @@ import :Types;
|
|||
|
||||
export namespace Crafter {
|
||||
class Window;
|
||||
struct Anchor {
|
||||
std::int_fast32_t x;
|
||||
std::int_fast32_t y;
|
||||
std::uint_fast32_t width;
|
||||
std::uint_fast32_t height;
|
||||
std::int_fast32_t offsetX;
|
||||
std::int_fast32_t offsetY;
|
||||
std::int_fast32_t z;
|
||||
Anchor() = default;
|
||||
Anchor(std::int_fast32_t x, std::int_fast32_t y, std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t offsetX, std::int_fast32_t offsetY, std::int_fast32_t z);
|
||||
};
|
||||
class Transform {
|
||||
public:
|
||||
std::int_fast32_t z;
|
||||
std::int_fast32_t anchorX;
|
||||
std::int_fast32_t anchorY;
|
||||
std::int_fast32_t anchorOffsetX;
|
||||
std::int_fast32_t anchorOffsetY;
|
||||
std::uint_fast32_t relativeWidth;
|
||||
std::uint_fast32_t relativeHeight;
|
||||
Anchor anchor;
|
||||
ScaleData scaled;
|
||||
std::vector<Transform*> children;
|
||||
Transform(std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0);
|
||||
Transform(Anchor anchor);
|
||||
Transform(Transform&) = delete;
|
||||
Transform(Transform&&) = delete;
|
||||
Transform& operator=(Transform&) = delete;
|
||||
virtual ~Transform() = default;
|
||||
virtual void UpdatePosition(Window& window);
|
||||
|
|
|
|||
|
|
@ -139,10 +139,20 @@ namespace Crafter {
|
|||
return pixel * (SCALE / width);
|
||||
}
|
||||
|
||||
export constexpr std::int_fast32_t RelativeToAbsolute(std::int_fast32_t relative, std::int_fast32_t full) {
|
||||
return static_cast<std::int_fast32_t>(
|
||||
(static_cast<__int128>(relative) * full) / SCALE
|
||||
);
|
||||
}
|
||||
|
||||
export constexpr std::int_fast32_t BoundToBoundless(std::int_fast32_t bound) {
|
||||
return bound * BOUND;
|
||||
}
|
||||
|
||||
export constexpr std::int_fast32_t BoundlessToBound(std::int_fast32_t bound) {
|
||||
return bound / BOUND;
|
||||
}
|
||||
|
||||
export constexpr std::int_fast32_t FractionalToMappedBoundless(double f) {
|
||||
return std::int_fast32_t(f * SCALEDOUBLEBOUNDLESS);
|
||||
}
|
||||
|
|
@ -162,4 +172,47 @@ namespace Crafter {
|
|||
export constexpr std::int_fast32_t PixelToMappedBoundless(std::int_fast32_t pixel, std::int_fast32_t width) {
|
||||
return pixel * (SCALEBOUNDLESS / width);
|
||||
}
|
||||
|
||||
export enum class CrafterKeys {
|
||||
// Alphabetic keys
|
||||
A, B, C, D, E, F, G, H, I, J, K, L, M,
|
||||
N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
|
||||
|
||||
// Numeric keys
|
||||
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9,
|
||||
|
||||
// Function keys
|
||||
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
|
||||
|
||||
// Control keys
|
||||
Escape, Tab, Enter, Space, Backspace, Delete, Insert,
|
||||
Home, End, PageUp, PageDown, CapsLock, NumLock, ScrollLock,
|
||||
|
||||
// Modifier keys
|
||||
LeftShift, RightShift, LeftCtrl, RightCtrl,
|
||||
LeftAlt, RightAlt, LeftSuper, RightSuper,
|
||||
|
||||
// Arrow keys
|
||||
Up, Down, Left, Right,
|
||||
|
||||
// Keypad keys
|
||||
keypad_0, keypad_1, keypad_2, keypad_3, keypad_4,
|
||||
keypad_5, keypad_6, keypad_7, keypad_8, keypad_9,
|
||||
keypad_enter, keypad_plus, keypad_minus, keypad_multiply,
|
||||
keypad_divide, keypad_decimal,
|
||||
|
||||
// Punctuation and special keys
|
||||
grave, minus, equal, bracket_left, bracket_right,
|
||||
backslash, semicolon, quote, comma, period, slash,
|
||||
print_screen, pause, menu,
|
||||
|
||||
// Additional keys
|
||||
volume_up, volume_down, volume_mute,
|
||||
media_play, media_stop, media_prev, media_next,
|
||||
browser_back, browser_forward, browser_refresh,
|
||||
browser_stop, browser_search, browser_home,
|
||||
launch_mail, launch_calculator, launch_media_player,
|
||||
|
||||
CrafterKeysMax
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ import Crafter.Event;
|
|||
|
||||
export namespace Crafter {
|
||||
class Transform;
|
||||
class RenderingElement;
|
||||
class Window {
|
||||
public:
|
||||
std::int_fast32_t width;
|
||||
|
|
@ -76,7 +75,7 @@ export namespace Crafter {
|
|||
std::chrono::nanoseconds totalUpdate;
|
||||
std::vector<std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>> updateTimings;
|
||||
std::chrono::nanoseconds totalRender;
|
||||
std::vector<std::tuple<const RenderingElement*, std::uint_fast32_t, std::uint_fast32_t, std::chrono::nanoseconds>> renderTimings;
|
||||
std::vector<std::tuple<const Transform*, std::uint_fast32_t, std::uint_fast32_t, std::chrono::nanoseconds>> renderTimings;
|
||||
std::chrono::nanoseconds vblank;
|
||||
std::chrono::nanoseconds totalFrame;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> frameEnd;
|
||||
|
|
@ -87,13 +86,13 @@ export namespace Crafter {
|
|||
|
||||
class WindowKeyboard {
|
||||
public:
|
||||
bool heldkeys[255] = {};
|
||||
Event<void> onKeyDown[255];
|
||||
Event<void> onKeyHold[255];
|
||||
Event<void> onKeyUp[255];
|
||||
Event<char> onAnyKeyDown;
|
||||
Event<char> onAnyKeyHold;
|
||||
Event<char> onAnyKeyUp;
|
||||
bool heldkeys[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)] = {};
|
||||
Event<void> onKeyDown[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
||||
Event<void> onKeyHold[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
||||
Event<void> onKeyUp[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
||||
Event<CrafterKeys> onAnyKeyDown;
|
||||
Event<CrafterKeys> onAnyKeyHold;
|
||||
Event<CrafterKeys> onAnyKeyUp;
|
||||
};
|
||||
|
||||
class MouseElement;
|
||||
|
|
@ -138,7 +137,6 @@ export namespace Crafter {
|
|||
};
|
||||
|
||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
||||
class RenderingElement;
|
||||
class WindowWayland final : public WindowKeyboard, public WindowMouse, public WindowFramebuffer, public WindowTitle {
|
||||
public:
|
||||
Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr;
|
||||
|
|
@ -162,6 +160,7 @@ export namespace Crafter {
|
|||
xkb_state* xkb_state;
|
||||
void RenderElement(Transform* transform);
|
||||
void Render() override;
|
||||
void QueueRender();
|
||||
void StartSync() override;
|
||||
void StartUpdate() override;
|
||||
void StopUpdate() override;
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ export import :Window;
|
|||
export import :Transform;
|
||||
export import :RenderingElement;
|
||||
export import :MouseElement;
|
||||
export import :ImageElement;
|
||||
export import :TextElement;
|
||||
export import :GridElement;
|
||||
export import :Types;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue