trust/js/core/Button.js

115 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-07-24 20:56:59 +00:00
Loader.addToManifest(Loader.manifest,{
// SFX
button1: "assets/sounds/button1.mp3",
button2: "assets/sounds/button2.mp3",
button3: "assets/sounds/button3.mp3"
});
2017-06-20 11:10:41 +00:00
function Button(config){
var self = this;
self.id = config.id;
2017-07-15 02:29:31 +00:00
self.config = config;
2017-06-20 11:10:41 +00:00
// Create DOM
2017-06-21 21:53:53 +00:00
var button = document.createElement("div");
2017-06-20 11:10:41 +00:00
button.className = "object";
2017-06-21 21:53:53 +00:00
button.classList.add("button");
2017-07-13 15:07:33 +00:00
if(config.size) button.setAttribute("size", config.size);
2017-06-20 11:10:41 +00:00
self.dom = button;
2017-07-27 16:49:04 +00:00
// TOOLTIP?
if(config.tooltip){
self.dom.style.width = 190;
self.dom.style.height = 55;
self.dom.style.position = "absolute";
self.dom.setAttribute("data-balloon-length", "large");
self.dom.setAttribute("data-balloon", Words.get(config.tooltip));
self.dom.setAttribute("data-balloon-pos", "left");
}
2017-06-21 21:53:53 +00:00
// BG
var bg = document.createElement("div");
bg.id = "background";
var text = document.createElement("div");
text.id = "text";
var hitbox = document.createElement("div");
hitbox.id = "hitbox";
button.appendChild(bg);
button.appendChild(text);
button.appendChild(hitbox);
2017-06-20 11:10:41 +00:00
// Customize DOM
button.style.left = config.x+"px";
button.style.top = config.y+"px";
2017-06-28 20:28:15 +00:00
self.setText = function(text_id){
var words = Words.get(text_id);
2017-07-13 15:07:33 +00:00
if(config.uppercase) words = words.toUpperCase();
2017-07-20 18:45:11 +00:00
self.setText2(words);
};
self.setText2 = function(words){
2017-06-28 20:28:15 +00:00
text.innerHTML = words;
};
self.setText(config.text_id);
2017-06-21 21:53:53 +00:00
// On hover...
hitbox.onmouseover = function(){
if(self.active) button.setAttribute("hover","yes");
};
hitbox.onmouseout = function(){
if(self.active) button.removeAttribute("hover");
};
2017-06-20 11:10:41 +00:00
// On click...
2017-06-21 21:53:53 +00:00
hitbox.onclick = function(){
2017-07-24 20:56:59 +00:00
if(parseFloat(getComputedStyle(self.dom).opacity)<0.5) return; // DON'T CLICK INVISIBLE BUTTONS
2017-06-28 20:28:15 +00:00
if(self.active){
2017-07-24 20:56:59 +00:00
// Sound!
if(config.sound){
Loader.sounds[config.sound].play();
}else{
var num = Math.ceil(Math.random()*3);
Loader.sounds["button"+num].play();
}
// Actual Logic
2017-06-28 20:28:15 +00:00
if(config.onclick) config.onclick();
if(config.message) publish(config.message);
2017-07-24 20:56:59 +00:00
2017-06-28 20:28:15 +00:00
}
2017-07-24 20:56:59 +00:00
2017-06-21 21:53:53 +00:00
};
// Activate/Deactivate
self.active = true;
self.activate = function(){
self.active = true;
button.removeAttribute("deactivated");
};
self.deactivate = function(){
self.active = false;
button.setAttribute("deactivated","yes");
button.removeAttribute("hover");
2017-06-20 11:10:41 +00:00
};
2017-06-21 21:53:53 +00:00
if(config.active===undefined) config.active=true;
if(!config.active) self.deactivate();
2017-06-20 11:10:41 +00:00
2017-07-05 18:19:46 +00:00
// Listeners!
2017-07-13 15:07:33 +00:00
if(self.id){
listen(self, self.id+"/activate", self.activate);
listen(self, self.id+"/deactivate", self.deactivate);
}
2017-07-05 18:19:46 +00:00
2017-07-13 18:49:49 +00:00
// Add & Remove
self.add = function(){ _add(self); };
2017-07-15 02:29:31 +00:00
self.remove = function(){
unlisten(self);
_remove(self);
};
2017-06-20 11:10:41 +00:00
}