--------------------------------------------------------------------------------
-- SimNetwork
-- input.lua
-- Handles user input:	- rotation of 3D scene
--			- zoom in/out
--------------------------------------------------------------------------------
SimNetwork_INPUT = true

if not SimNetwork_SCENE then	dofile("scene.lua") end

-- Global variables
-- ----------------
input = {
	MouseX = 0,		-- Absolute location of mouse pointer in coordinates
	MouseY = 0,		-- of Electro's window (and 2D scene)
	
	MouseLDown = false,	-- Status of mouse buttons (false for up, true for down)
	MouseRDown = false,
	MouseMDown = false,

	ScrollLeft = false,	-- Indicates whether left, right, up, or down
	ScrollRight = false,	-- keys are pressed (for scrolling the map)
	ScrollUp = false,
	ScrollDown = false,
}	
zoomRate = 2

selection = {
	current = nil
}
-- -----------------------------------------------------------------------------
-- Function: handle_3d_rotation
-- Inputs:	dx, dy (integer): relative pointer movement
-- Returns:	boolean: indicates whether screen needs refresh
-- Description: rotates 3D scene about X and Y, also restricts rotation
-- to (-360, 360) about Y, [0, 90] about X
-- -----------------------------------------------------------------------------
function handle_3d_rotation(dx, dy)
	local refresh = false
	local xRotation = 0
	local yRotation = 0
	
	if dx ~= 0 then
		-- Rotate 3D scene about Y axis 
		yRotation = dx
		refresh = true
	end
	
	if dy ~= 0 then
		-- Rotate 3D scene about X axis
		xRotation = dy
		refresh = true
	end
	
	if refresh then
		-- gameScene:registerRotation(0, yRotation)
		gameScene:rotate(xRotation, yRotation)	
	end
	
	return refresh
end

-- -----------------------------------------------------------------------------
-- Function: handle_3d_zoom
-- Inputs:	dy (integer): relative pointer movement
-- Returns:	boolean: indicates whether screen needs refresh
-- Description: Zooms in/out
-- -----------------------------------------------------------------------------
function handle_3d_zoom(dy)
	if dy ~= 0 then
		local zoom = (dy / 500) * zoomRate
		gameScene:changeZoom(zoom)
		return true
	end
	return false
end

function handle_scroll()
	
	local scrollV = {0, 0}
	
	if input.scrollLeft then
			scrollV[1] = -1
	elseif input.scrollRight then
			scrollV[1] = 1
	end
	
	if input.scrollDown then
			scrollV[2] = 1
	elseif input.scrollUp then
			scrollV[2] = -1
	end

	if scrollV[1] ~= 0 or scrollV[2] ~= 0 then
		gameScene:scroll(scrollV)
	end
end
-- -----------------------------------------------------------------------------
-- Function: do_click() (callback)
-- Inputs:	b: button clicked
--		s: status; true for pressed, false for released
-- Description: handle toolbar clicking (selecting tools) and 3D mouse picking
-- in 3D scene.
-- -----------------------------------------------------------------------------
function do_click(b, s)

	if b==1 then
		input.MouseLDown = s
		
		if	input.MouseLDown and input.MouseX > 0 and 
			input.MouseX < toolbarWidth and 
			input.MouseY > 0 and input.MouseY < 32 then
			-- left click within tool bar
			return handle_toolbar_click()
		
		elseif input.MouseLDown then
			-- left click within 3D scene, do mouse picking
			-- mouse picking on 3D objects
			-- remove previously selected element
			E.set_entity_alpha(gameScene.selected, 1.0)
			gameScene.selected = nil
			gameScene:enableMouseRay(gameScene.categorySelectable)
		end
	
	elseif b==3 then
		input.MouseRDown = s
	elseif b==4 then
		-- Mouse wheel down
		return handle_3d_zoom(-4)
	elseif b==5 then
		-- Mouse wheel up
		return handle_3d_zoom(4)
	elseif b == 2 then
		input.MouseMDown = s
	end
	
	return false
	
end

-- -----------------------------------------------------------------------------
-- Function: do_point() (callback)
-- Inputs:	dx, dy: pointer movement, relative to last position
-- Description: Check whether Right/Middle button are down and perform
-- 3D rotation/zoom repspectively
-- -----------------------------------------------------------------------------
function do_point(dx, dy)

	input.MouseX = input.MouseX + dx
	input.MouseY = input.MouseY + dy
		
	local refresh = false
	
	if input.MouseLDown then
	end

	-- 3D rotation
	if input.MouseRDown then
		refresh = handle_3d_rotation(dx, dy)
	end	
	
	-- zoom in/out
	if input.MouseMDown then
		refresh = refresh or handle_3d_zoom(dy)
	end	

	if not input.MouseLDown and not input.MouseRDown and not input.MouseMDown then
		return handle_toolbar_hover()
		
	end
	
	--[[
	-- enable collision between mouse ray and floor
	gameScene:enableMouseRay(gameScene.categoryFloor)
	
	-- if drop router visible, hide it, it will be redisplayed
	-- by Scene:mousePick if necessary
	if not (E.get_entity_flags(gameScene.dropRouter, E.entity_flag_hidden)) then
		E.set_entity_flags(gameScene.dropRouter, E.entity_flag_hidden, true)
		refresh = true
	end
	--]]
	
	return refresh
end

-- -----------------------------------------------------------------------------
-- Function: do_keyboard() (callback)
-- Inputs:	b: button, s: pressed/released
-- -----------------------------------------------------------------------------
function do_keyboard(b, s)
	-- Map scrolling keys
	if b == E.key_left and s then
		input.scrollLeft = true
	elseif b == E.key_left and not s then
		input.scrollLeft = false
	elseif b == E.key_down and s then
		input.scrollDown = true
	elseif b == E.key_down and not s then
		input.scrollDown = false
	elseif b == E.key_right and s then
		input.scrollRight = true
	elseif b == E.key_right and not s then
		input.scrollRight = false
	elseif b == E.key_up and s then
		input.scrollUp = true
	elseif b == E.key_up and not s then
		input.scrollUp = false
	end
end

function do_timer(dt)
	handle_scroll()
	return true
end

