aboutsummaryrefslogtreecommitdiff
path: root/src/win/graph/win_painter.cpp
blob: f75cf4e066417ecc0fd51c5801b5d5acc5984077 (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
#include "cru/win/graph/win_painter.hpp"

#include "cru/win/exception.hpp"
#include "cru/win/graph/win_native_factory.hpp"
#include "cru/win/graph/util/convert_util.hpp"
#include "cru/win/graph/win_brush.hpp"
#include "cru/win/graph/win_geometry.hpp"
#include "cru/win/graph/win_text_layout.hpp"

#include <cassert>

namespace cru::win::graph {
WinPainter::WinPainter(ID2D1RenderTarget* render_target) {
  assert(render_target);
  render_target_ = render_target;
}

platform::Matrix WinPainter::GetTransform() {
  assert(!IsEnded());
  D2D1_MATRIX_3X2_F m;
  render_target_->GetTransform(&m);
  return util::Convert(m);
}

void WinPainter::SetTransform(const platform::Matrix& matrix) {
  assert(!IsEnded());
  render_target_->SetTransform(util::Convert(matrix));
}

void WinPainter::Clear(const ui::Color& color) {
  assert(!IsEnded());
  render_target_->Clear(util::Convert(color));
}

void WinPainter::StrokeRectangle(const ui::Rect& rectangle,
                                 platform::graph::IBrush* brush, float width) {
  assert(!IsEnded());
  const auto b = dynamic_cast<IWinBrush*>(brush);
  assert(b);
  render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(),
                                width);
}

void WinPainter::FillRectangle(const ui::Rect& rectangle,
                               platform::graph::IBrush* brush) {
  assert(!IsEnded());
  const auto b = dynamic_cast<IWinBrush*>(brush);
  assert(b);
  render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush());
}

void WinPainter::StrokeGeometry(platform::graph::IGeometry* geometry,
                                platform::graph::IBrush* brush, float width) {
  assert(!IsEnded());
  const auto g = dynamic_cast<WinGeometry*>(geometry);
  assert(g);
  const auto b = dynamic_cast<IWinBrush*>(brush);
  assert(b);

  render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width);
}

void WinPainter::FillGeometry(platform::graph::IGeometry* geometry,
                              platform::graph::IBrush* brush) {
  assert(!IsEnded());
  const auto g = dynamic_cast<WinGeometry*>(geometry);
  assert(g);
  const auto b = dynamic_cast<IWinBrush*>(brush);
  assert(b);

  render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush());
}

void WinPainter::DrawText(const ui::Point& offset,
                          platform::graph::ITextLayout* text_layout,
                          platform::graph::IBrush* brush) {
  assert(!IsEnded());
  const auto t = dynamic_cast<WinTextLayout*>(text_layout);
  assert(t);
  const auto b = dynamic_cast<IWinBrush*>(brush);
  assert(b);

  render_target_->DrawTextLayout(util::Convert(offset),
                                 t->GetDWriteTextLayout(), b->GetD2DBrush());
}

void WinPainter::End() {
  if (!is_draw_ended_) {
    is_draw_ended_ = true;
    DoEndDraw();
  }
}
}  // namespace cru::win::graph