// ArmyView.m
// Part of Risk by Mike Ferris

#import "ArmyView.h"
#import <appkit/TextField.h>
#import <appkit/NXImage.h>
#import <appkit/Panel.h>

@implementation ArmyView

+ initialize
{
	if (self == [ArmyView class])  {
		[self setVersion:1];
	}
	return self;
}

- initFrame:(NXRect *)frm
{
	[super initFrame:frm];
	armiesLeft=0;
	soldierImage = [NXImage findImageNamed:"Soldier"];
	fiveImage = [NXImage findImageNamed:"5Soldiers"];
	tenImage = [NXImage findImageNamed:"10Soldiers"];
	if (soldierImage==nil)  {
		NXRunAlertPanel("Error", "Couldn't find soldier image.", "OK", NULL, 
						NULL);
	}
	
	return self;
}

- free
{
	[soldierImage free];
	[fiveImage free];
	[tenImage free];
	return [super free];
}

- (int)armiesLeft
{
	return armiesLeft;
}

- setArmiesLeft:(int)num
{
	armiesLeft=num;
	[armiesLeftTextField setIntValue:armiesLeft];
	[self display];
	
	return self;
}

- decreaseArmiesLeftBy:(int)num
{
	armiesLeft-=num;
	[armiesLeftTextField setIntValue:armiesLeft];
	[self display];
	
	return self;
}

- increaseArmiesLeftBy:(int)num;
{
	armiesLeft+=num;
	[armiesLeftTextField setIntValue:armiesLeft];
	[self display];
	
	return self;
}

#define INTERSPACE	-1.0
- drawSelf:(NXRect *)rects :(int)rectCount
{
	NXPoint point={3.0, 5.0};
	NXSize imageSize;
	int i;
	int tens, fives, ones;
	
	i=armiesLeft;
	tens=i/10; i=i%10;
	fives=i/5;
	ones=i%5;
	
	NXDrawWhiteBezel(&bounds, NULL);
	[tenImage getSize:&imageSize];
	for (i=0;i<tens;i++)  {
		[tenImage composite:NX_SOVER toPoint:&point];
		point.x+=imageSize.width+INTERSPACE;
	}
	[fiveImage getSize:&imageSize];
	for (i=0;i<fives;i++)  {
		[fiveImage composite:NX_SOVER toPoint:&point];
		point.x+=imageSize.width+INTERSPACE;
	}
	[soldierImage getSize:&imageSize];
	for (i=0;i<ones;i++)  {
		[soldierImage composite:NX_SOVER toPoint:&point];
		point.x+=imageSize.width+INTERSPACE;
	}
	
	return self;
}

@end
