-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.cpp
executable file
·157 lines (121 loc) · 3.68 KB
/
canvas.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
150
151
152
153
154
155
156
157
/*
Custom Canvas
*/
#include "canvas.h"
#include "font.h"
Canvas::Canvas(wxDC * dc, int w, int h)
{
m_DC = dc;
m_Size[0] = w;
m_Size[1] = h;
}
Canvas::~Canvas()
{
}
void Canvas::Clear(unsigned long color)
{
m_DC->SetBackground(wxBrush(wxColor((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF)));
m_DC->Clear();
}
void Canvas::Print(int x, int y, const wchar_t * text, unsigned long color, bool rightalign, int fontsize,int clipwidth, int clipheight)
{
if (x > (rightalign == true ? m_Size[0] + 100 : m_Size[0]) || y > m_Size[1] || y + fontsize*2 < 0)
return ;
// y -=2;
wxFont * f = CFont::GetInstance()->getFont(fontsize);
m_DC->SetFont(*f);
m_DC->SetTextForeground(wxColor((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF));
if (rightalign == false)
{
if( clipwidth != -1 && clipheight != -1 )
{
m_DC->SetClippingRegion(x,y,clipwidth, clipheight);
}
m_DC->DrawText(text, x, y+2);
m_DC->DestroyClippingRegion();
}
else
{
UINT mode = ::GetTextAlign((HDC)m_DC->GetHDC());
::SetTextAlign((HDC)m_DC->GetHDC(), TA_RIGHT);
m_DC->DrawText(text, x, y+2);
::SetTextAlign((HDC)m_DC->GetHDC(), mode);
}
}
wxSize Canvas::GetTextSize(const wchar_t * text, int fontsize)
{
wxFont * f = CFont::GetInstance()->getFont(fontsize);
m_DC->SetFont(*f);
wxSize size = m_DC->GetTextExtent(text);
return size;
}
void Canvas::DrawRect(int x, int y, int w, int h, unsigned long color, bool outline)
{
if (x + w < 0 || x > m_Size[0] || y + h < 0 || y > m_Size[1])
return ;
if (outline == true)
{
m_DC->SetPen(wxPen(wxColor((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF)));
m_DC->SetBrush(wxBrush(*wxRED, wxTRANSPARENT));
m_DC->DrawRectangle(x, y, w, h);
}
else
{
m_DC->SetPen(wxPen(wxColor((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF, (color>>24)&0xFF)));
m_DC->SetBrush(wxBrush(wxColor((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF, (color>>24)&0xFF)));
m_DC->DrawRectangle(x, y, w, h);
}
}
void Canvas::DrawRectClip(int x, int y, int w, int h, unsigned long color, bool outline,int clipwidth, int clipheight)
{
m_DC->SetClippingRegion(x,y,clipwidth, clipheight);
DrawRect(x, y, w, h, color, outline);
m_DC->DestroyClippingRegion();
}
void Canvas::DrawLine(int x1, int y1, int x2, int y2, unsigned long color, int width)
{
if ((x1 < 0 && x2 < 0) || (x1 > m_Size[0] && x2 > m_Size[0]) ||
(y1 < 0 && y2 < 0) || (y1 > m_Size[1] && y2 > m_Size[1]))
return ;
wxPen pen(wxColor((color>>16)&0xFF, (color>>8)&0xFF, color&0xFF));
pen.SetWidth(width);
m_DC->SetPen(pen);
m_DC->DrawLine(x1, y1, x2, y2);
}
void Canvas::DrawBitmap(int x, int y, wxBitmap &bitmap, bool usealpha)
{
m_DC->DrawBitmap(bitmap, x, y, usealpha);
}
void Canvas::DrawImage(int x, int y, wxBitmap &bitmap, bool balance)
{
wxMemoryDC memDC;
memDC.SelectObject( bitmap );
int imagew = bitmap.GetWidth();
int imageh = bitmap.GetHeight();
float neww = IMAGEVIEW_X;
float newh = IMAGEVIEW_Y;
float newx = x;
float newy = y;
if( imagew > imageh )
{
neww = IMAGEVIEW_X - 1;
newh = (float)imageh/(float)imagew * IMAGEVIEW_Y;
newy = y + ((float)IMAGEVIEW_Y - newh) / 2.f;
}
else if( imagew < imageh )
{
neww = (float)imagew/(float)imageh * IMAGEVIEW_X;
newh = IMAGEVIEW_Y - 1;
newx = x + ((float)IMAGEVIEW_X - neww) / 2.f;
}
m_DC->StretchBlit(newx , newy, neww, newh, &memDC, 0, 0, imagew, imageh, wxCOPY, true);
DrawRect(newx , newy, neww, newh, 0xff00ffff, true);
}
void Canvas::SetClipping(int x, int y, int w, int h)
{
m_DC->SetClippingRegion(wxPoint(x,y), wxSize(w,h));
}
void Canvas::ClearClipping()
{
m_DC->DestroyClippingRegion();
}