From 43635f08ebf7c813b69c0d02c39062f83b0826f3 Mon Sep 17 00:00:00 2001
From: Edward Rudd <urkle@outoforder.cc>
Date: Tue, 11 Aug 2015 21:59:53 +0000
Subject: [PATCH] add ability to register JS functions for the callbacks as well.

---
 client/library/library_humble.js |   41 ++++++++++++++++++++++++++++++-----------
 1 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/client/library/library_humble.js b/client/library/library_humble.js
index 022f3fc..0f23c07 100644
--- a/client/library/library_humble.js
+++ b/client/library/library_humble.js
@@ -35,19 +35,32 @@
              */
             setPlayerFullscreen: null
         },
+        _callCallback: function(callback, arg_types) {
+            var cbInfo = HUMBLE_API.callbacks[callback];
+            if (cbInfo == null) return false;
+            var cb = cbInfo[0],
+                data = cbInfo[1],
+                js = cbInfo[2],
+                args = Array.prototype.slice.call(arguments, 2);
+
+            args.push(data);
+
+            if (js) {
+                cb.apply(null, args);
+            } else {
+                Runtime.dynCall(arg_types, cb, args);
+            }
+            return true;
+        },
         /**
          * Attempts to call into the app to inform it that the volume should be adjusted
          * @param volume  the float volume (between 0.0 and 1.0)
          * @return bool.. true if the callback was successful.. false if no callback registered
          */
         setPlayerVolume: function(volume) {
-            if (HUMBLE_API.callbacks.setPlayerVolume == null) return false;
-            var cb = HUMBLE_API.callbacks.setPlayerVolume[0],
-                data = HUMBLE_API.callbacks.setPlayerVolume[1];
             if (volume > 1.0) volume = 1.0;
             if (volume < 0.0) volume = 0.0;
-            Runtime.dynCall('vdi', cb, [volume, data]);
-            return true;
+            return this._callCallback('setPlayerVolume', 'vdi', volume);
         },
         /**
          * Attempts to call into the app to inform it that it should change to or from fullscreen mode
@@ -55,11 +68,17 @@
          * @return bool.. true if the callback was successful.. false if no callback registered
          */
         setPlayerFullscreen: function(fullscreen) {
-            if (HUMBLE_API.callbacks.setPlayerFullscreen == null) return false;
-            var cb = HUMBLE_API.callbacks.setPlayerFullscreen[0],
-                data = HUMBLE_API.callbacks.setPlayerFullscreen[1];
-            Runtime.dynCall('vii', cb, [fullscreen ? 1 : 0, data]);
-            return true;
+            return this._callCallback('setPlayerFullscreen', 'vii', fullscreen ? 1 : 0);
+        },
+        /**
+         * setCallback
+         */
+        setCallback: function(type, cb, data) {
+            if (cb == null) {
+                HUMBLE_API.callbacks[type] = null;
+            } else {
+                HUMBLE_API.callbacks[type] = [cb, data, true];
+            }
         },
         /**
          * allows configuring the HUMBLE_API  simply call this method in a preRun to initialize..
@@ -282,7 +301,7 @@
         if (cb == 0) {
             HUMBLE_API.callbacks[key] = null;
         } else {
-            HUMBLE_API.callbacks[key] = [cb, data];
+            HUMBLE_API.callbacks[key] = [cb, data, false];
         }
         return 1;
     },

--
Gitblit v1.9.3