From 01436675b113c4b96128cbd0e05be7c5e40e8629 Mon Sep 17 00:00:00 2001
From: humbletim <humbletim@gmail.com>
Date: Wed, 21 Jun 2017 15:18:07 -0400
Subject: [PATCH] * recent changes in master broke event bridge; updated to
 latest scheme * fix update throttling for rapidly-changed values from the
 tablet UI * include default values for several advanced settings (so if user
 changes the RESET button works better) * reduce log spam

---
 .../camera-move/app-camera-move.js            | 20 +++++++++++++++----
 .../marketplace/camera-move/app.html          | 14 +++++++++----
 .../marketplace/camera-move/app.js            |  5 +++++
 .../marketplace/camera-move/hifi-jquery-ui.js |  8 ++++----
 .../marketplace/camera-move/modules/_utils.js |  4 +++-
 .../camera-move/modules/config-utils.js       |  6 +++---
 6 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/unpublishedScripts/marketplace/camera-move/app-camera-move.js b/unpublishedScripts/marketplace/camera-move/app-camera-move.js
index ae28e9edc1..f58dd3d3bd 100644
--- a/unpublishedScripts/marketplace/camera-move/app-camera-move.js
+++ b/unpublishedScripts/marketplace/camera-move/app-camera-move.js
@@ -81,6 +81,12 @@ var DEFAULTS = {
 
     'ui-enable-tooltips': true,
     'ui-show-advanced-options': false,
+
+    'Avatar/Draw Mesh': true,
+    'Scene/shouldRenderEntities': true,
+    'Scene/shouldRenderAvatars': true,
+    'Avatar/Show My Eye Vectors': false,
+    'Avatar/Show Other Eye Vectors': false,
 };
 
 // map setting names to/from corresponding Menu and API properties
@@ -203,8 +209,9 @@ function main() {
         log('settingsApp.valueUpdated: '+ key + ' = ' + JSON.stringify(value) + ' (was: ' + JSON.stringify(oldValue) + ')');
         if (/tablet/i.test(origin)) {
             // apply relevant settings immediately if changed from the tablet UI
-            log('applying immediate setting', key, value);
-            applicationConfig.applyValue(key, value, origin);
+            if (applicationConfig.applyValue(key, value, origin)) {
+                log('settingsApp applied immediate setting', key, value);
+            }
         }
     });
 
@@ -228,6 +235,9 @@ function main() {
             } break;
             case 'reset': {
                 var resetValues = {};
+                // maintain current value of 'show advanced' so user can observe any advanced settings being reset
+                var showAdvancedKey = cameraConfig.resolve('ui-show-advanced-options');
+                resetValues[showAdvancedKey] = cameraConfig.getValue(showAdvancedKey);
                 Object.keys(DEFAULTS).reduce(function(out, key) {
                     var resolved = cameraConfig.resolve(key);
                     out[resolved] = resolved in out ? out[resolved] : DEFAULTS[key];
@@ -396,8 +406,10 @@ function main() {
             'triggerReset: ' + triggerReset);
 
         if (/tablet/i.test(origin)) {
-            log('applying immediate setting', key, value);
-            applicationConfig.applyValue(key, value, origin);
+            if (applicationConfig.applyValue(key, value, origin)) {
+                log('cameraConfig applied immediate setting', key, value);
+            }
+
         }
         triggerReset && cameraControls.reset();
     });
diff --git a/unpublishedScripts/marketplace/camera-move/app.html b/unpublishedScripts/marketplace/camera-move/app.html
index b23ecd99ac..00df8e7514 100644
--- a/unpublishedScripts/marketplace/camera-move/app.html
+++ b/unpublishedScripts/marketplace/camera-move/app.html
@@ -119,6 +119,7 @@ try {
         function onValueReceived(key, value, oldValue, origin) {
             onMutationEvent.paused++;
             try {
+                //debugPrint('>>> onValueReceived', key, value, origin);
                 jquerySettings.setValue(key, value, origin);
             } finally {
                 setTimeout(function() { onMutationEvent.paused--; }, 1);
@@ -135,10 +136,11 @@ try {
 
         var THROTTLE_DELAY_MS = 500;
         function throttledSend(update) {
-            var hash = [update.key, update.method].join('-');
-            window.clearTimeout(throttledSend[hash]);
-            throttledSend[hash] = window.setTimeout(function() {
-                delete throttledSend[hash];
+            var $hash = [update.params[0], update.method].join('-'); // key-method
+            window.clearTimeout(throttledSend[$hash]);
+            throttledSend[$hash] = window.setTimeout(function() {
+                delete throttledSend[$hash];
+                //debugPrint('>>> throttled mutation event:', $hash, update.method, update.params);
                 bridgedSettings.sendEvent(update);
             }, THROTTLE_DELAY_MS);
         }
@@ -147,11 +149,14 @@ try {
         function onMutationEvent(event) {
             assert(onMutationEvent.paused >= 0);
             if (!onMutationEvent.paused) {
+                //debugPrint('>>> mutation event:', event.key, event.value);
                 throttledSend({
                     method: 'valueUpdated', params: [
                         event.key, event.value, event.oldValue, event.hifiType
                     ]
                 });
+            } else {
+                //debugPrint('>> (paused) mutation event:', event.key, event.value);
             }
         }
 
@@ -492,6 +497,7 @@ try {
                       trigger <b>MyAvatar &gt; Reset Sensors</b>;reset <b>bodyPitch</b> and <b>bodyYaw</b>
                     </span>
                   </button>
+                  <button title='test EventBridge.emitWebEvent' class='localhost-only' id='test-event-bridge'>EventBridge test</button>
                   <button title='reload the tablet app web view' class='localhost-only' id='page-reload'>refresh page</button>
                   <button title='reload the Client script' class='localhost-only' id='script-reload'>reload script</button>
                 </div>
diff --git a/unpublishedScripts/marketplace/camera-move/app.js b/unpublishedScripts/marketplace/camera-move/app.js
index 0ab6884bac..96805b9140 100644
--- a/unpublishedScripts/marketplace/camera-move/app.js
+++ b/unpublishedScripts/marketplace/camera-move/app.js
@@ -19,6 +19,11 @@ function setupUI() {
     var $json = SettingsJSON;
 
     window.buttonHandlers = {
+        'test-event-bridge': function() {
+            log('bridgedSettings.eventBridge === Window.EventBridge', bridgedSettings.eventBridge === window.EventBridge);
+            bridgedSettings.sendEvent({ method: 'test-event-bridge' });
+            EventBridge.emitWebEvent('EventBridge.emitWebEvent: testing 1..2..3..');
+        },
         'page-reload': function() {
             log('triggering location.reload');
             location.reload();
diff --git a/unpublishedScripts/marketplace/camera-move/hifi-jquery-ui.js b/unpublishedScripts/marketplace/camera-move/hifi-jquery-ui.js
index 5ad4cfe4d6..31f2186ee1 100644
--- a/unpublishedScripts/marketplace/camera-move/hifi-jquery-ui.js
+++ b/unpublishedScripts/marketplace/camera-move/hifi-jquery-ui.js
@@ -105,11 +105,11 @@ $.widget('ui.hifiButton', $.ui.button, {
             var checked = (dataset.checked === 'true');
             nv = (nv === 'true' || !!nv);
             if (nv !== checked) {
-                log('hifibutton checked changed', nv, checked);
+                debugPrint('hifibutton checked changed', nv, checked);
                 dataset.checked = nv;
                 this.element.change();
             } else {
-                log('hifibutton value same', nv, checked);
+                debugPrint('hifibutton value same', nv, checked);
             }
         }
         return dataset.checked === 'true';
@@ -133,11 +133,11 @@ $.widget('ui.hifiButton', $.ui.button, {
             this.element.find('.tooltip-target').removeClass('tooltip-target');
             this.element.prop('id', 'button-'+this.element.prop('id'));
             checkbox.element.on('change._hifiButton', function() {
-                log('checkbox -> button');
+                debugPrint('checkbox -> button');
                 this.value(checkbox.value());
             }.bind(this));
             this.element.on('change', function() {
-                log('button -> checkbox');
+                debugPrint('button -> checkbox');
                 checkbox.value(this.value());
             }.bind(this));
             this.checkbox = checkbox;
diff --git a/unpublishedScripts/marketplace/camera-move/modules/_utils.js b/unpublishedScripts/marketplace/camera-move/modules/_utils.js
index 4e25b603d4..8e57e5b7cf 100644
--- a/unpublishedScripts/marketplace/camera-move/modules/_utils.js
+++ b/unpublishedScripts/marketplace/camera-move/modules/_utils.js
@@ -244,7 +244,9 @@ function BrowserUtils(global) {
                     qt = assert(global.qt, 'expected global.qt to exist');
                 assert(qt.webChannelTransport, 'expected global.qt.webChannelTransport to exist');
                 new QWebChannel(qt.webChannelTransport, bind(this, function (channel) {
-                    global.EventBridge = channel.objects.eventBridgeWrapper.eventBridge;
+                    var objects = channel.objects;
+                    global.EventBridge = objects.eventBridge || (objects.eventBridgeWrapper && objects.eventBridgeWrapper.eventBridge);
+                    assert(global.EventBridge, '!global.EventBridge');
                     global.EventBridge.$WebChannel = channel;
                     this.log('openEventBridge opened -- invoking callback', 'typeof EventBridge === ' + typeof global.EventBridge);
                     callback(global.EventBridge);
diff --git a/unpublishedScripts/marketplace/camera-move/modules/config-utils.js b/unpublishedScripts/marketplace/camera-move/modules/config-utils.js
index 62d614d379..3b037edbea 100644
--- a/unpublishedScripts/marketplace/camera-move/modules/config-utils.js
+++ b/unpublishedScripts/marketplace/camera-move/modules/config-utils.js
@@ -93,12 +93,12 @@ ApplicationConfig.prototype = {
     applyValue: function applyValue(key, value, origin) {
         if (this.resolve(key)) {
             var appValue = this.getValue(key, value);
-            log('applyValue', key, value, origin ? '['+origin+']' : '', appValue);
+            debugPrint('applyValue', key, value, origin ? '['+origin+']' : '', appValue);
             if (appValue !== value) {
                 this.setValue(key, value);
-                log('applied new setting', key, value, '(was:'+appValue+')');
+                debugPrint('applied new setting', key, value, '(was:'+appValue+')');
+                return true;
             }
-            return true;
         }
     }
 };