var script = 'interface.xml'; var Interface= { mouseFocus : false, dom: {windows:{}}, descriptions : {batiments : [], vaisseaux : [],defenses : [],technologies : []}, planetes : [{name: 'Pegasi Alpha',position: '01:30:05',img: '1'},{name: 'Pegasi Beta',position: '01:30:06',img: '2'},{name: 'Pegasi Gamma',position: '01:30:08',img: '3'}], menuList: [], menu : { objects : {}, add : function(text,funct) { Interface.menu.objects[text] = {}; Interface.menu.objects[text]['object'] = create('li',Interface.dom.menu); Interface.menu.objects[text]['object'].innerHTML = text; Interface.menu.objects[text]['function'] = funct; Interface.menu.objects[text]['event'] = Event.observe(Interface.menu.objects[text]['object'], 'click', funct); }, remove : function(text) { if(typeof Interface.menu.objects[text] != 'object') return false; Interface.menu.objects[text]['object'].remove(); delete Interface.menu.objects[text]; return true; } }, /*barre : { objects : {}, add : function(text,funct) { Interface.menu.objects[text] = {}; Interface.menu.objects[text].object = create('li',Interface.dom.menu); Interface.menu.objects[text].object.innerHTML = text; Interface.menu.objects[text].funct = funct; Interface.menu.objects[text].evt = Event.observe(Interface.menu.objects[text].object, 'click', funct); }, remove : function() { } },*/ planete : { objects : {}, add : function(name,position,img) { Interface.planete.objects[position] = {}; Interface.planete.objects[position].name = name; Interface.planete.objects[position].position = position; Interface.planete.objects[position].object = create('img',Interface.dom.planetes); Interface.planete.objects[position].object.src = 'planetes/' + img + '.jpg'; Interface.planete.objects[position].object.alt = name+ ' [' + position + ']'; Interface.planete.objects[position].event = Event.observe(Interface.planete.objects[position].object, 'click', Interface.planete.change); }, focus : function(position) { for(var i in Interface.planete.objects) if(i == position) { $(Interface.planete.objects[i].object).addClassName('selected'); break; } }, change : function() { }, remove : function() { } }, ressources : { metal : 10000, cristal : 5000, ions : 7500, energie : 300, allEnergie: 450, set : function(type,number) { switch(type) { default: return false; case 'metal': case 'cristal': case 'ions': Interface.ressources[type] = number; Interface.dom.ressources[type].innerHTML = number; break; case 'energie': Interface.ressources[type] = number; Interface.dom.ressources.energie.innerHTML = number + '/' + Interface.ressources.allEnergie; break; case 'allEnergie': Interface.ressources[type] = number; Interface.dom.ressources.energie.innerHTML = Interface.ressources.energie + '/' + number; break; } } }, updater : function() { }, init : function() { var oldNodes = $(document.body).childElements(); for(var i =0; i < oldNodes.length; i++) if(oldNodes[i].tagName != 'script') oldNodes[i].remove(); Interface.dom.deskbar = create('div',document.body,'barre'); Interface.dom.menu = create('ul',document.body,'menu'); for(var i = 0; i < Interface.menuList.length; i++) Interface.menu.add(Interface.menuList[i][0],Interface.menuList[i][1]); Interface.dom.planetes = create('div',document.body,'planetes'); create('span',Interface.dom.planetes,0,'up'); for(var i = 0; i < Interface.planetes.length; i++) Interface.planete.add(Interface.planetes[i].name,Interface.planetes[i].position,Interface.planetes[i].img); create('span',Interface.dom.planetes,0,'down'); Interface.planete.focus(Interface.planetes[0].position); Interface.dom.ressources = {}; Interface.dom.ressources.all = create('div',document.body,'ressources'); var metal = create('div',Interface.dom.ressources.all); metal.innerHTML = 'Métal : '; Interface.dom.ressources.metal = create('strong',metal,'metal'); Interface.dom.ressources.metal.innerHTML = Interface.ressources.metal; var cristal = create('div',Interface.dom.ressources.all); cristal.innerHTML = 'Cristal : '; Interface.dom.ressources.cristal = create('strong',cristal,'metal'); Interface.dom.ressources.cristal.innerHTML = Interface.ressources.cristal; var ions = create('div',Interface.dom.ressources.all); ions.innerHTML = 'Métal : '; Interface.dom.ressources.ions = create('strong',ions,'metal'); Interface.dom.ressources.ions.innerHTML = Interface.ressources.ions; var energie = create('div',Interface.dom.ressources.all); energie.innerHTML = 'Métal : '; Interface.dom.ressources.energie = create('strong',energie,'metal'); Interface.dom.ressources.energie.innerHTML = Interface.ressources.energie + '/' + Interface.ressources.allEnergie; desc = new Ajax.Request('descriptions.xml',{onSuccess: function(xhr) { batiments = xhr.responseXML.getElementsByTagName('batiment'); for(var i = 0; i < batiments.length; i++) { Interface.descriptions.batiments[batiments[i].getAttribute('id')] = {name: batiments[i].getAttribute('name'),value:batiments[i].text || batiments[i].textContent}; var img = create('img'); img.src = 'images/batiments/' + batiments[i].getAttribute('img'); img.alt = batiments[i].getAttribute('name'); Interface.descriptions.batiments[batiments[i].getAttribute('id')].img = img; } } }); }, /*console : { active : true, print : function(message) { //if(!Interface.console.active) return false; Interface.dom.console.innerHTHML += message + '
'; }, flush : function() { if(!Interface.console.active) return false; Interface.dom.console.innerHTHML = ''; return true; } },*/ batiments : { initialized : false, window : null, init : function() { if(Interface.batiments.initialized) return false; Interface.batiments.initialized = true; var batiments = Interface.descriptions.batiments; if(typeof Interface.dom.batiment == 'undefined') Interface.dom.batiment = {}; var dom = Interface.dom.batiment; dom.all = create('div'); dom.all.style.height = '100%'; dom.left = create('dl',dom.all,0,'menu'); dom.dt = create('dt',dom.left); dom.dt.innerHTML = 'Batiments'; dom.right = create('div',dom.all,0,'description'); dom.titre = create('h4',dom.right); dom.titre.innerHTML = batiments[1].name; dom.img = dom.right.appendChild(batiments[1].img.cloneNode(true)); dom.p = create('p',dom.right); dom.p.innerHTML = batiments[1].value; dom.button = create('button',dom.right); dom.button.innerHTML = 'Construire'; dom.button.onclick = function() { alert('Construction!') }; Event.observe(dom.img,'click',function(){Interface.info('batiments',1)}); var change = function() { var dom = Interface.dom.batiment; var dd = dom.dd[this.i]; var dds = dd.parentNode.childNodes; for(var j = 1; j < dds.length; j++) { Element.removeClassName(dds[j],'selected'); } Element.addClassName(dd,'selected'); dom.titre.innerHTML = batiments[this.i].name; var newimg = batiments[this.i].img.cloneNode(true); dom.right.replaceChild(newimg,dom.img); dom.img = newimg; var i = this.i; Event.observe(dom.img,'click',function(){Interface.info('batiments',i)}); dom.p.innerHTML = batiments[this.i].value; }; dom.dd = []; for(var i = 1; i < batiments.length; i++) { dom.dd[i] = create('dd',dom.left); dom.dd[i].innerHTML = batiments[i].name; if(i == 1) Element.addClassName(dom.dd[i],'selected'); Event.observe(dom.dd[i],'click',change.bindAsEventListener({i:i})); } Interface.batiments.window = new Interface.window('batiments','Batiments',dom.all); } }, info : function(groupe,id) { if(typeof Interface.descriptions[groupe][id] == 'undefined') return false; var description = Interface.descriptions[groupe][id]; var container = create('div',0,0,'info'); var title = create('h5',container); title.innerHTML = description.name; container.appendChild(description.img.cloneNode(true)); var preview = create('p',container); preview.innerHTML = description.value; if(typeof Interface.dom.info == 'object') { Interface.dom.info.title(description.name); Interface.dom.info.content(container); Interface.dom.info.focus(); } else { Interface.dom.info = new Interface.window('description',description.name,container); Interface.dom.info.closeCallback = function() {Interface.dom.info = false;}; } }, notification : function(text) { if(typeof Interface.dom.notification == 'object') return false; Interface.dom.notification = create('p',document.body,'notification'); Interface.dom.notification.innerHTML = text; Effect.move(Interface.dom.notification,'bottom',-110,35,2000); setTimeout(function() {Effect.move(Interface.dom.notification,'bottom',35,-110,2000);},7000); setTimeout(function() {Element.remove(Interface.dom.notification); Interface.dom.notification = false;},10010); }, dialog : function(text,callback) { if(typeof Interface.dom.dialog == 'object') return false; Interface.dom.darkness = create('div',document.body,'darkness'); Effect.animate(Interface.dom.darkness,{opacity: [0,80]},500); Interface.dom.dialog = create('div',document.body,'dialog'); var p = create('p',Interface.dom.dialog); p.innerHTML = text; var ok = create('button',Interface.dom.dialog); ok.innerHTML = 'ok'; var cancel = create('button',Interface.dom.dialog); cancel.innerHTML = 'Annuler'; var quit = create('span',Interface.dom.dialog,0,'quit'); var close = function() { Element.remove(Interface.dom.dialog); Element.remove(Interface.dom.darkness); Interface.dom.dialog = false; Interface.dom.darkness = false; }; Event.observe(quit,'click',close); Event.observe(cancel,'click',close); Event.observe(ok,'click',close); Event.observe(ok,'click',callback); } } Interface.window = Class.create({ initialize : function(id,title,content) { if(!(id && title && content)) return false; this.id = id; this.titre = title; this.dom = {}; this.load(title,content); this.focus(); }, load : function(title,content) { if(this.dom.win || Interface.dom.windows[this.id]) return false; var title = title || this.titre; var content = content || this.dom.content; this.dom.deskbar = create('div',Interface.dom.deskbar); this.dom.deskbar.innerHTML = title; Event.observe(this.dom.deskbar,'click',this.focus.bindAsEventListener(this)); this.dom.win = create('div',document.body,'window_'+this.id,'window'); this.dom.win.style.top = (this.dom.win.offsetTop + Math.round(Math.random()*30 -10)) + 'px'; this.dom.win.style.left = (this.dom.win.offsetLeft + Math.round(Math.random()*30 -10)) + 'px'; this.dom.win.style.zIndex = 10; this.dom.top = create('div',this.dom.win,0,'top'); this.dom.topleft = create('span',this.dom.top,0,'topleft'); this.dom.topcenter = create('span',this.dom.top,0,'topcenter'); this.dom.topright = create('span',this.dom.top,0,'topright'); this.dom.barre = create('div',this.dom.win,0,'barre'); Event.observe(this.dom.win, 'mousedown', this.focus.bindAsEventListener(this)); Event.observe(this.dom.barre, 'mousedown', this.move.mouseDown.bindAsEventListener(this)); this.dom.titre = create('span',this.dom.barre,0,'titre'); this.dom.button = create('div',this.dom.barre,0,'button'); this.dom.hide = create('span',this.dom.button,0,'hide'); Event.observe(this.dom.hide, 'click', this.hide.bindAsEventListener(this)); this.dom.full = create('span',this.dom.button,0,'full'); Event.observe(this.dom.full, 'click', this.full.bindAsEventListener(this)); this.dom.close = create('span',this.dom.button,0,'close'); Event.observe(this.dom.close, 'click', this.close.bindAsEventListener(this)); this.dom.content = create('div',this.dom.win,0,'content'); this.dom.bottom = create('div',this.dom.win,0,'bottom'); this.dom.bottomleft = create('span',this.dom.bottom,0,'bottomleft'); this.dom.bottomcenter = create('span',this.dom.bottom,0,'bottomcenter'); this.dom.bottomright = create('span',this.dom.bottom,0,'bottomright'); Event.observe(this.dom.bottomright, 'mousedown', this.resize.mouseDown.bindAsEventListener(this)); this.x = this.dom.win.offsetLeft; this.y = this.dom.win.offsetTop; this.h = this.dom.win.offsetHeight; this.w = this.dom.win.offsetWidth; this.position = false; this.movePosition = false; Interface.dom.windows[this.id] = this.dom; //for(var i in this.dom) this.dom[i].windowId = this.id; if(typeof(content) == 'object') this.dom.content.appendChild(content); else if(typeof(content) == 'string') this.dom.content.innerHTML = content; this.dom.titre.innerHTML = title; }, focus : function() { this.dom.win.style.display = ''; var z = Number(this.dom.win.style.zIndex); for(var i in Interface.dom.windows) { if(Interface.dom.windows[i]) z = z > Number(Interface.dom.windows[i].win.style.zIndex) ? z : Number(Interface.dom.windows[i].win.style.zIndex); } this.dom.win.style.zIndex = z+1; }, hide : function() { this.dom.win.style.display = 'none'; }, full : function() { if(this.position) { this.dom.win.style.left = this.position.left+'px'; this.dom.win.style.top = this.position.top+'px'; this.dom.win.style.width = this.position.width+'px'; this.dom.win.style.height = this.position.height+'px'; this.position = false; } else { this.position = {left : this.dom.win.offsetLeft, top: this.dom.win.offsetTop, width: this.dom.win.offsetWidth, height: this.dom.win.offsetHeight}; this.dom.win.style.top = '0'; this.dom.win.style.left = '0'; this.dom.win.style.width = '100%'; this.dom.win.style.height = (document.documentElement.offsetHeight - this.dom.barre.offsetHeight - 49) + 'px'; } this.focus(); }, close : function() { if(typeof this.closeCallback == 'function') this.closeCallback(); this.dom.deskbar.remove(); this.dom.win.remove(); delete this.dom; this.dom = false; Interface.dom.windows[this.id] = false; return true; }, resize : { mouseDown : function(event) { Interface.mouseFocus = this.dom; Interface.mouseFocus.resizePosition = [Interface.mouseFocus.win.offsetLeft, Interface.mouseFocus.win.offsetTop]; Interface.dom.resize = create('div',document.body,'resize'); Interface.dom.resize.style.left = Interface.mouseFocus.win.offsetLeft + 'px'; Interface.dom.resize.style.top = Interface.mouseFocus.win.offsetTop + 'px'; Interface.dom.resize.style.width = (Event.pointerX(event) - Interface.mouseFocus.win.offsetLeft) + 'px'; Interface.dom.resize.style.height = (Event.pointerY(event) - Interface.mouseFocus.win.offsetTop) + 'px'; Event.observe(document.body, 'mousemove',Interface.window.prototype.resize.mouseMove); Event.observe(document.body, 'mouseup', Interface.window.prototype.resize.mouseUp); }, mouseMove : function(event) { var w = Event.pointerX(event) - Interface.mouseFocus.resizePosition[0]; var h = Event.pointerY(event) - Interface.mouseFocus.resizePosition[1]; Interface.dom.resize.style.width = (w > 0 ? w : 0) + 'px'; Interface.dom.resize.style.height = (h > 0 ? h : 0) + 'px'; }, mouseUp : function(event) { Interface.mouseFocus.win.style.width = (Event.pointerX(event) - Interface.mouseFocus.resizePosition[0] + 2) + 'px'; Interface.mouseFocus.win.style.height = (Event.pointerY(event) - Interface.mouseFocus.resizePosition[1] - 51) + 'px'; Element.remove(Interface.dom.resize); Interface.mouseFocus.resizePosition = false; Interface.mouseFocus = false; Event.stopObserving(document.body,'mousemove',Interface.window.prototype.resize.mouseMove); Event.stopObserving(document.body,'mouseup',Interface.window.prototype.resize.mouseUp); } }, move : { mouseDown : function(event) { if(Event.element(event).parentNode == this.dom.button) return false; Interface.mouseFocus = this.dom; Effect.opacity(Interface.mouseFocus.win,60); Interface.mouseFocus.movePosition = [Event.pointerX(event) - Interface.mouseFocus.win.offsetLeft,Event.pointerY(event) - Interface.mouseFocus.win.offsetTop]; Event.observe(document.body, 'mousemove',Interface.window.prototype.move.mouseMove); Event.observe(document.body, 'mouseup', Interface.window.prototype.move.mouseUp); }, mouseMove : function(event) { var x = Event.pointerX(event) - Interface.mouseFocus.movePosition[0]; var y = Event.pointerY(event) - Interface.mouseFocus.movePosition[1]; Interface.mouseFocus.win.style.left = (x > 0 ? x : 0) + 'px'; Interface.mouseFocus.win.style.top = (y > 0 ? y : 0) + 'px'; }, mouseUp : function(event) { Effect.opacity(Interface.mouseFocus.win,100); Interface.mouseFocus.movePosition = false; Interface.mouseFocus = false; Event.stopObserving(document.body,'mousemove',Interface.window.prototype.move.mouseMove); Event.stopObserving(document.body,'mouseup',Interface.window.prototype.move.mouseUp); }, to : function(object,x,y) { } }, title : function(title) { if(typeof title != 'string') return this.titre; var old = this.title; this.titre = title; this.dom.titre.innerHTML = title; return old; }, content : function(content) { var old = this.dom.content.cloneNode(true); if(typeof(content) == 'object') { Element.remove(this.dom.content.firstChild); this.dom.content.appendChild(content); } else if(typeof(content) == 'string') this.dom.content.innerHTML = content ; else return this.dom.content; return old; } }); Interface.menuList = [['Hello World',helloWorld], ['Batiments',Interface.batiments.init], ['Doubler',doubler], //['Background',background], ['Notification',notif], ['Dialog',dialog]]; function dialog() { Interface.dialog('WTF??',function(){alert(':P')}); } function background() { if($('background')) return false; bg = create('div',document.body,'background'); bg2 = create('div',bg,0,'content'); } function doubler() { Interface.ressources.set('metal',Interface.ressources.metal*2); Interface.ressources.set('cristal',Interface.ressources.cristal*2); Interface.ressources.set('ions',Interface.ressources.ions*2); Interface.ressources.set('energie',Interface.ressources.energie*2); Interface.ressources.set('allEnergie',Interface.ressources.allEnergie*2); } function notif() { Interface.notification('test! :P'); } function helloWorld() { hw = new Interface.window('helloWorld','Hello World','Hello World!'); } Interface.init();