###########################################################
# Mouse Controls - Railshooter Module
# Defines the Mouse Controls
###########################################################

# DESCRIPTION:
# This Class handles the Mouse Controls for the game.  The current
# method for accessing the Mouse Controls is to re-center the mouse
# pointer each frame, and check to see how far the mouse cursor was
# from the center point.  This way, we can measure how strong the X
# and Y input from the mouse is.
 

from direct.showbase.ShowBaseGlobal import *
from direct.showbase.DirectObject import DirectObject
from direct.task import Task

# Import Railshooter Specific Modules
import ClassGlobals


########################################
#   Handles Mouse Input and Controls   #
########################################
class MouseControls(DirectObject):
    def __init__(self):

    	self.inverted = 0
    	base.disableMouse()
	self.mouseChangeX = 0
	self.mouseChangeY = 0
	self.deadZone = 0.5
	self.maxZone = 80.0
	self.mouseSensitivity = 160.0

    # Monitor Mouse Coordinates
    def mouseMonitor(self, task):

	# Get Mouse Coordinates and Compare Difference from 
	# Center of Window to get Mouse Input
        md = base.win.getPointer(0)
        x = md.getX()
        y = md.getY()
        if base.win.movePointer(0, self.mouseCenter[0], self.mouseCenter[1]):
            self.mouseChangeX = x - self.mouseCenter[0]

            if self.inverted == 0:
                self.mouseChangeY = y - self.mouseCenter[1]
            else:
                self.mouseChangeY = (y - self.mouseCenter[1]) * -1

        else:
            # We weren't allowed to zap the pointer back to the
            # center, so pretend that the pointer hasn't moved.
            self.mouseChangeX = 0
            self.mouseChangeY = 0
            
	return Task.cont


    # Initialize the Mouse Controls
    def startMouse(self):

	# Get Center Point of Screen
    	props = base.win.getProperties()
        self.mouseCenter = (props.getXOrigin() + props.getXSize()/2, props.getYOrigin() + props.getYSize()/2)

        # Make the mouse invisible
        props = WindowProperties()
        props.setCursorHidden(True)
        base.win.requestProperties(props)

        # Put the mouse in the center
        base.win.movePointer(0, self.mouseCenter[0], self.mouseCenter[1])
        
	ClassGlobals.mouseIsEnabled = 1

	# Add Task to Monitor Mouse Behavior
	taskMgr.add(self.mouseMonitor, 'mouseMonitor1')


    # Disable the Mouse Controls
    def stopMouse(self):
	ClassGlobals.mouseIsEnabled = 0

        # Restore the mouse's visibility
        props = WindowProperties()
        props.setCursorHidden(False)
        base.win.requestProperties(props)

        taskMgr.remove('mouseMonitor1')










