Use bind() instead of that = this

master
William Toohey 10 years ago
parent b4a278f630
commit e5c01801d3
  1. 42
      js/HuesCore.js
  2. 23
      js/HuesSettings.js
  3. 67
      js/HuesUI.js
  4. 151
      js/ResourceManager.js
  5. 69
      js/ResourcePack.js
  6. 69
      js/SoundManager.js

@ -108,12 +108,11 @@ function HuesCore(defaults) {
settingsupdated : [] settingsupdated : []
}; };
var that = this;
window.onerror = function(msg, url, line, col, error) { window.onerror = function(msg, url, line, col, error) {
that.error(msg); this.error(msg);
// Get more info in console // Get more info in console
return false; return false;
}; }.bind(this);
console.log("0x40 Hues - start your engines!"); console.log("0x40 Hues - start your engines!");
this.colours = this.oldColours; this.colours = this.oldColours;
@ -129,8 +128,8 @@ function HuesCore(defaults) {
return; return;
} }
setInterval(function() { setInterval(function() {
that.loopCheck(); this.loopCheck();
}, 1000); }.bind(this), 1000);
this.renderer = new HuesCanvas("waifu", this.soundManager.context, this); this.renderer = new HuesCanvas("waifu", this.soundManager.context, this);
this.visualiser = document.createElement("canvas"); this.visualiser = document.createElement("canvas");
@ -148,18 +147,18 @@ function HuesCore(defaults) {
this.resourceManager.addAll(defaults.respacks, function() { this.resourceManager.addAll(defaults.respacks, function() {
document.getElementById("preloadHelper").classList.add("loaded"); document.getElementById("preloadHelper").classList.add("loaded");
if(defaults.firstImage) { if(defaults.firstImage) {
that.setImageByName(defaults.firstImage); this.setImageByName(defaults.firstImage);
} else { } else {
that.setImage(0); this.setImage(0);
} }
if(defaults.autoplay) { if(defaults.autoplay) {
if(defaults.firstSong) { if(defaults.firstSong) {
that.setSongByName(defaults.firstSong); this.setSongByName(defaults.firstSong);
} else { } else {
that.setSong(0); this.setSong(0);
} }
} }
}, function(progress) { }.bind(this), function(progress) {
var prog = document.getElementById("preMain"); var prog = document.getElementById("preMain");
var scale = Math.floor(progress * defaults.preloadMax); var scale = Math.floor(progress * defaults.preloadMax);
var padding = defaults.preloadMax.toString(defaults.preloadBase).length; var padding = defaults.preloadMax.toString(defaults.preloadBase).length;
@ -182,8 +181,8 @@ function HuesCore(defaults) {
return true; return true;
} }
var key = e.keyCode || e.which; var key = e.keyCode || e.which;
return that.keyHandler(key); return this.keyHandler(key);
}; }.bind(this);
this.animationLoop(); this.animationLoop();
} }
@ -258,8 +257,7 @@ HuesCore.prototype.updateVisualiser = function() {
} }
HuesCore.prototype.animationLoop = function() { HuesCore.prototype.animationLoop = function() {
var that = this; requestAnimationFrame(this.animationLoop.bind(this));
requestAnimationFrame(function() {that.animationLoop();});
if(!this.soundManager.playing) { if(!this.soundManager.playing) {
this.callEventListeners("frame"); this.callEventListeners("frame");
return; return;
@ -349,12 +347,11 @@ HuesCore.prototype.setSong = function(index) {
} }
} }
this.setInvert(false); this.setInvert(false);
var that = this;
this.soundManager.playSong(this.currentSong, this.doBuildup, function() { this.soundManager.playSong(this.currentSong, this.doBuildup, function() {
that.resetAudio(); this.resetAudio();
that.fillBuildup(); this.fillBuildup();
that.callEventListeners("songstarted"); this.callEventListeners("songstarted");
}); }.bind(this));
}; };
HuesCore.prototype.fillBuildup = function() { HuesCore.prototype.fillBuildup = function() {
@ -417,7 +414,6 @@ HuesCore.prototype.onLoop = function() {
HuesCore.prototype.doAutoSong = function() { HuesCore.prototype.doAutoSong = function() {
var func = null; var func = null;
var that = this;
if(localStorage["autoSongShuffle"] == "on") { if(localStorage["autoSongShuffle"] == "on") {
func = this.randomSong; func = this.randomSong;
} else { } else {
@ -425,10 +421,10 @@ HuesCore.prototype.doAutoSong = function() {
} }
if(localStorage["autoSongFadeout"] == "on") { if(localStorage["autoSongFadeout"] == "on") {
this.soundManager.fadeOut(function() { this.soundManager.fadeOut(function() {
func.call(that); func.call(this);
}); }.bind(this));
} else { } else {
func.call(that); func.call(this);
} }
} }

@ -215,8 +215,9 @@ function HuesSettings(defaults) {
} }
// because we still care about the main window // because we still care about the main window
var that = this; document.getElementById("closeButton").onclick = function() {
document.getElementById("closeButton").onclick = function() {that.hide();}; this.hide();
}.bind(this);
if(!this.defaults.noUI) { if(!this.defaults.noUI) {
this.initUI(); this.initUI();
} }
@ -265,8 +266,6 @@ HuesSettings.prototype.showInfo = function() {
HuesSettings.prototype.initUI = function() { HuesSettings.prototype.initUI = function() {
var doc = this.root.ownerDocument; var doc = this.root.ownerDocument;
var that = this;
// To order things nicely // To order things nicely
for(var cat in this.settingsCategories) { for(var cat in this.settingsCategories) {
if(this.settingsCategories.hasOwnProperty(cat)) { if(this.settingsCategories.hasOwnProperty(cat)) {
@ -294,9 +293,9 @@ HuesSettings.prototype.initUI = function() {
if(localStorage[setName] == option) { if(localStorage[setName] == option) {
checkbox.checked = true; checkbox.checked = true;
} }
checkbox.onclick = function() { checkbox.onclick = function(self) {
that.set(this.name, this.value); self.set(this.name, this.value);
}; }.bind(checkbox, this);
buttonContainer.appendChild(checkbox); buttonContainer.appendChild(checkbox);
// So we can style this nicely // So we can style this nicely
var label = doc.createElement("label"); var label = doc.createElement("label");
@ -317,18 +316,16 @@ HuesSettings.prototype.initUI = function() {
input.value = localStorage[option.variable]; input.value = localStorage[option.variable];
// TODO: support more than just positive ints when the need arises // TODO: support more than just positive ints when the need arises
if(option.inputType == "int") { if(option.inputType == "int") {
input.oninput = (function(variable) { input.oninput = (function(self, variable) {
return function() {
this.value = this.value.replace(/\D/g,''); this.value = this.value.replace(/\D/g,'');
if(this.value == "" || this.value < 1) { if(this.value == "" || this.value < 1) {
this.value = ""; this.value = "";
return; return;
} }
localStorage[variable] = this.value; localStorage[variable] = this.value;
that.updateConditionals(); self.updateConditionals();
that.core.settingsUpdated(); self.core.settingsUpdated();
} }.bind(input, this, option.variable));
})(option.variable);
} }
input.autofocus = false; input.autofocus = false;
buttonContainer.appendChild(input); buttonContainer.appendChild(input);

@ -76,8 +76,6 @@ HuesUI.prototype.addCoreCallback = function(name, func) {
} }
HuesUI.prototype.initUI = function() { HuesUI.prototype.initUI = function() {
var that = this;
// Major info, image, song names // Major info, image, song names
var imageName = document.createElement("div"); var imageName = document.createElement("div");
this.imageName = imageName; this.imageName = imageName;
@ -99,28 +97,28 @@ HuesUI.prototype.initUI = function() {
// Prev/next controls // Prev/next controls
var imagePrev = document.createElement("div"); var imagePrev = document.createElement("div");
imagePrev.textContent = "<"; imagePrev.textContent = "<";
imagePrev.onclick = function() {that.core.previousImage();}; imagePrev.onclick = function() {this.core.previousImage();}.bind(this);
this.imagePrev = imagePrev; this.imagePrev = imagePrev;
var imageNext = document.createElement("div"); var imageNext = document.createElement("div");
imageNext.textContent = ">"; imageNext.textContent = ">";
imageNext.onclick = function() {that.core.nextImage();}; imageNext.onclick = function() {this.core.nextImage();}.bind(this);
this.imageNext = imageNext; this.imageNext = imageNext;
var songPrev = document.createElement("div"); var songPrev = document.createElement("div");
songPrev.textContent = "<"; songPrev.textContent = "<";
this.songPrev = songPrev; this.songPrev = songPrev;
songPrev.onclick = function() {that.core.previousSong();}; songPrev.onclick = function() {this.core.previousSong();}.bind(this);
var songNext = document.createElement("div"); var songNext = document.createElement("div");
songNext.textContent = ">"; songNext.textContent = ">";
songNext.onclick = function() {that.core.nextSong();}; songNext.onclick = function() {this.core.nextSong();}.bind(this);
this.songNext = songNext; this.songNext = songNext;
var songList = document.createElement("div"); var songList = document.createElement("div");
songList.textContent = "SONGS"; songList.textContent = "SONGS";
songList.onclick = function() {that.core.toggleSongList();}; songList.onclick = function() {this.core.toggleSongList();}.bind(this);
this.songList = songList; this.songList = songList;
var imageList = document.createElement("div"); var imageList = document.createElement("div");
imageList.textContent = "IMAGES"; imageList.textContent = "IMAGES";
imageList.onclick = function() {that.core.toggleImageList();}; imageList.onclick = function() {this.core.toggleImageList();}.bind(this);
this.imageList = imageList; this.imageList = imageList;
// Beat timer, x and y blur, millis timer // Beat timer, x and y blur, millis timer
@ -140,14 +138,14 @@ HuesUI.prototype.initUI = function() {
this.settingsToggle = document.createElement("div"); this.settingsToggle = document.createElement("div");
this.settingsToggle.innerHTML = '<i class="fa fa-cog"></i>'; this.settingsToggle.innerHTML = '<i class="fa fa-cog"></i>';
this.settingsToggle.onclick = function() { this.settingsToggle.onclick = function() {
that.core.settings.toggle(); this.core.settings.toggle();
}; }.bind(this);
this.hideToggle = document.createElement("div"); this.hideToggle = document.createElement("div");
this.hideToggle.innerHTML = "&#x25BC;"; this.hideToggle.innerHTML = "&#x25BC;";
this.hideToggle.onclick = function() { this.hideToggle.onclick = function() {
that.toggleHide(); this.toggleHide();
}; }.bind(this);
this.listContainer = document.createElement("div"); this.listContainer = document.createElement("div");
this.visualiserContainer = document.createElement("div"); this.visualiserContainer = document.createElement("div");
@ -284,8 +282,6 @@ RetroUI.prototype.constructor = RetroUI;
RetroUI.prototype.initUI = function() { RetroUI.prototype.initUI = function() {
HuesUI.prototype.initUI.call(this); HuesUI.prototype.initUI.call(this);
var that = this;
var container = document.createElement("div"); var container = document.createElement("div");
container.className = "hues-r-container"; container.className = "hues-r-container";
this.root.appendChild(container); this.root.appendChild(container);
@ -318,11 +314,15 @@ RetroUI.prototype.initUI = function() {
var imageMode = document.createElement("div"); var imageMode = document.createElement("div");
this.imageModeManual = document.createElement("div"); this.imageModeManual = document.createElement("div");
this.imageModeManual.textContent = "NORMAL"; this.imageModeManual.textContent = "NORMAL";
this.imageModeManual.onclick = function() {that.core.setIsFullAuto(false);}; this.imageModeManual.onclick = function() {
this.core.setIsFullAuto(false);
}.bind(this);
this.imageModeManual.className = "hues-r-manualmode hues-r-button"; this.imageModeManual.className = "hues-r-manualmode hues-r-button";
this.imageModeAuto = document.createElement("div"); this.imageModeAuto = document.createElement("div");
this.imageModeAuto.textContent = "FULL AUTO"; this.imageModeAuto.textContent = "FULL AUTO";
this.imageModeAuto.onclick = function() {that.core.setIsFullAuto(true);}; this.imageModeAuto.onclick = function() {
this.core.setIsFullAuto(true);
}.bind(this);
this.imageModeAuto.className = "hues-r-automode hues-r-button"; this.imageModeAuto.className = "hues-r-automode hues-r-button";
imageMode.appendChild(this.imageModeManual); imageMode.appendChild(this.imageModeManual);
imageMode.appendChild(this.imageModeAuto); imageMode.appendChild(this.imageModeAuto);
@ -356,8 +356,8 @@ RetroUI.prototype.initUI = function() {
this.hideRestore.className = "hues-r-hiderestore"; this.hideRestore.className = "hues-r-hiderestore";
this.hideRestore.innerHTML = "&#x25B2;"; this.hideRestore.innerHTML = "&#x25B2;";
this.hideRestore.onclick = function() { this.hideRestore.onclick = function() {
that.toggleHide(); this.toggleHide();
}; }.bind(this);
this.root.appendChild(this.hideRestore); this.root.appendChild(this.hideRestore);
this.listContainer.className = "hues-r-listcontainer"; this.listContainer.className = "hues-r-listcontainer";
@ -490,7 +490,7 @@ WeedUI.prototype.beat = function(beats, index) {
beatCenter.style.transform = transform; beatCenter.style.transform = transform;
beatCenter.textContent = beats[0].toUpperCase(); beatCenter.textContent = beats[0].toUpperCase();
this.root.appendChild(beatCenter); this.root.appendChild(beatCenter);
window.setTimeout(this.getRemoveBeat(beatCenter), 1500); window.setTimeout(this.removeBeat.bind(this, beatCenter), 1500);
} }
}; };
@ -498,11 +498,8 @@ WeedUI.prototype.round10 = function(num) {
return Math.round(num * 10) / 10; return Math.round(num * 10) / 10;
}; };
WeedUI.prototype.getRemoveBeat = function(element) { WeedUI.prototype.removeBeat = function(element) {
var that = this; this.root.removeChild(element);
return function() {
that.root.removeChild(element);
};
}; };
function ModernUI() { function ModernUI() {
@ -532,8 +529,6 @@ ModernUI.prototype.constructor = ModernUI;
ModernUI.prototype.initUI = function() { ModernUI.prototype.initUI = function() {
HuesUI.prototype.initUI.call(this); HuesUI.prototype.initUI.call(this);
var that = this;
this.imageName.className = "hues-m-imagename"; this.imageName.className = "hues-m-imagename";
this.songName.className = "hues-m-songtitle"; this.songName.className = "hues-m-songtitle";
@ -572,8 +567,8 @@ ModernUI.prototype.initUI = function() {
label.textContent = "VOL"; label.textContent = "VOL";
label.className = "hues-m-vol-label"; label.className = "hues-m-vol-label";
label.onclick = function() { label.onclick = function() {
that.core.soundManager.toggleMute(); this.core.soundManager.toggleMute();
}; }.bind(this);
volBar.appendChild(label); volBar.appendChild(label);
this.volLabel = label; this.volLabel = label;
@ -581,8 +576,8 @@ ModernUI.prototype.initUI = function() {
this.infoToggle.innerHTML = '?'; this.infoToggle.innerHTML = '?';
this.infoToggle.className = "hues-m-question"; this.infoToggle.className = "hues-m-question";
this.infoToggle.onclick = function() { this.infoToggle.onclick = function() {
that.core.settings.showInfo(); this.core.settings.showInfo();
}; }.bind(this);
volCluster.appendChild(this.infoToggle); volCluster.appendChild(this.infoToggle);
var input = document.createElement("input"); var input = document.createElement("input");
@ -593,8 +588,8 @@ ModernUI.prototype.initUI = function() {
volBar.appendChild(input); volBar.appendChild(input);
this.volInput = input; this.volInput = input;
input.oninput = function() { input.oninput = function() {
that.core.soundManager.setVolume(parseFloat(input.value)); this.core.soundManager.setVolume(parseFloat(input.value));
}; }.bind(this);
var rightBox = document.createElement("div"); var rightBox = document.createElement("div");
rightBox.className = "hues-m-rightbox"; rightBox.className = "hues-m-rightbox";
@ -614,7 +609,7 @@ ModernUI.prototype.initUI = function() {
this.songShuffle = document.createElement("div"); this.songShuffle = document.createElement("div");
this.songShuffle.innerHTML = '<i class="fa fa-random"></i>'; this.songShuffle.innerHTML = '<i class="fa fa-random"></i>';
this.songShuffle.className = "hues-m-actbutton"; this.songShuffle.className = "hues-m-actbutton";
this.songShuffle.onclick = function() {that.core.randomSong();}; this.songShuffle.onclick = function() {this.core.randomSong();}.bind(this);
songs.appendChild(this.songList); songs.appendChild(this.songList);
songControls.appendChild(this.songPrev); songControls.appendChild(this.songPrev);
songControls.appendChild(this.songShuffle); songControls.appendChild(this.songShuffle);
@ -633,7 +628,7 @@ ModernUI.prototype.initUI = function() {
this.imageMode = document.createElement("div"); this.imageMode = document.createElement("div");
this.imageMode.innerHTML = "&#9654;"; // PLAY this.imageMode.innerHTML = "&#9654;"; // PLAY
this.imageMode.className = "hues-m-actbutton"; this.imageMode.className = "hues-m-actbutton";
this.imageMode.onclick = function() {that.core.toggleFullAuto();}; this.imageMode.onclick = function() {this.core.toggleFullAuto();}.bind(this);
this.imagePrev.className = "hues-m-prevbutton"; this.imagePrev.className = "hues-m-prevbutton";
this.imageNext.className = "hues-m-nextbutton"; this.imageNext.className = "hues-m-nextbutton";
images.appendChild(this.imageList); images.appendChild(this.imageList);
@ -682,8 +677,8 @@ ModernUI.prototype.initUI = function() {
this.hideRestore = document.createElement("div"); this.hideRestore = document.createElement("div");
this.hideRestore.className = "hues-m-hiderestore"; this.hideRestore.className = "hues-m-hiderestore";
this.hideRestore.onclick = function() { this.hideRestore.onclick = function() {
that.toggleHide(); this.toggleHide();
}; }.bind(this);
this.root.appendChild(this.hideRestore); this.root.appendChild(this.hideRestore);
this.listContainer.className = "hues-m-listcontainer"; this.listContainer.className = "hues-m-listcontainer";

@ -80,7 +80,6 @@ function Resources(core) {
// Array of URLs to load, and a callback for when we're done // Array of URLs to load, and a callback for when we're done
// Preserves order of URLs being loaded // Preserves order of URLs being loaded
Resources.prototype.addAll = function(urls, callback, progressCallback) { Resources.prototype.addAll = function(urls, callback, progressCallback) {
var that = this;
this.toLoad += urls.length; this.toLoad += urls.length;
if(progressCallback) { if(progressCallback) {
this.progressCallback = progressCallback; this.progressCallback = progressCallback;
@ -93,31 +92,26 @@ Resources.prototype.addAll = function(urls, callback, progressCallback) {
var r = new Respack(); var r = new Respack();
this.rToLoad.push(r); this.rToLoad.push(r);
r.loadFromURL(urls[i], function() { r.loadFromURL(urls[i], function() {
that.toLoad--; this.toLoad--;
if(that.toLoad <= 0) { if(this.toLoad <= 0) {
// could use a while() and shift(), but it'd be slower // could use a while() and shift(), but it'd be slower
for(var i = 0; i < that.rToLoad.length; i++) { for(var i = 0; i < this.rToLoad.length; i++) {
that.addPack(that.rToLoad[i]); this.addPack(this.rToLoad[i]);
} }
that.rToLoad = []; this.rToLoad = [];
if(that.loadFinishCallback) { if(this.loadFinishCallback) {
that.loadFinishCallback(); this.loadFinishCallback();
that.loadFinishCallback = null; this.loadFinishCallback = null;
} }
that.progressCallback = null; this.progressCallback = null;
} }
}, this.createProgCallback(i)); }.bind(this), function(index, progress, pack) {
this.progressState[index] = progress;
this.updateProgress(pack);
}.bind(this, i));
} }
}; };
Resources.prototype.createProgCallback = function(i) {
var that = this;
return function(progress, pack) {
that.progressState[i] = progress;
that.updateProgress(pack);
};
};
Resources.prototype.updateProgress = function(pack) { Resources.prototype.updateProgress = function(pack) {
var total = 0; var total = 0;
for(var i = 0; i < this.progressState.length; i++) { for(var i = 0; i < this.progressState.length; i++) {
@ -129,19 +123,20 @@ Resources.prototype.updateProgress = function(pack) {
Resources.prototype.addPack = function(pack) { Resources.prototype.addPack = function(pack) {
console.log("Added", pack.name, "to respacks"); console.log("Added", pack.name, "to respacks");
var that = this;
var id = this.resourcePacks.length; var id = this.resourcePacks.length;
this.resourcePacks.push(pack); this.resourcePacks.push(pack);
this.addResourcesToArrays(pack); this.addResourcesToArrays(pack);
this.rebuildEnabled(); this.rebuildEnabled();
this.updateTotals(); this.updateTotals();
var self = this;
this.appendListItem("respacks", pack.name, "res" + id, this.packsView.respackList, this.appendListItem("respacks", pack.name, "res" + id, this.packsView.respackList,
function() { function() {
pack.enabled = this.checked; pack.enabled = this.checked;
that.rebuildEnabled(); self.rebuildEnabled();
}, }, function(id) {
this.selectPackCallback(id) this.selectPack(id);
}.bind(this, id)
); );
}; };
@ -193,31 +188,21 @@ Resources.prototype.rebuildEnabled = function() {
} }
for(var i = 0; i < this.enabledSongs.length; i++) { for(var i = 0; i < this.enabledSongs.length; i++) {
var song = this.enabledSongs[i]; var song = this.enabledSongs[i];
this.appendSimpleListItem(song.title, songList, this.playSongCallback(i)); this.appendSimpleListItem(song.title, songList, function(index) {
this.core.setSong(index);
}.bind(this, i));
} }
for(var i = 0; i < this.enabledImages.length; i++) { for(var i = 0; i < this.enabledImages.length; i++) {
var image = this.enabledImages[i]; var image = this.enabledImages[i];
this.appendSimpleListItem(image.name, imageList, this.selectImageCallback(i)); this.appendSimpleListItem(image.name, imageList, function(index) {
this.core.setImage(index);
this.core.setIsFullAuto(false);
}.bind(this, i));
} }
} }
this.updateTotals(); this.updateTotals();
}; };
Resources.prototype.playSongCallback = function(index) {
var that = this;
return function() {
that.core.setSong(index);
};
};
Resources.prototype.selectImageCallback = function(index) {
var that = this;
return function() {
that.core.setImage(index);
that.core.setIsFullAuto(false);
};
};
Resources.prototype.removePack = function(pack) { Resources.prototype.removePack = function(pack) {
var index = this.resourcePacks.indexOf(pack); var index = this.resourcePacks.indexOf(pack);
if (index != -1) { if (index != -1) {
@ -249,7 +234,6 @@ Resources.prototype.loadLocal = function() {
}; };
Resources.prototype.parseLocalQueue = function(recursing) { Resources.prototype.parseLocalQueue = function(recursing) {
var that = this;
// avoid race conditions // avoid race conditions
if(this.currentlyParsing && !recursing) { if(this.currentlyParsing && !recursing) {
return; return;
@ -259,12 +243,12 @@ Resources.prototype.parseLocalQueue = function(recursing) {
var r = new Respack(); var r = new Respack();
r.loadBlob(this.fileParseQueue.shift(), r.loadBlob(this.fileParseQueue.shift(),
function() { function() {
that.addPack(r); this.addPack(r);
that.localComplete(); this.localComplete();
that.parseLocalQueue(true); this.parseLocalQueue(true);
}, }.bind(this),
function(progress, respack) {that.localProgress(progress, respack);}, function(progress, respack) {this.localProgress(progress, respack);}.bind(this),
function() {that.parseLocalQueue(true);}); function() {this.parseLocalQueue(true);}.bind(this));
} else { } else {
console.log("Local respack parsing complete"); console.log("Local respack parsing complete");
this.currentlyParsing = false; this.currentlyParsing = false;
@ -294,7 +278,6 @@ Resources.prototype.localComplete = function(progress) {
Resources.prototype.initUI = function() { Resources.prototype.initUI = function() {
this.root = document.getElementById("huesResources"); this.root = document.getElementById("huesResources");
var that = this;
var packsContainer = document.createElement("div"); var packsContainer = document.createElement("div");
packsContainer.className = "res-packscontainer"; packsContainer.className = "res-packscontainer";
@ -318,7 +301,7 @@ Resources.prototype.initUI = function() {
remoteList.className = "res-list"; remoteList.className = "res-list";
remoteList.id = "res-remotelist"; remoteList.id = "res-remotelist";
this.appendSimpleListItem("Click to load the list", remoteList, this.appendSimpleListItem("Click to load the list", remoteList,
function() {that.loadRemotes();}); function() {this.loadRemotes();}.bind(this));
this.packsView.remoteList = remoteList; this.packsView.remoteList = remoteList;
} else { } else {
packList.className += " noremotes"; packList.className += " noremotes";
@ -329,11 +312,11 @@ Resources.prototype.initUI = function() {
var loadRemote = document.createElement("div"); var loadRemote = document.createElement("div");
loadRemote.className = "res-button hidden"; loadRemote.className = "res-button hidden";
loadRemote.textContent = "LOAD REMOTE"; loadRemote.textContent = "LOAD REMOTE";
loadRemote.onclick = function() {that.loadCurrentRemote();}; loadRemote.onclick = function() {this.loadCurrentRemote();}.bind(this);
var loadLocal = document.createElement("div"); var loadLocal = document.createElement("div");
loadLocal.className = "res-button"; loadLocal.className = "res-button";
loadLocal.textContent = "LOAD ZIPS"; loadLocal.textContent = "LOAD ZIPS";
loadLocal.onclick = function() {that.fileInput.click();}; loadLocal.onclick = function() {this.fileInput.click();}.bind(this);
buttons.appendChild(loadLocal); buttons.appendChild(loadLocal);
buttons.appendChild(loadRemote); buttons.appendChild(loadRemote);
this.packsView.loadRemote = loadRemote; this.packsView.loadRemote = loadRemote;
@ -342,7 +325,7 @@ Resources.prototype.initUI = function() {
this.fileInput.type ="file"; this.fileInput.type ="file";
this.fileInput.accept="application/zip"; this.fileInput.accept="application/zip";
this.fileInput.multiple = true; this.fileInput.multiple = true;
this.fileInput.onchange = function() {that.loadLocal();}; this.fileInput.onchange = function() {this.loadLocal();}.bind(this);
var progressContainer = document.createElement("div"); var progressContainer = document.createElement("div");
progressContainer.id = "res-progress-container"; progressContainer.id = "res-progress-container";
@ -448,15 +431,15 @@ Resources.prototype.initUI = function() {
var enableAll = document.createElement("div"); var enableAll = document.createElement("div");
enableAll.textContent = "ENABLE ALL"; enableAll.textContent = "ENABLE ALL";
enableAll.className = "res-button"; enableAll.className = "res-button";
enableAll.onclick = function() {that.enableAll();}; enableAll.onclick = function() {this.enableAll();}.bind(this);
var invert = document.createElement("div"); var invert = document.createElement("div");
invert.textContent = "INVERT"; invert.textContent = "INVERT";
invert.className = "res-button"; invert.className = "res-button";
invert.onclick = function() {that.invert();}; invert.onclick = function() {this.invert();}.bind(this);
var disableAll = document.createElement("div"); var disableAll = document.createElement("div");
disableAll.textContent = "DISABLE ALL"; disableAll.textContent = "DISABLE ALL";
disableAll.className = "res-button"; disableAll.className = "res-button";
disableAll.onclick = function() {that.disableAll();}; disableAll.onclick = function() {this.disableAll();}.bind(this);
packButtons.appendChild(enableAll); packButtons.appendChild(enableAll);
packButtons.appendChild(invert); packButtons.appendChild(invert);
packButtons.appendChild(disableAll); packButtons.appendChild(disableAll);
@ -590,7 +573,7 @@ Resources.prototype.selectPack = function(id) {
var song = pack.songs[i]; var song = pack.songs[i];
this.appendListItem("songs", song.title, "song" + i, songList, this.appendListItem("songs", song.title, "song" + i, songList,
this.selectResourceCallback(song), this.selectResourceCallback(song),
this.clickResourceCallback(song, true), this.clickResourceCallback.bind(this, song, true),
song.enabled); song.enabled);
} }
@ -598,41 +581,33 @@ Resources.prototype.selectPack = function(id) {
var image = pack.images[i]; var image = pack.images[i];
this.appendListItem("images", image.name, "image" + i, imageList, this.appendListItem("images", image.name, "image" + i, imageList,
this.selectResourceCallback(image), this.selectResourceCallback(image),
this.clickResourceCallback(image, false), this.clickResourceCallback.bind(this, image, false),
image.enabled); image.enabled);
} }
}; };
Resources.prototype.selectPackCallback = function(id) {
var that = this;
return function() {that.selectPack(id);};
};
Resources.prototype.selectResourceCallback = function(res) { Resources.prototype.selectResourceCallback = function(res) {
var that = this; var self = this;
return function() { return function() {
res.enabled = this.checked; res.enabled = this.checked;
that.rebuildEnabled(); self.rebuildEnabled();
}; };
}; };
Resources.prototype.clickResourceCallback = function(res, isSong) { Resources.prototype.clickResourceCallback = function(res, isSong) {
var that = this;
return function() {
if(!res.enabled) { if(!res.enabled) {
res.enabled = true; res.enabled = true;
that.rebuildEnabled(); this.rebuildEnabled();
// rebuild display // rebuild display
that.selectPack(that.resourcePacks.indexOf(that.packView.pack)); this.selectPack(this.resourcePacks.indexOf(this.packView.pack));
} }
if(isSong) { if(isSong) {
that.core.setSong(that.enabledSongs.indexOf(res)); this.core.setSong(this.enabledSongs.indexOf(res));
} else { } else {
that.core.setImage(that.enabledImages.indexOf(res)); this.core.setImage(this.enabledImages.indexOf(res));
that.core.setIsFullAuto(false); this.core.setIsFullAuto(false);
} }
}; };
};
Resources.prototype.getEnabledTabContents = function() { Resources.prototype.getEnabledTabContents = function() {
var pack = this.packView.pack; var pack = this.packView.pack;
@ -707,7 +682,6 @@ Resources.prototype.appendListItem = function(name, value, id, root, oncheck, on
}; };
Resources.prototype.loadRemotes = function() { Resources.prototype.loadRemotes = function() {
var that = this;
var remoteList = this.packsView.remoteList; var remoteList = this.packsView.remoteList;
while(remoteList.firstElementChild) { while(remoteList.firstElementChild) {
remoteList.removeChild(remoteList.firstElementChild); remoteList.removeChild(remoteList.firstElementChild);
@ -721,13 +695,13 @@ Resources.prototype.loadRemotes = function() {
if(!req.response) { if(!req.response) {
req.onerror(); req.onerror();
} }
that.remotes = req.response; this.remotes = req.response;
that.populateRemotes(); this.populateRemotes();
}; }.bind(this);
req.onerror = function() { req.onerror = function() {
item.textContent = "Could not load list! Click to try again"; item.textContent = "Could not load list! Click to try again";
item.onclick = function() {that.loadRemotes();}; item.onclick = function() {this.loadRemotes();}.bind(this);
}; }.bind(this);
req.send(); req.send();
}; };
@ -739,15 +713,12 @@ Resources.prototype.populateRemotes = function() {
for(var i = 0; i < this.remotes.length; i++) { for(var i = 0; i < this.remotes.length; i++) {
this.remotes[i].loaded = false; this.remotes[i].loaded = false;
this.appendSimpleListItem(this.remotes[i].name, remoteList, this.appendSimpleListItem(this.remotes[i].name, remoteList,
this.getRemoteCallback(i)); function(index) {
this.selectRemotePack(index);
}.bind(this, i));
} }
}; };
Resources.prototype.getRemoteCallback = function(index) {
var that = this;
return function() {that.selectRemotePack(index);};
};
Resources.prototype.selectRemotePack = function(id) { Resources.prototype.selectRemotePack = function(id) {
var pack = this.remotes[id]; var pack = this.remotes[id];
this.packView.pack = pack; this.packView.pack = pack;
@ -796,7 +767,6 @@ Resources.prototype.selectRemotePack = function(id) {
Resources.prototype.loadCurrentRemote = function() { Resources.prototype.loadCurrentRemote = function() {
var pack = this.packView.pack; var pack = this.packView.pack;
var that = this;
// Not actually a remote, ignore. How did you press this :< // Not actually a remote, ignore. How did you press this :<
if(pack.loaded === undefined || pack.loaded) { if(pack.loaded === undefined || pack.loaded) {
@ -805,11 +775,14 @@ Resources.prototype.loadCurrentRemote = function() {
// TODO Error checking on failure // TODO Error checking on failure
pack.loaded = true; pack.loaded = true;
that.packsView.loadRemote.className = "res-button loaded"; this.packsView.loadRemote.className = "res-button loaded";
that.packsView.loadRemote.textContent = "LOADING"; this.packsView.loadRemote.textContent = "LOADING";
this.addAll([pack.url], function() { this.addAll([pack.url], function() {
that.remoteComplete(); this.remoteComplete();
}, function(progress, respack) {that.remoteProgress(progress, respack);} }.bind(this),
function(progress, respack) {
this.remoteProgress(progress, respack);
}.bind(this)
); );
}; };

@ -75,51 +75,48 @@ Respack.prototype.updateProgress = function() {
}; };
Respack.prototype.loadFromURL = function(url, callback, progress) { Respack.prototype.loadFromURL = function(url, callback, progress) {
var that = this;
this.loadedFromURL = true; this.loadedFromURL = true;
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
req.open('GET', url, true); req.open('GET', url, true);
req.responseType = 'blob'; req.responseType = 'blob';
req.onload = function() { req.onload = function() {
that.loadBlob(req.response, callback, progress); this.loadBlob(req.response, callback, progress);
}; }.bind(this);
req.onerror = function() { req.onerror = function() {
console.log("Could not load respack at URL", url); console.log("Could not load respack at URL", url);
}; };
req.onprogress = function(event) { req.onprogress = function(event) {
if (event.lengthComputable) { if (event.lengthComputable) {
that.size = event.total; this.size = event.total;
that.downloaded = event.loaded; this.downloaded = event.loaded;
var percent = event.loaded / event.total; var percent = event.loaded / event.total;
if(progress) { if(progress) {
progress(percent / 2, that); // because of processing too progress(percent / 2, this); // because of processing too
} }
} else { } else {
// Unable to compute progress information since the total size is unknown // Unable to compute progress information since the total size is unknown
} }
}; }.bind(this);
req.send(); req.send();
}; };
Respack.prototype.loadBlob = function(blob, callback, progress, errorCallback) { Respack.prototype.loadBlob = function(blob, callback, progress, errorCallback) {
this._completionCallback = callback; this._completionCallback = callback;
this.progressCallback = progress; this.progressCallback = progress;
var that = this;
this.size = blob.size; this.size = blob.size;
this.file = new zip.fs.FS(); this.file = new zip.fs.FS();
this.file.importBlob(blob, this.file.importBlob(blob,
function() { // success function() { // success
that.parseWholeZip(); this.parseWholeZip();
}, }.bind(this),
function(error) { // failure function(error) { // failure
console.log("Error loading respack :", error.toString()); console.log("Error loading respack :", error.toString());
that.file = null; this.file = null;
if(errorCallback) { if(errorCallback) {
errorCallback(error.toString()); errorCallback(error.toString());
} }
} }.bind(this)
); );
}; };
@ -179,16 +176,14 @@ Respack.prototype.parseImage = function(file) {
}; };
Respack.prototype.parseXML = function() { Respack.prototype.parseXML = function() {
var that = this;
if (this._infoFile) { if (this._infoFile) {
this._infoFile.getText(function(text) { this._infoFile.getText(function(text) {
text = text.replace(/&amp;/g, '&'); text = text.replace(/&amp;/g, '&');
text = text.replace(/&/g, '&amp;'); text = text.replace(/&/g, '&amp;');
that.parseInfoFile(text); this.parseInfoFile(text);
that._infoFile = null; this._infoFile = null;
that.parseXML(); this.parseXML();
}); }.bind(this));
return; return;
} }
if (this.songs.length > 0) { if (this.songs.length > 0) {
@ -197,12 +192,12 @@ Respack.prototype.parseXML = function() {
//XML parser will complain about a bare '&', but some respacks use &amp //XML parser will complain about a bare '&', but some respacks use &amp
text = text.replace(/&amp;/g, '&'); text = text.replace(/&amp;/g, '&');
text = text.replace(/&/g, '&amp;'); text = text.replace(/&/g, '&amp;');
that.parseSongFile(text); this.parseSongFile(text);
// Go to next in series // Go to next in series
that._songFile = null; this._songFile = null;
that._songFileParsed = true; this._songFileParsed = true;
that.parseXML(); this.parseXML();
}); }.bind(this));
return; return;
} else if(!this._songFileParsed) { } else if(!this._songFileParsed) {
console.log("!!!", "Got songs but no songs.xml!"); console.log("!!!", "Got songs but no songs.xml!");
@ -213,10 +208,10 @@ Respack.prototype.parseXML = function() {
this._imageFile.getText(function(text) { this._imageFile.getText(function(text) {
text = text.replace(/&amp;/g, '&'); text = text.replace(/&amp;/g, '&');
text = text.replace(/&/g, '&amp;'); text = text.replace(/&/g, '&amp;');
that.parseImageFile(text); this.parseImageFile(text);
that._imageFile = null; this._imageFile = null;
that.parseXML(); this.parseXML();
}); }.bind(this));
return; return;
} }
@ -415,7 +410,6 @@ Respack.prototype.getImage = function(name) {
}; };
Respack.prototype.parseSongQueue = function() { Respack.prototype.parseSongQueue = function() {
var that = this;
var songFile = this.songQueue.shift(); var songFile = this.songQueue.shift();
var name = songFile.name.replace(this.audioExtensions, ""); var name = songFile.name.replace(this.audioExtensions, "");
@ -449,14 +443,14 @@ Respack.prototype.parseSongQueue = function() {
songFile.getBlob(mime, function(sound) { songFile.getBlob(mime, function(sound) {
// Because blobs are crap // Because blobs are crap
var fr = new FileReader(); var fr = new FileReader();
fr.onload = function() { fr.onload = function(self) {
newSong.sound = this.result; newSong.sound = this.result;
that.filesLoaded++; self.filesLoaded++;
that.updateProgress(); self.updateProgress();
that.tryFinish(); self.tryFinish();
}; }.bind(fr, this);
fr.readAsArrayBuffer(sound); fr.readAsArrayBuffer(sound);
}); }.bind(this));
this.songs.push(newSong); this.songs.push(newSong);
} }
}; };
@ -500,7 +494,6 @@ Respack.prototype.parseImageQueue = function() {
}; };
Respack.prototype.imageLoadStart = function(imgFile, imageObj) { Respack.prototype.imageLoadStart = function(imgFile, imageObj) {
var that = this;
var extension = imgFile.name.split('.').pop().toLowerCase(); var extension = imgFile.name.split('.').pop().toLowerCase();
var mime = ""; var mime = "";
switch(extension) { switch(extension) {
@ -518,8 +511,8 @@ Respack.prototype.imageLoadStart = function(imgFile, imageObj) {
mime = "application/octet-stream"; mime = "application/octet-stream";
} }
imgFile.getData64URI(mime, function(image) { imgFile.getData64URI(mime, function(image) {
that.imageLoadComplete(image, imageObj); this.imageLoadComplete(image, imageObj);
}); }.bind(this));
}; };
Respack.prototype.imageLoadComplete = function(imageBmp, imageObj) { Respack.prototype.imageLoadComplete = function(imageBmp, imageObj) {

@ -83,24 +83,22 @@ function SoundManager(core) {
return; return;
} }
var that = this;
window.addEventListener('touchend', function() { window.addEventListener('touchend', function() {
// create empty buffer // create empty buffer
var buffer = that.context.createBuffer(1, 1, 22050); var buffer = this.context.createBuffer(1, 1, 22050);
var source = that.context.createBufferSource(); var source = this.context.createBufferSource();
source.buffer = buffer; source.buffer = buffer;
// connect to output (your speakers) // connect to output (your speakers)
source.connect( that.context.destination); source.connect( this.context.destination);
// play the file // play the file
source.start(0); source.start(0);
}, false); }.bind(this), false);
} }
SoundManager.prototype.playSong = function(song, playBuild, callback) { SoundManager.prototype.playSong = function(song, playBuild, callback) {
var that = this;
if(this.song == song) { if(this.song == song) {
return; return;
} }
@ -120,50 +118,50 @@ SoundManager.prototype.playSong = function(song, playBuild, callback) {
this.loadBuffer(song, function() { this.loadBuffer(song, function() {
// To prevent race condition if you press "next" twice fast // To prevent race condition if you press "next" twice fast
if(song == that.song) { if(song == this.song) {
// more racing than the Melbourne Cup // more racing than the Melbourne Cup
try { try {
that.bufSource.stop(0); this.bufSource.stop(0);
} catch(err) {} } catch(err) {}
that.bufSource = that.context.createBufferSource(); this.bufSource = this.context.createBufferSource();
that.bufSource.buffer = that.buffer; this.bufSource.buffer = this.buffer;
that.bufSource.loop = true; this.bufSource.loop = true;
that.bufSource.loopStart = that.loopStart; this.bufSource.loopStart = this.loopStart;
that.bufSource.loopEnd = that.buffer.duration; this.bufSource.loopEnd = this.buffer.duration;
that.bufSource.connect(that.gainNode); this.bufSource.connect(this.gainNode);
// This fixes sync issues on Firefox and slow machines. // This fixes sync issues on Firefox and slow machines.
if(that.context.suspend && that.context.resume) { if(this.context.suspend && this.context.resume) {
that.context.suspend().then(function() { this.context.suspend().then(function() {
if(playBuild) { if(playBuild) {
// mobile webkit requires offset, even if 0 // mobile webkit requires offset, even if 0
that.bufSource.start(0); this.bufSource.start(0);
that.startTime = that.context.currentTime + that.loopStart; this.startTime = this.context.currentTime + this.loopStart;
} else { } else {
that.bufSource.start(0, that.loopStart); this.bufSource.start(0, this.loopStart);
that.startTime = that.context.currentTime; this.startTime = this.context.currentTime;
} }
that.context.resume().then(function() { this.context.resume().then(function() {
that.playing = true; this.playing = true;
if(callback) if(callback)
callback(); callback();
}); }.bind(this));
}); }.bind(this));
} else { } else {
if(playBuild) { if(playBuild) {
// mobile webkit requires offset, even if 0 // mobile webkit requires offset, even if 0
that.bufSource.start(0); this.bufSource.start(0);
that.startTime = that.context.currentTime + that.loopStart; this.startTime = this.context.currentTime + this.loopStart;
} else { } else {
that.bufSource.start(0, that.loopStart); this.bufSource.start(0, this.loopStart);
that.startTime = that.context.currentTime; this.startTime = this.context.currentTime;
} }
that.playing = true; this.playing = true;
if(callback) if(callback)
callback(); callback();
} }
} }
}); }.bind(this));
}; };
SoundManager.prototype.stop = function() { SoundManager.prototype.stop = function() {
@ -223,7 +221,6 @@ SoundManager.prototype.loadAudioFile = function(song, isBuild) {
/* decodeAudioData nukes our original MP3 array, but we want to keep it around /* decodeAudioData nukes our original MP3 array, but we want to keep it around
for memory saving purposes, so we must duplicate it locally here */ for memory saving purposes, so we must duplicate it locally here */
SoundManager.prototype.getAudioCallback = function(song, isBuild) { SoundManager.prototype.getAudioCallback = function(song, isBuild) {
var that = this;
var current = isBuild ? song.buildup : song.sound; var current = isBuild ? song.buildup : song.sound;
var copy = current.slice(0); var copy = current.slice(0);
return function(buffer) { return function(buffer) {
@ -234,16 +231,16 @@ SoundManager.prototype.getAudioCallback = function(song, isBuild) {
song.sound = copy; song.sound = copy;
} }
// race condition prevention // race condition prevention
if(that.song != song) { if(this.song != song) {
return; return;
} }
if(isBuild) { if(isBuild) {
that.tmpBuild = that.trimMP3(buffer, song.forceTrim, song.noTrim); this.tmpBuild = this.trimMP3(buffer, song.forceTrim, song.noTrim);
} else { } else {
that.tmpBuffer = that.trimMP3(buffer, song.forceTrim, song.noTrim); this.tmpBuffer = this.trimMP3(buffer, song.forceTrim, song.noTrim);
} }
that.onSongLoad(song); this.onSongLoad(song);
}; }.bind(this);
}; };
SoundManager.prototype.onSongLoad = function(song) { SoundManager.prototype.onSongLoad = function(song) {

Loading…
Cancel
Save