-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaskTransformDialog.cpp
314 lines (260 loc) · 11.9 KB
/
askTransformDialog.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
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
/* OpenBRF -- by marco tarini. Provided under GNU General Public License */
#include "askTransformDialog.h"
#include "ui_askTransformDialog.h"
#include <math.h>
#include <vcg/space/point3.h>
#include <vcg/math/matrix44.h>
#include <vcg/space/box3.h>
void AskTransformDialog::setApplyToAllLoc(bool *loc){
applyToAll = loc;
ui->applyToLastSel->setChecked(!(*loc));
updateSensitivity();
}
void AskTransformDialog::setBoundingBox(float *minv, float *maxv){
for (int i=0; i<3; i++) {
bb_min[i]= minv[i];
bb_max[i]= maxv[i];
}
}
void AskTransformDialog::setBoundingBoxForAllButLast(float *minv, float *maxv){
for (int i=0; i<3; i++) {
bb_min_all_but_last[i]= minv[i];
bb_max_all_but_last[i]= maxv[i];
}
}
/* swy: save the position/coords of the middle of the bounding box so that we
can go from global to local coordinates and do local rotations */
void AskTransformDialog::setRotCenterPoint(float lx, float ly, float lz, float ax, float ay, float az){
rot_center_point[1][0] = lx;
rot_center_point[1][1] = ly;
rot_center_point[1][2] = lz; /* swy: the center coordinates of the bounding box of just the last selected object */
rot_center_point[0][0] = ax; /* swy: the center coordinates of the bounding box of the sum of all the selected elements together */
rot_center_point[0][1] = ay;
rot_center_point[0][2] = az;
}
AskTransformDialog::AskTransformDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AskTransformDialog)
{
sensitivityOne = sensitivityAll = 0.25;
applyToAll = NULL;
ui->setupUi(this);
//reset();
/* swy: make the alignment icons vertical for the Y axis; making them way more intuitive to use.
suggested by @tos1 in the TaleWorlds forum. https://stackoverflow.com/a/66429169/674685
https://stackoverflow.com/a/57692067/674685 */
QTransform tr; tr.rotate(90); /* swy: clockwise */
ui-> midY->setIcon(ui-> midY->icon().pixmap(16, 16, QIcon::Mode::Normal, QIcon::State::On).transformed(tr));
ui-> leftY->setIcon(ui-> leftY->icon().pixmap(16, 16, QIcon::Mode::Normal, QIcon::State::On).transformed(tr));
ui->rightY->setIcon(ui->rightY->icon().pixmap(16, 16, QIcon::Mode::Normal, QIcon::State::On).transformed(tr));
//connect(ui->rotx,SIGNAL())
connect(ui->rotx,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->roty,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->rotz,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->trax,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->tray,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->traz,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->trax,SIGNAL(valueChanged(double)),this,SLOT(disableAlignX()));
connect(ui->tray,SIGNAL(valueChanged(double)),this,SLOT(disableAlignY()));
connect(ui->traz,SIGNAL(valueChanged(double)),this,SLOT(disableAlignZ()));
connect(ui->scx,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->scy,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->scz,SIGNAL(valueChanged(double)),this,SLOT(update()));
connect(ui->midX,SIGNAL(toggled(bool)),this,SLOT(onAlignmentMX(bool)));
connect(ui->midY,SIGNAL(toggled(bool)),this,SLOT(onAlignmentMY(bool)));
connect(ui->midZ,SIGNAL(toggled(bool)),this,SLOT(onAlignmentMZ(bool)));
connect(ui->leftX,SIGNAL(toggled(bool)),this,SLOT(onAlignmentLX(bool)));
connect(ui->leftY,SIGNAL(toggled(bool)),this,SLOT(onAlignmentLY(bool)));
connect(ui->leftZ,SIGNAL(toggled(bool)),this,SLOT(onAlignmentLZ(bool)));
connect(ui->rightX,SIGNAL(toggled(bool)),this,SLOT(onAlignmentRX(bool)));
connect(ui->rightY,SIGNAL(toggled(bool)),this,SLOT(onAlignmentRY(bool)));
connect(ui->rightZ,SIGNAL(toggled(bool)),this,SLOT(onAlignmentRZ(bool)));
connect(ui->localRot,SIGNAL(clicked(bool)),this,SLOT(update()));
connect(ui->localScl,SIGNAL(clicked(bool)),this,SLOT(update()));
connect(ui->applyToLastSel,SIGNAL(clicked(bool)),this,SLOT(update()));
connect(ui->checkBox,SIGNAL(clicked(bool)),this,SLOT(update()));
connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onButton(QAbstractButton*)));
}
void AskTransformDialog::onButton(QAbstractButton *b){
if (b==ui->buttonBox->button(QDialogButtonBox::Reset)) reset();
}
void AskTransformDialog::setMultiObj(bool multObj){
ui->applyToLastSel->setEnabled(multObj);
}
void AskTransformDialog::disableAlignX(){
ui->midX->setChecked(false);
ui->leftX->setChecked(false);
ui->rightX->setChecked(false);
}
void AskTransformDialog::disableAlignY(){
ui->midY->setChecked(false);
ui->leftY->setChecked(false);
ui->rightY->setChecked(false);
}
void AskTransformDialog::disableAlignZ(){
ui->midZ->setChecked(false);
ui->leftZ->setChecked(false);
ui->rightZ->setChecked(false);
}
int AskTransformDialog::exec(){
applyAlignments();
return QDialog::exec();
}
void AskTransformDialog::applyAlignments(){
ui->trax->blockSignals(true);
ui->tray->blockSignals(true);
ui->traz->blockSignals(true);
if (ui->applyToLastSel->isChecked()) {
/* swy: new! when the «Apply to last selected object only» mode is on; we center/align around the cumulative
bounding box of all the other meshes; that way we can recenter objects around other objects */
if (ui->midX->isChecked()) ui->trax->setValue( ((bb_min_all_but_last[0]+bb_max_all_but_last[0])/2.f) - rot_center_point[1][0] );
if (ui->midY->isChecked()) ui->tray->setValue( ((bb_min_all_but_last[1]+bb_max_all_but_last[1])/2.f) - rot_center_point[1][1] );
if (ui->midZ->isChecked()) ui->traz->setValue( ((bb_min_all_but_last[2]+bb_max_all_but_last[2])/2.f) - rot_center_point[1][2] );
if (ui->leftX->isChecked()) ui->trax->setValue( bb_min_all_but_last[0] - rot_center_point[1][0]);
if (ui->leftY->isChecked()) ui->tray->setValue( bb_min_all_but_last[1] - rot_center_point[1][1]);
if (ui->leftZ->isChecked()) ui->traz->setValue( bb_min_all_but_last[2] - rot_center_point[1][2]);
if (ui->rightX->isChecked()) ui->trax->setValue( bb_max_all_but_last[0] - rot_center_point[1][0]);
if (ui->rightY->isChecked()) ui->tray->setValue( bb_max_all_but_last[1] - rot_center_point[1][1]);
if (ui->rightZ->isChecked()) ui->traz->setValue( bb_max_all_but_last[2] - rot_center_point[1][2]);
} else {
/* swy: when not, keep using the classic alignment mode that Marco wrote */
if (ui->midX->isChecked()) ui->trax->setValue( - (bb_min[0]+bb_max[0])/2 );
if (ui->midY->isChecked()) ui->tray->setValue( - (bb_min[1]+bb_max[1])/2 );
if (ui->midZ->isChecked()) ui->traz->setValue( - (bb_min[2]+bb_max[2])/2 );
if (ui->leftX->isChecked()) ui->trax->setValue( - bb_min[0] );
if (ui->leftY->isChecked()) ui->tray->setValue( - bb_min[1] );
if (ui->leftZ->isChecked()) ui->traz->setValue( - bb_min[2] );
if (ui->rightX->isChecked()) ui->trax->setValue( - bb_max[0] );
if (ui->rightY->isChecked()) ui->tray->setValue( - bb_max[1] );
if (ui->rightZ->isChecked()) ui->traz->setValue( - bb_max[2] );
}
ui->trax->blockSignals(false);
ui->tray->blockSignals(false);
ui->traz->blockSignals(false);
update();
}
void AskTransformDialog::onAlignmentLX(bool b){ if (b) {
ui->midX->setChecked(false); ui->rightX->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentMX(bool b){ if (b) {
ui->leftX->setChecked(false); ui->rightX->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentRX(bool b){ if (b) {
ui->midX->setChecked(false); ui->leftX->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentLZ(bool b){ if (b) {
ui->midZ->setChecked(false); ui->rightZ->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentMZ(bool b){ if (b) {
ui->leftZ->setChecked(false); ui->rightZ->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentRZ(bool b){ if (b) {
ui->midZ->setChecked(false); ui->leftZ->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentLY(bool b){ if (b) {
ui->midY->setChecked(false); ui->rightY->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentMY(bool b){ if (b) {
ui->leftY->setChecked(false); ui->rightY->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::onAlignmentRY(bool b){ if (b) {
ui->midY->setChecked(false); ui->leftY->setChecked(false);
applyAlignments();
}}
void AskTransformDialog::setSensitivityAll(double sens){
sensitivityAll = sens;
}
void AskTransformDialog::setSensitivityOne(double sens){
sensitivityOne = sens;
}
void AskTransformDialog::updateSensitivity(){
//if (!applyToAll) return;
float sens = (applyToAll)? sensitivityAll:sensitivityOne;
ui->trax->setSingleStep(sens);
ui->tray->setSingleStep(sens);
ui->traz->setSingleStep(sens);
}
void AskTransformDialog::reset(){
ui->scx->setValue(100);
ui->scy->setValue(100);
ui->scz->setValue(100);
ui->trax->setValue(0);
ui->tray->setValue(0);
ui->traz->setValue(0);
ui->rotx->setValue(0);
ui->roty->setValue(0);
ui->rotz->setValue(0);
disableAlignX();
disableAlignY();
disableAlignZ();
update();
}
static float toRad(float t) {return t*M_PI/180;}
void AskTransformDialog::update(){
if (applyToAll) *applyToAll = ! ( ui->applyToLastSel->isChecked());
updateSensitivity();
if (ui->checkBox->isChecked()) {
ui->scy->blockSignals(true);
ui->scz->blockSignals(true);
ui->scy->setValue( ui->scx->value());
ui->scz->setValue( ui->scx->value());
ui->scy->blockSignals(false);
ui->scz->blockSignals(false);
}
ui->scy->setEnabled( !ui->checkBox->isChecked() );
ui->scz->setEnabled( !ui->checkBox->isChecked() );
vcg::Matrix44f rot_move_to_center;
vcg::Matrix44f rot;
vcg::Matrix44f t;
vcg::Matrix44f sc;
/* swy: compensate for the original mesh offset respect to the {0,0,0}
world position and any extra movement we do here */
rot_move_to_center.Identity();
rot_move_to_center.SetTranslate(
vcg::Point3f(
rot_center_point[ui->applyToLastSel->isChecked()][0] + ui->trax->value(), /* swy: switch between the center of all the elements (index 0), or the center of just the last element (index 1) */
rot_center_point[ui->applyToLastSel->isChecked()][1] + ui->tray->value(),
rot_center_point[ui->applyToLastSel->isChecked()][2] + ui->traz->value())
);
rot.FromEulerAngles(toRad( ui->rotx->value() ),toRad( ui->roty->value() ),toRad( ui->rotz->value() ));
/* swy: move it to the center of the universe before doing
rotations and then move the rotated mesh back */
if (ui->localRot->isChecked())
rot = rot_move_to_center * rot * vcg::Inverse(rot_move_to_center);
t.SetTranslate(ui->trax->value(),ui->tray->value(),ui->traz->value());
sc.SetScale(vcg::Point3f(ui->scx->value(),ui->scy->value(),ui->scz->value())/100.0f);
/* swy: move it to the center of the universe before doing
re-scaling and then move the scaled mesh back */
if (ui->localScl->isChecked())
sc = rot_move_to_center * sc * vcg::Inverse(rot_move_to_center);
/* swy: changed the order of operations from sc*rot*t to rot*sc*t, to avoid
shearing while combining rotation with non-uniform scaling */
vcg::Matrix44f res = rot*sc*t;
for (int i=0,x=0; x<4; x++)
for (int y=0; y<4; y++,i++)
matrix[i]=res[y][x];
emit changed();
}
AskTransformDialog::~AskTransformDialog()
{
delete ui;
}
void AskTransformDialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}