package com.osnova;
import android.view.*;
import android.content.Context;
import android.graphics.*;
import com.tankalfa.Tank;

  

public class DD extends SurfaceView implements SurfaceHolder.Callback
{
 
	private float width=800;
	private float height=500;
	private Bitmap ek;	
 private Rect di;
	public Context con;
			
  
 private float we,he;
	
	private  Grap g;
 
			private Tank tan;
			 
public float ex,ey;
public boolean stop=true;

private DrawThread drawThread;
 
			
			
			public DD(Context con,float w,float h) {
				super(con);
				
		  	this.con=con;
			
				ek=Bitmap.createBitmap((int)width,(int)height,Bitmap.Config.ARGB_8888);
				
				 	
						
						 
				g=new Grap(this,ek);
		
				tan=new Tank(g);
						
				we=width/w; 
				he=height/h;
						
			
				di=new Rect();
			
		
				 
						
			
		
						
				getHolder().addCallback(this);
		
				
				
				}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
									   int height) {

			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				drawThread = new DrawThread(getHolder(),this);
				drawThread.setRunning(true);
				drawThread.start();
			}

		
			
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				boolean retry = true;
				drawThread.setRunning(false);
				while (retry) {
					
					
					try {
						drawThread.join();
						retry = false;
					}
					catch (InterruptedException e)
					{}}}

	
public boolean onTouchEvent(MotionEvent ev)
			{
	
		  // if(ev.getAction()==ev.ACTION_DOWN){stop=false;}
		  // if(ev.getAction()==ev.ACTION_UP){stop=true;}
				
		    //ex=(int)(ev.getX());
		    //ey=(int)(ev.getY());
				
				//int in=we,im=he;		 
			int mask=ev.getActionMasked();	
			int po=ev.getPointerCount();
			
			switch(mask){
				
			case MotionEvent.ACTION_UP:
				
			case MotionEvent.ACTION_POINTER_UP:	
				
				ex=0;
				ey=0;
				stop=true;
				
				break;
				
			case MotionEvent.ACTION_DOWN:	
				
			case MotionEvent.ACTION_POINTER_DOWN:	
				
			case MotionEvent.ACTION_MOVE:	
				for(int i=0;i<10;i++){
					
				if(i<po){
					ex=ev.getX(i)*we;
					ey=ev.getY(i)*he;}
				
					}
				stop=false;
				
				break;
				
			}
			
			
			 
			
	

				return true;
	}		
		
			
		
			public boolean getDown(int x,int y,int w,int h){
						
						
			return	
						ex>=x & ex<=x+w-1 &
						ey>=y & ey<=y+h-1;
						
						
						
			}
			
			
	 
	
	
	   
	public void draw(Canvas c){
		
		 
		  
		
c.getClipBounds(di); 
c.drawBitmap(ek,null,di,null);

	tan.draw();
	
	}
	
	
			
			
	
	
}




