Cursor={}
function Cursor.new(man,fichier)
     local cursor={add=1,
	               mana=man,
				   pic=Image.load(fichier),
				   menu=Image.load("data/image/zone_texte.png"),
				   start=Image.load("data/image/menu.png"),
				   ame=0,
				   distance=7,
				   t=0,
				   m="",
				   x=0,
				   y=0,
				   continue=true}
				   
	 function cursor:play(pad)
	     if cursor:click() then  
                         local x=cursor:getX()
						 local y=cursor:getY()
						 
				         if x>=GamePlayer.x-cursor.distance and x<=GamePlayer.x+cursor.distance and y>=GamePlayer.y-cursor.distance and y<=GamePlayer.y+cursor.distance then --verification dans la zone autour du heros  
						 --if cursor.add~=0 then screen:blit(GameMap.xr+x*20,GameMap.yr+y*20,Element[cursor.add].tile.pic,0,0,20,20) end
						   if cursor.add~=0 then
						        if cursor.mana>=Element[cursor.add].cout then
					                 GameMap:set(x,y,Element[cursor.add].n) 
								     cursor.mana=cursor.mana-Element[cursor.add].cout
							    end
					        else
					           GameMap:set(x,y,"0") 
					        end
						 
						 end
		 elseif cursor:clickR() then
		     if cursor.add==Element.n then
			     cursor.add=0
			 else
			     cursor.add=cursor.add+1
			 end
		 elseif pad:start() then
		     cursor:menuPlay()
	     end
		 
		 --Tempo :
		 cursor.t=cursor.t+1
		 --screen:print(0,0,cursor.t)
		 if EarthEngine.type=="psp" then
		     cursor:blit(pad)
		 end
	 end
	 
	 function cursor:message(m)
          cursor.m=m
	 end
	 
	 function cursor:playMouseDs(pad)
           
		
	 end
	 
	 
	 function cursor:playMousePSP(pad)
           dx = pad:analogX()
           if dx > 32 then
              cursor.x = cursor.x + 1--dx / 14
           end
           if dx < -32 then
              cursor.x = cursor.x - 1--dx / 14
           end
          
          dy = pad:analogY()
          if dy > 32 then
             cursor.y = cursor.y + 1--dy / 14
          end
          if dy < -32 then
             cursor.y = cursor.y - 1--dy / 14
          end
         
        if cursor.x<0 then
           cursor.x=0
        end
        if cursor.y<0 then
           cursor.y=0
        end
	    if cursor.x>GameMap.lr-20 then
           cursor.x=GameMap.lr-20
        end
	    if cursor.y>GameMap.hr-20 then
           cursor.x=GameMap.hr-20
        end
		
	 end
	 
	    function cursor:getX()
		     return cursor.x
		end
		
		function cursor:getY()
		     return cursor.y
		end
	 
	 	function cursor:click()
		     if EarthEngine.type=="psp" then
		         return pad:cross()
			 elseif EarthEngine.type=="ds" then
			     return Stylus.Down()
			 end
		end
		
		function cursor:clickR()
		     if pad:r() and pad~=oldpad then
			     return true
			 else
			     return false
			 end
		end
	 
	 function cursor:blit(pad)

	         screen:blit(GameMap.xr+cursor:getX()*20,GameMap.yr+cursor:getY()*20,cursor.pic)
		if (GameMap.xr+cursor:getX()*20)<0  or (GameMap.yr+cursor:getY()*20)<0 or (GameMap.xr+cursor:getX()*20)>GameMap.lr or (GameMap.yr+cursor:getY()*20)>GameMap.hr then
		     cursor.x=GamePlayer.x+1
			 cursor.y=GamePlayer.y+1
		 end
	 end
	 
	 function cursor:seeInfo(y)
	     screen:blit((GameMap.lr-cursor.menu:width())/2,y,cursor.menu)
		  screen:fontPrint(futura,(GameMap.lr-cursor.menu:width())/2+27,y+16,cursor.mana,Color.new(0,0,0))
		  screen:fontPrint(futura,(GameMap.lr-cursor.menu:width())/2+137,y+16,cursor.ame,Color.new(0,0,0))
		  if cursor.add~=0 then
		      screen:blit((GameMap.lr-cursor.menu:width())/2+208,y+2,Element[cursor.add].tile.pic,20,20,20,20)
		  end
	 end
	 
	 function cursor:menuPlay()
	     screen:save("./temp.png")
	     local fond=Image.load("./temp.png")
		 local y=EarthEngine.h
		 local select=1
		 local choix={"Continuer","Menu","Quitter"}
		 oldpad=Controls.read()
		 while true do
		     --screen:clear()
			 pad=Controls.read()
		     --screen:fillRect(0,0,EarthEngine.h,EarthEngine.h,Color.new(0,0,0))
			 screen:blit(0,0,fond)
			 
			 screen:blit((EarthEngine.l-cursor.start:width())/2,y,cursor.start)
			 if y>=EarthEngine.h-cursor.start:height() then
			     y=y-2
			 end
			 for i=1,table.getn(choix) do
			     if i==select then
				     screen:fontPrint(futura,((EarthEngine.l-cursor.start:width())/2)+20,y+(20*i),choix[i],Color.new(255,0,0))
				 else
				     screen:fontPrint(futura,((EarthEngine.l-cursor.start:width())/2)+20,y+(20*i),choix[i],Color.new(0,0,0))
				 end
			 end
			 if pad:up() and pad~=oldpad and select>1 then
			     select=select-1
			 elseif pad:down() and pad~=oldpad  and select<table.getn(choix) then
			     select=select+1
			 end
			 
			 if pad:cross() and pad~=oldpad then
			     if select==1 then
			         break
			     elseif select==2 then
			         cursor.continue=false
					 break
			     elseif select==3 then
			         
			     end
             end
			 
			 screen.flip()
			 screen.waitVblankStart()
			 oldpad=pad
		 end
	 
	 end
	 
	 
	 function cursor:addMana(n)
	     cursor.mana=cursor.mana+n
	 end
	 
     return cursor
end