-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTMainWindow.cpp
More file actions
404 lines (378 loc) · 11.4 KB
/
TMainWindow.cpp
File metadata and controls
404 lines (378 loc) · 11.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
/********************************************************************************
* Copyright (c) 2024 Pleshkov Maksim
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
********************************************************************************/
#include <vcl.h>
#pragma hdrstop
#include "TMainWindow.h"
#include "TFigEditDialog.h"
#include "Figures/figures3d.h"
//----------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//Ãëîáàëüíûé îáúåêò îñíîâíîé ôîðìû
TMainWindow *MainWindow;
//----------------------------------------------------------
//Êîíñòðóêòîð ñ ïðèâÿçêîé ê ñîçäàþùåìó îáúåêòó
__fastcall TMainWindow::TMainWindow(TComponent* Owner)
: TForm(Owner) {}
//Çàïîëíèòü ñïèñîê ôèãóð è îïåðàöèé ïóíêòàìè
void TMainWindow::FillFigureList() {
TTreeNode *Parent, *Node;
Parent = TreeView1->Items->Add(0, "Ïëàòîíîâû òåëà");
Node = TreeView1->Items->AddChild(Parent, "Òåòðàýäð");
Node->Data = (void*)[](){
MainWindow->AddFigure(
ArrayFig::NewTetrahedron(MainWindow->glContextWeak),
"Òåòðàýäð");
};
Node = TreeView1->Items->AddChild(Parent, "Êóá");
Node->Data = (void*)[](){
MainWindow->AddFigure(
ArrayFig::NewCube(MainWindow->glContextWeak), "Êóá");
};
Node = TreeView1->Items->AddChild(Parent, "Îêòàýäð");
Node->Data = (void*)[](){
MainWindow->AddFigure(
ArrayFig::NewOctahedron(MainWindow->glContextWeak),
"Îêòàýäð");
};
Node = TreeView1->Items->AddChild(Parent, "Èêîñàýäð");
Node->Data = (void*)[](){
MainWindow->AddFigure(
ArrayFig::NewIcosahedron(MainWindow->glContextWeak),
"Èêîñàýäð");
};
Node = TreeView1->Items->AddChild(Parent, "Äîäåêàýäð");
Node->Data = (void*)[](){
MainWindow->AddFigure(
ArrayFig::NewDodecahedron(MainWindow->glContextWeak),
"Äîäåêàýäð");
};
Node = TreeView1->Items->
AddChild(Parent, "Ðîìáîäîäåêàýäð");
Node->Data = (void*)[](){
MainWindow->AddFigure(
ArrayFig::NewRhombicdodecahedron(
MainWindow->glContextWeak), "Ðîìáîäîäåêàýäð");
};
Parent = TreeView1->Items->Add(0, "Òåëà âðàùåíèÿ");
Node = TreeView1->Items->AddChild(Parent, "Êîíóñ");
Node->Data = (void*)[](){
MainWindow->AddFigure(new Cone_fig(
MainWindow->glContextWeak, 100),
"Êîíóñ");
};
Node = TreeView1->Items->AddChild(Parent, "Öèëèíäð");
Node->Data = (void*)[](){
MainWindow->AddFigure(
new Cylinder_fig(MainWindow->glContextWeak, 100),
"Öèëèíäð");
};
Node = TreeView1->Items->AddChild(Parent, "Ñôåðà (ãëîáóñ)");
Node->Data = (void*)[](){
MainWindow->AddFigure(
new UV_Sphere_fig(MainWindow->glContextWeak, 100, 50),
"Ñôåðà (ãëîáóñ)");
};
Node = TreeView1->Items->AddChild(Parent, "Áóáëèê");
Node->Data = (void*)[](){
MainWindow->AddFigure(
new Torus_fig(
MainWindow->glContextWeak, 100, 100, 1.0, 0.5),
"Áóáëèê");
};
Parent = TreeView1->Items->Add(0, "Îïåðàöèè");
Node = TreeView1->Items->AddChild(Parent, "Ñå÷åíèå");
Node->Data = (void*)[](){
if(MainWindow->glContext->SetCuttingMode()) {
MainWindow->UpdateButtons();
MainWindow->glContext->DrawFrame();
}
};
Node = TreeView1->Items->AddChild(Parent, "Çàãðóçèòü STL");
Node->Data = (void*)[](){
if(MainWindow->STLOpenDialog->Execute(
MainWindow->Handle)) {
AnsiString ansiLine(
MainWindow->STLOpenDialog->FileName.c_str());
MainWindow->AddFigure(
new STLLoadableFig(MainWindow->glContextWeak,
ansiLine.c_str()), ansiLine.c_str());
}
};
Node = TreeView1->Items->AddChild(Parent,
"Êîïèðîâàòü ôèãóðó");
Node->Data = (void*)[](){
auto fig = MainWindow->glContext->GetSelectedFig();
if(fig != nullptr)
MainWindow->AddFigure(
fig->VirtualCopy(),
AnsiString(MainWindow->ListView1->Selected->
Caption).c_str());
};
}
//Äîáàâèòü ôèãóðó â ïðîñòðàíñòâî è çàðåãèñòðèðîâàòü
//â ñïèñêå ïîä çàäàííûì èìåíåì
void TMainWindow::AddFigure(Figure3d_unit *fig,
const char* name) {
OwnedFigures.push_back(fig);
glContext->AddFigure(fig);
ListView1->AddItem(name, (TObject*)fig);
for(int i = 0; i<ListView1->Items->Count; ++i) {
if(ListView1->Items->Item[i]->Data == (void*)fig) {
Figure_fig_to_list[fig] = ListView1->Items->Item[i];
ListView1->Selected = ListView1->Items->Item[i];
break;
}
}
}
//Îáðàáîò÷èê íàæàòèÿ êí. äîáàâëåíèÿ ôèãóðû èëè îïåðàöèè
//èëè îòìåíû òåêóùåãî äåéñòâèÿ (F2Mode!=F2Normal)
void __fastcall TMainWindow::Item_add_btnClick(
TObject *Sender) {
switch (glContext->GetFigMode()) {
case F2CuttingFig:
if(glContext->CancelOperationMode()) {
UpdateButtons();
glContext->DrawFrame();
}
break;
default:
//Åñëè äëÿ ïóíêòà ñïèñêà ñóùåñòâóåò äåéñòâèå - âûïîëíèòü
if(TreeView1->Selected == NULL) return;
ActionSelector sel =
(ActionSelector)TreeView1->Selected->Data;
if(sel != NULL) {
sel();
glContext->DrawFrame();
}
break;
}
}
//Îáðàáîò÷èê íàæàòèÿ êí. óäàëåíèÿ ôèãóðû
//Óäàëèòü ôèãóðó èç ïðîñòðàíñòâà è ñïèñêà â ôîðìå
void __fastcall TMainWindow::Item_remove_btnClick(
TObject *Sender) {
Figure3d_unit *fig = glContext->GetSelectedFig();
if(fig != nullptr) {
auto it = Figure_fig_to_list.find(fig);
if(it != Figure_fig_to_list.end()) {
ListView1->Items->Delete(it->second->Index);
Figure_fig_to_list.erase(it);
}
//Íå çàáûòü ñíÿòü âûáîð è ïî÷èñòèòü ïàìÿòü
glContext->RemoveFigure(fig);
auto dl_it = std::remove(OwnedFigures.begin(),
OwnedFigures.end(), fig);
OwnedFigures.erase(dl_it, OwnedFigures.end());
delete fig;
glContext->DrawFrame();
}
}
//Âûçîâ äèàëîãà èçìåíåíèÿ ôèãóðû ïî êí. "Èçìåíèòü"
void __fastcall TMainWindow::Item_edit_btnClick(
TObject *Sender) {
Figure3d_unit *fig = glContext->GetSelectedFig();
if(fig != nullptr) {
//Ñîçäàòü è âûïîëíèòü äèàëîã èçìåíåíèÿ âûáð. ôèãóðû
//Åñëè áûëî íàæàòî "ÎÊ"
TFigEditDialog *dialog = new TFigEditDialog(this, fig);
if(dialog->Execute()) {
double v[3];
fig->GetScale(v);
for(int i=0; i<3; ++i) {
if(std::fabs(dialog->Scale[i]-v[i]) > 1e-9) {
fig->Resize(dialog->Scale);
break;
}
}
for(int i=0; i<3; ++i) {
if(std::fabs(dialog->Rotation[i]) > 1e-9) {
fig->Rotate(dialog->Rotation);
break;
}
}
fig->GetCenterPos(v);
for(int i=0; i<3; ++i) {
if(std::fabs(dialog->Offset[i]-v[i]) > 1e-9) {
fig->Move(dialog->Offset);
break;
}
}
glContext->DrawFrame();
}
delete dialog;
}
}
//Íàñòðîéêà ýëåìåíòîâ ôîðìû è êîíòåêñòà îòðèñîâêè
void __fastcall TMainWindow::FormCreate(
TObject *Sender) {
//Ñîçäàòü è âûáðàòü êîíòåêñòû îêíà è îòðèñîâêè OpenGL
glContextWeak = glContext = AppGLContext::Create(
Panel1->Handle, Panel1->Width, Panel1->Height);
//Çàâåðøèòü ïîäãîòîâêó ôîðìû
KeyPreview = true;
FillFigureList();
}
//Îñâîáîæäåíèå êîíòåêñòà îòðèñîâêè è ïàìÿòè
void __fastcall TMainWindow::FormDestroy(
TObject *Sender) {
//Îñâîáîæäàåì êîíòåêñò îòðèñîâêè
glContext.reset();
//×èñòèì ñîáñòâåííûå ôèãóðû
for(auto x: OwnedFigures) delete x;
OwnedFigures.clear();
}
//Ìàñøòàáèðîâàíèå è íîðìàëèçàöèÿ ãðàôè÷åñêîãî îêíà
void __fastcall TMainWindow::FormResize(
TObject *Sender) {
glContext->Setup_viewPort(Panel1->Width, Panel1->Height);
}
//Îáíîâëåíèå ñîäåðæèìîãî ãðàôè÷åñêîãî îêíà
void __fastcall TMainWindow::FormPaint(TObject *Sender) {
glContext->DrawFrame();
}
//Îáíîâëåíèå êíîïîê
void TMainWindow::UpdateButtons() {
//Íàçíà÷åíèå êíîïêè "Äîáàâèòü" çàâèñèò îò ðåæèìà
switch(glContext->GetFigMode()) {
case F2CuttingFig:
Item_add_btn->Caption = "Îòìåíà";
Item_edit_btn->Enabled = false;
Item_remove_btn->Enabled = false;
break;
default:
Item_add_btn->Caption = "Äîáàâèòü";
Item_edit_btn->Enabled = true;
Item_remove_btn->Enabled = true;
break;
}
}
//Îáðàáîò÷èê íà÷àëà îïåðàöèé ñ êí. ìûøè
void __fastcall TMainWindow::Panel1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y) {
mouse_btn_state |= (1 << (int)Button);
// íà÷àëå íàæàòèÿ ËÊÌ - ñ÷èòàåì íàæàòèå ïðîñòûì
if(mouse_btn_state & 1) {
mouse_move_dst = 0;
simple_click = true;
}
}
//Îáðàáîò÷èê íàæàòèé ËÊÌ è îòïóñêàíèÿ êí. ìûøè
void __fastcall TMainWindow::Panel1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y) {
//Îáðàáîòêà ïðîñòîãî íàæàòèÿ ïðè îòïóñêàíèè ËÊÌ
if(simple_click && (int)Button == 0 &&
(mouse_btn_state & 1)) {
Figure3d_unit *fig;
const Figure3d_unit *fig_selection =
glContext->GetSelectedFig();
glContext->CtrlSimpleClick(X, Y);
fig = glContext->GetSelectedFig();
// îáû÷íîì ðåæèìå âûáèðàåì ôèãóðó êëèêîì
switch(glContext->GetFigMode()) {
case F2Normal:
//Åñëè íèêàêàÿ ôèãóðà íå êëèêíóòà, ñíèìàåì âûáîð
if(fig == nullptr) ListView1->Selected = NULL;
else {
if(fig != fig_selection) {
auto it = Figure_fig_to_list.find(fig);
if(it != Figure_fig_to_list.end()) {
ListView1->Selected = it->second;
}
else {
ListView1->Selected = NULL;
}
glContext->DrawFrame();
}
}
break;
default:
glContext->DrawFrame(); //Îáíîâëÿåì êàäð
break;
}
UpdateButtons();
}
mouse_btn_state &= ~(1 << (int)Button);
}
//Îáðàáîò÷èê ïåðåìåùåíèÿ êóðñîðà ñ çàæàòûìè êí. ìûøè
void __fastcall TMainWindow::Panel1MouseMove(
TObject *Sender, TShiftState Shift, int X, int Y) {
static int mx, my; //Ïðîøëîå ïîëîæåíèå êóðñîðà
//Åñëè çàæàòûõ êíîïîê íåò - ïîäìå÷àåì è âûõîäèì
if (!mouse_btn_state) {
mx = -1;
return;
}
//Äëÿ íà÷àëà äâèæåíèÿ ìûøè ñ çàæàòîé êíîïêîé
if(mx == -1) {
mx = X;
my = Y;
}
//äâèæåíèå ñ ËÊÌ - âðàùåíèå êàìåðû
if(mouse_btn_state & 1) {
//Åñëè äâèæåíèå çíà÷èòåëüíî - êëèê íå ïðîñòîé
mouse_move_dst += abs(X-mx)+abs(Y-my);
if(mouse_move_dst > 5) simple_click = false;
glContext->CtrlCamRotate(X-mx, Y-my);
}
//äâèæåíèå ñ ÏÊÌ ïî âåðòèêàëè - îòäàëåíèå êàìåðû (äâ. âíèç)
else if(mouse_btn_state & 2) {
glContext->CtrlCamZoom(Y-my);
}
//äâèæåíèå ñ ÑÊÌ äâèæåíèå òî÷êè îáçîðà è êàìåðû ïî xOy
else if(mouse_btn_state & 4) {
glContext->CtrlCamMoveXoY(X-mx, Y-my);
}
//Ïåðåíàñòðîèòü ìàòðèöó îáçîðà è îáíîâèòü êàäð
glContext->Setup_camera();
glContext->DrawFrame();
//Çàïîìíèòü êîîðäèíàòû äëÿ ñëåäóþùåãî äâèæåíèÿ
mx = X;
my = Y;
}
//Îáðàáîò÷èê ñîáûòèÿ ïîêèäàíèÿ êóðñîðîì îêíà
void __fastcall TMainWindow::Panel1MouseLeave(
TObject *Sender) {
//Åñëè ìûøü ïîêèíóëà ôîðìó - ñáðîñ ôëàãîâ ìûøè è
//ïðîñòîãî íàæàòèÿ
mouse_btn_state = 0;
simple_click = false;
}
//Îáðàáîò÷èê íàæàòèÿ êí. äîáàâëåíèÿ ôèãóðû èëè îïåðàöèè
//èëè îòìåíû òåêóùåãî äåéñòâèÿ (F2Mode!=F2Normal)
void __fastcall TMainWindow::ListView1SelectItem(
TObject *Sender, TListItem *Item, bool Selected) {
if(glContext->CancelOperationMode()) {
UpdateButtons();
}
if(!Selected) glContext->SetSelectedFig(nullptr);
else {
glContext->SetSelectedFig((Figure3d_unit*)Item->Data);
if(glContext->GetSelectedFig() == NULL)
glContext->SetSelectedFig(nullptr);
}
glContext->DrawFrame(); //Îáíîâëÿåì êàäð
}
//Ïåðåìåùåíèå êàìåðû ê âûáð. ôèãóðå
void __fastcall TMainWindow::ListView1DblClick(
TObject *Sender) {
if(ListView1->Selected && ListView1->Selected->Data) {
double pos[3];
const Figure3d_unit* fig =
(Figure3d_unit*)ListView1->Selected->Data;
//Ïåðåìåùåíèå òî÷êè îáçîðà ê ôèãóðå
fig->GetCenterPos(pos);
glContext->SetViewPos(pos[0], pos[1]);
//Ïåðåíàñòðîèòü ìàòðèöó îáçîðà è îáíîâèòü êàäð
glContext->Setup_camera();
glContext->DrawFrame();
}
}