From 8541df9c32093360cfd4b8aa5bbdf9cb2853864c Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 12 May 2014 12:16:30 -0700 Subject: [PATCH] Remember attachment parameters per joint (as well as the last joint used) so that the gun script correctly allows both guns' positions to be tweaked. --- interface/src/avatar/MyAvatar.cpp | 40 ++++++++++++------- interface/src/avatar/MyAvatar.h | 2 +- interface/src/ui/AttachmentsDialog.cpp | 55 ++++++++++++++++++++------ interface/src/ui/AttachmentsDialog.h | 4 ++ libraries/avatars/src/AvatarData.h | 2 + 5 files changed, 77 insertions(+), 26 deletions(-) diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 6778460ec0..36c51dc9fd 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -520,8 +520,9 @@ void MyAvatar::saveAttachmentData(const AttachmentData& attachment) const { settings->beginGroup("savedAttachmentData"); settings->beginGroup(_skeletonModel.getURL().toString()); settings->beginGroup(attachment.modelURL.toString()); - settings->setValue("jointName", attachment.jointName); + + settings->beginGroup(attachment.jointName); settings->setValue("translation_x", attachment.translation.x); settings->setValue("translation_y", attachment.translation.y); settings->setValue("translation_z", attachment.translation.z); @@ -534,10 +535,11 @@ void MyAvatar::saveAttachmentData(const AttachmentData& attachment) const { settings->endGroup(); settings->endGroup(); settings->endGroup(); + settings->endGroup(); Application::getInstance()->unlockSettings(); } -AttachmentData MyAvatar::loadAttachmentData(const QUrl& modelURL) const { +AttachmentData MyAvatar::loadAttachmentData(const QUrl& modelURL, const QString& jointName) const { QSettings* settings = Application::getInstance()->lockSettings(); settings->beginGroup("savedAttachmentData"); settings->beginGroup(_skeletonModel.getURL().toString()); @@ -545,20 +547,30 @@ AttachmentData MyAvatar::loadAttachmentData(const QUrl& modelURL) const { AttachmentData attachment; attachment.modelURL = modelURL; - attachment.jointName = settings->value("jointName").toString(); - attachment.translation.x = loadSetting(settings, "translation_x", 0.0f); - attachment.translation.y = loadSetting(settings, "translation_y", 0.0f); - attachment.translation.z = loadSetting(settings, "translation_z", 0.0f); - glm::vec3 eulers; - eulers.x = loadSetting(settings, "rotation_x", 0.0f); - eulers.y = loadSetting(settings, "rotation_y", 0.0f); - eulers.z = loadSetting(settings, "rotation_z", 0.0f); - attachment.rotation = glm::quat(eulers); - attachment.scale = loadSetting(settings, "scale", 1.0f); + if (jointName.isEmpty()) { + attachment.jointName = settings->value("jointName").toString(); + } else { + attachment.jointName = jointName; + } + settings->beginGroup(attachment.jointName); + if (settings->contains("translation_x")) { + attachment.translation.x = loadSetting(settings, "translation_x", 0.0f); + attachment.translation.y = loadSetting(settings, "translation_y", 0.0f); + attachment.translation.z = loadSetting(settings, "translation_z", 0.0f); + glm::vec3 eulers; + eulers.x = loadSetting(settings, "rotation_x", 0.0f); + eulers.y = loadSetting(settings, "rotation_y", 0.0f); + eulers.z = loadSetting(settings, "rotation_z", 0.0f); + attachment.rotation = glm::quat(eulers); + attachment.scale = loadSetting(settings, "scale", 1.0f); + } else { + attachment = AttachmentData(); + } settings->endGroup(); settings->endGroup(); settings->endGroup(); + settings->endGroup(); Application::getInstance()->unlockSettings(); return attachment; @@ -650,8 +662,8 @@ void MyAvatar::attach(const QString& modelURL, const QString& jointName, const g return; } if (useSaved) { - AttachmentData attachment = loadAttachmentData(modelURL); - if (!attachment.jointName.isEmpty()) { + AttachmentData attachment = loadAttachmentData(modelURL, jointName); + if (attachment.isValid()) { Avatar::attach(modelURL, attachment.jointName, attachment.translation, attachment.rotation, attachment.scale, allowDuplicates, useSaved); return; diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 2e47d9c973..d446c2e895 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -67,7 +67,7 @@ public: void loadData(QSettings* settings); void saveAttachmentData(const AttachmentData& attachment) const; - AttachmentData loadAttachmentData(const QUrl& modelURL) const; + AttachmentData loadAttachmentData(const QUrl& modelURL, const QString& jointName = QString()) const; // Set what driving keys are being pressed to control thrust levels void setDriveKeys(int key, float val) { _driveKeys[key] = val; }; diff --git a/interface/src/ui/AttachmentsDialog.cpp b/interface/src/ui/AttachmentsDialog.cpp index f9f49738e9..44dd2452e6 100644 --- a/interface/src/ui/AttachmentsDialog.cpp +++ b/interface/src/ui/AttachmentsDialog.cpp @@ -97,7 +97,8 @@ static QDoubleSpinBox* createRotationBox(AttachmentPanel* panel, float value) { } AttachmentPanel::AttachmentPanel(AttachmentsDialog* dialog, const AttachmentData& data) : - _dialog(dialog) { + _dialog(dialog), + _applying(false) { setFrameStyle(QFrame::StyledPanel); QFormLayout* layout = new QFormLayout(); @@ -121,7 +122,7 @@ AttachmentPanel::AttachmentPanel(AttachmentsDialog* dialog, const AttachmentData } } _jointName->setCurrentText(data.jointName); - connect(_jointName, SIGNAL(currentIndexChanged(int)), SLOT(updateAttachmentData())); + connect(_jointName, SIGNAL(currentIndexChanged(int)), SLOT(jointNameChanged())); QHBoxLayout* translationBox = new QHBoxLayout(); translationBox->addWidget(_translationX = createTranslationBox(this, data.translation.x)); @@ -171,25 +172,57 @@ void AttachmentPanel::setModelURL(const QString& url) { void AttachmentPanel::modelURLChanged() { // check for saved attachment data + if (_modelURL->text().isEmpty()) { + _dialog->updateAttachmentData(); + return; + } AttachmentData attachment = Application::getInstance()->getAvatar()->loadAttachmentData(_modelURL->text()); - if (!attachment.jointName.isEmpty()) { + if (attachment.isValid()) { + _applying = true; _jointName->setCurrentText(attachment.jointName); - _translationX->setValue(attachment.translation.x); - _translationY->setValue(attachment.translation.y); - _translationZ->setValue(attachment.translation.z); - glm::vec3 eulers = glm::degrees(safeEulerAngles(attachment.rotation)); - _rotationX->setValue(eulers.x); - _rotationY->setValue(eulers.y); - _rotationZ->setValue(eulers.z); - _scale->setValue(attachment.scale); + applyAttachmentData(attachment); } _dialog->updateAttachmentData(); } +void AttachmentPanel::jointNameChanged() { + if (_applying) { + return; + } + // check for saved attachment data specific to this joint + if (_modelURL->text().isEmpty()) { + _dialog->updateAttachmentData(); + return; + } + AttachmentData attachment = Application::getInstance()->getAvatar()->loadAttachmentData( + _modelURL->text(), _jointName->currentText()); + if (attachment.isValid()) { + applyAttachmentData(attachment); + } + updateAttachmentData(); +} + void AttachmentPanel::updateAttachmentData() { + if (_applying) { + return; + } // save the attachment data under the model URL (if any) if (!_modelURL->text().isEmpty()) { Application::getInstance()->getAvatar()->saveAttachmentData(getAttachmentData()); } _dialog->updateAttachmentData(); } + +void AttachmentPanel::applyAttachmentData(const AttachmentData& attachment) { + _applying = true; + _translationX->setValue(attachment.translation.x); + _translationY->setValue(attachment.translation.y); + _translationZ->setValue(attachment.translation.z); + glm::vec3 eulers = glm::degrees(safeEulerAngles(attachment.rotation)); + _rotationX->setValue(eulers.x); + _rotationY->setValue(eulers.y); + _rotationZ->setValue(eulers.z); + _scale->setValue(attachment.scale); + _applying = false; + _dialog->updateAttachmentData(); +} diff --git a/interface/src/ui/AttachmentsDialog.h b/interface/src/ui/AttachmentsDialog.h index 7e9319fba8..59696c96f1 100644 --- a/interface/src/ui/AttachmentsDialog.h +++ b/interface/src/ui/AttachmentsDialog.h @@ -61,10 +61,13 @@ private slots: void chooseModelURL(); void setModelURL(const QString& url); void modelURLChanged(); + void jointNameChanged(); void updateAttachmentData(); private: + void applyAttachmentData(const AttachmentData& attachment); + AttachmentsDialog* _dialog; QLineEdit* _modelURL; QComboBox* _jointName; @@ -75,6 +78,7 @@ private: QDoubleSpinBox* _rotationY; QDoubleSpinBox* _rotationZ; QDoubleSpinBox* _scale; + bool _applying; }; #endif // hifi_AttachmentsDialog_h diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index dd604d06f5..072070e98c 100755 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -352,6 +352,8 @@ public: AttachmentData(); + bool isValid() const { return modelURL.isValid(); } + bool operator==(const AttachmentData& other) const; };