Files
ImXui/include/ImXui/RenderCommands.h
T
2026-08-27 17:15:05 -07:00

152 lines
4.7 KiB
C++

#pragma once
#include "ImXui.h"
#include <cstdint>
#include <string>
#include <vector>
namespace ImXui {
enum RenderCommandType {
RenderCommandType_BeginScene,
RenderCommandType_PushTransform,
RenderCommandType_PopTransform,
RenderCommandType_PushClip,
RenderCommandType_PopClip,
RenderCommandType_ColoredQuad,
RenderCommandType_TexturedQuad,
RenderCommandType_NineGrid,
RenderCommandType_Figure,
RenderCommandType_Text,
RenderCommandType_CustomDraw,
RenderCommandType_SetBlendMode,
RenderCommandType_EndScene,
};
struct Transform2D {
float m00, m01, m10, m11, tx, ty;
Transform2D()
: m00(1.0f), m01(0.0f), m10(0.0f), m11(1.0f), tx(0.0f), ty(0.0f) {}
};
struct RenderCommand {
RenderCommandType type;
Rect rect;
Color color;
Color strokeColor;
float opacity;
float strokeWidth;
uint32_t blendMode;
uint32_t leftInset;
uint32_t topInset;
uint32_t rightInset;
uint32_t bottomInset;
bool noCenter;
bool closed;
Transform2D transform;
std::wstring resource;
std::wstring text;
std::wstring fontName;
float fontSize;
bool centered;
std::vector<Vec2> points;
RenderCommand()
: type(RenderCommandType_BeginScene)
, rect()
, color()
, strokeColor()
, opacity(1.0f)
, strokeWidth(0.0f)
, blendMode(0)
, leftInset(0)
, topInset(0)
, rightInset(0)
, bottomInset(0)
, noCenter(false)
, closed(false)
, transform()
, fontSize(0.0f)
, centered(false)
{
}
};
class IRenderCommandSink {
public:
virtual ~IRenderCommandSink() {}
virtual void beginScene(float width, float height) = 0;
virtual void drawColoredQuad(const Rect& rect, const Color& color, float opacity) = 0;
virtual void drawTexturedQuad(const Rect& rect, const std::wstring& resource,
const Color& color, float opacity) = 0;
virtual void drawText(const Rect& rect, const std::wstring& text,
const std::wstring& fontName, float fontSize,
const Color& color, float opacity, bool centered) = 0;
virtual void endScene() = 0;
virtual void pushTransform(const Transform2D&) {}
virtual void popTransform() {}
virtual void pushClip(const Rect&) {}
virtual void popClip() {}
virtual void drawNineGrid(const Rect&, const std::wstring&, const Color&, float,
uint32_t, uint32_t, uint32_t, uint32_t, bool) {}
virtual void drawFigure(const Rect&, const std::vector<Vec2>&, bool,
const Color&, const Color&, float, float, uint32_t) {}
virtual void setBlendMode(uint32_t) {}
virtual void drawCustom(const std::wstring& drawId, const Rect&, float opacity) {
(void)drawId; (void)opacity;
}
};
class RenderCommandBuffer : public IRenderCommandSink {
public:
RenderCommandBuffer() : m_width(0.0f), m_height(0.0f) {}
void clear();
const std::vector<RenderCommand>& getCommands() const { return m_commands; }
std::vector<RenderCommand>& getCommands() { return m_commands; }
float getWidth() const { return m_width; }
float getHeight() const { return m_height; }
void beginScene(float width, float height);
void drawColoredQuad(const Rect& rect, const Color& color, float opacity);
void drawTexturedQuad(const Rect& rect, const std::wstring& resource,
const Color& color, float opacity);
void drawText(const Rect& rect, const std::wstring& text,
const std::wstring& fontName, float fontSize,
const Color& color, float opacity, bool centered);
void endScene();
void pushTransform(const Transform2D& transform);
void popTransform();
void pushClip(const Rect& rect);
void popClip();
void drawNineGrid(const Rect& rect, const std::wstring& resource,
const Color& color, float opacity,
uint32_t leftInset, uint32_t topInset,
uint32_t rightInset, uint32_t bottomInset,
bool noCenter);
void drawFigure(const Rect& rect, const std::vector<Vec2>& points, bool closed,
const Color& fillColor, const Color& strokeColor,
float opacity, float strokeWidth, uint32_t blendMode);
void setBlendMode(uint32_t blendMode);
void drawCustom(const std::wstring& drawId, const Rect& rect, float opacity);
private:
float m_width;
float m_height;
std::vector<RenderCommand> m_commands;
};
void emitRenderCommands(const Scene* scene, IRenderCommandSink* sink);
}