Add canvas UI node

This commit is contained in:
ShadelessFox
2025-01-19 14:11:56 +01:00
parent cb960ceaad
commit ee31f401aa
12 changed files with 279 additions and 2 deletions
+19
View File
@@ -0,0 +1,19 @@
#include "Canvas.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Texture.hpp"
gui::Canvas::Canvas(ImageFormat inFormat, glm::uvec2 inSize) : UINode(inSize) {
ImageData data {inFormat, inSize.x, inSize.y};
mTexture = Texture::from(&data);
}
void gui::Canvas::draw(const DrawContext& pctx, const Assets& assets) {
auto pos = calcPos();
auto col = calcColor();
auto batch = pctx.getBatch2D();
batch->texture(mTexture.get());
batch->rect(pos.x, pos.y, size.x, size.y, 0, 0, 0, {}, false, true, col);
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include "UINode.hpp"
#include "graphics/core/ImageData.hpp"
#include "graphics/core/Texture.hpp"
class Texture;
namespace gui {
class Canvas final : public UINode {
public:
explicit Canvas(ImageFormat inFormat, glm::uvec2 inSize);
~Canvas() override = default;
void draw(const DrawContext& pctx, const Assets& assets) override;
[[nodiscard]] std::shared_ptr<::Texture> texture() const {
return mTexture;
}
private:
std::shared_ptr<::Texture> mTexture;
std::unique_ptr<ImageData> mData;
};
}
+14
View File
@@ -4,6 +4,7 @@
#include "elements/Image.hpp"
#include "elements/Menu.hpp"
#include "elements/Button.hpp"
#include "elements/Canvas.hpp"
#include "elements/CheckBox.hpp"
#include "elements/TextBox.hpp"
#include "elements/TrackBar.hpp"
@@ -455,6 +456,18 @@ static std::shared_ptr<UINode> readImage(
return image;
}
static std::shared_ptr<UINode> readCanvas(
const UiXmlReader& reader, const xml::xmlelement& element
) {
auto size = glm::uvec2{32, 32};
if (element.has("size")) {
size = element.attr("size").asVec2();
}
auto image = std::make_shared<Canvas>(ImageFormat::rgba8888, size);
_readUINode(reader, element, *image);
return image;
}
static std::shared_ptr<UINode> readTrackBar(
const UiXmlReader& reader, const xml::xmlelement& element
) {
@@ -634,6 +647,7 @@ static std::shared_ptr<UINode> readPageBox(UiXmlReader& reader, const xml::xmlel
UiXmlReader::UiXmlReader(const scriptenv& env) : env(env) {
contextStack.emplace("");
add("image", readImage);
add("canvas", readCanvas);
add("label", readLabel);
add("panel", readPanel);
add("button", readButton);