

ERR={"obj":null};
ERR.listupInfo=function(gobj){
return "components<"+gobj.components.length+">:"+gobj.components.join("\n");};
ERR.watch("obj",
function(id,oldval,newval){
console.log("--------\n<<my_lib>> ERROR occurs : ________\n"
+newval+"\n--------\n");
return newval;});
var LibFunc={
"required_func":["apply","instanceof",],
"make":function(obj){
for(var i=0;i<this.required_func.length;i++){
if(typeof obj[this.required_func[i]]=="undefined"){
obj[this.required_func[i]]=this[this.required_func[i]];}else{
throw new Error("Cannot apply \""+name+"\" function to: "+obj);}
}
return obj;},
"combine":function(lib0,lib1,lib2){
var newlib={};
for(var i=0;i<arguments.length;i++){
arguments[i].apply(newlib);}
return newlib;},
"apply":function(obj){
var ok=[];
for(var name in this){
if(typeof obj[name]=="undefined"){
obj[name]=this[name];
ok.push(name);}else if(typeof obj[name]!="function"){
throw new Error("Cannot apply \""+name+"\" function to: "+obj);}else{
}
}
return ok;},
"instanceof":function(obj){
for(var name in this){
if((typeof this[name]=="function")
&&(typeof obj[name]!="function")
&&(LibFunc.required_func.indexOf(name)<0)){
return false;}
}
return true;},
};
var GraphicsLib={

setSize:function(w,h){
if((this.width==undefined)||(this.height==undefined)){
ERR.obj=this;
throw new Error("[GraphicsLib.setSize()] cannot find \"width\" and \"height\".\n");}
this.width=Math.floor(w);
this.height=Math.floor(h);},
getSize:function(){
if((this.width==undefined)||(this.height==undefined)){
ERR.obj=this;
throw new Error("[GraphicsLib.getSize()] cannot find \"width\" and \"height\".\n"+this);}
return [this.width,this.height];},
setPosition:function(axis,val){
this.translate[axis]=val;},
getPosition:function(axis){
return this.translate[axis];},
setTranslate:function(x,y,z){
this.translate[0]=x;
this.translate[1]=y;
this.translate[2]=z;},
getTranslate:function(){
return this.translate;},
setAngle:function(angle){
if(this.rotation==undefined){
ERR.obj=this;
throw new Error("[GraphicsLib] cannot find \"rotation\".");}
if(typeof angle=="number"){
this.rotation[0]=angle;}
},
getAngle:function(){
if(this.rotation==undefined){
ERR.obj=this;
throw new Error("[GraphicsLib] cannot find \"rotation\".");}
return this.rotation[0];},
setPivot:function(x,y,z){
if(this.rotation==undefined){
ERR.obj=this;
throw new Error("[GraphicsLib] cannot find \"rotation\".");}
this.rotation[1]=x;
this.rotation[2]=y;
this.rotation[3]=z;},
getPivot:function(){
if(this.rotation==undefined){
ERR.obj=this;
throw new Error("[GraphicsLib] cannot find \"rotation\".");}
return [
this.rotation[1],
this.rotation[2],
this.rotation[3],
];},
setAlpha:function(alpha){
if(this.color==undefined){
ERR.obj=this;
throw new Error("[GraphicsLib] cannot find \"color\".");}
this.color[3]=alpha;},
getAlpha:function(){
if(this.color==undefined){
ERR.obj=this;
throw new Error("[GraphicsLib] cannot find \"color\".");}
return this.color[3];},
};
LibFunc.make(GraphicsLib);
var AnimationLib={

AXIS_X:0,
AXIS_Y:1,
AXIS_Z:2,
TIME_INTERVAL:40,

slide:function(axis,target,speed,func){
delete_timer(this);
var tmp=(speed>0)?speed:6;
var vector=((target-this.getPosition(axis))>0)?1:-1;
var timer_func=function(obj,count){
force_redraw();
obj.setPosition(axis,
obj.getPosition(axis)+vector*tmp*(count+1));
if(vector*obj.getPosition(axis)>=vector*target){
obj.setPosition(axis,target);
delete_timer(obj);
try{func(obj,count);}catch(e){}
return true;}else{
return false;}
};
if(timer_func(this,0)==false){
if(isNumber(this.TIME_INTERVAL)){
append_timer(this,this.TIME_INTERVAL,timer_func);}else{
append_timer(this,AnimationLib.TIME_INTERVAL,timer_func);}
}
},

rotate:function(target,speed,func){
delete_timer(this);
var tmp=(speed>0)?speed:2;
var vector=((target-this.getAngle())>0)?1:-1;
var timer_func=function(obj,count){
force_redraw();
obj.setAngle(obj.getAngle()+vector*tmp);
if(vector*obj.getAngle()>=vector*target){
obj.setAngle(target);
delete_timer(obj);
try{func(obj,count);}catch(e){}
return true;}else{
return false;}
};
if(timer_func(this,0)==false){
if(isNumber(this.TIME_INTERVAL)){
append_timer(this,this.TIME_INTERVAL,timer_func);}else{
append_timer(this,AnimationLib.TIME_INTERVAL,timer_func);}
}
},

expandTo:function(w,h,angle,msec,func){
delete_timer(this);
var max_cnt=Math.ceil(msec/((isNumber(this.TIME_INTERVAL)?this.TIME_INTERVAL:AnimationLib.TIME_INTERVAL)));
var _size=this.getSize();var _angle=this.getAngle();var dw=(w-_size[0])/max_cnt;
var dh=(h-_size[1])/max_cnt;
var da=(angle==null)?0:(angle-_angle)/max_cnt;
var timer_func=function(obj,count){
force_redraw();
obj.setSize(_size[0]+dw*(count+1),_size[1]+dh*(count+1));
if(angle!=null)obj.setAngle(_angle+da*(count+1));
if((count+1)>=max_cnt){
obj.setSize(w,h);
if(angle!=null)obj.setAngle(angle);
delete_timer(obj);
try{func(obj,count);}catch(e){}
return true;}else{
return false;}
};
if(timer_func(this,0)==false){
if(isNumber(this.TIME_INTERVAL)){
append_timer(this,this.TIME_INTERVAL,timer_func);}else{
append_timer(this,AnimationLib.TIME_INTERVAL,timer_func);}
}
},

moveTo:function(pos,size,angle,speed,func){
delete_timer(this);
speed=(speed>0)?speed:6;
var p=this.getTranslate();
var d=[pos[0]-p[0],
pos[1]-p[1],
pos[2]-p[2]];
var vector=[(d[0]>0)?1:-1,
(d[1]>0)?1:-1,
(d[2]>0)?1:-1];
d=d.map(function(val){return Math.abs(val);});
var max=Math.max(d[0],d[1],d[2]);
var ratio=null;
if(max!=0){
ratio=[d[0]/max, d[1]/max,d[2]/max];}else{
ratio=[1,1,1];}
var max_cnt=0;
var tmp=max;
for(var i=0;i<1000;i++){
tmp-=speed*(i+1);
if(tmp<=0)break;
else max_cnt++;}
var _size=this.getSize();var _angle=this.getAngle();var da=(angle-this.getAngle())/max_cnt;
var is_finish=function(val,index,me){
return (vector[index]*val>=vector[index]*pos[index]);};
var distance=Math.sqrt(d[0]*d[0]+d[1]*d[1]+d[2]*d[2]);
var timer_func=function(obj,count){
force_redraw();
p=obj.getTranslate();
obj.setTranslate(p[0]+vector[0]*speed*(count+1)*ratio[0],
p[1]+vector[1]*speed*(count+1)*ratio[1],
p[2]+vector[2]*speed*(count+1)*ratio[2]);
if(size!=null){
var pp=obj.getTranslate();
var dd=[pos[0]-pp[0],pos[1]-pp[1],pos[2]-pp[2]];
var dist=Math.sqrt(dd[0]*dd[0]+dd[1]*dd[1]+dd[2]*dd[2]);
obj.setSize((_size[0]-size[0])*dist/distance+size[0],
(_size[1]-size[1])*dist/distance+size[1],
(_size[2]-size[2])*dist/distance+size[2]);}
if(angle!=null)obj.setAngle(_angle+da*(count+1));
if(obj.getTranslate().every(is_finish)){
obj.setTranslate(pos[0],pos[1],pos[2]);
if(size!=null){
obj.setSize(size[0],size[1]);}
if(angle!=null)obj.setAngle(angle);
delete_timer(obj);
if(!func)return true;
try{func(obj,count);}catch(e){
var dlog="[JS error] ";
dlog+=e.fileName+":"+e.lineNumber+": ";
dlog+=e.name+": "+e.message;
console.log(dlog);}
return true;}else{
return false;}
};

if(timer_func(this,0)==false){
if(isNumber(this.TIME_INTERVAL)){
append_timer(this,this.TIME_INTERVAL,timer_func);}else{
append_timer(this,AnimationLib.TIME_INTERVAL,timer_func);}
}
},
moveTo2:function(pos,angle,alpha,max_count,func){
delete_timer(this);
var p=this.getTranslate();
var d=[pos[0]-p[0],
pos[1]-p[1],
pos[2]-p[2]];
var timer_func=function(obj,count){

if(count>max_count){
delete_timer(obj);
obj.setTranslate(pos[0],pos[1],pos[2]);
if(angle!=null)obj.setAngle(angle);
if(!func)return true;
try{func(obj,count);}catch(e){
var dlog="[JS error] ";
dlog+=e.fileName+":"+e.lineNumber+": ";
dlog+=e.name+": "+e.message;
console.log(dlog);}
force_redraw();
return true;}else {

force_redraw();
var p=obj.getTranslate();
var _angle=obj.getAngle();var da=(angle-_angle)*alpha;
var d=[pos[0]-p[0],
pos[1]-p[1],
pos[2]-p[2]];

obj.setTranslate(p[0]+d[0]*alpha,
p[1]+d[1]*alpha,
p[2]+d[2]*alpha);
if(angle!=null)obj.setAngle(_angle+da);

return false;}
};

if(timer_func(this,0)==false){
append_timer(this,40,timer_func);}
},

transparentTo:function(alpha,msec,func){
delete_timer(this);
var max_cnt=Math.ceil(msec/((isNumber(this.TIME_INTERVAL)?this.TIME_INTERVAL:AnimationLib.TIME_INTERVAL)));
var _alpha=this.getAlpha();
var dal=(alpha-_alpha)/max_cnt;
var timer_func=function(obj,count){
force_redraw();
obj.setAlpha(
Math.ceil(_alpha+dal*(count+1))
);
if((count+1)>=max_cnt){
delete_timer(obj);
obj.setAlpha(alpha);
try{func(obj,count);}catch(e){}
return true;}else{
return false;}
};
if(timer_func(this,0)==false){
if(isNumber(this.TIME_INTERVAL)){
append_timer(this,this.TIME_INTERVAL,timer_func);}else{
append_timer(this,AnimationLib.TIME_INTERVAL,timer_func);}
}
},
};
if(SU_IS_SLOW_ANIM){
AnimationLib.moveToDirect=function(pos,size,angle,speed,func){
force_redraw();
var obj=this;
var count=0;
obj.setTranslate(pos[0],pos[1],pos[2]);
if(size!=null){
obj.setSize(size[0],size[1]);}
if(angle!=null)obj.setAngle(angle);
delete_timer(obj);
if(!func)
return true;
try{func(obj,count);}catch(e){
var dlog="[JS error] ";
dlog+=e.fileName+":"+e.lineNumber+": ";
dlog+=e.name+": "+e.message;
console.log(dlog);}
};
AnimationLib.transparentToDirect=function(alpha,msec,func){
force_redraw();
var obj=this;
var count=0;
delete_timer(obj);
obj.setAlpha(alpha);
if(!func)return;
try{func(obj,count);}catch(e){}
};
AnimationLib.rotateDirect=function(target,speed,func){
force_redraw();
var obj=this;
var count=0;
obj.setAngle(target);
delete_timer(obj);
if(!func)return true;
try{func(obj,count);}catch(e){}
return true;};
AnimationLib.slideDirect=function(axis,target,speed,func){
force_redraw();
var obj=this;
var count=0;
obj.setPosition(axis,target);
delete_timer(obj);
if(!func)return true;
try{func(obj,count);}catch(e){}
return true;};};
LibFunc.make(AnimationLib);
var FocusLib={


setFocus:function(b,keysym){

},

requestFocus:function(focus_obj){
focus_obj.moveTo(this.getTranslate(),this.getSize(),0,null,null);},
key_hook:function(updown,keysym){return false;}
};
LibFunc.make(FocusLib);


var AnimationGraphics=LibFunc.combine(GraphicsLib,AnimationLib);
var FocusableGraphics=LibFunc.combine(GraphicsLib,FocusLib);
