aboutsummaryrefslogtreecommitdiff
path: root/src/win/graphics/direct/Painter.cpp
blob: 395b1d905fc7f3cf9a607f3a99b0e94e98d98580 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "cru/win/graphics/direct/Painter.hpp"

#include "cru/platform/Check.hpp"
#include "cru/win/graphics/direct/Brush.hpp"
#include "cru/win/graphics/direct/ConvertUtil.hpp"
#include "cru/win/graphics/direct/Exception.hpp"
#include "cru/win/graphics/direct/Geometry.hpp"
#include "cru/win/graphics/direct/Image.hpp"
#include "cru/win/graphics/direct/TextLayout.hpp"

#include <type_traits>

namespace cru::platform::graphics::win::direct {
D2DDeviceContextPainter::D2DDeviceContextPainter(
    ID2D1DeviceContext1* device_context) {
  Expects(device_context);
  device_context_ = device_context;
}

platform::Matrix D2DDeviceContextPainter::GetTransform() {
  CheckValidation();
  D2D1_MATRIX_3X2_F m;
  device_context_->GetTransform(&m);
  return Convert(m);
}

void D2DDeviceContextPainter::SetTransform(const platform::Matrix& matrix) {
  CheckValidation();
  device_context_->SetTransform(Convert(matrix));
}

void D2DDeviceContextPainter::ConcatTransform(const Matrix& matrix) {
  SetTransform(GetTransform() * matrix);
}

void D2DDeviceContextPainter::Clear(const Color& color) {
  CheckValidation();
  device_context_->Clear(Convert(color));
}

void D2DDeviceContextPainter::DrawLine(const Point& start, const Point& end,
                                       IBrush* brush, float width) {
  CheckValidation();
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->DrawLine(Convert(start), Convert(end),
                            b->GetD2DBrushInterface(), width);
}

void D2DDeviceContextPainter::StrokeRectangle(const Rect& rectangle,
                                              IBrush* brush, float width) {
  CheckValidation();
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(),
                                 width);
}

void D2DDeviceContextPainter::FillRectangle(const Rect& rectangle,
                                            IBrush* brush) {
  CheckValidation();
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface());
}

void D2DDeviceContextPainter::StrokeEllipse(const Rect& outline_rect,
                                            IBrush* brush, float width) {
  CheckValidation();
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->DrawEllipse(
      D2D1::Ellipse(Convert(outline_rect.GetCenter()),
                    outline_rect.width / 2.0f, outline_rect.height / 2.0f),
      b->GetD2DBrushInterface(), width);
}
void D2DDeviceContextPainter::FillEllipse(const Rect& outline_rect,
                                          IBrush* brush) {
  CheckValidation();
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->FillEllipse(
      D2D1::Ellipse(Convert(outline_rect.GetCenter()),
                    outline_rect.width / 2.0f, outline_rect.height / 2.0f),
      b->GetD2DBrushInterface());
}

void D2DDeviceContextPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush,
                                             float width) {
  CheckValidation();
  const auto g = CheckPlatform<D2DGeometry>(geometry, GetPlatformId());
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(),
                                width);
}

void D2DDeviceContextPainter::FillGeometry(IGeometry* geometry, IBrush* brush) {
  CheckValidation();
  const auto g = CheckPlatform<D2DGeometry>(geometry, GetPlatformId());
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->FillGeometry(g->GetComInterface(),
                                b->GetD2DBrushInterface());
}

void D2DDeviceContextPainter::DrawText(const Point& offset,
                                       ITextLayout* text_layout,
                                       IBrush* brush) {
  CheckValidation();
  const auto t = CheckPlatform<DWriteTextLayout>(text_layout, GetPlatformId());
  const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
  device_context_->DrawTextLayout(Convert(offset), t->GetComInterface(),
                                  b->GetD2DBrushInterface());
}

void D2DDeviceContextPainter::DrawImage(const Point& offset, IImage* image) {
  CheckValidation();
  const auto i = CheckPlatform<Direct2DImage>(image, GetPlatformId());

  Microsoft::WRL::ComPtr<ID2D1DeviceContext> device_context;

  device_context_->QueryInterface(device_context.GetAddressOf());
  device_context->DrawImage(i->GetD2DBitmap().Get(), Convert(offset));
}

void D2DDeviceContextPainter::PushLayer(const Rect& bounds) {
  CheckValidation();

  Microsoft::WRL::ComPtr<ID2D1Layer> layer;
  ThrowIfFailed(device_context_->CreateLayer(&layer));

  device_context_->PushLayer(D2D1::LayerParameters(Convert(bounds)),
                             layer.Get());

  layers_.push_back(std::move(layer));
}

void D2DDeviceContextPainter::PopLayer() {
  device_context_->PopLayer();
  layers_.pop_back();
}

void D2DDeviceContextPainter::PushState() {
  Microsoft::WRL::ComPtr<ID2D1Factory> factory = nullptr;
  device_context_->GetFactory(&factory);

  Microsoft::WRL::ComPtr<ID2D1DrawingStateBlock> state_block;
  factory->CreateDrawingStateBlock(&state_block);
  device_context_->SaveDrawingState(state_block.Get());

  drawing_state_stack_.push_back(std::move(state_block));
}

void D2DDeviceContextPainter::PopState() {
  Expects(!drawing_state_stack_.empty());
  auto drawing_state = drawing_state_stack_.back();
  drawing_state_stack_.pop_back();
  device_context_->RestoreDrawingState(drawing_state.Get());
}

void D2DDeviceContextPainter::EndDraw() {
  if (is_drawing_) {
    is_drawing_ = false;
    DoEndDraw();
  }
}

void D2DDeviceContextPainter::CheckValidation() {
  if (!is_drawing_) {
    throw cru::platform::ReuseException(
        u"Can't do that on painter after end drawing.");
  }
}
}  // namespace cru::platform::graphics::win::direct