-
Notifications
You must be signed in to change notification settings - Fork 0
/
XyUI1.h
231 lines (197 loc) · 6.37 KB
/
XyUI1.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
#include "resource.h"
#include<d2d1.h>
#include<d2d1_3.h>
#include<dwrite.h>
#include<Windows.h>
#include<Shlwapi.h>
#include<vector>
#include<functional>
#include<initializer_list>
#include<algorithm>
#include"xy_ctl.h"
#include"load_svgs.h"
#pragma comment(lib,"d2d1.lib")
#pragma comment(lib,"dwrite.lib")
#pragma comment(lib,"shlwapi.lib")
ID2D1DeviceContext5* context = NULL;
ID2D1Factory* factory = NULL;
IDWriteFactory* pDWriteFactory = NULL;
HWND hwnd{};
RECT rect{};
std::vector<Control>controls;
std::vector<ID2D1SvgDocument*>svgs;
int id_ = 0;
void init_x(HWND h);
void update();
void get_clk_controls(UINT message, int x, int y);
void load_controls();
void init_x(HWND h) {
hwnd = h;
GetClientRect(hwnd, &rect);
D2D1_HWND_RENDER_TARGET_PROPERTIES hwndProperties{};
hwndProperties.hwnd = hwnd;
hwndProperties.pixelSize = D2D1::SizeU(rect.right, rect.bottom);
hwndProperties.presentOptions = D2D1_PRESENT_OPTIONS_NONE;
D2D1_RENDER_TARGET_PROPERTIES properties{};
properties.dpiX = 96.0f;
properties.dpiY = 96.0f;
properties.minLevel = D2D1_FEATURE_LEVEL_DEFAULT;
properties.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE);
properties.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
properties.usage = D2D1_RENDER_TARGET_USAGE_NONE;
HRESULT hr = S_OK;
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &factory);
if (FAILED(hr)) {
return;
}
ID2D1HwndRenderTarget* rt = NULL;
hr = factory->CreateHwndRenderTarget(&properties, &hwndProperties, &rt);
if (FAILED(hr)) {
return;
}
ID2D1DeviceContext* context1 = NULL;
hr = rt->QueryInterface(__uuidof(ID2D1DeviceContext), (void**)&context1);
if (FAILED(hr)) {
return;
}
hr = context1->QueryInterface(__uuidof(ID2D1DeviceContext5), (void**)&context);
if (FAILED(hr)) {
return;
}
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown**>(&pDWriteFactory));
if (FAILED(hr)) {
return;
}
load_controls();
load_svg();
}
inline void update()
{
context->BeginDraw();
context->Clear(D2D1::ColorF(1, 1, 1));
for (auto& c : controls) {
if (c.mode == 1) {
if (c.color != NULL) {
context->DrawLine(D2D1::Point2F(c.x, c.y), D2D1::Point2F(c.x + c.width, c.y + c.height), c.color);
}
}
else if (c.mode == 3) {
D2D1_ROUNDED_RECT roundedRect = D2D1::RoundedRect(D2D1::RectF(c.x, c.y, c.x + c.width, c.y + c.height), c.radius, c.radius);
if (c.backgroundColor != NULL) {
context->FillRoundedRectangle(roundedRect, c.backgroundColor);
}
if (c.color != NULL) {
context->DrawRoundedRectangle(roundedRect, c.color);
}
}
else if (c.mode == 5) {
D2D1_ROUNDED_RECT roundedRect = D2D1::RoundedRect(D2D1::RectF(c.x, c.y, c.x + c.width, c.y + c.height), c.radius, c.radius);
if (c.backgroundColor != NULL) {
context->FillRoundedRectangle(roundedRect, c.backgroundColor);
}
if (c.color != NULL) {
context->DrawRoundedRectangle(roundedRect, c.color);
}
}
else if (c.mode == 6) {
if (c.text != NULL && c.format != NULL && c.color != NULL) {
D2D1_RECT_F layoutRect = D2D1::RectF(c.x, c.y, c.x + c.width, c.y + c.height);
context->DrawTextW(c.text, static_cast<UINT32>(wcslen(c.text)), c.format, &layoutRect, c.color);
}
}
else if (c.mode == 7) {
D2D1_ELLIPSE ellipse = D2D1::Ellipse(D2D1::Point2F(c.x + c.width, c.y + c.height), c.width, c.height);
if (c.backgroundColor != NULL) {
context->FillEllipse(ellipse, c.backgroundColor);
}
if (c.color != NULL) {
context->DrawEllipse(ellipse, c.color);
}
}
else if (c.mode == 8) {
D2D1_ELLIPSE ellipse = D2D1::Ellipse(D2D1::Point2F(c.x + c.width, c.y + c.width), c.width, c.width);
if (c.backgroundColor != NULL) {
context->FillEllipse(ellipse, c.backgroundColor);
}
if (c.color != NULL) {
context->DrawEllipse(ellipse, c.color);
}
}
else if (c.mode == 9) {
D2D1_RECT_F rect1 = D2D1::RectF(c.x, c.y, c.width + c.x, c.y + c.height);
if (c.backgroundColor != NULL) {
//context->FillRectangle(rect1, c.backgroundColor);
if (c.radius > 0) {
context->FillRoundedRectangle(D2D1::RoundedRect(rect1, c.radius, c.radius), c.backgroundColor);
}
else {
context->FillRectangle(rect1, c.backgroundColor);
}
}
if (c.color != NULL) {
//context->DrawRectangle(rect1, c.color);
if (c.radius > 0) {
context->DrawRoundedRectangle(D2D1::RoundedRect(rect1, c.radius, c.radius), c.color);
}
else {
context->DrawRectangle(rect1, c.color);
}
}
}
else if (c.mode == 10) {
//menu
if (c.svgs != NULL) {
int o = 0;
while (c.svgs[o] >= 0) {
o++;
}
for (int i = 0;i < o;i++) {
if (i == 0) {
D2D1_RECT_F backgroundRect{};
float x = c.x + c.width / 2;
float y = c.y + c.height / o / 2;
backgroundRect = D2D1::RectF(x - 16, y - 16, x + 16, y + 16);
context->FillRoundedRectangle(D2D1::RoundedRect(backgroundRect, 6, 6), c.backgroundColor);
}
D2D1_MATRIX_3X2_F matrix{};
context->GetTransform(&matrix);
matrix.dx = c.x + c.width / 2 - 8;
matrix.dy = c.y + c.height / o * (i + 1) / 2 - 8;
context->SetTransform(matrix);
context->DrawSvgDocument(svgs.at(c.svgs[i]));
matrix.dx = 0;
matrix.dy = 0;
context->SetTransform(matrix);
}
}
}
}
context->EndDraw();
}
inline void get_clk_controls(UINT message, int x, int y)
{
}
bool cmp(Control const* c, Control const* c1) {
return c1->id > c->id;
}
inline void load_controls()
{
Grid({ -1 }, { 80,-1,-21.6,-1,-10 })
.addViews({
Grid({80,-1,80},{-1})
.addView(Menu({ICON_HOME,ICON_CH,ICON_FLOW,ICON_GEAR,ICON_CA}).setRow(1)),
VStack().setColumn(1).setBackgroundColor(239,239,239),
VStack().setBackgroundColor(239,239,239).setColumn(2),
Text(L"Dashboard").setColor(0,0,0).setFontSize(30).setColumn(2).withMargin(0,15,0,0),
Grid({-1}, { -1.9,15,-1.9,15,-1 })
.addViews({
XyRectangle(190,65).setBackgroundColor(255,255,255).withRadius(7),
Text(L"$1032.43").setFontSize(19).setColor(0,0,0).setWidth(150).withMargin(15,5,0,0),
Text(L"Daily revenue").setFontSize(13).setColor(190,190,190).setWidth(150).withMargin(15,35,0,0),
XyRectangle(35,35).setBackgroundColor(236,252,238).withRadius(7).withMargin(150,15,0,0),
Text(L"+11%").setColor(93,150,93).setFontSize(9).setWidth(35).withMargin(155,25,0,0)
}).withMargin(0,80,0,0).setColumn(2)
});
std::sort(controls.begin(), controls.end());
}