-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationWidget.cpp
More file actions
executable file
·444 lines (364 loc) · 13.8 KB
/
AnimationWidget.cpp
File metadata and controls
executable file
·444 lines (364 loc) · 13.8 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
#include <QDebug>
#include <QInputDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QPointer>
#include <OgreAnimationState.h>
#include "GlobalDefinitions.h"
#include "SkeletonTransform.h"
#include "SelectionSet.h"
#include "Manager.h"
#include "AnimationWidget.h"
#include "SentryReporter.h"
AnimationWidget::AnimationWidget(QWidget *parent) :
QWidget(parent)
{
ui->setupUi(this);
ui->animTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->animTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->skeletonTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->skeletonTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
auto updateTables = [this]() {
updateAnimationTable();
updateSkeletonTable();
};
connect(SelectionSet::getSingleton(),&SelectionSet::entitySelectionChanged,this,updateTables);
connect(SelectionSet::getSingleton(),&SelectionSet::nodeSelectionChanged,this,updateTables);
connect(Manager::getSingleton(), &Manager::sceneNodeDestroyed, this, [this](Ogre::SceneNode* const& node) {
// Clean up any SkeletonDebug and BoneWeightOverlay instances for entities attached to this node
// Signal fires before entities are destroyed, so we can safely access them
const auto& attachedObjects = node->getAttachedObjects();
for(auto* obj : attachedObjects)
{
if(obj->getMovableType() != "Entity")
continue;
auto* entity = static_cast<Ogre::Entity*>(obj);
if(mWeightOverlays.contains(entity))
{
mWeightOverlays.value(entity)->deleteLater();
mWeightOverlays.remove(entity);
}
if(mShowSkeleton.contains(entity))
{
mShowSkeleton.value(entity)->deleteLater();
mShowSkeleton.remove(entity);
}
}
});
m_pollTimer = new QTimer(this);
connect(m_pollTimer, &QTimer::timeout, this, &AnimationWidget::pollAnimationState);
m_pollTimer->start(200);
}
AnimationWidget::~AnimationWidget()
{
disableAllSkeletonDebug();
}
bool AnimationWidget::isSkeletonShown(Ogre::Entity * entity) const
{
return mShowSkeleton.contains(entity) && mShowSkeleton.find(entity).value()->bonesShown();
}
bool AnimationWidget::isSkeletonDebugActive(Ogre::Entity* entity) const
{
return mShowSkeleton.contains(entity);
}
bool AnimationWidget::isBoneWeightsShown(Ogre::Entity* entity) const
{
return mWeightOverlays.contains(entity);
}
bool AnimationWidget::toggleSkeletonDebug(Ogre::Entity* entity, bool show)
{
if (!entity || !entity->hasSkeleton())
return false;
SkeletonDebug* sd;
if (mShowSkeleton.contains(entity))
sd = mShowSkeleton.value(entity);
else
{
sd = new SkeletonDebug(entity, Manager::getSingleton()->getSceneMgr(), 0.1f, 0.01f);
mShowSkeleton.insert(entity, sd);
}
sd->showBones(show);
if (!show && mShowSkeleton.contains(entity))
{
sd->showAxes(false);
sd->showNames(false);
mShowSkeleton.remove(entity);
}
else if (show && mWeightOverlays.contains(entity))
{
// Reconnect to existing bone weight overlay so bone clicks update it
auto* overlay = mWeightOverlays.value(entity);
connect(sd, &SkeletonDebug::boneSelected, overlay, &BoneWeightOverlay::setSelectedBone);
if (sd->selectedBoneIndex() >= 0)
overlay->setSelectedBone(static_cast<unsigned short>(sd->selectedBoneIndex()));
}
updateSkeletonTable();
return true;
}
bool AnimationWidget::toggleBoneWeights(Ogre::Entity* entity, bool show)
{
if (!entity || !entity->hasSkeleton())
return false;
if (show)
{
if (mWeightOverlays.contains(entity))
return true; // already shown
auto* overlay = new BoneWeightOverlay(entity, Manager::getSingleton()->getSceneMgr());
mWeightOverlays.insert(entity, overlay);
if (mShowSkeleton.contains(entity))
{
auto* sd = mShowSkeleton.value(entity);
connect(sd, &SkeletonDebug::boneSelected, overlay, &BoneWeightOverlay::setSelectedBone);
if (sd->selectedBoneIndex() >= 0)
overlay->setSelectedBone(static_cast<unsigned short>(sd->selectedBoneIndex()));
}
overlay->setVisible(true);
}
else
{
if (mWeightOverlays.contains(entity))
{
delete mWeightOverlays.value(entity);
mWeightOverlays.remove(entity);
}
}
updateSkeletonTable();
return true;
}
SkeletonDebug* AnimationWidget::getSkeletonDebug(Ogre::Entity* entity) const
{
if (mShowSkeleton.contains(entity))
return mShowSkeleton.value(entity);
return nullptr;
}
BoneWeightOverlay* AnimationWidget::getBoneWeightOverlay(Ogre::Entity* entity) const
{
if (mWeightOverlays.contains(entity))
return mWeightOverlays.value(entity);
return nullptr;
}
void AnimationWidget::updateAnimationTable()
{
while(ui->animTable->rowCount())
{
ui->animTable->removeRow(0);
}
auto entities = SelectionSet::getSingleton()->getResolvedEntities();
if (entities.isEmpty())
return;
bool hasAnimationEnable = false;
for(Ogre::Entity* entity : entities)
{
//Animation
const Ogre::AnimationStateSet* set = entity->getAllAnimationStates();
if(!set) continue;
for (const auto &animationState:set->getAnimationStates())
{
auto entityItem = new QTableWidgetItem;
entityItem->setText(entity->getName().data());
entityItem->setData(ENTITY_DATA,QVariant::fromValue((void *) entity));
entityItem->setFlags(entityItem->flags() & ~Qt::ItemIsEditable);
const QString animationName = animationState.second->getAnimationName().c_str();
auto animationItem = new QTableWidgetItem;
animationItem->setText(animationName);
animationItem->setFlags(animationItem->flags() & ~Qt::ItemIsEditable);
auto enabledCB = new QTableWidgetItem(0);
enabledCB->setCheckState(animationState.second->getEnabled()?Qt::Checked:Qt::Unchecked);
enabledCB->setFlags(enabledCB->flags() & ~Qt::ItemIsEditable);
hasAnimationEnable = hasAnimationEnable || animationState.second->getEnabled();
auto loopCB = new QTableWidgetItem(0);
loopCB->setCheckState(animationState.second->getLoop()?Qt::Checked:Qt::Unchecked);
loopCB->setFlags(loopCB->flags() & ~Qt::ItemIsEditable);
ui->animTable->insertRow(0);
ui->animTable->setItem(0,0,entityItem);
ui->animTable->setItem(0,1,animationItem);
ui->animTable->setItem(0,2,enabledCB);
ui->animTable->setItem(0,3,loopCB);
}
}
}
void AnimationWidget::updateSkeletonTable()
{
while(ui->skeletonTable->rowCount())
{
ui->skeletonTable->removeRow(0);
}
auto entities = SelectionSet::getSingleton()->getResolvedEntities();
if (entities.isEmpty())
return;
for(Ogre::Entity* entity : entities)
{
QString str = entity->getName().data();
auto entityItem = new QTableWidgetItem;
entityItem->setText(str);
entityItem->setData(ENTITY_DATA,QVariant::fromValue((void *) entity));
entityItem->setFlags(entityItem->flags() & ~Qt::ItemIsEditable);
auto showSkeletonCB = new QTableWidgetItem(0);
showSkeletonCB->setCheckState(mShowSkeleton.contains(entity)?Qt::Checked:Qt::Unchecked);
showSkeletonCB->setFlags(showSkeletonCB->flags() & ~Qt::ItemIsEditable);
auto showWeightsCB = new QTableWidgetItem(0);
showWeightsCB->setCheckState(mWeightOverlays.contains(entity)?Qt::Checked:Qt::Unchecked);
showWeightsCB->setFlags(showWeightsCB->flags() & ~Qt::ItemIsEditable);
if (!entity->hasSkeleton())
showWeightsCB->setFlags(showWeightsCB->flags() & ~Qt::ItemIsEnabled);
ui->skeletonTable->insertRow(0);
ui->skeletonTable->setItem(0,0,entityItem);
ui->skeletonTable->setItem(0,1,showSkeletonCB);
ui->skeletonTable->setItem(0,2,showWeightsCB);
}
}
void AnimationWidget::on_PlayPauseButton_toggled(bool checked)
{
SentryReporter::addBreadcrumb("ui.animation", "Toggle animation playback");
setAnimationState(checked);
}
void AnimationWidget::setAnimationState(bool playing)
{
auto icon = QIcon(playing?":/icones/pause.png":":/icones/play.png");
ui->PlayPauseButton->setIcon(icon);
emit changeAnimationState(playing);
}
void AnimationWidget::pollAnimationState()
{
// Refresh table checkboxes to match actual Ogre animation state
// (e.g. changed externally via MCP), but do NOT touch Play/Pause button
for(int row = 0; row < ui->animTable->rowCount(); ++row)
{
auto* entityItem = ui->animTable->item(row, 0);
auto* enabledItem = ui->animTable->item(row, 2);
if(!entityItem || !enabledItem) continue;
auto* entity = static_cast<Ogre::Entity*>(entityItem->data(ENTITY_DATA).value<void*>());
if(!entity) continue;
auto* animNameItem = ui->animTable->item(row, 1);
if(!animNameItem) continue;
Ogre::AnimationState* animState = entity->getAnimationState(animNameItem->text().toStdString());
if(!animState) continue;
Qt::CheckState expected = animState->getEnabled() ? Qt::Checked : Qt::Unchecked;
if(enabledItem->checkState() != expected)
enabledItem->setCheckState(expected);
}
}
void AnimationWidget::on_skeletonTable_clicked(const QModelIndex &index)
{
if(index.column() != 1 && index.column() != 2)
return;
auto entity = (Ogre::Entity*)ui->skeletonTable->model()->data(ui->skeletonTable->model()->index(index.row(),0), ENTITY_DATA).value<void *>();
if(!entity || !entity->hasSkeleton())
return;
if(index.column() == 1)
{
bool checked = (index.data(Qt::CheckStateRole) == Qt::Checked);
toggleSkeletonDebug(entity, checked);
// Also toggle axes when using GUI (original behavior)
if(checked)
{
auto* sd = getSkeletonDebug(entity);
if(sd) sd->showAxes(true);
}
}
else if(index.column() == 2)
{
bool checked = (index.data(Qt::CheckStateRole) == Qt::Checked);
toggleBoneWeights(entity, checked);
}
}
void AnimationWidget::on_animTable_cellDoubleClicked(int row, int column)
{
if(column != 1)
return;
bool ok;
QString oldName = ui->animTable->item(row,column)->text();
QString newName = QInputDialog::getText(this, tr("Change Animation Name"),
tr("New name:"), QLineEdit::Normal,
oldName, &ok);
if(!ok) return;
if(oldName == newName) return;
if(!newName.size())
{
QMessageBox::warning(this,tr("Error when renaming the animation"),tr("The animation name couldn't be changed to empty\nPlease type a name."),QMessageBox::Ok);
return;
}
Ogre::Entity* entity = nullptr;
entity = (Ogre::Entity*)ui->animTable->model()->data(ui->animTable->model()->index(row,0), ENTITY_DATA).value<void *>();
if(!entity) return;
disableAllSkeletonDebug();
if(Manager::getSingleton()->hasAnimationName(entity, newName))
{
QMessageBox::warning(this,tr("Error when renaming the animation"),tr("This name already exists."),QMessageBox::Ok);
return;
}
setAnimationState(false);
if(SkeletonTransform::renameAnimation(entity,oldName,newName))
{
updateAnimationTable();
ui->animTable->sortItems(0);
emit changeAnimationName(newName.toStdString());
}
else
QMessageBox::warning(this,tr("Error when renaming the animation"),tr("The animation name couldn't be changed, look into the graphics log for details."),QMessageBox::Ok);
}
void AnimationWidget::disableEntityAnimations(Ogre::Entity* entity)
{
if(auto set = entity->getAllAnimationStates(); set)
{
for (const auto &[key, value] : set->getAnimationStates())
{
value->setEnabled(false);
}
}
updateAnimationTable();
}
void AnimationWidget::disableAllSelectedAnimations()
{
if(!SelectionSet::getSingleton()->hasEntities())
return;
for(const Ogre::Entity* entity : SelectionSet::getSingleton()->getEntitiesSelectionList())
{
if(auto set = entity->getAllAnimationStates(); set)
{
for (const auto &[key, value] : set->getAnimationStates())
{
value->setEnabled(false);
}
}
}
updateAnimationTable();
}
void AnimationWidget::disableAllSkeletonDebug()
{
for(BoneWeightOverlay *overlay : mWeightOverlays.values())
{
delete overlay;
}
mWeightOverlays.clear();
for(SkeletonDebug *sd : mShowSkeleton.values())
{
sd->showAxes(false);
sd->showBones(false);
delete sd;
}
mShowSkeleton.clear();
updateSkeletonTable();
}
void AnimationWidget::on_animTable_clicked(const QModelIndex &index) const
{
if(index.column()<2)
return;
const Ogre::Entity* entity = (Ogre::Entity*)ui->animTable->model()->data(ui->animTable->model()->index(index.row(),0), ENTITY_DATA).value<void *>();
if(!entity)
return;
Ogre::AnimationState* animationState = entity->getAnimationState(ui->animTable->item(index.row(),1)->text().toStdString().data());
if(!animationState)
return;
switch(index.column())
{
case 2:
animationState->setEnabled(index.data(Qt::CheckStateRole) == Qt::Checked);
break;
case 3:
animationState->setLoop(index.data(Qt::CheckStateRole) == Qt::Checked);
break;
default:
break;
}
}