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
|
// SpriteEd - Simple sprite editor
// Copyright 2020 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 3 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, see <http://www.gnu.org/licenses/>.
//
using System;
using System.IO;
using AppKit;
using Foundation;
namespace SpriteEd
{
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private Sprite m_copy;
/// <summary>
/// Default constructor.
/// </summary>
public AppDelegate()
{
}
/// <summary>
/// Initialise the application/
/// </summary>
/// <param name="notification">The notification.</param>
public override void DidFinishLaunching(NSNotification notification)
{
}
/// <summary>
/// Tear-down the application.
/// </summary>
/// <param name="notification">The notification.</param>
public override void WillTerminate(NSNotification notification)
{
}
/// <summary>
/// Called to see if the app should terminate.
/// </summary>
/// <param name="sender">The sending application.</param>
/// <returns></returns>
public override NSApplicationTerminateReply ApplicationShouldTerminate(NSApplication sender)
{
NSApplicationTerminateReply reply = NSApplicationTerminateReply.Cancel;
bool changed = false;
foreach (NSWindow window in NSApplication.SharedApplication.DangerousWindows)
{
changed = changed || window.DocumentEdited;
}
if (changed)
{
NSAlert alert = new NSAlert ()
{
AlertStyle = NSAlertStyle.Critical,
InformativeText = "Save changes to document(s) before closing window?",
MessageText = "Save Document",
};
alert.AddButton ("Save");
alert.AddButton ("Delete");
alert.AddButton ("Cancel");
switch (alert.RunModal())
{
case 1000:
bool saved = true;
foreach (NSWindow window in NSApplication.SharedApplication.DangerousWindows)
{
if (window.DocumentEdited)
{
ViewController view = window.ContentViewController as ViewController;
if (view.Untitled)
{
saved = saved && view.SaveAs();
}
else
{
saved = saved && view.Save();
}
}
}
if (saved)
{
reply = NSApplicationTerminateReply.Now;
}
break;
case 1001:
reply = NSApplicationTerminateReply.Now;
break;
default:
break;
}
}
else
{
reply = NSApplicationTerminateReply.Now;
}
return reply;
}
/// <summary>
/// Get the current view controller.
/// </summary>
private ViewController ViewController
{
get
{
return NSApplication.SharedApplication.KeyWindow.ContentViewController as ViewController;
}
}
/// <summary>
/// Handle Save Document As.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("saveDocumentAs:")]
private void OnSaveDocumentAs(NSObject sender)
{
ViewController.SaveAs();
}
/// <summary>
/// Handle Save Document.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("saveDocument:")]
private void OnSaveDocument(NSObject sender)
{
ViewController view = ViewController;
if (view.Untitled)
{
view.SaveAs();
}
else
{
view.Save();
}
}
/// <summary>
/// Handle Open Document.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("openDocument:")]
private void OnOpenDocument(NSObject sender)
{
NSOpenPanel fsel = new NSOpenPanel();
fsel.Title = "Open Sprite Set";
fsel.AllowedFileTypes = new string[] {"sprset"};
if (fsel.RunModal() == 1)
{
try
{
FileStream stream = File.OpenRead(fsel.Url.Path);
SpriteSet set = SpriteSet.Load(stream);
stream.Close();
NSStoryboard storyboard = NSStoryboard.FromName("Main", null);
NSWindowController controller = storyboard.InstantiateInitialController() as NSWindowController;
controller.ShowWindow(this);
ViewController view = controller.ContentViewController as ViewController;
view.SpriteSet = set;
view.Url = fsel.Url;
}
catch (Exception e)
{
Util.DisplayAlert("Error loading sprite set", e.Message);
}
}
}
/// <summary>
/// Handle Revert Document To Saved.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("revertDocumentToSaved:")]
private void OnRevertDocumentToSaved(NSObject sender)
{
ViewController view = ViewController;
try
{
FileStream stream = File.OpenRead(view.Url.Path);
SpriteSet set = SpriteSet.Load(stream);
stream.Close();
view.SpriteSet = set;
}
catch (Exception e)
{
Util.DisplayAlert("Error reverting sprite set", e.Message);
}
}
/// <summary>
/// Handle Cut.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("cut:")]
private void OnCut(NSObject sender)
{
m_copy = ViewController.CutOrCopy(true);
}
/// <summary>
/// Handle Copy.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("copy:")]
private void OnCopy(NSObject sender)
{
m_copy = ViewController.CutOrCopy(false);
}
/// <summary>
/// Handle Paste.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("paste:")]
private void OnPaste(NSObject sender)
{
if (m_copy != null)
{
ViewController.Paste(m_copy);
}
}
/// <summary>
/// Is a sprite blank (all background colour).
/// </summary>
/// <param name="s">The sprite.</param>
/// <returns>True if the sprite is blank.</returns>
private bool IsSpriteBlank(Sprite s)
{
for(uint x = 0; x < s.Width; x++)
{
for(uint y = 0; y < s.Height; y++)
{
if (s[x, y] != 0)
{
return false;
}
}
}
return true;
}
/// <summary>
/// Convert a sprite set into a stream of bytes.
/// </summary>
/// <param name="write">The action to call with each byte.</param>
/// <param name="set">The sprite set.</param>
/// <param name="export_blank">Export blank sprites.</param>
private void StreamSpriteSet(Action<byte> write, SpriteSet set, bool export_blank)
{
int bits_per_pixel = 1;
switch(set.Palette.Size)
{
case 4:
bits_per_pixel = 2;
break;
case 16:
bits_per_pixel = 4;
break;
case 256:
bits_per_pixel = 8;
break;
}
for(uint f = 0; f < 256; f++)
{
Sprite sprite = set[(byte)f];
if (export_blank || !IsSpriteBlank(sprite))
{
for(uint y = 0; y < sprite.Height; y++)
{
switch(bits_per_pixel)
{
case 1:
for(uint x = 0; x < sprite.Width; x += 8)
{
uint bit = 128;
uint b = 0;
for(uint sx = 0; sx < 8; sx++)
{
if ((x + sx) < sprite.Width)
{
if (sprite[x + sx,y] != 0)
{
b |= bit;
}
}
bit /= 2;
}
write((byte)b);
}
break;
case 2:
for(uint x = 0; x < sprite.Width; x += 4)
{
uint b = sprite[x, y] << 6;
if ((x + 1) < sprite.Width)
{
b |= sprite[x + 1, y] << 4;
}
if ((x + 2) < sprite.Width)
{
b |= sprite[x + 2, y] << 2;
}
if ((x + 3) < sprite.Width)
{
b |= sprite[x + 3, y];
}
write((byte)b);
}
break;
case 4:
for(uint x = 0; x < sprite.Width; x += 2)
{
uint b = sprite[x, y] << 4;
if ((x + 1) < sprite.Width)
{
b |= sprite[x + 1, y];
}
write((byte)b);
}
break;
case 8:
for(uint x = 0; x < sprite.Width; x++)
{
write((byte)sprite[x,y]);
}
break;
}
}
}
}
}
/// <summary>
/// Handle export binary.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("exportBinary:")]
private void ExportBinary(NSObject sender)
{
if (NSApplication.SharedApplication.KeyWindow == null)
{
return;
}
NSSavePanel fsel = new NSSavePanel();
fsel.Title = "Export binary";
fsel.ShowsTagField = false;
if (fsel.RunModal() == 1)
{
try
{
ViewController view = ViewController;
bool export_blank = view.SpriteSet.Type == SpriteSet.SetType.C64CharacterSet ||
view.SpriteSet.Type == SpriteSet.SetType.MonoCharacterSet;
FileStream stream = File.Create(fsel.Url.Path);
StreamSpriteSet
(delegate (byte b)
{
stream.WriteByte(b);
},
view.SpriteSet, export_blank);
stream.Close();
}
catch (Exception e)
{
Util.DisplayAlert("Error exporting sprite set", e.Message);
}
}
}
/// <summary>
/// Handle export assembly.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("exportAssembly:")]
private void ExportAssembly(NSObject sender)
{
if (NSApplication.SharedApplication.KeyWindow == null)
{
return;
}
NSSavePanel fsel = new NSSavePanel();
fsel.Title = "Export assembly";
fsel.AllowedFileTypes = new string[] {"asm"};
fsel.ShowsTagField = false;
if (fsel.RunModal() == 1)
{
try
{
ViewController view = ViewController;
bool export_blank = view.SpriteSet.Type == SpriteSet.SetType.C64CharacterSet ||
view.SpriteSet.Type == SpriteSet.SetType.MonoCharacterSet;
StreamWriter stream = File.CreateText(fsel.Url.Path);
stream.Write("sprite_data:");
int col = 0;
StreamSpriteSet
(delegate (byte b)
{
if (col == 0)
{
stream.Write("\n\tbyte\t");
}
else
{
stream.Write(",");
}
stream.Write("{0}", b);
if (++col == 8)
{
col = 0;
}
},
view.SpriteSet, export_blank);
stream.Close();
}
catch (Exception e)
{
Util.DisplayAlert("Error exporting sprite set", e.Message);
}
}
}
/// <summary>
/// Handle export C.
/// </summary>
/// <param name="sender">The event sender.</param>
[Export("exportC:")]
private void ExportC(NSObject sender)
{
if (NSApplication.SharedApplication.KeyWindow == null)
{
return;
}
NSSavePanel fsel = new NSSavePanel();
fsel.Title = "Export C";
fsel.AllowedFileTypes = new string[] {"c"};
fsel.ShowsTagField = false;
if (fsel.RunModal() == 1)
{
try
{
ViewController view = ViewController;
bool export_blank = view.SpriteSet.Type == SpriteSet.SetType.C64CharacterSet ||
view.SpriteSet.Type == SpriteSet.SetType.MonoCharacterSet;
StreamWriter stream = File.CreateText(fsel.Url.Path);
stream.Write("unsigned char sprite_data[] =\n{");
int col = 0;
bool first_line = true;
StreamSpriteSet
(delegate (byte b)
{
if (col == 0)
{
if (first_line)
{
stream.Write("\n\t");
}
else
{
stream.Write(",\n\t");
}
first_line = false;
}
else
{
stream.Write(",");
}
stream.Write("{0}", b);
if (++col == 8)
{
col = 0;
}
},
view.SpriteSet, export_blank);
stream.Write("\n};");
stream.Close();
}
catch (Exception e)
{
Util.DisplayAlert("Error exporting sprite set", e.Message);
}
}
}
}
}
|