aboutsummaryrefslogtreecommitdiff
path: root/include/cru/osx/graphics/quartz/Geometry.hpp
blob: 33a2d8a8f972926d4726abb29b8550ba42f3666f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "Resource.hpp"
#include "cru/platform/graphics/Geometry.hpp"

#include <memory>

#include <CoreGraphics/CoreGraphics.h>

namespace cru::platform::graphics::osx::quartz {
class QuartzGeometry : public OsxQuartzResource, public virtual IGeometry {
 public:
  QuartzGeometry(IGraphFactory *graphics_factory, CGContextRef cg_context,
                 CGPathRef cg_path);

  CRU_DELETE_COPY(QuartzGeometry)
  CRU_DELETE_MOVE(QuartzGeometry)

  ~QuartzGeometry() override;

  CGPathRef GetCGPath() const { return cg_path_; }

  bool FillContains(const Point &point) override;

 private:
  CGContextRef cg_context_;
  CGPathRef cg_path_;
};

namespace details {
struct GeometryBeginFigureAction : Object {
  explicit GeometryBeginFigureAction(Point point) : point(point) {}

  Point point;
};

struct GeometryCloseFigureAction : Object {
  explicit GeometryCloseFigureAction(bool close) : close(close) {}

  bool close;
};

struct GeometryLineToAction : Object {
  explicit GeometryLineToAction(Point point) : point(point) {}

  Point point;
};

struct GeometryQuadraticBezierToAction : Object {
  GeometryQuadraticBezierToAction(Point control_point, Point end_point)
      : control_point(control_point), end_point(end_point) {}

  Point control_point;
  Point end_point;
};
}  // namespace details

class QuartzGeometryBuilder : public OsxQuartzResource,
                              public virtual IGeometryBuilder {
 public:
  explicit QuartzGeometryBuilder(IGraphFactory *graphics_factory,
                                 CGContextRef cg_context);

  CRU_DELETE_COPY(QuartzGeometryBuilder)
  CRU_DELETE_MOVE(QuartzGeometryBuilder)

  ~QuartzGeometryBuilder() override = default;

  void BeginFigure(const Point &point) override {
    actions_.push_back(
        std::make_unique<details::GeometryBeginFigureAction>(point));
  }
  void CloseFigure(bool close) override {
    actions_.push_back(
        std::make_unique<details::GeometryCloseFigureAction>(close));
  }
  void LineTo(const Point &point) override {
    actions_.push_back(std::make_unique<details::GeometryLineToAction>(point));
  }
  void QuadraticBezierTo(const Point &control_point,
                         const Point &end_point) override {
    actions_.push_back(
        std::make_unique<details::GeometryQuadraticBezierToAction>(
            control_point, end_point));
  }

  std::unique_ptr<IGeometry> Build() override;

 private:
  CGContextRef cg_context_;

  std::vector<std::unique_ptr<Object>> actions_;
};
}  // namespace cru::platform::graphics::osx::quartz