From 481624b48c0921c5b7f7a4b78a3f8ac1639ca675 Mon Sep 17 00:00:00 2001 From: Zander Otavka Date: Wed, 5 Aug 2015 14:21:23 -0700 Subject: [PATCH] Re-work panel position and rotation binding. --- examples/controlPanel.js | 9 +- examples/example/ui/floatingUIExample.js | 8 +- examples/libraries/overlayManager.js | 6 +- interface/src/ui/overlays/FloatingUIPanel.cpp | 188 ++++++++---------- interface/src/ui/overlays/FloatingUIPanel.h | 36 ++-- interface/src/ui/overlays/Overlays.cpp | 8 +- interface/src/ui/overlays/PanelAttachable.cpp | 4 +- 7 files changed, 112 insertions(+), 147 deletions(-) diff --git a/examples/controlPanel.js b/examples/controlPanel.js index e579eddc63..3ff054f3c8 100644 --- a/examples/controlPanel.js +++ b/examples/controlPanel.js @@ -23,9 +23,7 @@ var FACE_IMAGE_URL = HIFI_PUBLIC_BUCKET + "images/tools/face-toggle.svg"; var ADDRESS_BAR_IMAGE_URL = HIFI_PUBLIC_BUCKET + "images/tools/address-bar-toggle.svg"; var panel = new FloatingUIPanel({ - anchorPosition: { - bind: "myAvatar" - }, + anchorPositionBinding: { avatar: "MyAvatar" }, offsetPosition: { x: 0, y: 0.4, z: 1 }, facingRotation: { w: 0, x: 0, y: 1, z: 0 } }); @@ -194,10 +192,7 @@ function onMouseUp(event) { if (event.isRightButton && Vec3.distance(mouseDown.pos, { x: event.x, y: event.y }) < 10) { panel.setProperties({ visible: !panel.visible, - offsetRotation: { - bind: "quat", - value: Quat.multiply(MyAvatar.orientation, { x: 0, y: 1, z: 0, w: 0 }) - } + offsetRotation: Quat.multiply(MyAvatar.orientation, { x: 0, y: 1, z: 0, w: 0 }) }); } diff --git a/examples/example/ui/floatingUIExample.js b/examples/example/ui/floatingUIExample.js index 59d3b67d38..8eaeeff98b 100644 --- a/examples/example/ui/floatingUIExample.js +++ b/examples/example/ui/floatingUIExample.js @@ -19,10 +19,8 @@ var RED_DOT_IMAGE_URL = HIFI_PUBLIC_BUCKET + "images/red-dot.svg"; var BLUE_SQUARE_IMAGE_URL = HIFI_PUBLIC_BUCKET + "images/blue-square.svg"; var mainPanel = new FloatingUIPanel({ - offsetRotation: { - bind: "quat", - value: { w: 1, x: 0, y: 0, z: 0 } - }, + anchorPositionBinding: null,//{ avatar: "MyAvatar" }, + offsetRotation: { w: 1, x: 0, y: 0, z: 0 }, offsetPosition: { x: 0, y: 0.4, z: 1 }, facingRotation: { w: 0, x: 0, y: 1, z: 0 } }); @@ -189,4 +187,4 @@ function onScriptEnd() { Controller.mousePressEvent.connect(onMouseDown); Controller.mouseReleaseEvent.connect(onMouseUp); -Script.scriptEnding.connect(onScriptEnd); \ No newline at end of file +Script.scriptEnding.connect(onScriptEnd); diff --git a/examples/libraries/overlayManager.js b/examples/libraries/overlayManager.js index 212e8beab7..147df87831 100644 --- a/examples/libraries/overlayManager.js +++ b/examples/libraries/overlayManager.js @@ -391,8 +391,10 @@ this.message = message; }; - var FIELDS = ["anchorPosition", "offsetRotation", "offsetPosition", "facingRotation"]; - FIELDS.forEach(function(prop) { + [ + "anchorPosition", "anchorPositionBinding", "offsetRotation", "offsetRotationBinding", + "offsetPosition", "facingRotation" + ].forEach(function(prop) { Object.defineProperty(that.prototype, prop, { get: function() { return Overlays.getPanelProperty(this._id, prop); diff --git a/interface/src/ui/overlays/FloatingUIPanel.cpp b/interface/src/ui/overlays/FloatingUIPanel.cpp index 496218d7ed..99eab0f241 100644 --- a/interface/src/ui/overlays/FloatingUIPanel.cpp +++ b/interface/src/ui/overlays/FloatingUIPanel.cpp @@ -20,41 +20,33 @@ #include "Application.h" #include "Base3DOverlay.h" -std::function const FloatingUIPanel::AVATAR_POSITION = []() -> glm::vec3 { - return DependencyManager::get()->getMyAvatar()->getPosition(); -}; +glm::vec3 FloatingUIPanel::getComputedAnchorPosition() const { + glm::vec3 pos = {}; + if (getAttachedPanel()) { + pos = getAttachedPanel()->getPosition(); + } else if (_anchorPositionBindMyAvatar) { + pos = DependencyManager::get()->getMyAvatar()->getPosition(); + } + return pos + getAnchorPosition(); +} -std::function const FloatingUIPanel::AVATAR_ORIENTATION = []() -> glm::quat { - return DependencyManager::get()->getMyAvatar()->getOrientation() * - glm::angleAxis(glm::pi(), IDENTITY_UP); -}; +glm::quat FloatingUIPanel::getComputedOffsetRotation() const { + glm::quat rot = {1, 0, 0, 0}; + if (getAttachedPanel()) { + rot = getAttachedPanel()->getRotation(); + } else if (_offsetRotationBindMyAvatar) { + rot = DependencyManager::get()->getMyAvatar()->getOrientation() * + glm::angleAxis(glm::pi(), IDENTITY_UP); + } + return rot * getOffsetRotation(); +} glm::vec3 FloatingUIPanel::getPosition() const { - return getOffsetRotation() * getOffsetPosition() + getAnchorPosition(); + return getComputedOffsetRotation() * getOffsetPosition() + getComputedAnchorPosition(); } glm::quat FloatingUIPanel::getRotation() const { - return getOffsetRotation() * getFacingRotation(); -} - -void FloatingUIPanel::setAnchorPosition(const glm::vec3& position) { - setAnchorPosition([position]() -> glm::vec3 { - return position; - }); -} - -void FloatingUIPanel::setOffsetRotation(const glm::quat& rotation) { - setOffsetRotation([rotation]() -> glm::quat { - return rotation; - }); -} - -void FloatingUIPanel::setAttachedPanel(unsigned int panelID) { - if (panelID) { - attachAnchorToPanel(panelID); - attachRotationToPanel(panelID); - } - _attachedPanel = panelID; + return getComputedOffsetRotation() * getFacingRotation(); } void FloatingUIPanel::addChild(unsigned int childId) { @@ -73,9 +65,25 @@ QScriptValue FloatingUIPanel::getProperty(const QString &property) { if (property == "anchorPosition") { return vec3toScriptValue(_scriptEngine, getAnchorPosition()); } + if (property == "anchorPositionBinding") { + QScriptValue obj = _scriptEngine->newObject(); + if (_anchorPositionBindMyAvatar) { + obj.setProperty("avatar", "MyAvatar"); + } + obj.setProperty("computed", vec3toScriptValue(_scriptEngine, getComputedAnchorPosition())); + return obj; + } if (property == "offsetRotation") { return quatToScriptValue(_scriptEngine, getOffsetRotation()); } + if (property == "offsetRotationBinding") { + QScriptValue obj = _scriptEngine->newObject(); + if (_offsetRotationBindMyAvatar) { + obj.setProperty("avatar", "MyAvatar"); + } + obj.setProperty("computed", quatToScriptValue(_scriptEngine, getComputedOffsetRotation())); + return obj; + } if (property == "offsetPosition") { return vec3toScriptValue(_scriptEngine, getOffsetPosition()); } @@ -87,80 +95,56 @@ QScriptValue FloatingUIPanel::getProperty(const QString &property) { } void FloatingUIPanel::setProperties(const QScriptValue &properties) { - QScriptValue anchor = properties.property("anchorPosition"); - if (anchor.isValid()) { - QScriptValue bindType = anchor.property("bind"); - QScriptValue value = anchor.property("value"); + QScriptValue anchorPosition = properties.property("anchorPosition"); + if (anchorPosition.isValid()) { + QScriptValue x = anchorPosition.property("x"); + QScriptValue y = anchorPosition.property("y"); + QScriptValue z = anchorPosition.property("z"); + if (x.isValid() && y.isValid() && z.isValid()) { + glm::vec3 newPosition; + newPosition.x = x.toVariant().toFloat(); + newPosition.y = y.toVariant().toFloat(); + newPosition.z = z.toVariant().toFloat(); + setAnchorPosition(newPosition); + } + } - if (bindType.isValid()) { - QString bindTypeString = bindType.toVariant().toString(); - if (bindTypeString == "myAvatar") { - setAnchorPosition(AVATAR_POSITION); - } else if (value.isValid()) { - if (bindTypeString == "overlay") { - Overlay::Pointer overlay = Application::getInstance()->getOverlays() - .getOverlay(value.toVariant().toUInt()); - if (overlay->is3D()) { - auto overlay3D = std::static_pointer_cast(overlay); - setAnchorPosition([&overlay3D]() -> glm::vec3 { - return overlay3D->getPosition(); - }); - } - } else if (bindTypeString == "panel") { - attachAnchorToPanel(value.toVariant().toUInt()); - } else if (bindTypeString == "vec3") { - QScriptValue x = value.property("x"); - QScriptValue y = value.property("y"); - QScriptValue z = value.property("z"); - if (x.isValid() && y.isValid() && z.isValid()) { - glm::vec3 newPosition; - newPosition.x = x.toVariant().toFloat(); - newPosition.y = y.toVariant().toFloat(); - newPosition.z = z.toVariant().toFloat(); - setAnchorPosition(newPosition); - } - } - } + QScriptValue anchorPositionBinding = properties.property("anchorPositionBinding"); + if (anchorPositionBinding.isValid()) { + _anchorPositionBindMyAvatar = false; + + QScriptValue avatar = anchorPositionBinding.property("avatar"); + + if (avatar.isValid()) { + _anchorPositionBindMyAvatar = (avatar.toVariant().toString() == "MyAvatar"); } } QScriptValue offsetRotation = properties.property("offsetRotation"); if (offsetRotation.isValid()) { - QScriptValue bindType = offsetRotation.property("bind"); - QScriptValue value = offsetRotation.property("value"); + QScriptValue x = offsetRotation.property("x"); + QScriptValue y = offsetRotation.property("y"); + QScriptValue z = offsetRotation.property("z"); + QScriptValue w = offsetRotation.property("w"); - if (bindType.isValid()) { - QString bindTypeString = bindType.toVariant().toString(); - if (bindTypeString == "myAvatar") { - setOffsetRotation(AVATAR_ORIENTATION); - } else if (value.isValid()) { - if (bindTypeString == "overlay") { - Overlay::Pointer overlay = Application::getInstance()->getOverlays() - .getOverlay(value.toVariant().toUInt()); - if (overlay->is3D()) { - auto overlay3D = std::static_pointer_cast(overlay); - setOffsetRotation([&overlay3D]() -> glm::quat { - return overlay3D->getRotation(); - }); - } - } else if (bindTypeString == "panel") { - attachRotationToPanel(value.toVariant().toUInt()); - } else if (bindTypeString == "quat") { - QScriptValue x = value.property("x"); - QScriptValue y = value.property("y"); - QScriptValue z = value.property("z"); - QScriptValue w = value.property("w"); + if (x.isValid() && y.isValid() && z.isValid() && w.isValid()) { + glm::quat newRotation; + newRotation.x = x.toVariant().toFloat(); + newRotation.y = y.toVariant().toFloat(); + newRotation.z = z.toVariant().toFloat(); + newRotation.w = w.toVariant().toFloat(); + setOffsetRotation(newRotation); + } + } - if (x.isValid() && y.isValid() && z.isValid() && w.isValid()) { - glm::quat newRotation; - newRotation.x = x.toVariant().toFloat(); - newRotation.y = y.toVariant().toFloat(); - newRotation.z = z.toVariant().toFloat(); - newRotation.w = w.toVariant().toFloat(); - setOffsetRotation(newRotation); - } - } - } + QScriptValue offsetRotationBinding = properties.property("offsetRotationBinding"); + if (offsetRotationBinding.isValid()) { + _offsetRotationBindMyAvatar = false; + + QScriptValue avatar = offsetRotationBinding.property("avatar"); + + if (avatar.isValid()) { + _offsetRotationBindMyAvatar = (avatar.toVariant().toString() == "MyAvatar"); } } @@ -195,17 +179,3 @@ void FloatingUIPanel::setProperties(const QScriptValue &properties) { } } } - -void FloatingUIPanel::attachAnchorToPanel(unsigned int panelID) { - FloatingUIPanel::Pointer panel = Application::getInstance()->getOverlays().getPanel(panelID); - setAnchorPosition([panel]() -> glm::vec3 { - return panel->getPosition(); - }); -} - -void FloatingUIPanel::attachRotationToPanel(unsigned int panelID) { - FloatingUIPanel::Pointer panel = Application::getInstance()->getOverlays().getPanel(panelID); - setOffsetRotation([panel]() -> glm::quat { - return panel->getRotation(); - }); -} diff --git a/interface/src/ui/overlays/FloatingUIPanel.h b/interface/src/ui/overlays/FloatingUIPanel.h index 1b9c732b47..a45f57fe4d 100644 --- a/interface/src/ui/overlays/FloatingUIPanel.h +++ b/interface/src/ui/overlays/FloatingUIPanel.h @@ -12,7 +12,6 @@ #ifndef hifi_FloatingUIPanel_h #define hifi_FloatingUIPanel_h -#include #include #include @@ -21,26 +20,29 @@ class FloatingUIPanel : public QObject { Q_OBJECT + public: typedef std::shared_ptr Pointer; void init(QScriptEngine* scriptEngine) { _scriptEngine = scriptEngine; } - glm::vec3 getAnchorPosition() const { return _anchorPosition(); } - glm::quat getOffsetRotation() const { return _offsetRotation(); } + // getters + glm::vec3 getAnchorPosition() const { return _anchorPosition; } + glm::vec3 getComputedAnchorPosition() const; + glm::quat getOffsetRotation() const { return _offsetRotation; } + glm::quat getComputedOffsetRotation() const; glm::vec3 getOffsetPosition() const { return _offsetPosition; } glm::quat getFacingRotation() const { return _facingRotation; } glm::vec3 getPosition() const; glm::quat getRotation() const; - unsigned int getAttachedPanel() const { return _attachedPanel; } + Pointer getAttachedPanel() const { return _attachedPanel; } - void setAnchorPosition(const std::function& func) { _anchorPosition = func; } - void setAnchorPosition(const glm::vec3& position); - void setOffsetRotation(const std::function& func) { _offsetRotation = func; } - void setOffsetRotation(const glm::quat& rotation); + // setters + void setAnchorPosition(const glm::vec3& position) { _anchorPosition = position; } + void setOffsetRotation(const glm::quat& rotation) { _offsetRotation = rotation; } void setOffsetPosition(const glm::vec3& position) { _offsetPosition = position; } void setFacingRotation(const glm::quat& rotation) { _facingRotation = rotation; } - void setAttachedPanel(unsigned int panelID); + void setAttachedPanel(Pointer panel) { _attachedPanel = panel; } const QList& getChildren() { return _children; } void addChild(unsigned int childId); @@ -51,17 +53,15 @@ public: void setProperties(const QScriptValue& properties); private: - static std::function const AVATAR_POSITION; - static std::function const AVATAR_ORIENTATION; + glm::vec3 _anchorPosition = {0, 0, 0}; + glm::quat _offsetRotation = {1, 0, 0, 0}; + glm::vec3 _offsetPosition = {0, 0, 0}; + glm::quat _facingRotation = {1, 0, 0, 0}; - void attachAnchorToPanel(unsigned int panelID); - void attachRotationToPanel(unsigned int panelID); + bool _anchorPositionBindMyAvatar = false; + bool _offsetRotationBindMyAvatar = false; - std::function _anchorPosition{AVATAR_POSITION}; - std::function _offsetRotation{AVATAR_ORIENTATION}; - glm::vec3 _offsetPosition{0, 0, 0}; - glm::quat _facingRotation{1, 0, 0, 0}; - unsigned int _attachedPanel{0}; + Pointer _attachedPanel = nullptr; QScriptEngine* _scriptEngine; QList _children; }; diff --git a/interface/src/ui/overlays/Overlays.cpp b/interface/src/ui/overlays/Overlays.cpp index a7762b3f81..a715001040 100644 --- a/interface/src/ui/overlays/Overlays.cpp +++ b/interface/src/ui/overlays/Overlays.cpp @@ -278,7 +278,7 @@ unsigned int Overlays::getAttachedPanel(unsigned int childId) const { if (attachable) { return _panels.key(attachable->getAttachedPanel()); } else if (_panels.contains(childId)) { - return getPanel(childId)->getAttachedPanel(); + return _panels.key(getPanel(childId)->getAttachedPanel()); } return 0; } @@ -299,13 +299,13 @@ void Overlays::setAttachedPanel(unsigned int childId, unsigned int panelId) { } } } else if (_panels.contains(childId)) { - auto child = getPanel(childId); + FloatingUIPanel::Pointer child = getPanel(childId); if (_panels.contains(panelId)) { auto panel = getPanel(panelId); panel->addChild(childId); - child->setAttachedPanel(panelId); + child->setAttachedPanel(panel); } else { - auto panel = getPanel(child->getAttachedPanel()); + auto panel = child->getAttachedPanel(); if (panel) { panel->removeChild(childId); child->setAttachedPanel(0); diff --git a/interface/src/ui/overlays/PanelAttachable.cpp b/interface/src/ui/overlays/PanelAttachable.cpp index 1de1efcf5e..fa029f12c9 100644 --- a/interface/src/ui/overlays/PanelAttachable.cpp +++ b/interface/src/ui/overlays/PanelAttachable.cpp @@ -29,8 +29,8 @@ PanelAttachable::PanelAttachable(const PanelAttachable* panelAttachable) : void PanelAttachable::setTransforms(Transform& transform) { if (getAttachedPanel()) { - transform.setTranslation(getAttachedPanel()->getAnchorPosition()); - transform.setRotation(getAttachedPanel()->getOffsetRotation()); + transform.setTranslation(getAttachedPanel()->getComputedAnchorPosition()); + transform.setRotation(getAttachedPanel()->getComputedOffsetRotation()); transform.postTranslate(getAttachedPanel()->getOffsetPosition()); transform.postRotate(getAttachedPanel()->getFacingRotation()); transform.postTranslate(getOffsetPosition());