Skip to content

Instantly share code, notes, and snippets.

@CrypticSwarm
Created June 1, 2009 16:57
Show Gist options
  • Save CrypticSwarm/121598 to your computer and use it in GitHub Desktop.
Save CrypticSwarm/121598 to your computer and use it in GitHub Desktop.
APE.Move = new Class({
Implements: [Options],
Extends: APE.UserClient,
options: {
container: document.body,
userEvents: [{cmd: 'send', raw: 'data'}, {cmd: 'setPos', raw: 'positions'}]
},
initialize: function(userClass, options){
this.parent(userClass, options);
this.addEvent('init', this.initPlayground);
this.onError('004',this.reset);
},
initPlayground: function(){
this.element = this.options.container;
this.els = {};
this.els.move_box = new Element('div',{'class':'move_box'}).inject(this.element);
this.els.move_box.addEvent('click',function(ev){
this.sendpos(ev.page.x,ev.page.y);
}.bindWithEvent(this));
this.els.more = new Element('div',{'id':'more'}).inject(this.element,'inside');
this.els.sendboxContainer = new Element('div',{'id':'ape_sendbox_container'}).inject(this.els.more);
this.els.sendBox = new Element('div',{'text':'Say : ','id':'ape_sendbox'}).inject(this.els.sendboxContainer,'bottom');
this.els.sendbox = new Element('input',{
'type':'text',
'id':'sendbox_input',
'autocomplete':'off',
'events':{
'keypress':function(ev){
if(ev.code == 13){
var val = this.els.sendbox.get('value');
if(val!=''){
this.pipe.send(val);
$(ev.target).set('value','');
}
}
}.bind(this)
}
}).inject(this.els.sendBox);
this.els.sendButton = new Element('input',{
'type':'button',
'id':'sendbox_button',
'value':'Send',
'events': {
'click': function(){
var val = this.els.sendbox.get('value');
if(val!=''){
this.pipe.send(val);
$(ev.target).set('value','');
}
}.bind(this)
}
}).inject(this.els.sendBox);
},
reset: function(){
this.core.clearSession();
if (this.element) {
this.element.empty();
}
this.core.initialize(this.core.options);
}
});
APE.Move.User = new Class({
Implements: [Options],
options: {
properties: {
x: 8,
y: 8
}
},
initialize: function(user, pipe, options){
this.setOptions({container: document.body}, options);
this.color = this.options.color;
this.x = this.options.properties.x;
this.y = this.options.properties.y;
var pos = this.options.container.getCoordinates();
this.x = this.x.toInt()+pos.left;
this.y = this.y.toInt()+pos.top;
this.element = new Element('div',{
'class':'demo_box_container',
'styles':{
'left': this.x+'px',
'top': this.y+'px'
},
'morph': { 'duration':300, 'fps':100, 'link': 'cancel' }
}).inject(this.options.container,'inside');
this.color_elem = new Element('div',{
'class':'user',
'styles':{
'background-color': this.color
},
'tween': { duration: 1000 }
}).inject(this.element,'inside');
new Element('span',{
'text':user.properties.name
}).inject(this.element,'inside');
},
positions: function(x, y){
if(x.x){
this.x = x.x.toInt();
this.y = x.y.toInt();
} else {
this.x = x.toInt();
this.y = y.toInt();
}
pos = this.options.container.getCoordinates();
this.element.morph({'left':pos.left+this.x,'top':pos.top+this.y});
},
deleteUser: function(){
this.element.dispose();
},
writeMessage: function(pipe,message,sender){
//Define sender
sender = this.pipe.getUser(sender.pubid);
//destory old message
sender.element.getElements('.ape_message_container').destroy();
//Create message container
var msg = new Element('div',{'opacity':'0','class':'ape_message_container'});
var cnt = new Element('div',{'class':'msg_top'}).inject(msg);
//Add message
new Element('div',{
'text':this.parseMessage(message),
'class':'ape_message'
}).inject(cnt);
new Element('div',{'class':'msg_bot'}).inject(cnt);
//Inject message
msg.inject(sender.element);
//Show message
var fx = msg.morph({'opacity':'1'});
//Delete old message
(function(el){
$(el).morph({'opacity':'0'});
(function(){
$(this).dispose();
}).delay(300,el);
}).delay(3000,this,msg);
},
userColor: function(nickname){
var color = new Array(0,0,0);
var i=0;
while(i<3 && i<nickname.length){
//Transformation du code ascii du caractère en code couleur
color[i] = Math.abs(Math.round(((nickname.charCodeAt(i)-97)/26)*200+10));
i++;
}
return color.join(',');
},
sendpos: function(x,y){
var pos=this.posToRelative(x,y);
this.core.request('SETPOS',[this.pipe.getPubid(),pos.x,pos.y]);
this.movePoint(this.core.user,pos.x,pos.y);
},
parseMessage: function(message){
return unescape(message);
},
posToRelative:function(x,y){
var pos = this.options.container.getCoordinates();
x = x-pos.left-36;
y = y-pos.top-46;
if(x<0) x = 10;
if(x>pos.width) x=pos.width-10;
if(y<0) y = 10;
if(y>pos.height) y = pos.height-10;
return {'x':x,'y':y};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment