-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandes.cpp
More file actions
310 lines (238 loc) · 8.15 KB
/
commandes.cpp
File metadata and controls
310 lines (238 loc) · 8.15 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
#include "Commandes.h"
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
static void appliquerTextureParId(NuageDePoints& racine,
int idPoint,
const std::string& nouvelleTexture)
{
vector<Point*> tousLesPoints;
racine.collecterPoints(tousLesPoints);
for (auto* p : tousLesPoints) {
if (p->getId() == idPoint) {
p->setTexture(creerTextureDepuisString(nouvelleTexture));
}
}
}
void Historique::pushExec(unique_ptr<ICommand> cmd) {
cmd->executer();
pileUndo_.push(std::move(cmd));
while (!pileRedo_.empty()) pileRedo_.pop();
}
void Historique::undo() {
if (pileUndo_.empty()) return;
auto cmd = std::move(pileUndo_.top());
pileUndo_.pop();
cmd->annuler();
pileRedo_.push(std::move(cmd));
}
void Historique::redo() {
if (pileRedo_.empty()) return;
auto cmd = std::move(pileRedo_.top());
pileRedo_.pop();
cmd->executer();
pileUndo_.push(std::move(cmd));
}
ControleurCommandes::ControleurCommandes(NuageDePoints& n,
AfficheurO1& a1,
AfficheurO2& a2,
GestionnaireSurface& g)
: nuage_(n), aff1_(a1), aff2_(a2), gest_(g) {}
void ControleurCommandes::executerCommande(const string& nom) {
creerEtExecuter(nom);
}
void ControleurCommandes::undo() { hist_.undo(); }
void ControleurCommandes::redo() { hist_.redo(); }
void ControleurCommandes::creerEtExecuter(const std::string& nom)
{
if (nom == "a") {
CmdAfficherListe cmd(nuage_);
cmd.executer();
}
else if (nom == "o1") {
CmdAfficherO1 cmd(aff1_, nuage_, gest_);
cmd.executer();
}
else if (nom == "o2") {
CmdAfficherO2 cmd(aff2_, nuage_, gest_);
cmd.executer();
}
else if (nom == "f") {
hist_.pushExec(std::make_unique<CmdFusion>(nuage_, aff1_, aff2_));
}
else if (nom == "d") {
hist_.pushExec(std::make_unique<CmdDeplacer>(nuage_, aff1_, aff2_));
}
else if (nom == "s") {
hist_.pushExec(std::make_unique<CmdSupprimer>(nuage_, aff1_, aff2_));
}
else if (nom == "c1") {
hist_.pushExec(std::make_unique<CmdSurfaceC1>(gest_, nuage_));
}
else if (nom == "c2") {
hist_.pushExec(std::make_unique<CmdSurfaceC2>(gest_, nuage_));
}
}
void CmdAfficherListe::executer() {
nuage_.afficherListe(cout);
}
void CmdAfficherO1::executer() {
vector<Point*> pts;
const_cast<NuageDePoints&>(nuage_).collecterPoints(pts);
vector<Pixel> px;
for (auto* p : pts)
px.push_back({ p->x(), p->y(), p->symbolePourAffichage() });
cout << "\n\n";
dessinerGrille(px, gest_.segmentsCourants());
}
void CmdAfficherO2::executer() {
vector<Point*> pts;
const_cast<NuageDePoints&>(nuage_).collecterPoints(pts);
vector<Pixel> px;
for (auto* p : pts) {
std::string s = (p->getId() < 10 ? std::string(1, char('0' + p->getId())) : std::string("*"));
px.push_back({ p->x(), p->y(), s });
}
cout << "\n\n";
dessinerGrille(px, gest_.segmentsCourants());
}
CmdFusion::CmdFusion(NuageDePoints& n, AfficheurO1& a1, AfficheurO2& a2)
: nuage_(n), a1_(a1), a2_(a2), idNuageCree_(-1) {}
void CmdFusion::executer() {
cout << "IDs des points/nuages a fusionner : ";
string l;
getline(cin, l);
ids_.clear();
int id;
istringstream iss(l);
while (iss >> id) ids_.push_back(id);
if (ids_.empty()) {
cout << "Aucun ID specifie.\n";
return;
}
static int compteurFusion = 0;
std::string textureCourante = (compteurFusion == 0) ? "o" : "#";
auto nouveauNuage = std::make_unique<NuageDePoints>(textureCourante);
idNuageCree_ = nouveauNuage->getId();
auto calculerNouvelleTexture = [&](const Point* p) {
std::string base = p->texture().valeur();
return base + textureCourante;
};
for (int idElement : ids_) {
if (Point* point = nuage_.trouverPointParId(idElement)) {
auto pointCopie = std::make_unique<Point>(point->getId(), point->x(), point->y());
std::string finale = calculerNouvelleTexture(point);
pointCopie->setTexture(creerTextureDepuisString(finale));
nouveauNuage->ajouter(std::move(pointCopie));
appliquerTextureParId(nuage_, point->getId(), finale);
}
else if (IElement* element = nuage_.trouverElementParId(idElement)) {
if (auto* nuageExist = dynamic_cast<NuageDePoints*>(element)) {
auto& elems = nuage_.elements();
unique_ptr<IElement> nuageADeplacer;
for (auto it = elems.begin(); it != elems.end(); ++it) {
if ((*it)->getId() == idElement) {
nuageADeplacer = std::move(*it);
elems.erase(it);
break;
}
}
if (nuageADeplacer) {
vector<Point*> pointsDuNuage;
nuageExist->collecterPoints(pointsDuNuage);
for (auto* pt : pointsDuNuage) {
std::string finale = calculerNouvelleTexture(pt);
appliquerTextureParId(nuage_, pt->getId(), finale);
}
nouveauNuage->ajouter(std::move(nuageADeplacer));
}
}
}
else {
cout << "Element " << idElement << " introuvable.\n";
}
}
if (!nouveauNuage->elements().empty()) {
nuage_.ajouter(std::move(nouveauNuage));
compteurFusion++;
cout << "Fusion terminee. Nuage " << idNuageCree_ << " cree.\n";
}
else {
cout << "Aucun element valide pour la fusion.\n";
}
}
CmdDeplacer::CmdDeplacer(NuageDePoints& n, AfficheurO1& a1, AfficheurO2& a2)
: nuage_(n), aff1_(a1), aff2_(a2) {}
void CmdDeplacer::executer()
{
if (!configure_) {
std::cout << "ID du point : ";
std::cin >> id_;
std::cout << "Nouvelle position (x y) : ";
std::cin >> nouvX_ >> nouvY_;
std::string dummy; std::getline(std::cin, dummy);
Point* p = nuage_.trouverPointParId(id_);
if (!p) {
std::cout << "ID introuvable.\n";
id_ = -1;
return;
}
ancienX_ = p->x();
ancienY_ = p->y();
configure_ = true;
}
if (id_ >= 0) {
if (auto* p = nuage_.trouverPointParId(id_)) {
p->setPos(nouvX_, nouvY_);
}
}
std::cout << "\n";
aff1_.afficher();
aff2_.afficher();
}
void CmdDeplacer::annuler()
{
if (!configure_ || id_ < 0) return;
if (auto* p = nuage_.trouverPointParId(id_)) {
p->setPos(ancienX_, ancienY_);
}
std::cout << "\n";
aff1_.afficher();
aff2_.afficher();
}
CmdSupprimer::CmdSupprimer(NuageDePoints& n, AfficheurO1& a1, AfficheurO2& a2)
: nuage_(n), aff1_(a1), aff2_(a2) {}
void CmdSupprimer::executer() {
cout << "ID du point a supprimer : ";
cin >> id_;
string dummy; getline(cin, dummy);
auto& elems = nuage_.elements();
index_ = -1;
for (size_t i = 0; i < elems.size(); ++i)
if (auto* p = dynamic_cast<Point*>(elems[i].get()))
if (p->getId() == id_) { index_ = i; break; }
if (index_ == -1) {
cout << "Introuvable\n";
return;
}
sauvegarde_ = std::move(elems[index_]);
elems.erase(elems.begin() + index_);
aEteExecute_ = true;
}
void CmdSupprimer::annuler() {
if (!aEteExecute_) return;
auto& elems = nuage_.elements();
elems.insert(elems.begin() + index_, std::move(sauvegarde_));
aEteExecute_ = false;
}
void CmdSurfaceC1::executer() {
gest_.setStrategie(make_unique<CreateurSurfaceC1>());
gest_.generer(nuage_);
cout << "Surfaces C1 generees.\n";
}
void CmdSurfaceC2::executer() {
gest_.setStrategie(make_unique<CreateurSurfaceC2>());
gest_.generer(nuage_);
cout << "Surfaces C2 generees.\n";
}