#########################################################
#A generic Jam-O-Drum class to interface with the  spinners
#This files is specific to those people who don't have access to the jamodrum
#these files are replaced with better files on the actual drum
#
#DO NOT MAKE CHANGES TO THIS FILE- YOU WON'T GET THEM ON THE DRUM
#
#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.mLastDirection=[]
        for j in range(NUM_STATIONS):
            self.mLastDirection.append(SPIN_NOSPIN)
            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,spin):
        print "station ", pSpinner," spun:"
        self.printDir(pDirection)
        self.mSpinnerState[pSpinner]= pDirection
        self.mLastDirection[pSpinner]=pDirection #having two variables is sorta wierd here.  i'm sure we had a reason back when we did this...
        s="SPIN_"+str(pSpinner)+"_"+str(pDirection)
        messenger.send(s, [spin]) 

    def printDir(self, pDir):
        if pDir == SPIN_NOSPIN:
            print "No Direction"
        elif pDir == SPIN_CW:
            print "clockwise"
        else:
            print "counterclockwise"
            
    def Poll(self):
        if self.mIsLoaded:
            return #this does nothign in this file, since everythign is key presses
    
