-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrawer.cpp
149 lines (134 loc) · 3.44 KB
/
drawer.cpp
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
#include "stdafx.h"
#include "Drawer.h"
using namespace std;
namespace Denisenko {
namespace Raskroy {
Drawer::Drawer(void)
: m_hwnd(0), m_hdc(0)
{
CreatePensBrushes();
}
Drawer::Drawer(HWND hwnd)
: m_hwnd(hwnd)
{
// own DC
assert(IsWindow(hwnd));
m_hdc = GetDC(hwnd);
assert(m_hdc);
CreatePensBrushes();
}
Drawer::Drawer(HDC hdc)
: m_hwnd(0), m_hdc(hdc)
{
// don't own DC
assert(hdc);
CreatePensBrushes();
}
Drawer::~Drawer(void)
{
IfOwnDCThanRelease();
BOOL bres;
bres = DeleteObject(m_hwhitebrush);
assert(bres);
bres = DeleteObject(m_hhatchbrush);
assert(bres);
bres = DeleteObject(m_hredpen);
assert(bres);
bres = DeleteObject(m_hblackpen);
assert(bres);
}
void Drawer::CreatePensBrushes(void)
{
m_hblackpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
assert(m_hblackpen);
m_hredpen = CreatePen(PS_DASH, 1, RGB(255, 0, 0));
assert(m_hredpen);
LOGBRUSH logbrush;
logbrush.lbStyle = BS_HATCHED;
logbrush.lbColor = RGB(255, 0, 0);
logbrush.lbHatch = HS_BDIAGONAL;
m_hhatchbrush = CreateBrushIndirect(&logbrush);
assert(m_hhatchbrush);
logbrush.lbStyle = BS_SOLID;
logbrush.lbColor = RGB(255, 255, 255);
m_hwhitebrush = CreateBrushIndirect(&logbrush);
assert(m_hwhitebrush);
}
void Drawer::IfOwnDCThanRelease(void)
{
if (m_hwnd) // this means owning DC
{
assert(IsWindow(m_hwnd));
int ires = ReleaseDC(m_hwnd, m_hdc);
assert(ires);
}
}
void Drawer::ResetDC(HDC hdc)
{
IfOwnDCThanRelease();
m_hwnd = 0;
m_hdc = hdc;
}
void Drawer::ResetWnd(HWND hwnd)
{
IfOwnDCThanRelease();
m_hwnd = hwnd;
assert(IsWindow(hwnd));
m_hdc = GetDC(hwnd);
assert(m_hdc);
}
void Drawer::Draw(int width, int height, const ParsedParts& parts, const ParsedCuts& cuts, const Part& sheet)
{
double scalex = width / (double)sheet.rect.Size[0];
double scaley = height / (double)sheet.rect.Size[1];
double scale;
if (scalex < scaley)
scale = scalex;
else
scale = scaley;
HGDIOBJ hres = SelectObject(m_hdc, m_hblackpen);
assert(hres);
// drawing sheet
hres = SelectObject(m_hdc, m_hhatchbrush);
assert(hres != NULL);
BOOL bres = Rectangle(m_hdc, 0, 0, int(sheet.rect.Size[0] * scale),
int(sheet.rect.Size[1] * scale));
assert(bres != FALSE);
// drawing parts
hres = SelectObject(m_hdc, m_hwhitebrush);
assert(hres != NULL);
{for (ParsedParts::const_iterator i = parts.begin(); i != parts.end(); i++)
{
RECT Rect;
Rect.left = LONG(i->pos[0] * scale);
Rect.top = LONG(i->pos[1] * scale);
Rect.right = LONG((i->pos[0] + i->rect.Size[0]) * scale);
Rect.bottom = LONG((i->pos[1] + i->rect.Size[1]) * scale);
bres = Rectangle(m_hdc, Rect.left, Rect.top, Rect.right, Rect.bottom);
assert(bres != FALSE);
ostringstream ss;
ss << i->rect.Size[0] << "x" << i->rect.Size[1];
int ires = DrawTextA(m_hdc, ss.str().c_str(), ss.str().length(), &Rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
assert(ires != 0);
}}
// drawing cuts
hres = SelectObject(m_hdc, m_hredpen);
assert(hres != NULL);
{for (ParsedCuts::const_iterator i = cuts.begin(); i != cuts.end(); i++)
{
bres = MoveToEx(m_hdc, int(i->pos[0] * scale), int(i->pos[1] * scale), NULL);
assert(bres);
if (i->s)
{
bres = LineTo(m_hdc, int((i->pos[0]) * scale), int((i->pos[1] + i->length) * scale));
assert(bres);
}
else
{
bres = LineTo(m_hdc, int((i->pos[0] + i->length) * scale), int(i->pos[1] * scale));
assert(bres);
}
}}
}
} // namespace Denisenko
} // namespace Raskroy