#########################################################
#A generic Jam-O-Drum class to interface with the  spinners
#Author jsmith2
#Last Updated Aug 11, 2004
#########################################################
from globals import * #these are our global definey thigns
from direct.showbase import DirectObject



class Spinners(DirectObject.DirectObject):
    #****************************************************************************
    #Constructor for a Drumpads object
    #****************************************************************************/
    def __init__(self):
         self.mIsLoaded=False
         
    #****************************************************************************
    #Initializes the Joysticks that are the spinners.
    #The computer sees the Spinners as two joysticks- with each joysticks controlling two 
    #@return true if InitJoysticks succeeds, else false
    #****************************************************************************/
    def Start(self):    
        #cheap jamodrum emulator
        self.accept('l', self.Handler, [STATION_0, SPIN_CW, .1])
        self.accept('f', self.Handler, [STATION_1, SPIN_CW, .1])
        self.accept('r', self.Handler, [STATION_2, SPIN_CW, .1])
        self.accept('o', self.Handler, [STATION_3, SPIN_CW, .1])
        self.accept('j', self.Handler, [STATION_0, SPIN_CCW, -.1])
        self.accept('s', self.Handler, [STATION_1, SPIN_CCW, -.1])
        self.accept('w', self.Handler, [STATION_2, SPIN_CCW, -.1])
        self.accept('u', self.Handler, [STATION_3, SPIN_CCW, -.1])
        #clean state all ready to be trampled
        self.mState=[]
        self.mSpinnerState=[]
        self.mLastPos=[]
        for j in range(NUM_STATIONS):
            self.mLastPos.append(0)
            self.mSpinnerState.append(SPIN_NOSPIN)
        self.mIsLoaded= True
         
        
    #****************************************************************************
    #Clear out drumpad data
    #@todo add error messages and such
    #***************************************************************************/     
    def Clear(self):
        for s in self.mSpinners:
            s=SPIN_NOSPIN
    
    #****************************************************************************/    
    #See if spinners were loaded successfully- that is, if the joysticks were found.
    #@return true is joysticks have been loaded
    #****************************************************************************/  
    def IsLoaded(self):
        return self.mIsLoaded

    #****************************************************************************
    #Retrieve state of requested spinner.
    #not really needed if we continue to use messages here- this was used for the c++ stuff
    #@param pSpinner requested spinner.
    #@return state of pSpinner.
    #****************************************************************************/
    def GetSpinnerState(self, pSpinner):
        return self.mSpinners[pSpinner]

    #****************************************************************************
    #Handle spinners based on poll results
    #@param pSpinner spinner whose state we are updating
    #@param pDirection direction spinner was spun
    #****************************************************************************/
    def Handler(self, pSpinner,pDirection, speed):
        print "station ", pSpinner," spun:" 
        self.printDir(pDirection)
        self.mSpinnerState[pSpinner]= pDirection
        s="SPIN_"+str(pSpinner)+"_"+str(pDirection)
        messenger.send(s, [speed]) 

    def printDir(self, pDir):
        if pDir == SPIN_NOSPIN:
            print "No Direction"
        elif pDir == SPIN_CW:
            print "clockwise"
        else:
            print "counterclockwise"
            
    def Poll(self):
        i= STATION_0
        for mw in  base.pointerWatcherNodes:
        #for i in range(3):
            #mw = base.pointerWatcherNodes[i+1]
            n = mw.getName()
            #print n
            if (mw.hasMouse() and i<3 and not(n == "ACPI#PNP0F13" or n == "keyboard/mouse")):
                #print n
                if i==0:
                    diff= self.mLastPos[i] - mw.getMouseY()
                    self.mLastPos[i]=mw.getMouseY()
                    if diff>0:
                        self.Handler(i, SPIN_CW, diff)
                    elif diff<0:
                        self.Handler(i, SPIN_CCW, diff)
    
    
                    diff= self.mLastPos[i+1] - mw.getMouseX()
                    self.mLastPos[i+1]=mw.getMouseX()
                    if diff>0:
                        self.Handler(i+1, SPIN_CCW, diff)
                    elif diff<0:
                        self.Handler(i+1, SPIN_CW, diff)
                else:
                    diff= self.mLastPos[i] - mw.getMouseX()
                    self.mLastPos[i]=mw.getMouseX()
                    if diff>0:
                        self.Handler(i, SPIN_CCW, diff)
                    elif diff<0:
                        self.Handler(i, SPIN_CW, diff)
    
    
                    diff= self.mLastPos[i+1] - mw.getMouseY()
                    self.mLastPos[i+1]=mw.getMouseY()
                    if diff>0:
                        self.Handler(i+1, SPIN_CW, diff)
                    elif diff<0:
                        self.Handler(i+1, SPIN_CCW, diff)

                i+=2




    
