aboutsummaryrefslogtreecommitdiff
path: root/src/graph/graph_util.hpp
blob: 2d5be5f3894843a81a09b6083d95c3f34c42de72 (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
#pragma once
#include "pre.hpp"

#include <d2d1_1.h>
#include <functional>

#include "exception.hpp"
#include "graph_manager.hpp"

namespace cru::graph {
struct Dpi {
  float x;
  float y;
};

inline Dpi GetDpi() {
  Dpi dpi;
  GraphManager::GetInstance()->GetD2D1Factory()->GetDesktopDpi(&dpi.x, &dpi.y);
  return dpi;
}

inline int DipToPixelInternal(const float dip, const float dpi) {
  return static_cast<int>(dip * dpi / 96.0f);
}

inline int DipToPixelX(const float dip_x) {
  return DipToPixelInternal(dip_x, GetDpi().x);
}

inline int DipToPixelY(const float dip_y) {
  return DipToPixelInternal(dip_y, GetDpi().y);
}

inline float DipToPixelInternal(const int pixel, const float dpi) {
  return static_cast<float>(pixel) * 96.0f / dpi;
}

inline float PixelToDipX(const int pixel_x) {
  return DipToPixelInternal(pixel_x, GetDpi().x);
}

inline float PixelToDipY(const int pixel_y) {
  return DipToPixelInternal(pixel_y, GetDpi().y);
}

inline void WithTransform(
    ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F matrix,
    const std::function<void(ID2D1RenderTarget*)>& action) {
  D2D1_MATRIX_3X2_F old_transform;
  render_target->GetTransform(&old_transform);
  render_target->SetTransform(old_transform * matrix);
  action(render_target);
  render_target->SetTransform(old_transform);
}

inline ID2D1SolidColorBrush* CreateSolidColorBrush(const D2D1_COLOR_F& color) {
  ID2D1SolidColorBrush* brush;
  ThrowIfFailed(GraphManager::GetInstance()
                    ->GetD2D1DeviceContext()
                    ->CreateSolidColorBrush(color, &brush));
  return brush;
}
}  // namespace cru::graph