aboutsummaryrefslogtreecommitdiff
path: root/CruUI/graph/graph.h
blob: 69d11b9c59bfb03a2c424401ee7e7ab92985367f (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
#pragma once

#include "system_headers.h"
#include <memory>

#include "base.h"


namespace cru
{
    namespace graph
	{
        class GraphManager;

        //Represents a window render target.
        class WindowRenderTarget : public Object
		{
        public:
            WindowRenderTarget(GraphManager* graph_manager, HWND hwnd);
            WindowRenderTarget(const WindowRenderTarget& other) = delete;
            WindowRenderTarget(WindowRenderTarget&& other) = delete;
            WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete;
            WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete;
            ~WindowRenderTarget() override;

        public:
            //Get the graph manager that created the render target.
            GraphManager* GetGraphManager() const
            {
                return graph_manager_;
            }

            //Get the d2d device context.
            inline Microsoft::WRL::ComPtr<ID2D1DeviceContext> GetD2DDeviceContext() const;

            //Get the target bitmap which can be set as the ID2D1DeviceContext's target.
            Microsoft::WRL::ComPtr<ID2D1Bitmap1> GetTargetBitmap() const
            {
                return target_bitmap_;
            }

            //Resize the underlying buffer.
            void ResizeBuffer(int width, int height);

            //Set this render target as the d2d device context's target.
            void SetAsTarget();

            //Present the data of the underlying buffer to the window.
            void Present();

        private:
            void CreateTargetBitmap();

        private:
            GraphManager* graph_manager_;
            Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_;
            Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_;
        };

        struct Dpi
		{
            float x;
            float y;
        };

        class GraphManager : public Object
		{
        public:
            GraphManager();
            GraphManager(const GraphManager& other) = delete;
            GraphManager(GraphManager&& other) = delete;
            GraphManager& operator=(const GraphManager& other) = delete;
            GraphManager& operator=(GraphManager&& other) = delete;
            ~GraphManager() override;

        public:
            Microsoft::WRL::ComPtr<ID2D1Factory1> GetD2D1Factory() const
            {
                return d2d1_factory_;
            }

            Microsoft::WRL::ComPtr<ID2D1DeviceContext> GetD2D1DeviceContext() const
            {
                return d2d1_device_context_;
            }

            Microsoft::WRL::ComPtr<ID3D11Device> GetD3D11Device() const
            {
                return d3d11_device_;
            }

            Microsoft::WRL::ComPtr<IDXGIFactory2> GetDxgiFactory() const
            {
                return dxgi_factory_;
            }

            //Create a window render target with the HWND.
            std::shared_ptr<WindowRenderTarget> CreateWindowRenderTarget(HWND hwnd);

            //Get the desktop dpi.
            Dpi GetDpi();

            //Reload system metrics including desktop dpi.
            void ReloadSystemMetrics();

        private:
            Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
            Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context_;
            Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_;
            Microsoft::WRL::ComPtr<ID2D1Device> d2d1_device_;
            Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_;
            Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_;
            Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_;
        };

        int DipToPixelX(float dip_x);
        int DipToPixelY(float dip_y);
        float PixelToDipX(int pixel_x);
        float PixelToDipY(int pixel_y);

        Microsoft::WRL::ComPtr<ID2D1DeviceContext> WindowRenderTarget::GetD2DDeviceContext() const
        {
            return graph_manager_->GetD2D1DeviceContext();
        }
    }
}