/* BWOverWorld */

#import "Variables.h"

struct sArea
{
	unsigned char x;
	unsigned char y;
	unsigned char z;
	unsigned char w;
};

@interface BWOverWorld : NSMatrix
{
	IBOutlet id objMain;
	IBOutlet id objSideView;
	
    IBOutlet id matrixTiles;
	IBOutlet id matrixTools;
	IBOutlet id scrollView;
	
	IBOutlet id textPosition;
	IBOutlet id textGroups;
	IBOutlet id fieldGroupsMax;
	
	IBOutlet id menuRegion;
	IBOutlet id menuArea;
	IBOutlet id buttonSideView;
	
	IBOutlet id fieldRegion;
	IBOutlet id fieldWorld;
	IBOutlet id fieldMap;
	IBOutlet id fieldEnter;
	IBOutlet id menuExit;
	IBOutlet id matrixFlags;
	
	IBOutlet id matrixPalettes;
	IBOutlet id matrixPalettesTiles;
	IBOutlet id matrixColors;
	IBOutlet id windowColors;
	IBOutlet id boxColor;
	
	IBOutlet id matrixLasso;
	IBOutlet id buttonLassoFill;
	IBOutlet id buttonLassoSwap1;
	IBOutlet id buttonLassoSwap2;
	IBOutlet id buttonLassoRandomize;
	IBOutlet id fieldLassoRandomize;
	
	IBOutlet id matrixStatsTiles;
	IBOutlet id matrixStatsValues;
	
	NSMutableData *rom;
	NSMutableArray *tiles;
	NSMutableArray *sprites;
	unsigned const char *ptr;
	
	unsigned char numberOfBanks;
	
	char currentTool;			// 0 = Selection // 1 = Bracket
	char currentTile;
	unsigned char sr;
	unsigned char cr;			// Current Region (WH, DM, EH, MI)
	
	int sel;					// Current selected area (-1 for Show All)
	
	BOOL lassoActive;
	unsigned short lassoLocation;
	char lassoWidth;
	char lassoHeight;
	
	id lassoWindowSelection;
	unsigned char lassoFill;
	unsigned char lassoSwap1;
	unsigned char lassoSwap2;
	unsigned char lassoRandomize;
	
	struct sArea keyAreas[0x3F][4];
	
	unsigned int offSet;
	unsigned int offSetPalaces;
	unsigned int maxGroups[4];
	
	unsigned short g[4];						// Current number of groups for each region
	unsigned char overworldData[4][0x400];		// Current map data for each region
	unsigned char overworldGrid[4][0x10000];	// Current matrix data for each region
	unsigned char brackets[4][0x10000];
	
	unsigned char palettes[0x20];
	unsigned char palettesCodes[0x10];
	unsigned char selPalette;					// Selected Palette
	unsigned char selColor;						// Selected Color
	
	BOOL showGrid;
	BOOL showAreas;
	BOOL showConnections;
	BOOL showBrackets;
	BOOL showLink;
	BOOL useDefaultTiles;
	
	NSPoint linkPosition;
	
	NSImage *testimg;
}

- (void)open:(NSMutableData *)theRom;
- (void)save;

- (void)loadPalettes;
- (void)loadTiles;
- (void)loadPalettesWindow;
- (void)loadRegions;
- (void)loadAreas;

- (NSImage *)tileWithOffset:(int)os palette:(struct sCol *)pal data:(NSData *)data;
- (NSImage *)graphicWithTiles:(NSArray *)tilesList type:(unsigned char)size;

- (IBAction)changeRegion:(id)sender;
- (IBAction)changeArea:(id)sender;

- (IBAction)changeTile:(id)sender;
- (IBAction)changeTool:(id)sender;
- (IBAction)actionMaxGroups:(id)sender;
- (IBAction)actionFields:(id)sender;
- (IBAction)changeExit:(id)sender;
- (IBAction)actionFlags:(id)sender;

- (IBAction)goToSideView:(id)sender;
- (unsigned char)enterCodeForArea:(unsigned char)areaCode world:(unsigned char)worldCode;

- (IBAction)lassoSelectTile:(id)sender;
- (IBAction)lassoChangeTile:(id)sender;
- (IBAction)lassoFill:(id)sender;
- (IBAction)lassoSwap:(id)sender;
- (IBAction)lassoRandomize:(id)sender;

- (IBAction)changeSelectedPalette:(id)sender;
- (IBAction)changeColor:(id)sender;
- (IBAction)changePaletteCode:(id)sender;

- (IBAction)changeShowGrid:(id)sender;
- (IBAction)changeShowAreas:(id)sender;
- (IBAction)changeShowConnections:(id)sender;
- (IBAction)changeShowBrackets:(id)sender;
- (IBAction)changeShowLink:(id)sender;
- (IBAction)changeUseDefaultTiles:(id)sender;

- (void)changeTileWithTag:(int)tag;
- (void)recalculate;
- (void)compileStats;
- (void)palacePointers;

- (IBAction)exportRegion:(id)sender;
- (IBAction)changeShowGrid:(id)sender;

- (unsigned int)stringToHex:(NSString *)str;
- (NSString *)hexToString:(unsigned int)hex;

@end
