From c8c39665e6069462e0cc955e75ec1bc96fbe1325 Mon Sep 17 00:00:00 2001 From: LingSamuel Date: Wed, 27 Nov 2024 01:12:47 +0800 Subject: [PATCH] add quad, fill_quad --- README.md | 27 +++++++++++++++++++++++++++ src/D2DPainter.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/D2DPainter.hpp | 2 ++ src/DrawList.cpp | 33 +++++++++++++++++++++++++++++++++ src/DrawList.hpp | 27 ++++++++++++++++++++++++++- src/Plugin.cpp | 16 ++++++++++++++++ 6 files changed, 142 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6aa58c2..7620ee3 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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 diff --git a/src/D2DPainter.cpp b/src/D2DPainter.cpp index 3716b54..d015117 100644 --- a/src/D2DPainter.cpp +++ b/src/D2DPainter.cpp @@ -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 pathGeometry; + m_d2d1->CreatePathGeometry(&pathGeometry); + + ComPtr 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 pathGeometry; + m_d2d1->CreatePathGeometry(&pathGeometry); + + ComPtr 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); diff --git a/src/D2DPainter.hpp b/src/D2DPainter.hpp index 8189152..f3f87bf 100644 --- a/src/D2DPainter.hpp +++ b/src/D2DPainter.hpp @@ -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& image, float x, float y); void image(std::shared_ptr& image, float x, float y, float w, float h); diff --git a/src/DrawList.cpp b/src/DrawList.cpp index 0a756c3..2658c2f 100644 --- a/src/DrawList.cpp +++ b/src/DrawList.cpp @@ -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; diff --git a/src/DrawList.hpp b/src/DrawList.hpp index 29c96e9..0e1983d 100644 --- a/src/DrawList.hpp +++ b/src/DrawList.hpp @@ -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; @@ -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{}; @@ -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& image, float x, float y, float w, float h); void fill_circle(float x, float y, float radiusX, float radiusY, unsigned int color); diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 0aee523..9fd3461 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -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); }; @@ -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;