
#import "WowView.h"

@implementation WowView

- initFrame:(const NXRect *)frm // initialize instance
{
	[super initFrame:frm];
	// load the fading and scrolling images that we will use.
	welcomeImage = [NXImage findImageNamed:"Fade2.tiff"];
	creditsImage = [NXImage findImageNamed:"Fade1.tiff"];
	scrollImage = [NXImage findImageNamed:"ScrollingImage.tiff"];
	// start at the beginning.
	state = FADE_IN_WELCOME; frameNumber = 0;
	return self;
}

- start:sender
{ // make sure we start out from the beginning of the sequence...
	state = FADE_IN_WELCOME; frameNumber = 0;
	return [super start:sender];
}

static framesInState[NUMSTATES] = { WELCOME_FRAMES, HOLD_FRAMES,
	SCROLL_FRAMES, HOLD_FRAMES, CREDIT_FRAMES, HOLD_FRAMES, CREDIT_FRAMES };
static nextState[NUMSTATES] = { WELCOME_HOLD, SCROLLING, SCROLL_HOLD, FADE_TO_CREDIT, CREDITS_HOLD, FADE_TO_GRAY, FADE_IN_WELCOME };

- autoUpdate:sender
{
	frameNumber++;
	if (state == SCROLLING) frameNumber += SCROLL_JUMP;
	if (frameNumber > framesInState[state]) {
		state = nextState[state];
		frameNumber = 0;
	}
	[window disableFlushWindow];
	[self update];
	[window display];
	[window reenableFlushWindow];
	[window flushWindow];
	NXPing();	// sync up with the window server.
	return self;
}

- drawSelf:(NXRect *)rects :(int)rectCount	// redraws the screen.
{
	switch (state) {
		case FADE_IN_WELCOME : {
			PSsetgray(0.666);
			NXRectFill(&bounds);
			[welcomeImage dissolve:((float)frameNumber/(float)WELCOME_FRAMES)
					toPoint:&(bounds.origin)];
			break;
		}
		case SCROLLING : {
			NXRect scrollRect =
					{{ 0.0, 650 - frameNumber }, { 354, MIN(frameNumber, 130) }};
			if (frameNumber < 130)
				[welcomeImage composite:NX_COPY toPoint:&(bounds.origin)];
			[scrollImage composite:NX_COPY fromRect:&scrollRect
					toPoint:&(bounds.origin)];
			break;
		}
		case FADE_TO_CREDIT : {
			NXRect scrollRect = {{ 0.0, 0.0 }, { 354, 130 }};
			[scrollImage composite:NX_COPY fromRect:&scrollRect
					toPoint:&(bounds.origin)];
			[creditsImage dissolve:((float)frameNumber/(float)CREDIT_FRAMES)
					toPoint:&(bounds.origin)];
			break;
		}
		case FADE_TO_GRAY : {
			PSsetgray(0.666);
			NXRectFill(&bounds);
			[creditsImage dissolve:(1.0-((float)frameNumber/(float)CREDIT_FRAMES))
					toPoint:&(bounds.origin)];
			break;
		}
		case CREDITS_HOLD : {
			[creditsImage composite:NX_COPY toPoint:&(bounds.origin)];
			break;
		}
		case WELCOME_HOLD : {
			[welcomeImage composite:NX_COPY toPoint:&(bounds.origin)];
			break;
		}
		case SCROLL_HOLD : {
			NXRect scrollRect = {{ 0.0, 0.0 }, { 354, 130 }};
			[scrollImage composite:NX_COPY fromRect:&scrollRect
					toPoint:&(bounds.origin)];
			break;
		}
		default : {
			PSsetgray(0.666);
			NXRectFill(&bounds);
			break;
		}
	}
	return self;
}

@end
