Re-work panel position and rotation binding.

This commit is contained in:
Zander Otavka 2015-08-05 14:21:23 -07:00
parent 5778502c54
commit 481624b48c
7 changed files with 112 additions and 147 deletions

View file

@ -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 })
});
}

View file

@ -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);
Script.scriptEnding.connect(onScriptEnd);

View file

@ -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);

View file

@ -20,41 +20,33 @@
#include "Application.h"
#include "Base3DOverlay.h"
std::function<glm::vec3()> const FloatingUIPanel::AVATAR_POSITION = []() -> glm::vec3 {
return DependencyManager::get<AvatarManager>()->getMyAvatar()->getPosition();
};
glm::vec3 FloatingUIPanel::getComputedAnchorPosition() const {
glm::vec3 pos = {};
if (getAttachedPanel()) {
pos = getAttachedPanel()->getPosition();
} else if (_anchorPositionBindMyAvatar) {
pos = DependencyManager::get<AvatarManager>()->getMyAvatar()->getPosition();
}
return pos + getAnchorPosition();
}
std::function<glm::quat()> const FloatingUIPanel::AVATAR_ORIENTATION = []() -> glm::quat {
return DependencyManager::get<AvatarManager>()->getMyAvatar()->getOrientation() *
glm::angleAxis(glm::pi<float>(), 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<AvatarManager>()->getMyAvatar()->getOrientation() *
glm::angleAxis(glm::pi<float>(), 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<Base3DOverlay>(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<Base3DOverlay>(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();
});
}

View file

@ -12,7 +12,6 @@
#ifndef hifi_FloatingUIPanel_h
#define hifi_FloatingUIPanel_h
#include <functional>
#include <memory>
#include <glm/glm.hpp>
@ -21,26 +20,29 @@
class FloatingUIPanel : public QObject {
Q_OBJECT
public:
typedef std::shared_ptr<FloatingUIPanel> 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<glm::vec3()>& func) { _anchorPosition = func; }
void setAnchorPosition(const glm::vec3& position);
void setOffsetRotation(const std::function<glm::quat()>& 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<unsigned int>& getChildren() { return _children; }
void addChild(unsigned int childId);
@ -51,17 +53,15 @@ public:
void setProperties(const QScriptValue& properties);
private:
static std::function<glm::vec3()> const AVATAR_POSITION;
static std::function<glm::quat()> 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<glm::vec3()> _anchorPosition{AVATAR_POSITION};
std::function<glm::quat()> _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<unsigned int> _children;
};

View file

@ -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);

View file

@ -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());