Skip to content

Commit

Permalink
add quad, fill_quad
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel committed Nov 26, 2024
1 parent 026e71d commit c8c3966
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function()
d2d.fill_rect(500, 100, w, h, 0xFFFFFFFF)
d2d.text(font, str, 500, 100, 0xFF000000)
d2d.outline_rect(500, 100, w, h, 5, 0xFF00FFFF)
d2d.quad(100, 100, 500, 100, 500, 500, 400, 500, 5, 0xFF00FFFF)
d2d.fill_quad(1100, 1100, 1500, 1100, 1500, 1500, 1400, 1500, 0xFF00FFFF)

local screen_w, screen_h = d2d.surface_size()
local img_w, img_h = image:size()
Expand Down Expand Up @@ -183,6 +185,31 @@ Draws a filled in a rounded rectangle

---

### `d2d.quad(x1, y1, x2, y2, x3, y3, x4, y4, thickness, color)`
Draws the outline of a quad

#### Params
* `x1, y1` the first coordinate
* `x2, y2` the second coordinate
* `x3, y3` the third coordinate
* `x4, y4` the fourth coordinate
* `thickness` the thickness of the outline
* `color` the ARGB color of the quad

---

### `d2d.fill__quad(x1, y1, x2, y2, x3, y3, x4, y4, color)`
Draws a filled in a quad

#### Params
* `x1, y1` the first coordinate
* `x2, y2` the second coordinate
* `x3, y3` the third coordinate
* `x4, y4` the fourth coordinate
* `color` the ARGB color of the quad

---

### `d2d.line(x1, y1, x2, y2, thickness, color)`
Draws a line between two points

Expand Down
38 changes: 38 additions & 0 deletions src/D2DPainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ void D2DPainter::fill_rounded_rect(float x, float y, float w, float h, float rad
m_context->FillRoundedRectangle({x, y, x + w, y + h, radiusX, radiusY}, m_brush.Get());
}

void D2DPainter::quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float thickness, unsigned int color) {
ComPtr<ID2D1PathGeometry> pathGeometry;
m_d2d1->CreatePathGeometry(&pathGeometry);

ComPtr<ID2D1GeometrySink> sink;
pathGeometry->Open(&sink);

sink->BeginFigure(D2D1::Point2F(x1, y1), D2D1_FIGURE_BEGIN_FILLED);
sink->AddLine(D2D1::Point2F(x2, y2));
sink->AddLine(D2D1::Point2F(x3, y3));
sink->AddLine(D2D1::Point2F(x4, y4));

sink->EndFigure(D2D1_FIGURE_END_CLOSED);
sink->Close();

set_color(color);
m_context->DrawGeometry(pathGeometry.Get(), m_brush.Get(), thickness);
}

void D2DPainter::fill_quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, unsigned int color) {
ComPtr<ID2D1PathGeometry> pathGeometry;
m_d2d1->CreatePathGeometry(&pathGeometry);

ComPtr<ID2D1GeometrySink> sink;
pathGeometry->Open(&sink);

sink->BeginFigure(D2D1::Point2F(x1, y1), D2D1_FIGURE_BEGIN_FILLED);
sink->AddLine(D2D1::Point2F(x2, y2));
sink->AddLine(D2D1::Point2F(x3, y3));
sink->AddLine(D2D1::Point2F(x4, y4));

sink->EndFigure(D2D1_FIGURE_END_CLOSED);
sink->Close();

set_color(color);
m_context->FillGeometry(pathGeometry.Get(), m_brush.Get());
}

void D2DPainter::line(float x1, float y1, float x2, float y2, float thickness, unsigned int color) {
set_color(color);
m_context->DrawLine({x1, y1}, {x2, y2}, m_brush.Get(), thickness);
Expand Down
2 changes: 2 additions & 0 deletions src/D2DPainter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class D2DPainter {
void outline_rect(float x, float y, float w, float h, float thickness, unsigned int color);
void rounded_rect(float x, float y, float w, float h, float radiusX, float radiusY, float thickness, unsigned int color);
void fill_rounded_rect(float x, float y, float w, float h, float radiusX, float radiusY, unsigned int color);
void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float thickness, unsigned int color);
void fill_quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, unsigned int color);
void line(float x1, float y1, float x2, float y2, float thickness, unsigned int color);
void image(std::shared_ptr<D2DImage>& image, float x, float y);
void image(std::shared_ptr<D2DImage>& image, float x, float y, float w, float h);
Expand Down
33 changes: 33 additions & 0 deletions src/DrawList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,39 @@ void DrawList::CommandLock::fill_rounded_rect(float x, float y, float w, float h
commands.emplace_back(std::move(cmd));
}

void DrawList::CommandLock::quad(
float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float thickness, unsigned int color) {
Command cmd{};
cmd.type = CommandType::QUAD;
cmd.quad.x1 = x1;
cmd.quad.y1 = y1;
cmd.quad.x2 = x2;
cmd.quad.y2 = y2;
cmd.quad.x3 = x3;
cmd.quad.y3 = y3;
cmd.quad.x4 = x4;
cmd.quad.y4 = y4;
cmd.quad.thickness = thickness;
cmd.quad.color = color;
commands.emplace_back(std::move(cmd));
}

void DrawList::CommandLock::fill_quad(
float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, unsigned int color) {
Command cmd{};
cmd.type = CommandType::FILL_QUAD;
cmd.fill_quad.x1 = x1;
cmd.fill_quad.y1 = y1;
cmd.fill_quad.x2 = x2;
cmd.fill_quad.y2 = y2;
cmd.fill_quad.x3 = x3;
cmd.fill_quad.y3 = y3;
cmd.fill_quad.x4 = x4;
cmd.fill_quad.y4 = y4;
cmd.fill_quad.color = color;
commands.emplace_back(std::move(cmd));
}

void DrawList::CommandLock::line(float x1, float y1, float x2, float y2, float thickness, unsigned int color) {
Command cmd{};
cmd.type = CommandType::LINE;
Expand Down
27 changes: 26 additions & 1 deletion src/DrawList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DrawList {
public:
enum class CommandType { TEXT, FILL_RECT, OUTLINE_RECT, ROUNDED_RECT, FILL_ROUNDED_RECT, LINE, IMAGE, FILL_CIRCLE, CIRCLE, PIE, RING };
enum class CommandType { TEXT, FILL_RECT, OUTLINE_RECT, ROUNDED_RECT, FILL_ROUNDED_RECT, QUAD, FILL_QUAD, LINE, IMAGE, FILL_CIRCLE, CIRCLE, PIE, RING };

struct Command {
CommandType type;
Expand Down Expand Up @@ -54,6 +54,29 @@ class DrawList {
float rY{};
unsigned int color{};
} fill_rounded_rect;
struct {
float x1{};
float y1{};
float x2{};
float y2{};
float x3{};
float y3{};
float x4{};
float y4{};
float thickness{};
unsigned int color{};
} quad;
struct {
float x1{};
float y1{};
float x2{};
float y2{};
float x3{};
float y3{};
float x4{};
float y4{};
unsigned int color{};
} fill_quad;
struct {
float x1{};
float y1{};
Expand Down Expand Up @@ -117,6 +140,8 @@ class DrawList {
void outline_rect(float x, float y, float w, float h, float thickness, unsigned int color);
void rounded_rect(float x, float y, float w, float h, float rX, float rY, float thickness, unsigned int color);
void fill_rounded_rect(float x, float y, float w, float h, float rX, float rY, unsigned int color);
void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float thickness, unsigned int color);
void fill_quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, unsigned int color);
void line(float x1, float y1, float x2, float y2, float thickness, unsigned int color);
void image(std::shared_ptr<D2DImage>& image, float x, float y, float w, float h);
void fill_circle(float x, float y, float radiusX, float radiusY, unsigned int color);
Expand Down
16 changes: 16 additions & 0 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ void on_ref_lua_state_created(lua_State* l) try {
d2d["fill_rounded_rect"] = [](float x, float y, float w, float h, float rX, float rY, unsigned int color) {
g_plugin->cmds->fill_rounded_rect(x, y, w, h, rX, rY, color);
};
d2d["quad"] = [](float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float thickness, unsigned int color) {
g_plugin->cmds->quad(x1, y1, x2, y2, x3, y3, x4, y4, thickness, color);
};
d2d["fill_quad"] = [](float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, unsigned int color) {
g_plugin->cmds->fill_quad(x1, y1, x2, y2, x3, y3, x4, y4, color);
};
d2d["line"] = [](float x1, float y1, float x2, float y2, float thickness, unsigned int color) {
g_plugin->cmds->line(x1, y1, x2, y2, thickness, color);
};
Expand Down Expand Up @@ -260,6 +266,16 @@ void on_ref_frame() try {
cmd.rounded_rect.rX, cmd.rounded_rect.rY, cmd.rounded_rect.color);
break;

case DrawList::CommandType::QUAD:
g_plugin->d2d->quad(cmd.quad.x1, cmd.quad.y1, cmd.quad.x2, cmd.quad.y2, cmd.quad.x3, cmd.quad.y3,
cmd.quad.x4, cmd.quad.y4, cmd.quad.thickness, cmd.quad.color);
break;

case DrawList::CommandType::FILL_QUAD:
g_plugin->d2d->fill_quad(cmd.fill_quad.x1, cmd.fill_quad.y1, cmd.fill_quad.x2, cmd.fill_quad.y2,
cmd.fill_quad.x3, cmd.fill_quad.y3, cmd.fill_quad.x4, cmd.fill_quad.y4, cmd.fill_quad.color);
break;

case DrawList::CommandType::LINE:
g_plugin->d2d->line(cmd.line.x1, cmd.line.y1, cmd.line.x2, cmd.line.y2, cmd.line.thickness, cmd.line.color);
break;
Expand Down

0 comments on commit c8c3966

Please sign in to comment.