-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cpp
213 lines (167 loc) · 4.89 KB
/
App.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
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
#include "App.h"
#include "Line.h"
#include "Rect.h"
#include "Curve.h"
App* App::_instance = nullptr;
void App::init() {
assert(_instance == nullptr);
_instance = this;
// create pen and brush..
solidBlackPen = ::CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); // black solid pen
dashRedPen = ::CreatePen(PS_DOT, 1, RGB(255, 0, 0)); // red dash pen
solidRedPen = ::CreatePen(PS_SOLID, 5, RGB(255, 0, 0)); // red solid pen
solidRedBrush = ::CreateSolidBrush(RGB(255, 0, 0)); // red solid brush
/*HMenuFileDropDown = GetSubMenu(mainMenu, 0);*/
// for testing purpose...
auto p = std::make_unique<Line>();
p->pt[0] = POINT{ 200, 200 };
p->pt[1] = POINT{ 400, 400 };
objList.emplace_back(std::move(p));
auto r = std::make_unique<Rect>();
r->corners[0] = POINT{ 500, 500 };
r->corners[3] = POINT{ 600, 600 };
r->updateCorner(RectCorner::RIGHT_TOP);
r->updateCorner(RectCorner::LEFT_BOTTOM);
objList.emplace_back(std::move(r));
}
void App::setHwnd(HWND hWnd_) {
_hWnd = hWnd_;
backBuffer.create(_hWnd);
}
void App::draw(HDC hdc_) {
backBuffer.clear();
for (const auto& p : objList) {
p->draw(backBuffer.dc());
}
if (tmpObj) {
tmpObj->draw(backBuffer.dc());
}
crossHair.draw(backBuffer.dc());
backBuffer.draw(hdc_);
}
void App::onMouseEvent(const MouseEvent& ev) {
crossHair.onMouseEvent(ev);
if (tmpObj) {
if (ev.isMove()) {
tmpObj->onMouseEvent(ev);
return;
}
if (ev.isUp()) {
if (ev.isLButton()) {
tmpObj->onMouseEvent(ev); //onEndCreateTmpObj
objList.emplace_back(std::move(tmpObj));
tmpObj = nullptr;
return;
}
}
}
else {
if (captureObj) {
captureObj->onMouseEvent(ev);
return;
}
for (auto& p : objList) {
if (p->onMouseEvent(ev)) {
return;
}
}
if (ev.isDown()) {
if (ev.isLButton()) {
using Type = AppObjectType;
switch (currentAppObjType) // how to make it better. same question: how to cast unique_ptr<AppObject> to its subType? i.e. unique_ptr<Line>
{
case Type::Line: {
auto p = std::make_unique<Line>();
p->onCreate(ev);
tmpObj = std::move(p);
} return;
case Type::Rect: {
auto p = std::make_unique<Rect>();
p->onCreate(ev);
tmpObj = std::move(p);
} return;
case Type::Curve: {
auto p = std::make_unique<Curve>();
p->onCreate(ev);
tmpObj = std::move(p);
} return;
default: {
assert(false);
} return;
}
}
}
}
}
void App::_onWin32MouseEvent(UINT msg, WPARAM wp, LPARAM lp) {
MouseEvent ev;
ev.pos = POINT{ GET_X_LPARAM(lp) , GET_Y_LPARAM(lp) };
switch (msg)
{
using B = MouseButton;
using T = MouseEventType;
case WM_LBUTTONDOWN: { ev.eventType = T::Down; ev.button = B::Left; _mouseButtonState |= B::Left; } break;
case WM_MBUTTONDOWN: { ev.eventType = T::Down; ev.button = B::Middle; _mouseButtonState |= B::Middle; } break;
case WM_RBUTTONDOWN: { ev.eventType = T::Down; ev.button = B::Right; _mouseButtonState |= B::Right; } break;
case WM_LBUTTONUP: { ev.eventType = T::Up; ev.button = B::Left; _mouseButtonState &= ~B::Left; } break;
case WM_MBUTTONUP: { ev.eventType = T::Up; ev.button = B::Middle; _mouseButtonState &= ~B::Middle; } break;
case WM_RBUTTONUP: { ev.eventType = T::Up; ev.button = B::Right; _mouseButtonState &= ~B::Right; } break;
case WM_MOUSEMOVE: { ev.eventType = T::Move; } break;
default: { assert(false); return; } break;
}
//ev.btnState // do it later
onMouseEvent(ev);
InvalidateRect(_hWnd, nullptr, false);
}
void App::setCaptureObject(AppObject* obj) {
assert(captureObj == nullptr);
captureObj = obj;
printf("[set] captureObj [%s id: %d]\n", obj->typeAsString(), obj->id);
}
void App::clearCaptureObject() {
assert(captureObj != nullptr);
printf("[clear] captureObj [%s id: %d]\n", captureObj->typeAsString(), captureObj->id);
captureObj = nullptr;
}
void App::save(const wchar_t* fpath) const {
std::ofstream of;
of.open(fpath, std::ios::out | std::ios::binary);
for (auto& obj : objList) {
obj->save(of);
}
of.close();
}
void App::load(const wchar_t* fpath) {
using Type = AppObjectType;
using Obj = AppObject;
objList.clear();
std::ifstream ifs;
ifs.open(fpath, std::ios::in | std::ios::binary);
char c;
int i = 0;
std::string objType;
while (ifs.get(c)) {
if (c == '\n' || c == ' ') continue;
if (c == ':') {
if (objType == Obj::typeAsString(Type::Line)) { Line::load(ifs); }
if (objType == Obj::typeAsString(Type::Rect)) { Rect::load(ifs); }
if (objType == Obj::typeAsString(Type::Curve)) { Curve::load(ifs); }
objType.clear();
continue;
}
else {
objType += c;
}
}
ifs.close();
}
void App::initMenu() {
assert(_hWnd);
Menu fileDropDownMenu;
fileDropDownMenu.addItem(L"Open", -1, MCMD_FILE_OPEN);
fileDropDownMenu.addItem(L"Save", -1, MCMD_FILE_SAVE);
fileDropDownMenu.addItem(L"Exit", -1, MCMD_FILE_EXIT);
menu.addItem(L"File", -1, MCMD_FILE, &fileDropDownMenu);
menu.addItem(L"About", -1, MCMD_FILE_ABOUT);
SetMenu(_hWnd, menu.hMenu);
}