History

Added local only spatialized button click sounds to tablet

Anthony J. Thibault committed 9 years ago

Commit 85de0d31 parent c972e9a7 files · +129 −6Browse files

Showing the changes to libraries/script-engine/src/TabletScriptingInterface.cpp (1 of 7 files). Show all

Unified · Split

libraries/script-engine/src/TabletScriptingInterface.cpp+57−1
@@ -10,8 +10,18 @@ · 9 unmodified lines
1010
1111 #include <QtCore/QThread>
1212
13+#include <AudioInjector.h>
14+#include <PathUtils.h>
1315 #include "ScriptEngineLogging.h"
1416
17+TabletScriptingInterface::TabletScriptingInterface() {
18+ qmlRegisterType<SoundEffect>("Hifi", 1, 0, "SoundEffect");
19+}
20+
21+TabletScriptingInterface::~TabletScriptingInterface() {
22+ qDebug() << "Shutting down TabletScriptingInterface";
23+}
24+
1525 QObject* TabletScriptingInterface::getTablet(const QString& tabletId) {
1626
1727 std::lock_guard<std::mutex> guard(_mutex);
@@ -31,7 +41,7 @@ QObject* TabletScriptingInterface::getTablet(const QString& tabletId) { · 13 unmodified lines
3141
3242 void TabletScriptingInterface::setQmlTabletRoot(QString tabletId, QQuickItem* qmlTabletRoot, QObject* qmlOffscreenSurface) {
3343 TabletProxy* tablet = qobject_cast<TabletProxy*>(getTablet(tabletId));
34− if (tablet && qmlOffscreenSurface) {
44+ if (tablet) {
3545 tablet->setQmlTabletRoot(qmlTabletRoot, qmlOffscreenSurface);
3646 } else {
3747 qCWarning(scriptengine) << "TabletScriptingInterface::setupTablet() bad tablet object";
@@ -155,6 +165,10 @@ void TabletProxy::updateAudioBar(const double micLevel) { · 117 unmodified lines
155165 }
156166 }
157167
168+void TabletProxy::updateTabletPosition(glm::vec3 tabletPosition) {
169+ _position.store(tabletPosition);
170+}
171+
158172 void TabletProxy::emitScriptEvent(QVariant msg) {
159173 if (_qmlOffscreenSurface) {
160174 QMetaObject::invokeMethod(_qmlOffscreenSurface, "emitScriptEvent", Qt::AutoConnection, Q_ARG(QVariant, msg));
@@ -233,4 +247,46 @@ void TabletButtonProxy::editProperties(QVariantMap properties) { · 72 unmodified lines
233247 }
234248 }
235249
250+//
251+// SoundEffect
252+//
253+
254+SoundEffect::~SoundEffect() {
255+ if (_sound) {
256+ _sound->deleteLater();
257+ }
258+ if (_injector) {
259+ _injector->deleteLater();
260+ }
261+}
262+
263+QUrl SoundEffect::getSource() const {
264+ return _url;
265+}
266+
267+void SoundEffect::setSource(QUrl url) {
268+ _url = url;
269+ _sound = DependencyManager::get<SoundCache>()->getSound(_url);
270+}
271+
272+void SoundEffect::play() {
273+ auto tsi = DependencyManager::get<TabletScriptingInterface>();
274+ if (tsi) {
275+ TabletProxy* tablet = qobject_cast<TabletProxy*>(tsi->getTablet("com.highfidelity.interface.tablet.system"));
276+ if (tablet) {
277+ AudioInjectorOptions options;
278+ options.position = tablet->getPosition();
279+ options.localOnly = true;
280+ if (_injector) {
281+ _injector->setOptions(options);
282+ _injector->restart();
283+ } else {
284+ QByteArray samples = _sound->getByteArray();
285+ _injector = AudioInjector::playSound(samples, options);
286+ }
287+ }
288+ }
289+}
290+
236291 #include "TabletScriptingInterface.moc"
292+