forked from lackhole/MenuAndControls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildView.cpp
More file actions
488 lines (384 loc) · 13.4 KB
/
ChildView.cpp
File metadata and controls
488 lines (384 loc) · 13.4 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// ChildView.cpp: CChildView 클래스의 구현
//
#include "pch.h"
#include "framework.h"
#include "MenuAndControls.h"
#include "ChildView.h"
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <string>
#include <sstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
namespace {
CString GetSystemTimeAndDate() {
return CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S");
}
/*
\brief: 원을 그립니다
\dc: 디바이스 컨텍스트
\center: 원의 중심 좌표
\radius: 원의 반지름
\color: 도형의 내부 색
\thickness: 테두리 두께
\color_line: 테두리의 색
*/
void Circle(CDC* dc,
CPoint center, int radius, COLORREF color,
int thickness = 1, COLORREF color_line = RGB(0, 0, 0))
{
// 새로운 펜 객체 사용
CPen pen(thickness <= 0 ? PS_NULL : PS_SOLID, thickness, color_line);
auto pen_prev = dc->SelectObject(&pen);
// 새로운 브러쉬(채우기) 객체 사용
CBrush brush(color);
CBrush* prev_brush = dc->SelectObject(&brush);
// 원(타원) 그리기
dc->Ellipse(center.x - radius, center.y - radius,
center.x + radius, center.y + radius);
// 사용하는 펜과 객체 도구를 원래대로 되돌리기
dc->SelectObject(prev_brush);
dc->SelectObject(pen_prev);
}
/*
\brief: 직사각형을 그립니다
\dc: 디바이스 컨텍스트
\rect: 직사각형의 좌표
\color: 도형의 내부 색
\thickness: 테두리 두께
\color_line: 테두리의 색
*/
void Rectangle(CDC* dc,
CRect rect, COLORREF color,
int thickness = 1, COLORREF color_line = RGB(0, 0, 0)) {
CPen pen(thickness <= 0 ? PS_NULL : PS_SOLID, thickness, color_line);
auto pen_prev = dc->SelectObject(&pen);
CBrush brush(color);
CBrush* prev_brush = dc->SelectObject(&brush);
dc->Rectangle(rect.left, rect.top, rect.right, rect.bottom);
dc->SelectObject(prev_brush);
dc->SelectObject(pen_prev);
}
/*
\brief: 직사각형을 그립니다
\dc: 디바이스 컨텍스트
\top_left: 직사각형의 왼쪽 위의 좌표
\bottom_right: 직사각형의 오른쪽 아래의 좌표
\color: 도형의 내부 색
\thickness: 테두리 두께
\color_line: 테두리의 색
*/
void Rectangle(CDC* dc,
CPoint top_left, CPoint bottom_right, COLORREF color,
int thickness = 1, COLORREF color_line = RGB(0, 0, 0))
{
Rectangle(dc, {top_left, bottom_right}, color, thickness, color_line);
}
/*
\brief: 직선을 그립니다
\dc: 디바이스 컨텍스트
\start: 직선의 시작점 좌표
\end: 직선의 끝점 좌표
\thickness: 직선의 두께
\color_line: 직선의 색
*/
void Line(CDC* dc,
CPoint start, CPoint end, int thickness = 1, COLORREF color = RGB(0, 0, 0)) {
CPen pen(PS_SOLID, thickness, color);
auto pen_prev = dc->SelectObject(&pen);
dc->MoveTo(start);
dc->LineTo(end);
dc->SelectObject(pen_prev);
}
/*
\brief: 다각형을 그립니다
\dc: 디바이스 컨텍스트
\points: 다각형의 점 좌표들
\color: 도형의 내부 색
\thickness: 테두리 두께
\color_line: 테두리의 색
*/
void Polygon(CDC* dc,
const std::vector<CPoint>& points, COLORREF color,
int thickness = 1, COLORREF color_line = RGB(0, 0, 0)) {
CPen pen(thickness <= 0 ? PS_NULL : PS_SOLID, thickness, color_line);
CBrush brush(color);
CPen* pen_prev = dc->SelectObject(&pen);
CBrush* prev_brush = dc->SelectObject(&brush);
dc->Polygon(points.data(), points.size());
dc->SelectObject(prev_brush);
dc->SelectObject(pen_prev);
}
} // anonymous namespace
// CChildView
CChildView::CChildView()
{
}
CChildView::~CChildView()
{
}
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
ON_WM_CONTEXTMENU()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDBLCLK()
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
ON_WM_MBUTTONDBLCLK()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_COMMAND(ID_SELECTAREA, &CChildView::OnSelectArea)
ON_UPDATE_COMMAND_UI(ID_SELECTAREA, &CChildView::OnUpdateSelectarea)
ON_COMMAND(ID_DRAW_LINE, &CChildView::OnDrawLine)
ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, &CChildView::OnUpdateDrawLine)
ON_COMMAND(ID_REMOVE_SELECTED, &CChildView::OnRemoveSelected)
ON_COMMAND(ID_DRAW_RECTANGLE, &CChildView::OnDrawRectangle)
ON_UPDATE_COMMAND_UI(ID_DRAW_RECTANGLE, &CChildView::OnUpdateDrawRectangle)
ON_COMMAND(ID_DRAW_CIRCLE, &CChildView::OnDrawCircle)
ON_UPDATE_COMMAND_UI(ID_DRAW_CIRCLE, &CChildView::OnUpdateDrawCircle)
ON_COMMAND(ID_DRAW_CURVE, &CChildView::OnDrawCurve)
ON_UPDATE_COMMAND_UI(ID_DRAW_CURVE, &CChildView::OnUpdateDrawCurve)
ON_WM_KEYDOWN()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CChildView 메시지 처리기
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(nullptr, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), nullptr);
return TRUE;
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // 그리기를 위한 디바이스 컨텍스트입니다.
// Rought double buffering
CRect rect;
GetClientRect(&rect);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
memDC.SelectObject(&bitmap);
memDC.Rectangle(0, 0, rect.Width(), rect.Height());
OnMyPaint(&memDC);
dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC,
0, 0, SRCCOPY);
}
afx_msg void CChildView::OnMyPaint(CDC* dc) {
// 현재 시간 표시
dc->TextOutW(10, 10, m_current_time);
// 마우스 위치 표시
std::string pos =
"(" + std::to_string(m_mouse_pos.x) + ", "
+ std::to_string(m_mouse_pos.y) + ")";
dc->TextOut(10, 30, CString(pos.c_str()));
// 마우스 이벤트 표시
dc->TextOut(10, 50, _T("Event: ") + m_mouse_event);
dc->TextOut(10, 70, _T("Keyboard: ") + CString(std::to_string(m_keyboard).c_str()));
// 튀기는 공 그리기
Circle(dc, m_ball_pos, m_ball_radius, RGB(0, 255, 255));
// 고무 벽 그리기
Rectangle(dc, m_wall_rect, RGB(255, 255, 0));
// 직선 그리기
Line(dc, {100, 100}, {200, 300});
// 폴리곤 그리기
Polygon(dc, {{300, 100}, {300, 50}, {250, 75}, {250, 100}}, RGB(255, 0, 255));
}
void CChildView::OnContextMenu(CWnd* pWnd, CPoint point)
{
/*
CMenu* pMenu = AfxGetMainWnd()->GetMenu()->GetSubMenu(2);
if(pMenu != NULL)
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, point.x, point.y,
AfxGetMainWnd());
*/
}
void CChildView::CalculateBall() {
m_ball_pos.x += m_ball_velocity.x;
m_ball_pos.y += m_ball_velocity.y;
// 화면 바깥을 넘어갔는지 검사
{
CRect rect;
GetClientRect(&rect);
if (m_ball_pos.x + m_ball_radius >= rect.Width()) {
m_ball_pos.x -= m_ball_pos.x + m_ball_radius - rect.Width();
m_ball_velocity.x *= -1;
} else if (m_ball_pos.x - m_ball_radius < 0) {
m_ball_pos.x = -(m_ball_pos.x - m_ball_radius);
m_ball_velocity.x *= -1;
}
if (m_ball_pos.y + m_ball_radius >= rect.Height()) {
m_ball_pos.y -= m_ball_pos.y + m_ball_radius - rect.Height();
m_ball_velocity.y *= -1;
} else if (m_ball_pos.y - m_ball_radius < 0) {
m_ball_pos.y = -(m_ball_pos.y - m_ball_radius);
m_ball_velocity.y *= -1;
}
}
// 고무벽과 충돌했는지 검사 (rough)
CRect collision;
if (collision.IntersectRect(CRect(m_ball_pos, SIZE{ m_ball_radius, m_ball_radius }), m_wall_rect)) {
auto ball_xl = m_ball_pos.x - m_ball_radius;
auto ball_xr = m_ball_pos.x + m_ball_radius;
if (ball_xl <= m_wall_rect.left) {
m_ball_pos.x -= 2 * (ball_xr - m_wall_rect.left);
m_ball_velocity.x *= -1;
} else if (ball_xr >= m_wall_rect.right) {
m_ball_pos.x += 2 * (m_wall_rect.right - ball_xl);
m_ball_velocity.x *= -1;
}
}
}
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 시계 타이머 설정(함수 호출 주기 설정)
SetTimer(kTimerClock, /* ms */ 1000, nullptr);
m_timer_event_listeners.Add(kTimerClock, [this](){ m_current_time = GetSystemTimeAndDate(); });
// 공 튀기기 타이머 설정
SetTimer(kTimerPhysics, /* ms */ 10, nullptr);
m_timer_event_listeners.Add(kTimerPhysics, [this]() { CalculateBall(); });
// 마우스 이동 이벤트 리스너 추가
m_mouse_event_listeners.Add(kMouseMove, [this](auto, auto p) { m_mouse_pos = p; });
m_mouse_event_listeners.Add(kMouseMove, [this](auto, auto p) {
auto w = m_wall_rect.Width();
auto h = m_wall_rect.Height();
m_wall_rect.left = p.x - w / 2;
m_wall_rect.right = p.x + w / 2;
m_wall_rect.top = p.y - h / 2;
m_wall_rect.bottom = p.y + h / 2;
});
// 마우스 클릭 이벤트 리스너
m_mouse_event_listeners.Add(kMouseLButtonDown, [this](auto, auto p) { m_mouse_event = "LButtonDown"; });
m_mouse_event_listeners.Add(kMouseLButtonUp, [this](auto, auto p) { m_mouse_event = "LButtonUp"; });
m_mouse_event_listeners.Add(kMouseLButtonDblClk, [this](auto, auto p) { m_mouse_event = "LButtonDblClk"; });
m_mouse_event_listeners.Add(kMouseRButtonDown, [this](auto, auto p) { m_mouse_event = "RButtonDown"; });
m_mouse_event_listeners.Add(kMouseRButtonUp, [this](auto, auto p) { m_mouse_event = "RButtonUp"; });
m_mouse_event_listeners.Add(kMouseRButtonDblClk, [this](auto, auto p) { m_mouse_event = "RButtonDblClk"; });
m_mouse_event_listeners.Add(kMouseMButtonDown, [this](auto, auto p) { m_mouse_event = "MButtonDown"; });
m_mouse_event_listeners.Add(kMouseMButtonUp, [this](auto, auto p) { m_mouse_event = "MButtonUp"; });
m_mouse_event_listeners.Add(kMouseMButtonDblClk, [this](auto, auto p) { m_mouse_event = "MButtonDblClk"; });
// 우클릭 시 그리기 모드 취소
m_mouse_event_listeners.Add(kMouseRButtonDown, [this](auto, auto p) { m_toolbar_mode = kToolbarNone; });
// ESC 시 그리기 모드 취소
m_keyboard_listeners.Add(VK_ESCAPE, [this](...) { m_toolbar_mode = kToolbarNone; });
return 0;
}
void CChildView::OnMouseMove(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseMove, nFlags, point);
CWnd::Invalidate();
CWnd::OnMouseMove(nFlags, point);
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseLButtonDown, nFlags, point);
CWnd::SetCapture();
CWnd::Invalidate();
CWnd::OnLButtonDown(nFlags, point);
}
void CChildView::OnLButtonUp(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseLButtonUp, nFlags, point);
ReleaseCapture();
CWnd::Invalidate();
CWnd::OnLButtonUp(nFlags, point);
}
void CChildView::OnLButtonDblClk(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseLButtonDblClk, nFlags, point);
CWnd::Invalidate();
CWnd::OnLButtonDblClk(nFlags, point);
}
void CChildView::OnRButtonDown(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseRButtonDown, nFlags, point);
CWnd::SetCapture();
CWnd::Invalidate();
CWnd::OnRButtonDown(nFlags, point);
}
void CChildView::OnRButtonUp(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseRButtonUp, nFlags, point);
ReleaseCapture();
CWnd::Invalidate();
CWnd::OnRButtonUp(nFlags, point);
}
void CChildView::OnRButtonDblClk(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseRButtonDblClk, nFlags, point);
CWnd::Invalidate();
CWnd::OnRButtonDblClk(nFlags, point);
}
void CChildView::OnMButtonDown(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseMButtonDown, nFlags, point);
CWnd::SetCapture();
CWnd::Invalidate();
CWnd::OnMButtonDown(nFlags, point);
}
void CChildView::OnMButtonUp(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseMButtonUp, nFlags, point);
ReleaseCapture();
CWnd::Invalidate();
CWnd::OnMButtonUp(nFlags, point);
}
void CChildView::OnMButtonDblClk(UINT nFlags, CPoint point) {
m_mouse_event_listeners(kMouseMButtonDblClk, nFlags, point);
CWnd::Invalidate();
CWnd::OnMButtonDblClk(nFlags, point);
}
void CChildView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
m_keyboard_listeners(nChar, nRepCnt, nFlags);
m_keyboard = nChar;
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CChildView::OnTimer(UINT_PTR nIDEvent) {
m_timer_event_listeners(static_cast<TimerEvent>(nIDEvent));
CWnd::Invalidate();
CWnd::OnTimer(nIDEvent);
}
//
void CChildView::OnSelectArea() {
m_toolbar_mode = (m_toolbar_mode == kToolbarSelectArea ? kToolbarNone : kToolbarSelectArea);
}
void CChildView::OnUpdateSelectarea(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_toolbar_mode == kToolbarSelectArea);
}
void CChildView::OnDrawLine() {
m_toolbar_mode = (m_toolbar_mode == kToolbarDrawLine ? kToolbarNone : kToolbarDrawLine);
}
void CChildView::OnUpdateDrawLine(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_toolbar_mode == kToolbarDrawLine);
}
void CChildView::OnDrawCurve() {
m_toolbar_mode = (m_toolbar_mode == kToolbarDrawCurve ? kToolbarNone : kToolbarDrawCurve);
}
void CChildView::OnUpdateDrawCurve(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_toolbar_mode == kToolbarDrawCurve);
}
void CChildView::OnRemoveSelected() {
// TODO: 여기에 명령 처리기 코드를 추가합니다.
}
void CChildView::OnDrawRectangle() {
m_toolbar_mode = (m_toolbar_mode == kToolbarDrawRectangle ? kToolbarNone : kToolbarDrawRectangle);
}
void CChildView::OnUpdateDrawRectangle(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_toolbar_mode == kToolbarDrawRectangle);
}
void CChildView::OnDrawCircle() {
m_toolbar_mode = (m_toolbar_mode == kToolbarDrawCircle? kToolbarNone : kToolbarDrawCircle);
}
void CChildView::OnUpdateDrawCircle(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_toolbar_mode == kToolbarDrawCircle);
}
BOOL CChildView::OnEraseBkgnd(CDC* pDC) {
return TRUE;
// return CWnd::OnEraseBkgnd(pDC);
}