From 698791295a6f2b60893619f1555a498084759e45 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 16 Mar 2017 18:37:58 -0700 Subject: [PATCH] Expose DriveKeys enum to JS --- interface/src/Application.cpp | 17 ++++--- interface/src/avatar/MyAvatar.cpp | 59 +++++++++++++------------ interface/src/avatar/MyAvatar.h | 48 +++++++++++--------- scripts/tutorials/entity_scripts/sit.js | 17 +++++-- 4 files changed, 79 insertions(+), 62 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index df9d10caa6..8cd5a9564a 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4383,16 +4383,16 @@ void Application::update(float deltaTime) { myAvatar->clearDriveKeys(); if (_myCamera.getMode() != CAMERA_MODE_INDEPENDENT) { if (!_controllerScriptingInterface->areActionsCaptured()) { - myAvatar->setDriveKey(TRANSLATE_Z, -1.0f * userInputMapper->getActionState(controller::Action::TRANSLATE_Z)); - myAvatar->setDriveKey(TRANSLATE_Y, userInputMapper->getActionState(controller::Action::TRANSLATE_Y)); - myAvatar->setDriveKey(TRANSLATE_X, userInputMapper->getActionState(controller::Action::TRANSLATE_X)); + myAvatar->setDriveKey(MyAvatar::TRANSLATE_Z, -1.0f * userInputMapper->getActionState(controller::Action::TRANSLATE_Z)); + myAvatar->setDriveKey(MyAvatar::TRANSLATE_Y, userInputMapper->getActionState(controller::Action::TRANSLATE_Y)); + myAvatar->setDriveKey(MyAvatar::TRANSLATE_X, userInputMapper->getActionState(controller::Action::TRANSLATE_X)); if (deltaTime > FLT_EPSILON) { - myAvatar->setDriveKey(PITCH, -1.0f * userInputMapper->getActionState(controller::Action::PITCH)); - myAvatar->setDriveKey(YAW, -1.0f * userInputMapper->getActionState(controller::Action::YAW)); - myAvatar->setDriveKey(STEP_YAW, -1.0f * userInputMapper->getActionState(controller::Action::STEP_YAW)); + myAvatar->setDriveKey(MyAvatar::PITCH, -1.0f * userInputMapper->getActionState(controller::Action::PITCH)); + myAvatar->setDriveKey(MyAvatar::YAW, -1.0f * userInputMapper->getActionState(controller::Action::YAW)); + myAvatar->setDriveKey(MyAvatar::STEP_YAW, -1.0f * userInputMapper->getActionState(controller::Action::STEP_YAW)); } } - myAvatar->setDriveKey(ZOOM, userInputMapper->getActionState(controller::Action::TRANSLATE_CAMERA_Z)); + myAvatar->setDriveKey(MyAvatar::ZOOM, userInputMapper->getActionState(controller::Action::TRANSLATE_CAMERA_Z)); } controller::Pose leftHandPose = userInputMapper->getPoseState(controller::Action::LEFT_HAND); @@ -5503,8 +5503,7 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri scriptEngine->registerGlobalObject("Rates", new RatesScriptingInterface(this)); // hook our avatar and avatar hash map object into this script engine - scriptEngine->registerGlobalObject("MyAvatar", getMyAvatar().get()); - qScriptRegisterMetaType(scriptEngine, audioListenModeToScriptValue, audioListenModeFromScriptValue); + getMyAvatar()->registerMetaTypes(scriptEngine); scriptEngine->registerGlobalObject("AvatarList", DependencyManager::get().data()); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 7b478b3726..401378fff1 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -228,6 +228,21 @@ MyAvatar::~MyAvatar() { _lookAtTargetAvatar.reset(); } +void MyAvatar::registerMetaTypes(QScriptEngine* engine) { + QScriptValue value = engine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater | QScriptEngine::ExcludeChildObjects); + engine->globalObject().setProperty("MyAvatar", value); + + QScriptValue driveKeys = engine->newObject(); + auto metaEnum = QMetaEnum::fromType(); + for (int i = 0; i < MAX_DRIVE_KEYS; ++i) { + driveKeys.setProperty(metaEnum.key(i), metaEnum.value(i)); + } + engine->globalObject().setProperty("DriveKeys", driveKeys); + + qScriptRegisterMetaType(engine, audioListenModeToScriptValue, audioListenModeFromScriptValue); + qScriptRegisterMetaType(engine, driveKeysToScriptValue, driveKeysFromScriptValue); +} + void MyAvatar::setOrientationVar(const QVariant& newOrientationVar) { Avatar::setOrientation(quatFromVariant(newOrientationVar)); } @@ -460,7 +475,7 @@ void MyAvatar::simulate(float deltaTime) { // When there are no step values, we zero out the last step pulse. // This allows a user to do faster snapping by tapping a control for (int i = STEP_TRANSLATE_X; !stepAction && i <= STEP_YAW; ++i) { - if (getDriveKey(i) != 0.0f) { + if (getDriveKey((DriveKeys)i) != 0.0f) { stepAction = true; } } @@ -2094,7 +2109,7 @@ void MyAvatar::clearDriveKeys() { _driveKeys.fill(0.0f); } -void MyAvatar::setDriveKey(int key, float val) { +void MyAvatar::setDriveKey(DriveKeys key, float val) { try { _driveKeys.at(key) = val; } catch (const std::exception&) { @@ -2102,11 +2117,11 @@ void MyAvatar::setDriveKey(int key, float val) { } } -float MyAvatar::getDriveKey(int key) const { +float MyAvatar::getDriveKey(DriveKeys key) const { return isDriveKeyDisabled(key) ? 0.0f : getRawDriveKey(key); } -float MyAvatar::getRawDriveKey(int key) const { +float MyAvatar::getRawDriveKey(DriveKeys key) const { try { return _driveKeys.at(key); } catch (const std::exception&) { @@ -2121,7 +2136,7 @@ void MyAvatar::relayDriveKeysToCharacterController() { } } -void MyAvatar::disableDriveKey(int key) { +void MyAvatar::disableDriveKey(DriveKeys key) { try { _disabledDriveKeys.set(key); } catch (const std::exception&) { @@ -2129,7 +2144,7 @@ void MyAvatar::disableDriveKey(int key) { } } -void MyAvatar::enableDriveKey(int key) { +void MyAvatar::enableDriveKey(DriveKeys key) { try { _disabledDriveKeys.reset(key); } catch (const std::exception&) { @@ -2137,27 +2152,7 @@ void MyAvatar::enableDriveKey(int key) { } } -void MyAvatar::disableDriveKeys(std::vector key) { - try { - std::for_each(std::begin(key), std::end(key), [&](int val){ - _disabledDriveKeys.set(val); - }); - } catch (const std::exception&) { - qCCritical(interfaceapp) << Q_FUNC_INFO << ": Index out of bounds"; - } -} - -void MyAvatar::enableDriveKeys(std::vector key) { - try { - std::for_each(std::begin(key), std::end(key), [&](int val) { - _disabledDriveKeys.reset(val); - }); - } catch (const std::exception&) { - qCCritical(interfaceapp) << Q_FUNC_INFO << ": Index out of bounds"; - } -} - -bool MyAvatar::isDriveKeyDisabled(int key) const { +bool MyAvatar::isDriveKeyDisabled(DriveKeys key) const { try { return _disabledDriveKeys.test(key); } catch (const std::exception&) { @@ -2251,7 +2246,15 @@ QScriptValue audioListenModeToScriptValue(QScriptEngine* engine, const AudioList } void audioListenModeFromScriptValue(const QScriptValue& object, AudioListenerMode& audioListenerMode) { - audioListenerMode = (AudioListenerMode)object.toUInt16(); + audioListenerMode = static_cast(object.toUInt16()); +} + +QScriptValue driveKeysToScriptValue(QScriptEngine* engine, const MyAvatar::DriveKeys& driveKeys) { + return driveKeys; +} + +void driveKeysFromScriptValue(const QScriptValue& object, MyAvatar::DriveKeys& driveKeys) { + driveKeys = static_cast(object.toUInt16()); } diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index cae445c226..9bcec1134f 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -31,20 +31,6 @@ class AvatarActionHold; class ModelItemID; -enum DriveKeys { - TRANSLATE_X = 0, - TRANSLATE_Y, - TRANSLATE_Z, - YAW, - STEP_TRANSLATE_X, - STEP_TRANSLATE_Y, - STEP_TRANSLATE_Z, - STEP_YAW, - PITCH, - ZOOM, - MAX_DRIVE_KEYS -}; - enum eyeContactTarget { LEFT_EYE, RIGHT_EYE, @@ -90,9 +76,26 @@ class MyAvatar : public Avatar { Q_PROPERTY(bool characterControllerEnabled READ getCharacterControllerEnabled WRITE setCharacterControllerEnabled) public: + enum DriveKeys { + TRANSLATE_X = 0, + TRANSLATE_Y, + TRANSLATE_Z, + YAW, + STEP_TRANSLATE_X, + STEP_TRANSLATE_Y, + STEP_TRANSLATE_Z, + STEP_YAW, + PITCH, + ZOOM, + MAX_DRIVE_KEYS + }; + Q_ENUM(DriveKeys) + explicit MyAvatar(RigPointer rig); ~MyAvatar(); + void registerMetaTypes(QScriptEngine* engine); + virtual void simulateAttachments(float deltaTime) override; AudioListenerMode getAudioListenerModeHead() const { return FROM_HEAD; } @@ -182,16 +185,14 @@ public: // Set what driving keys are being pressed to control thrust levels void clearDriveKeys(); - void setDriveKey(int key, float val); - float getDriveKey(int key) const; - Q_INVOKABLE float getRawDriveKey(int key) const; + void setDriveKey(DriveKeys key, float val); + float getDriveKey(DriveKeys key) const; + Q_INVOKABLE float getRawDriveKey(DriveKeys key) const; void relayDriveKeysToCharacterController(); - Q_INVOKABLE void disableDriveKey(int key); - Q_INVOKABLE void enableDriveKey(int key); - Q_INVOKABLE void disableDriveKeys(std::vector key); - Q_INVOKABLE void enableDriveKeys(std::vector key); - Q_INVOKABLE bool isDriveKeyDisabled(int key) const; + Q_INVOKABLE void disableDriveKey(DriveKeys key); + Q_INVOKABLE void enableDriveKey(DriveKeys key); + Q_INVOKABLE bool isDriveKeyDisabled(DriveKeys key) const; eyeContactTarget getEyeContactTarget(); @@ -552,4 +553,7 @@ private: QScriptValue audioListenModeToScriptValue(QScriptEngine* engine, const AudioListenerMode& audioListenerMode); void audioListenModeFromScriptValue(const QScriptValue& object, AudioListenerMode& audioListenerMode); +QScriptValue driveKeysToScriptValue(QScriptEngine* engine, const MyAvatar::DriveKeys& driveKeys); +void driveKeysFromScriptValue(const QScriptValue& object, MyAvatar::DriveKeys& driveKeys); + #endif // hifi_MyAvatar_h diff --git a/scripts/tutorials/entity_scripts/sit.js b/scripts/tutorials/entity_scripts/sit.js index 420a77e2b8..82afdc8974 100644 --- a/scripts/tutorials/entity_scripts/sit.js +++ b/scripts/tutorials/entity_scripts/sit.js @@ -14,7 +14,14 @@ var DESKTOP_MAX_DISTANCE = 5; var SIT_DELAY = 25; var MAX_RESET_DISTANCE = 0.5; // meters - var OVERRIDEN_DRIVE_KEYS = [0, 1, 2, 4, 5, 6]; + var OVERRIDEN_DRIVE_KEYS = [ + DriveKeys.TRANSLATE_X, + DriveKeys.TRANSLATE_Y, + DriveKeys.TRANSLATE_Z, + DriveKeys.STEP_TRANSLATE_X, + DriveKeys.STEP_TRANSLATE_Y, + DriveKeys.STEP_TRANSLATE_Z, + ]; this.entityID = null; this.animStateHandlerID = null; @@ -125,14 +132,18 @@ return { headType: 0 }; }, ["headType"]); Script.update.connect(this, this.update); - MyAvatar.disableDriveKey(OVERRIDEN_DRIVE_KEYS); + for (var i in OVERRIDEN_DRIVE_KEYS) { + MyAvatar.disableDriveKey(OVERRIDEN_DRIVE_KEYS[i]); + } } this.standUp = function() { print("Standing up (" + this.entityID + ")"); MyAvatar.removeAnimationStateHandler(this.animStateHandlerID); Script.update.disconnect(this, this.update); - MyAvatar.enableDriveKey(OVERRIDEN_DRIVE_KEYS); + for (var i in OVERRIDEN_DRIVE_KEYS) { + MyAvatar.enableDriveKey(OVERRIDEN_DRIVE_KEYS[i]); + } this.setSeatUser(null); if (Settings.getValue(SETTING_KEY) === this.entityID) {