const Z_BACK = 100;
const Z_D3 = 110 ;
const Z_FRONT = 120;
const Z_BASIC = 130 ;
class module_D2
{
SCREEN = [] ;
Screen = null ;
pen_size = 1 ;
pixel_pen = 1 ;
scale_x = 1.0 ;
scale_y = 1.0 ;
origin_x = 0.0 ;
origin_y = 0.0 ;
path = false ;
Dispose()
{
for( let id in this.SCREEN )
{ this.SCREEN[id].Dispose();
}
this.SCREEN = [] ;
this.Screen = null ;
}
constructor()
{
this.AddLayer("front",Z_FRONT);
this.scale_screen_to(-100,100,100,-100);
}
PosX(x)
{ return (x-this.origin_x)*this.scale_x;
}
PosY(y)
{ return (y-this.origin_y)*this.scale_y ;
}
ScaleX(x)
{ return Math.abs(x*this.scale_x);
}
ScaleY(y)
{ return Math.abs(y*this.scale_y);
}
AddLayer(id, order)
{
this.SCREEN[id] = new Screen(order);
this.Screen = this.SCREEN[id] ;
this.SetColor( µB.$Config.color ) ;
}
Resize(x, y)
{
for( let id in this.SCREEN )
{ this.SCREEN[id].Resize(x,y);
}
}
SetColor(color)
{
let rgba = µB.$Config.Color2Hex(color);
for( let index in this.SCREEN )
{ this.SCREEN[index].ctx.fillStyle = rgba ;
this.SCREEN[index].ctx.strokeStyle = rgba ;
}
}
select_screen(id)
{
id = µB.$Config.GetEnum(id);
if( id != "front" && id != "back" )
{
µB.RuntimeError( "Screen " + id + " doesn't exist" ) ;
}
else if( this.SCREEN[id] === undefined )
{
this.AddLayer(id,Z_BACK) ;
}
else
{ this.Screen = this.SCREEN[id] ;
}
}
scale_screen(x1, y1)
{ this.scale_screen_to(0,0,x1,y1);
}
scale_screen_to(x1, y1, x2, y2)
{
this.origin_x = x1*1 ;
this.origin_y = y1*1 ;
this.scale_x = this.Screen.canvas.width/(x2-x1) ;
this.scale_y = this.Screen.canvas.height/(y2-y1) ;
Log( x2+","+y2+ " = " + this.scale_x+","+this.scale_y );
}
clear_screen()
{ this.Screen.Clear();
}
fade_screen(a)
{
let alpha = this.Screen.alpha ;
if( a === "in" )
{ alpha += .01 ;
}
else if( IsNumber(a) )
{ alpha = a/100;
}
else
{ alpha -= .01 ;
}
alpha = MinMax(alpha,0,1);
if( alpha != this.Screen.alpha )
{ this.Screen.canvas.style.opacity = this.Screen.alpha = alpha
}
}
scroll_screen(x, y)
{
let src = this.Screen.ctx.getImageData(0, 0, this.Screen.width, this.Screen.height);
this.Screen.ctx.putImageData(src, x, y);
}
screen_show_mode(e)
{
e = (e==="default") ? "source-over" : e ;
this.Screen.ctx.globalCompositeOperation = e;
}
pen(p)
{
this.pixel_pen = 0 ;
this.pen_size = Math.abs(p*1);
this.Screen.ctx.lineWidth = .5*(this.ScaleX(this.pen_size)+this.ScaleY(this.pen_size)) ;
}
pen_pixel(p)
{ this.pixel_pen = Math.abs(p);
this.pen_size = 0;
this.Screen.ctx.lineWidth = this.pixel_pen;
}
font_size(size)
{
size = MinMax(size,1,100);
this.Screen.SetFont(size);
}
/*
draw_opacity($e)
{
this.$Draw.globalAlpha = $e;
}
*/
draw_background()
{ this.Screen.ctx.fillRect(0,0, this.Screen.width, this.Screen.height);
}
draw_dot(x, y)
{
if( this.pixel_pen )
{ this.Screen.ctx.fillRect( this.PosX(x),this.PosY(y),this.pixel_pen,this.pixel_pen) ;
}
else
{
this.Screen.ctx.beginPath();
this.Screen.ctx.ellipse(this.PosX(x),this.PosY(y),this.ScaleX(this.pen_size),this.ScaleY(this.pen_size),0,0,2*Math.PI);
this.Screen.ctx.fill();
}
}
draw_line(x1, y1, x2, y2)
{
this.Screen.ctx.beginPath();
this.Screen.ctx.moveTo(this.PosX(x1),this.PosY(y1));
this.Screen.ctx.lineTo(this.PosX(x2),this.PosY(y2));
this.Screen.ctx.stroke();
}
draw_frame(x, y, u, v)
{
this.Screen.ctx.beginPath();
this.Screen.ctx.rect(this.PosX(x),this.PosY(y),this.ScaleX(u-x),this.ScaleY(v-y));
this.Screen.ctx.stroke();
}
draw_fill()
{
this.Screen.ctx.fill();
this.$path = false ;
}
draw_circle(r,x,y)
{
this.Screen.ctx.beginPath();
this.Screen.ctx.ellipse(this.PosX(x),this.PosY(y),this.ScaleX(r),this.ScaleY(r),0,0,2*Math.PI);
this.Screen.ctx.stroke();
}
draw_text(text, x, y)
{
this.Screen.ctx.textAlign = "left";
this.Screen.ctx.fillText( text, this.PosX(x),this.PosY(y) ) ;
}
draw_text_center(text, x, y)
{
this.Screen.ctx.textAlign = "center";
this.Screen.ctx.fillText( text, this.PosX(x),this.PosY(y) ) ;
}
draw_text_align(text, x, y)
{
this.Screen.ctx.textAlign = "right";
this.Screen.ctx.fillText( text, this.PosX(x),this.PosY(y) ) ;
}
draw_path(x, y)
{
if( this.path == false )
{ this.Screen.ctx.beginPath();
this.Screen.ctx.moveTo(this.PosX(x),this.PosY(y));
this.path = true ;
}
else
{
this.Screen.ctx.lineTo(this.PosX(x),this.PosY(y));
}
}
};