
#import "demoDirector.h"
#import "UnderstudyAgent.h"
#import "PAStringList.h"

@implementation demoDirector

+ (BOOL)allowMultipleLoads
{
  return NO ;
}

- initWithController:aController andTag:(int)aTag 
{
  char nibPath[MAXPATHLEN+1] ;

  [super initWithController:aController andTag:aTag] ;

  if ([[NXBundle bundleForClass:[self class]] getPath:nibPath forResource:"demoDirector" ofType:"nib"])
  {
    [NXApp loadNibFile:nibPath owner:self] ;
  }

  if ((mySound=[Sound findSoundFor:"ILive"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- activationOccurredOn:anActor 
{
  if ((mySound=[Sound findSoundFor:"ActorActivated"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- clearOccurredOn:anActor 
{
  if ((mySound=[Sound findSoundFor:"AttachmentRemoved"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- didAddActor:anActor 
{
  if ((mySound=[Sound findSoundFor:"ActorAdded"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- documentWillClose 
{
  if ((mySound=[Sound findSoundFor:"CloseDocument"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- dragOccurredOn:anActor 
{
  if ((mySound=[Sound findSoundFor:"ActorDragged"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- dropOccurredOn:anActor 
{
  id newActor ;
  char actorPath[MAXPATHLEN+1] ;

  if ((mySound=[Sound findSoundFor:"AttachmentAdded"]))
  {
    [mySound play:self] ;
  }

  /* add a new actor with the same attachment */
  sprintf(actorPath, "%s/Library/Actors/RedBall.actor", getenv("HOME")) ;
  newActor=[self createNewActorFrom:actorPath] ;
  [newActor setAttachments:[anActor droppedFiles]] ;

  /* make the actor move around */
  [newActor flipWhileStrolling:self] ;
  [newActor strollBounce:7 :8 interval:0.02] ;

  return self ;
}

- launchOccurredOn:anActor 
{
  if ((mySound=[Sound findSoundFor:"AttachmentLaunched"]))
  {
    [mySound play:self] ;
  }
  
  return self ;
}

- specialActivationOccurredOn:anActor
{
  char **theAttachments ;
  int looper=-1 ;
  char theTitle[100] ;

  /* get the attachments on the actor */
  if (!(theAttachments=[anActor droppedFiles]))
  {				/* no attachments, so just show alert panel */
    NXRunAlertPanel([[self class] name], "Special Activation Occurred for %s", NULL, NULL, NULL,
		    [anActor alias]) ;
    return self ;
  }

  /* empty the stringHandler */
  [stringHandler freeStrings] ;

  /* fill the string handler with the attachment names */
  while (theAttachments[++looper])
  {
    [stringHandler addString:theAttachments[looper]] ;
  }

  /* have the attachmentsBrowser fill itself */
  [attachmentsBrowser loadColumnZero] ;

  /* change the title on the attachmentsPanel */
  sprintf(theTitle, "%s:Attachments for %s", [[self class] name], [anActor alias]) ;
  [attachmentsPanel setTitle:theTitle] ;

  /* bring up the attachmentsPanel */
  [attachmentsPanel makeKeyAndOrderFront:self] ;

  return self ;
}

- willEnterScreenSaverMode 
{
  if ((mySound=[Sound findSoundFor:"StartScreenSave"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- willExitScreenSaverMode 
{
  if ((mySound=[Sound findSoundFor:"StopScreenSave"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

- willRemoveActor:anActor 
{
  if ((mySound=[Sound findSoundFor:"ActorRemoved"]))
  {
    [mySound play:self] ;
  }

  return self ;
}

@end
