From 500e8e4e3b1865f69929acdd516e29d79253c758 Mon Sep 17 00:00:00 2001 From: JonasAugust12 <111167499+JonasAugust12@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:58:13 +0700 Subject: [PATCH] Add render shape --- src/graphics/Renderer.cpp | 154 +++++++++++++++++++++++++++++--------- 1 file changed, 117 insertions(+), 37 deletions(-) diff --git a/src/graphics/Renderer.cpp b/src/graphics/Renderer.cpp index e4443715..2cb8d0c0 100644 --- a/src/graphics/Renderer.cpp +++ b/src/graphics/Renderer.cpp @@ -64,41 +64,9 @@ void Renderer::drawRectangle(Rect* rectangle) const { rectangle->getHeight()); } -void Renderer::drawCircle(Circle* circle) const { - mColor fill_color = circle->getFillColor(); - Gdiplus::SolidBrush circleFill( - Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b)); +void Renderer::drawCircle(Circle* circle) const {} - mColor outline_color = circle->getOutlineColor(); - Gdiplus::Pen circleOutline(Gdiplus::Color(outline_color.a, outline_color.r, - outline_color.g, outline_color.b), - circle->getOutlineThickness()); - float ellipseX = circle->getPosition().x - circle->getRadius().x; - float ellipseY = circle->getPosition().y - circle->getRadius().y; - float ellipseWidth = 2 * circle->getRadius().x; - float ellipseHeight = 2 * circle->getRadius().y; - graphics.FillEllipse(&circleFill, ellipseX, ellipseY, ellipseWidth, - ellipseHeight); - graphics.DrawEllipse(&circleOutline, ellipseX, ellipseY, ellipseWidth, - ellipseHeight); -} - -void Renderer::drawEllipse(Ell* ellipse) const { - mColor fill_color = ellipse->getFillColor(); - Gdiplus::SolidBrush ellipseFill( - Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b)); - graphics.FillEllipse(ellipseFill, ellipse->getPosition().x, - ellipse->getPosition().y, ellipse->getRadius().x, - ellipse->getRadius().y); - mColor outline_color = ellipse->getOutlineColor(); - Gdiplus::Pen ellipseOutline( - Gdiplus::Color(outline_color.a, outline_color.r, outline_color.g, - outline_color.b), - ellipse->getOutlineThickness()); - graphics.DrawEllipse(&ellipseOutline, ellipse->getPosition().x, - ellipse->getPosition().y, 2 * ellipse->getRadius().x, - 2 * ellipse->getRadius().y); -} +void Renderer::drawEllipse(Ell* ellipse) const {} void Renderer::drawPolygon(Plygon* polygon) const { mColor fill_color = polygon->getFillColor(); @@ -123,8 +91,120 @@ void Renderer::drawPolygon(Plygon* polygon) const { static_cast< int >(points.size())); } -void Renderer::drawText(Text* text) const {} +void Renderer::drawText(Text* text) const { + mColor outline_color = text->getOutlineColor(); + mColor fill_color = text->getFillColor(); + + Gdiplus::SolidBrush textFill( + Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b)); + + Gdiplus::Pen textOutline(Gdiplus::Color(outline_color.a, outline_color.r, + outline_color.g, outline_color.b), + text->getOutlineThickness()); + + // Gdiplus::FontFamily fontFamily(L"Arial"); + // Gdiplus::Font font(&fontFamily, text->getFontSize(), + // Gdiplus::FontStyleRegular, Gdiplus::UnitPixel); + + // Gdiplus::PointF position(text->getPosition().x, text->getPosition().y); + // Gdiplus::GraphicsPath path; + + // path.AddString(text->getContent().c_str(), -1, &fontFamily, + // Gdiplus::FontStyleRegular, text->getOutlineThickness(), + // position, NULL); + + // graphics.DrawPath(&textOutline, &path); + // graphics.FillPath(&textFill, &path); +} + +void Renderer::drawPolyline(Plyline* polyline) const { + mColor color = polyline->getOutlineColor(); + Gdiplus::Pen polylinePen(Gdiplus::Color(color.a, color.r, color.g, color.b), + polyline->getOutlineThickness()); + Gdiplus::GraphicsPath path; + const std::vector< Vector2Df >& points = polyline->getPoints(); + if (points.size() < 2) { + return; + } -void Renderer::drawPolyline(Plyline* polyline) const {} + path.StartFigure(); + path.AddLine(points[0].x, points[0].y, points[1].x, points[1].y); + for (size_t i = 2; i < points.size(); ++i) { + path.AddLine(points[i - 1].x, points[i - 1].y, points[i].x, + points[i].y); + } + graphics.DrawPath(&polylinePen, &path); +} -void Renderer::drawPath(Path* path) const {} +void Renderer::drawPath(Path* path) const { + mColor outline_color = path->getOutlineColor(); + Gdiplus::Pen pathPen(Gdiplus::Color(outline_color.a, outline_color.r, + outline_color.g, outline_color.b), + path->getOutlineThickness()); + + Gdiplus::GraphicsPath gdiPath; + + const std::vector< PathPoint >& points = path->getPoints(); + int n = points.size(); + Vector2Df firstPoint{0, 0}, curPoint{0, 0}; + + for (int i = 0; i < n; ++i) { + if (points[i].TC == 'M') { + firstPoint = points[i].Point; + gdiPath.StartFigure(); + gdiPath.AddLine(firstPoint.x, firstPoint.y, firstPoint.x, + firstPoint.y); + curPoint = firstPoint; + } else if (points[i].TC == 'm') { + firstPoint.x = curPoint.x + points[i].Point.x; + firstPoint.y = curPoint.y + points[i].Point.y; + gdiPath.StartFigure(); + gdiPath.AddLine(firstPoint.x, firstPoint.y, firstPoint.x, + firstPoint.y); + curPoint = firstPoint; + } else if (points[i].TC == 'L') { + gdiPath.AddLine(points[i].Point.x, points[i].Point.y, + points[i].Point.x, points[i].Point.y); + curPoint = points[i].Point; + } else if (points[i].TC == 'l') { + Vector2Df endPoint{curPoint.x + points[i].Point.x, + curPoint.y + points[i].Point.y}; + gdiPath.AddLine(endPoint.x, endPoint.y, endPoint.x, endPoint.y); + curPoint = endPoint; + } else if (points[i].TC == 'C') { + if (i + 2 < n) { + Vector2Df controlPoint1 = points[i].Point; + Vector2Df controlPoint2 = points[i + 1].Point; + Vector2Df controlPoint3 = points[i + 2].Point; + gdiPath.AddBezier(curPoint.x, curPoint.y, controlPoint1.x, + controlPoint1.y, controlPoint2.x, + controlPoint2.y, controlPoint3.x, + controlPoint3.y); + i += 2; + } + curPoint = points[i].Point; + } else if (points[i].TC == 'c') { + if (i + 2 < n) { + Vector2Df controlPoint1 = + Vector2Df{curPoint.x + points[i].Point.x, + curPoint.y + points[i].Point.y}; + Vector2Df controlPoint2 = + Vector2Df{curPoint.x + points[i + 1].Point.x, + curPoint.y + points[i + 1].Point.y}; + Vector2Df controlPoint3 = + Vector2Df{curPoint.x + points[i + 1].Point.x, + curPoint.y + points[i + 1].Point.y}; + gdiPath.AddBezier(curPoint.x, curPoint.y, controlPoint1.x, + controlPoint1.y, controlPoint2.x, + controlPoint2.y, controlPoint3.x, + controlPoint3.y); + i += 2; + curPoint = controlPoint3; + } + } else if (points[i].TC == 'Z' || points[i].TC == 'z') { + gdiPath.CloseFigure(); + curPoint = firstPoint; + } + } + graphics.DrawPath(&pathPen, &gdiPath); +}