Skip to content

Commit

Permalink
Editor: Unable to set keyframe on property changed event #611 (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
eprikazchikov authored Nov 11, 2023
1 parent ed9be41 commit 9389a10
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 87 deletions.
2 changes: 2 additions & 0 deletions engine/includes/editor/asseteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public slots:
virtual void onDragMove(QDragMoveEvent *event);
virtual void onDragLeave(QDragLeaveEvent *event);

virtual void onObjectsChanged(const QList<Object *> &objects, QString property, const Variant &value);

protected:
virtual void setModified(bool flag);

Expand Down
6 changes: 6 additions & 0 deletions engine/src/editor/asseteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ void AssetEditor::onDragLeave(QDragLeaveEvent *event) {
A_UNUSED(event);
}

void AssetEditor::onObjectsChanged(const QList<Object *> &objects, QString property, const Variant &value) {
A_UNUSED(objects);
A_UNUSED(property);
A_UNUSED(value);
}

QMenu *AssetEditor::objectMenu(Object *object) {
A_UNUSED(object);

Expand Down
60 changes: 38 additions & 22 deletions modules/editor/timeline/editor/timelineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ TimelineEdit::TimelineEdit(QWidget *parent) :
ui->setupUi(this);
ui->pause->setVisible(false);

ui->value->setVisible(false);
ui->valueEdit->setVisible(false);

setController(nullptr);

ui->record->setProperty("checkred", true);
ui->play->setProperty("checkgreen", true);
ui->curve->setProperty("checkgreen", true);
ui->splineMode->setProperty("checkgreen", true);

ui->widget->setModel(m_model);

ui->timeEdit->setValidator(new QIntValidator(0, INT32_MAX, this));

connect(m_model, &AnimationClipModel::rebind, this, &TimelineEdit::onRebind);

connect(ui->valueEdit, &QLineEdit::editingFinished, this, &TimelineEdit::onKeyChanged);
Expand Down Expand Up @@ -114,6 +119,31 @@ void TimelineEdit::onItemsSelected(QList<QObject *> objects) {
}

void TimelineEdit::updateClips() {
m_clips.clear();

if(m_controller) {
AnimationStateMachine *stateMachine = m_controller->stateMachine();
if(stateMachine) {
for(auto it : stateMachine->states()) {
string ref = Engine::reference(it->m_clip);
QFileInfo info(AssetManager::instance()->guidToPath(ref).c_str());
m_clips[info.baseName()] = it->m_clip;
}
if(!m_clips.isEmpty()) {
m_currentClip = m_clips.begin().key();
m_model->setClip(m_clips.begin().value(), m_controller->actor());
m_controller->setClip(m_clips.begin().value());
}
} else {
m_currentClip.clear();
m_model->setClip(nullptr, nullptr);
m_controller->setClip(nullptr);
}
} else {
m_currentClip.clear();
m_model->setClip(nullptr, nullptr);
}

ui->clipBox->clear();
ui->clipBox->addItems(m_clips.keys());

Expand All @@ -139,6 +169,7 @@ void TimelineEdit::setPosition(uint32_t position) {
}

ui->widget->setPosition(position);
ui->timeEdit->setText(QString::number(position));

emit updated();
}
Expand All @@ -164,34 +195,15 @@ void TimelineEdit::setController(Animator *controller) {
if(m_controller != controller) {
saveClip();

m_clips.clear();
m_controller = controller;
m_armature = nullptr;

ui->toolBar->setVisible(m_controller != nullptr);

if(m_controller) {
AnimationStateMachine *stateMachine = controller->stateMachine();
if(stateMachine) {
for(auto it : stateMachine->states()) {
string ref = Engine::reference(it->m_clip);
QFileInfo info(AssetManager::instance()->guidToPath(ref).c_str());
m_clips[info.baseName()] = it->m_clip;
}
if(!m_clips.isEmpty()) {
m_currentClip = m_clips.begin().key();
m_model->setClip(m_clips.begin().value(), controller->actor());
m_controller->setClip(m_clips.begin().value());
}
}

m_armature = static_cast<NativeBehaviour *>(m_controller->actor()->componentInChild("Armature"));

} else {
m_currentClip.clear();
m_model->setClip(nullptr, nullptr);
}

ui->toolBar->setVisible(m_controller != nullptr);

updateClips();
}
}
Expand Down Expand Up @@ -350,3 +362,7 @@ void TimelineEdit::changeEvent(QEvent *event) {
ui->retranslateUi(this);
}
}

void TimelineEdit::on_timeEdit_editingFinished() {
setPosition(ui->timeEdit->text().toUInt());
}
2 changes: 2 additions & 0 deletions modules/editor/timeline/editor/timelineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private slots:

void on_breakKey_clicked();

void on_timeEdit_editingFinished();

private:
void timerEvent(QTimerEvent *) override;

Expand Down
12 changes: 6 additions & 6 deletions modules/editor/timeline/editor/timelineedit.ui
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QHBoxLayout" name="playbackLayout">
<property name="spacing">
<number>2</number>
</property>
Expand Down Expand Up @@ -182,15 +182,12 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<layout class="QHBoxLayout" name="actionsLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="curve">
<widget class="QToolButton" name="splineMode">
<property name="enabled">
<bool>false</bool>
</property>
Expand Down Expand Up @@ -248,6 +245,9 @@
</property>
<item>
<widget class="QLabel" name="time">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Time</string>
</property>
Expand Down
38 changes: 1 addition & 37 deletions worldeditor/src/screens/propertyedit/nextobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,6 @@ void NextObject::onDeleteComponent() {
UndoManager::instance()->push(new RemoveComponent(component(name), this));
}

void NextObject::onObjectsChanged(QList<Object *> objects, const QString property, Variant value) {
QUndoCommand *group = UndoManager::instance()->group();
QString name;
if(group == nullptr) {
QString capital = property;
capital[0] = capital[0].toUpper();
name = QObject::tr("Change %1").arg(capital);
}
UndoManager::instance()->push(new ChangeProperty(objects.first(), property, value, this, name, group));
}

void NextObject::onPropertyContextMenuRequested(QString property, const QPoint point) {
QMenu menu;
QAction *action = menu.addAction(tr("Insert Keyframe"), this, SLOT(onInsertKeyframe()));
Expand Down Expand Up @@ -264,7 +253,7 @@ bool NextObject::event(QEvent *e) {
}

if(target.isValid() && current != target) {
onObjectsChanged({o}, propertyName, target);
emit propertyChanged({o}, propertyName, target);
}
}
}
Expand Down Expand Up @@ -590,31 +579,6 @@ PropertyEdit *NextObject::createCustomEditor(int userType, QWidget *parent, cons
return result;
}

ChangeProperty::ChangeProperty(Object *object, const QString &property, const Variant &value, NextObject *next, const QString &name, QUndoCommand *group) :
UndoCommand(name, next, group),
m_next(next),
m_value(value),
m_property(property),
m_object(object->uuid()) {

}
void ChangeProperty::undo() {
ChangeProperty::redo();
}
void ChangeProperty::redo() {
Variant value(m_value);

Scene *scene = nullptr;

Object *object = m_next->findById(m_object);
if(object) {
m_value = object->property(qPrintable(m_property));
object->setProperty(qPrintable(m_property), value);
}

m_next->onUpdated();
}

RemoveComponent::RemoveComponent(const Object *component, NextObject *next, const QString &name, QUndoCommand *group) :
UndoCommand(name + " " + component->typeName().c_str(), next, group),
m_next(next),
Expand Down
17 changes: 0 additions & 17 deletions worldeditor/src/screens/propertyedit/nextobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public slots:
void onUpdated();
void onCreateComponent(QString type);

void onObjectsChanged(QList<Object *> objects, const QString property, Variant value);

void onPropertyContextMenuRequested(QString property, const QPoint point);

void onInsertKeyframe();
Expand Down Expand Up @@ -80,21 +78,6 @@ protected slots:

};

class ChangeProperty : public UndoCommand {
public:
ChangeProperty(Object *objects, const QString &property, const Variant &value, NextObject *next, const QString &name, QUndoCommand *group = nullptr);
void undo() override;
void redo() override;

protected:
NextObject *m_next;

QString m_property;
Variant m_value;
uint32_t m_object;

};

class RemoveComponent : public UndoCommand {
public:
RemoveComponent(const Object *component, NextObject *next, const QString &name = QObject::tr("Remove Component"), QUndoCommand *group = nullptr);
Expand Down
16 changes: 12 additions & 4 deletions worldeditor/src/screens/propertyedit/propertyeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "screens/componentbrowser/componentbrowser.h"
#include "editor/assetmanager.h"
#include "editor/asseteditor.h"
#include "editor/projectmanager.h"
#include "editor/settingsmanager.h"

Expand Down Expand Up @@ -156,12 +157,12 @@ PropertyEditor::PropertyEditor(QWidget *parent) :
ui(new Ui::PropertyEditor),
m_filter(new PropertyFilter(this)),
m_propertyObject(nullptr),
m_nextObject(new NextObject(this)) {
m_nextObject(new NextObject(this)),
m_editor(nullptr) {

ui->setupUi(this);

connect(m_nextObject, &NextObject::updated, this, &PropertyEditor::onUpdated);
connect(m_nextObject, &NextObject::propertyChanged, this, &PropertyEditor::objectsChanged);
connect(m_nextObject, &NextObject::structureChanged, this, &PropertyEditor::onStructureChanged);

m_filter->setSourceModel(new PropertyModel(this));
Expand Down Expand Up @@ -352,9 +353,16 @@ void PropertyEditor::onSettingsUpdated() {
}

void PropertyEditor::onObjectsChanged(QList<Object *> objects, const QString property, Variant value) {
if(m_propertyObject == m_nextObject) {
m_nextObject->onObjectsChanged(objects, property, value);

}

void PropertyEditor::setCurrentEditor(AssetEditor *editor) {
if(m_editor) {
disconnect(m_nextObject, &NextObject::propertyChanged, m_editor, &AssetEditor::onObjectsChanged);
}

m_editor = editor;
connect(m_nextObject, &NextObject::propertyChanged, m_editor, &AssetEditor::onObjectsChanged);
}

void PropertyEditor::updatePersistent(const QModelIndex &index) {
Expand Down
4 changes: 4 additions & 0 deletions worldeditor/src/screens/propertyedit/propertyeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class PropertyEditor : public EditorGadget {
void reverted();

protected:
void setCurrentEditor(AssetEditor *editor);

void updatePersistent(const QModelIndex &index);

void addObject(QObject *propertyObject, const QString &name = QString(), QObject *parent = nullptr);
Expand Down Expand Up @@ -64,6 +66,8 @@ protected slots:

NextObject *m_nextObject;

AssetEditor *m_editor;

};

#endif
50 changes: 49 additions & 1 deletion worldeditor/src/screens/scenecomposer/objectcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void ObjectController::update() {
if(value != data) {
property.write(component, data);

emit propertyChanged({component}, property.name(), value);
UndoManager::instance()->push(new ChangeProperty({component}, property.name(), value, this, "", UndoManager::instance()->group()));
}
}
}
Expand Down Expand Up @@ -898,3 +898,51 @@ void SelectScene::redo() {
m_object = back;
}
}

ChangeProperty::ChangeProperty(const QList<Object *> &objects, const QString &property, const Variant &value, ObjectController *ctrl, const QString &name, QUndoCommand *group) :
UndoObject(ctrl, name, group),
m_value(value),
m_property(property) {

for(auto it : objects) {
m_objects.push_back(it->uuid());
}

}
void ChangeProperty::undo() {
ChangeProperty::redo();
}
void ChangeProperty::redo() {
QSet<Scene *> scenes;
QList<Object *> objects;

Variant value(m_value);

for(auto it : m_objects) {
Object *object = m_controller->findObject(it);
if(object) {
m_value = object->property(qPrintable(m_property));
object->setProperty(qPrintable(m_property), value);

objects.push_back(object);

Actor *actor = dynamic_cast<Actor *>(object);
if(actor) {
scenes.insert(actor->scene());
} else {
Component *component = dynamic_cast<Component *>(object);
if(component) {
scenes.insert(component->actor()->scene());
}
}
}
}

if(!objects.isEmpty()) {
emit m_controller->propertyChanged(objects, m_property, value);
}

for(auto it : scenes) {
emit m_controller->sceneUpdated(it);
}
}
Loading

0 comments on commit 9389a10

Please sign in to comment.