summaryrefslogtreecommitdiff
path: root/GfxEdInterface.cs
blob: 2491a080a1cced071dd85c31ad05bf9ba65c032f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// GfxEd interface
// Copyright (C) 2004  Ian Cowburn
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// $Id$
//
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace GfxEdInterface
{
	/// <summary>
	/// Defines the interface a plugin must follow
	/// </summary>
	public interface IPlugin
	{
		/// <summary>
		/// The description for this plugin
		/// </summary>
		string			Description		{get;}
		
		/// <summary>
		/// The short name (for drop down list use) of this plugin
		/// </summary>
		string			ShortName		{get;}
		
		/// <summary>
		/// The copyright and author information for this plugin
		/// </summary>
		string			Author			{get;}

		/// <summary>
		/// The URL where this plugin can be found
		/// </summary>
		string			URL				{get;}

		/// <summary>
		/// The maximum number of colours for a sprite
		/// </summary>
		uint			MaxColours		{get;}
		
		/// <summary>
		/// The machines palette.  Sprite colours are selected from this.
		/// </summary>
		/// <remarks>
		/// Note that colour zero is considered the transparent/background
		/// colour and cannot be selected for drawing.  On platforms where the
		/// actual colour could be used, duplicate it and weight your palette
		/// routines when outputing accordingly.
		/// </remarks>
		Colour[]		Palette			{get;}
		
		/// <summary>
		/// An array indicating that that colour in the sprite can be changed.
		/// </summary>
		bool[]			SprPalEditable	{get;}
		
		/// <summary>
		/// An array indicating that that colour in the sprite is common to all.
		/// </summary>
		bool[]			SprPalCommon	{get;}
		
		/// <summary>
		/// The allowable sprite sizes.
		/// </summary>
		SpriteSize[]	AllowedSizes	{get;}

		/// <summary>
		/// The default extension for exports 
		/// </summary>
		string			ExportExtension	{get;}

		/// <summary>
		/// Exports the sprites.
		/// </summary>
		/// <param name="path">The exported filename</param>
		/// <param name="sprites">A list of sprites to export</param>
		/// <returns></returns>
		bool			Export(string path, SpriteList sprites);
		
		/// <summary>
		/// Get the configuration object for the plugin.
		/// </summary>
		/// <remarks>
		/// If there is no configuration required for the plugin, null can be
		/// returned.
		/// </remarks>
		IConfig			Config			{get;}
		
		/// <summary>
		/// Get the processing object for the plugin.
		/// </summary>
		/// <remarks>
		/// If there is no processing supplied by the plugin, null can be
		/// returned.
		/// </remarks>
		IProcess			Process			{get;}
	}

	
	/// <summary>
	/// Defines the configuration interface for a plugin.
	/// </summary>
	public interface IConfig
	{
		/// <summary>
		/// Shows the configuration dialog.
		/// </summary>
		void	Settings(Form parent);
		
		/// <summary>
		/// Outputs the configuration to the suppluied stream.
		/// </summary>
		/// <remarks>
		/// All file operations are done using text files.
		/// </remarks>
		/// <param name="stream">The output stream</param>
		void	Output(TextWriter stream);
		
		/// <summary>
		/// Inputs the configuration from the suppluied stream.
		/// </summary>
		/// <remarks>
		/// All file operations are done using text files.
		/// </remarks>
		/// <param name="stream">The output stream</param>
		void	Input(TextReader stream);
	}

	
	/// <summary>
	/// Defines the processing interface for a plugin.
	/// </summary>
	public interface IProcess
	{
		/// <summary>
		/// Process the sprite.
		/// </summary>
		void	Single(Sprite s);
		
		/// <summary>
		/// Process all the sprites.
		/// </summary>
		void	All(SpriteList s);
	}

	
	/// <summary>
	/// Defines the allowable sizes for a sprite and their aspect ratios
	/// </summary>
	public class SpriteSize
	{
		public SpriteSize(uint w, uint h, uint xaspect, uint yaspect)
		{
			m_width=w;
			m_height=h;
			m_xaspect=xaspect;
			m_yaspect=yaspect;
		}
		
		public uint Width	{get {return m_width;}}
		public uint Height	{get {return m_height;}}
		public uint XAspect	{get {return m_xaspect;}}
		public uint YAspect	{get {return m_yaspect;}}
		
		public override string ToString()
		{
			return m_width.ToString()+","+m_height.ToString();
		}

		private uint	m_width;
		private uint	m_height;
		private uint	m_xaspect;
		private uint	m_yaspect;
	}


	/// <summary>
	/// Describes a colour.  And yes, a *very* dumb class name.
	/// </summary>
	public class Colour
	{
		public Colour(string name, byte red, byte green, byte blue)
		{
			m_name=name;
			m_red=red;
			m_blue=blue;
			m_green=green;
		}
		
		public override string ToString()
		{
			return Name;
		}

		public string Name
		{
			get {return m_name;}
			set {m_name=value;}
		}
		
		public byte Red
		{
			get {return m_red;}
			set {m_red=value;}
		}
		
		public byte Green
		{
			get {return m_green;}
			set {m_green=value;}
		}
		
		public byte Blue
		{
			get {return m_blue;}
			set {m_blue=value;}
		}
		
		public Color DrawCol
		{
			get {return Color.FromArgb(m_red,m_green,m_blue);}
		}
		
		private string	m_name;
		private byte	m_red;
		private byte	m_green;
		private byte	m_blue;
	}


	/// <summary>
	/// Describes a sprite
	/// </summary>
	public class Sprite
	{
		public Sprite(string name, uint width, uint height, uint maxcol)
		{
			m_name=name;
			m_width=width;
			m_height=height;
			m_data=new uint[m_width,m_height];
			m_pal=new uint[maxcol];
			
			for(uint f=0;f<maxcol;f++)
			{
				m_pal[f]=f;
			}
		}
		
		public Sprite(string name, SpriteSize size, uint maxcol)
		{
			m_name=name;
			m_width=size.Width;
			m_height=size.Height;
			m_data=new uint[m_width,m_height];
			m_pal=new uint[maxcol];
			
			for(uint f=0;f<maxcol;f++)
			{
				m_pal[f]=f;
			}
		}
		
		public Sprite(Sprite old)
		{
			m_name=old.m_name;
			m_width=old.m_width;
			m_height=old.m_height;
			m_data=new uint[m_width,m_height];
			m_pal=new uint[old.m_pal.Length];
			
			for(int x=0;x<m_width;x++)
				for(int y=0;y<m_height;y++)
					m_data[x,y]=old.m_data[x,y];
			
			for(int f=0;f<m_pal.Length;f++)
				m_pal[f]=old.m_pal[f];
		}
		
		public string Name
		{
			get {return m_name;}
			set {m_name=value;}
		}
		
		public uint Width
		{
			get {return m_width;}
		}
		
		public uint Height
		{
			get {return m_height;}
		}
		
		public void Resize(uint width, uint height)
		{
			uint[,] data=new uint[width,height];
			uint mx=Math.Min(width,m_width);
			uint my=Math.Min(height,m_height);
			
			for(int x=0;x<mx;x++)
				for(int y=0;y<my;y++)
					data[x,y]=m_data[x,y];
			
			m_width=width;
			m_height=height;
			m_data=data;
		}
		
		public uint this [uint x, uint y]
		{
			set {m_data[x,y]=value;}
			get {return m_data[x,y];}
		}
		
		public uint Pal(uint i)
		{
			return m_pal[i];
		}
		
		public void Pal(uint i, uint c)
		{
			m_pal[i]=c;
		}
		
		public void Clear()
		{
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
					m_data[x,y]=0;
		}
		
		public void MirrorHorizontal()
		{
			uint[,] d=new uint[m_width,m_height];
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				d[x,y]=m_data[m_width-x-1,y];
			
			m_data=d;
		}

		public void MirrorVertical()
		{
			uint[,] d=new uint[m_width,m_height];
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				d[x,y]=m_data[x,m_height-y-1];
			
			m_data=d;
		}
		
		public void Scroll(int dx, int dy)
		{
			uint[,] d=new uint[m_width,m_height];
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				d[Mod(x+dx,m_width),Mod(y+dy,m_height)]=m_data[x,y];
			
			m_data=d;
		}
		
		public void RotateRight()
		{
			uint[,] d=new uint[m_width,m_height];
			uint min=Math.Min(m_width,m_height);
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				{
					long nx=min-y-1;
					long ny=x;
					
					if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
					{
						d[nx,ny]=m_data[x,y];
					}
				}
			
			m_data=d;
		}

		public void RotateLeft()
		{
			uint[,] d=new uint[m_width,m_height];
			uint min=Math.Min(m_width,m_height);
			
			for(int y=0;y<m_height;y++)
				for(int x=0;x<m_width;x++)
				{
					long nx=y;
					long ny=min-x-1;
					
					if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
					{
						d[nx,ny]=m_data[x,y];
					}
				}
			
			m_data=d;
		}

		public void Output(TextWriter stream)
		{
			stream.WriteLine(m_name);
			stream.WriteLine(m_width);
			stream.WriteLine(m_height);

			stream.Write(m_pal.Length);
			
			for(int f=0;f<m_pal.Length;f++)
			{
				stream.Write(","+m_pal[f]);
			}
			
			stream.WriteLine();
			
			for(int y=0;y<m_height;y++)
			{
				for(int x=0;x<m_width;x++)
				{
					if (x==0)
					{
						stream.Write(m_data[x,y]);
					}
					else
					{
						stream.Write(","+m_data[x,y]);
					}
				}
				
				stream.WriteLine();
			}
		}
		
		public static Sprite Input(TextReader stream)
		{
			string name=stream.ReadLine();
			uint width=Convert.ToUInt32(stream.ReadLine());
			uint height=Convert.ToUInt32(stream.ReadLine());
			string[] pal=stream.ReadLine().Split(new Char[1] {','});
			
			Sprite s=new Sprite(name, width, height, Convert.ToUInt32(pal[0]));
			
			for(int f=0;f<s.m_pal.Length;f++)
			{
				s.m_pal[f]=Convert.ToUInt32(pal[f+1]);
			}
			
			for(uint y=0;y<height;y++)
			{
				string[] data=stream.ReadLine().Split(new Char[1] {','});
				
				for(uint x=0;x<width;x++)
				{
					s[x,y]=Convert.ToUInt32(data[x]);
				}
			}
			
			return s;
		}
		
		// ------------------------------------------------
		// PRIVATE
		//
		private string		m_name;
		private uint		m_width;
		private uint		m_height;
		private uint[,]		m_data;
		private uint[]		m_pal;
		
		private int Mod(int v, uint max)
		{
			while(v<0)
				v+=(int)max;
			
			v=v%(int)max;
			
			return v;
		}
	}


	/// <summary>
	/// Describes a list of sprites
	/// </summary>
	public class SpriteList : IEnumerable
	{
		public SpriteList()
		{
			m_list=new ArrayList();
			m_changed=false;
		}
		
		public Sprite this [int i]
		{
			get {return (Sprite)m_list[i];}
			set
			{
				m_list[i]=value;
				m_changed=true;
			}
		}
		
		public void Add(Sprite s)
		{
			m_list.Add(s);
			m_changed=true;
		}
		
		public void Insert(int i, Sprite s)
		{
			m_list.Insert(i,s);
			m_changed=true;
		}
		
		public void RemoveAt(int i)
		{
			m_list.RemoveAt(i);
		}
		
		public int Count
		{
			get {return m_list.Count;}
		}
		
		public void Clear()
		{
			m_list.Clear();
		}
		
		public bool Changed
		{
			get {return m_changed;}
			set {m_changed=value;}
		}
		
		public IEnumerator GetEnumerator()
		{
			return m_list.GetEnumerator();
		}
		
		public void Output(TextWriter stream)
		{
			stream.WriteLine(m_list.Count);
			
			foreach (Sprite s in m_list)
			{
				s.Output(stream);
			}
		}
		
		public static SpriteList Input(TextReader stream)
		{
			SpriteList l=new SpriteList();
			int count=Convert.ToInt32(stream.ReadLine());
			
			for(int f=0;f<count;f++)
			{
				l.Add(Sprite.Input(stream));
			}
			
			return l;
		}
		
		// ------------------------------------------------
		// PRIVATE
		//
		private ArrayList	m_list;
		private bool		m_changed;
	}
}