-- mapArea.lua

addSearchResourcesPath( moduleDirectory .. "/configuration/" )
local WorldMap = sasl.gl.loadImage("world1200_2.png",0,0,1198,509)
local ToggleMap = sasl.gl.loadImage("ToggleMap.png",0,0,80,60)
local ToggleCam = sasl.gl.loadImage("ToggleCam.png", 0, 0, 80, 60)
local green = {0, 1, 0, 1}
local white = {1, 1, 1, 1}
set(wheelZoom, 1)

function draw()
	if get(mapIsOn) == 1 then
		sasl.gl.drawTexture(WorldMap, 0, 0, 1198, 509)
		xx = 595 + 598*get(curLon)/180
		yy = 208 + 304*get(curLat)/90
		sasl.gl.drawRectangle( xx , yy , 10, 10, white )
		sasl.gl.drawWidePolyLine( points1, 2, green )
		sasl.gl.drawWidePolyLine( points2, 2, green )
		xxx= 599 + 598*get(lonD)/180
		yyy= 212 + 304*get(latD)/90
		sasl.gl.drawWideLine(xxx+7,yyy+7,xxx-7,yyy-7,2,white)
		sasl.gl.drawWideLine(xxx+7,yyy-7,xxx-7,yyy+7,2,white)
	end

	if get(hideShip2) == 0 and get(simStage) == 2 then
		sasl.gl.drawTexture(ToggleCam,85,0,80,60) -- Draw "CABIN" button
	end
	sasl.gl.drawTexture(ToggleMap, 0, 0, 80, 60)  -- Draw "MAP" button
end

function onMouseDown(component , x , y , button , parentX , parentY)
	if button == MB_LEFT then
		if x < 80 and y < 60 then               -- "MAP" pressed
			set(mapIsOn, 1 - get(mapIsOn))
		end

		if get(simStage) == 2 then
			if y < 60 then
				if 85 < x and x < 145 then      -- "CABIN" pressed
					set(isInternal, 1 - get(isInternal))
				end
			end
			if get(isInternal) == 1 then
				sasl.commandBegin( inner_view )
				sasl.commandEnd( viewSet )
			else
				sasl.commandBegin( viewSet )
				sasl.commandEnd( inner_view )
				set(wheelZoom, 1)
			end
		end
	end
	return true
end

function onMouseWheel( component, x, y, button, parentX, parentY, value )
	if get(isInLimit) == 1 then                     -- View is in Cabin
		set(driftX, 0.05*value*math.sin(math.rad(get(headPsi))))
		set(driftY, 0.05*value*math.sin(math.rad(get(headThe))))
		set(driftZ, -0.05*value*math.cos(math.rad(get(headPsi))))
		set(headX, get(headX) + get(driftX))
		set(headY, get(headY) + get(driftY))
		set(headZ, get(headZ) + get(driftZ))
--		print(get(isInLimit),get(headX),get(headY),get(headZ),wallR,limitR)
	end
	return true
end
