From 79d1d76509dbf6cf9c79f8eb55968535982975aa Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 Mar 2019 19:06:17 +0800 Subject: ... --- src/graph/graph_util.hpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/graph/graph_util.hpp (limited to 'src/graph/graph_util.hpp') diff --git a/src/graph/graph_util.hpp b/src/graph/graph_util.hpp new file mode 100644 index 00000000..2d5be5f3 --- /dev/null +++ b/src/graph/graph_util.hpp @@ -0,0 +1,63 @@ +#pragma once +#include "pre.hpp" + +#include +#include + +#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(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(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& 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 -- cgit v1.2.3