-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmSprite.cs
More file actions
1898 lines (1607 loc) · 61.6 KB
/
frmSprite.cs
File metadata and controls
1898 lines (1607 loc) · 61.6 KB
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using SpriteEditor.Models;
using SpriteEditor.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace SpriteEditor
{
[SupportedOSPlatform("windows")]
public partial class FrmSprite : Form
{
private static readonly Brush BackgroundBrush =
new HatchBrush(HatchStyle.LargeCheckerBoard, Color.LightGray, Color.White);
private static readonly Brush SourceRectBrush =
new SolidBrush(Color.FromArgb(150, Color.DarkGray));
private static readonly Brush SelectedRectBrush =
new SolidBrush(Color.FromArgb(150, Color.YellowGreen));
private SpriteLogic spriteLogic;
private Pose selectedPose;
private Frame selectedFrame;
private Pose copiedPose;
private Frame copiedFrame;
private readonly frmGridSize gridSizeForm = new();
private readonly frmOffset offsetForm = new();
private readonly frmAddFrames addFramesForm;
private string lastImageName;
private Bitmap lastBitmap;
private readonly float[] scales = [0.5f, 1.0f, 2.0f, 3.0f, 4.0f, 8.0f, 16.0f];
private float dpiX, dpiY;
private int scaleIndex = 1;
public FrmSprite()
{
addFramesForm = new frmAddFrames(this);
InitializeComponent();
pnlSprite.MouseWheel += pnlSprite_MouseWheel;
}
public void AddPoses(List<Pose> newPoses)
{
if (newPoses.Count == 0) return;
if (selectedPose != null)
{
foreach (var pose in newPoses)
{
if (selectedPose.BoundingCircle != null)
{
pose.BoundingCircle = new Circle(selectedPose.BoundingCircle);
}
pose.BoundingBox = new Rect(selectedPose.BoundingBox);
pose.DefaultDuration = selectedPose.DefaultDuration;
}
}
spriteLogic.SpriteData.Poses.AddRange(newPoses);
PopulateSprite(spriteLogic.SpriteData);
}
public void AddFrames(List<Frame> newFrames)
{
selectedPose.Frames.AddRange(newFrames);
PopulatePose(selectedPose);
changeSelectedFrame(selectedPose.Frames.Count - 1);
spriteLogic.Reset(Environment.TickCount);
}
public void ShowError(string message)
{
stlMessage.Text = message;
}
/// <inheritdoc />
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
components?.Dispose();
spriteLogic.Dispose();
}
base.Dispose(disposing);
}
private bool initializing = false;
private void frmSprite_Load(object sender, EventArgs e)
{
initializing = true;
SetViewChecked(Settings.Default.DisplayMode);
InitializeMenuItems();
Zoom(Settings.Default.ZoomLevel);
SelectGridSize();
PopulateRecentFiles();
cbDirection.SelectedIndex = 0;
var args = Environment.GetCommandLineArgs();
bool opened = false;
if (args.Length > 1 && File.Exists(args[1]))
{
opened = OpenSprite(args[1]);
}
if (!opened)
{
miNew_Click(sender, e);
}
initializing = false;
}
private void InitializeMenuItems()
{
miShowSrcRect.Checked = Settings.Default.ShowSrcRect;
miShowBoundingBox.Checked = Settings.Default.ShowBoundingBox;
miShowGrid.Checked = Settings.Default.ShowGrid;
miAutoReload.Checked = Settings.Default.AutoReloadImages;
miGridSelection.Checked = Settings.Default.UseGridSelection;
miTransparent.Checked = Settings.Default.UseTransparentColor;
}
private void PopulateRecentFiles()
{
miRecentFiles.DropDownItems.Clear();
foreach (var file in Settings.Default.RecentFiles)
{
if (string.IsNullOrWhiteSpace(file)) continue;
var miRecentFile = new ToolStripMenuItem(file, null, miRecentFile_Click);
miRecentFiles.DropDownItems.Add(miRecentFile);
}
}
private float Magnification => scales[scaleIndex];
private float ScalingX => dpiX / lastBitmap.HorizontalResolution * Magnification;
private float ScalingY => dpiY / lastBitmap.VerticalResolution * Magnification;
private int ScaledWidth => lastBitmap != null ? (int)(lastBitmap.Width * ScalingX) : 0;
private int ScaledHeight => lastBitmap != null ? (int)(lastBitmap.Height * ScalingY) : 0;
private void pnlSprite_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(pnlSprite.BackColor);
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
var currentPose = spriteLogic.CurrentPose;
var currentFrame = spriteLogic.CurrentFrame;
if (currentPose == null || (currentFrame == null && !miFull.Checked)) return;
if (spriteLogic.Image != lastImageName)
{
lastImageName = spriteLogic.Image;
lastBitmap = BitmapHelpers.OpenBitmap(spriteLogic, lastImageName);
fswUpdatedImageWatcher.Path = Path.GetDirectoryName(spriteLogic.ResolvePath(lastImageName));
fswUpdatedImageWatcher.Filter = Path.GetFileName(lastImageName);
}
if (lastBitmap == null) return;
// Default values (for full sprite view)
dpiX = e.Graphics.DpiX;
dpiY = e.Graphics.DpiY;
var midX = Math.Max(0, pnlSprite.Width / 2 - ScaledWidth / 2);
var midY = Math.Max(0, pnlSprite.Height / 2 - ScaledHeight / 2);
var source = new Rectangle(0, 0, lastBitmap.Width, lastBitmap.Height);
var dest = new Rectangle(midX, midY, ScaledWidth, ScaledHeight);
var transform = new Matrix();
// Transformed values (for animated and non-animated views)
if (miPreview.Checked || miAnimated.Checked)
{
if (miPreview.Checked && selectedFrame != null)
{
currentFrame = selectedFrame;
}
source = (Rectangle)currentFrame.Rectangle;
var magnifiedWidth = (int)(source.Width * ScalingX * currentFrame.Magnification.X);
var magnifiedHeight = (int)(source.Height * ScalingY * currentFrame.Magnification.Y);
midX = Math.Max(0, pnlSprite.Width / 2 - magnifiedWidth / 2);
midY = Math.Max(0, pnlSprite.Height / 2 - magnifiedHeight / 2);
dest = new Rectangle(midX, midY, magnifiedWidth, magnifiedHeight);
var origin = new Point(
(int)(midX + magnifiedWidth * currentPose.Origin.X),
(int)(midY + magnifiedHeight * currentPose.Origin.Y));
transform = e.Graphics.Transform;
transform.RotateAt(currentFrame.Angle, origin);
}
pnlSprite.AutoScrollMinSize = new Size(dest.Width, dest.Height);
e.Graphics.TranslateTransform(pnlSprite.AutoScrollPosition.X, pnlSprite.AutoScrollPosition.Y);
e.Graphics.MultiplyTransform(transform);
ImageAttributes attributes = GetImageAttributes(currentFrame);
// Draw the sprite
e.Graphics.FillRectangle(BackgroundBrush, dest);
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.DrawImage(
lastBitmap,
dest,
source.X,
source.Y,
source.Width,
source.Height,
GraphicsUnit.Pixel,
attributes);
// Draw the grid
var gridWidth = (int)(Settings.Default.GridWidth * ScalingX);
var gridHeight = (int)(Settings.Default.GridHeight * ScalingY);
if (Settings.Default.ShowGrid)
{
DrawGrid(e, dest, gridWidth, gridHeight);
}
// Draw bounding box in animated and non-animated views
var showBoundingBox = Settings.Default.ShowBoundingBox &&
!miFull.Checked && selectedPose != null;
if (showBoundingBox && selectedPose.BoundingCircle != null)
{
DrawBoundingCircle(e, midX, midY);
}
else if (showBoundingBox)
{
DrawBoundingBox(e, midX, midY, dest);
}
// Draw source rect and selected rect in full sprite view
if (miFull.Checked)
{
if (Settings.Default.ShowSrcRect && selectedFrame != null)
{
DrawSourceRect(e, midX, midY);
}
if (Settings.Default.UseGridSelection)
{
DrawSelectedRect(e, midX, midY, gridWidth, gridHeight);
}
}
e.Graphics.ResetTransform();
}
private ImageAttributes GetImageAttributes(Frame currentFrame)
{
// Set alpha
var attributes = new ImageAttributes();
if (currentFrame != null && !Utilities.CheckClose(currentFrame.Opacity, 1.0f, 0.01f))
{
var matrix = new ColorMatrix
{
Matrix33 = currentFrame.Opacity
};
attributes.SetColorMatrix(matrix);
}
if (Settings.Default.UseTransparentColor)
{
var transparentColorHex = spriteLogic.TransparentColorHex;
if (!string.IsNullOrEmpty(transparentColorHex))
{
var transparentColor = Utilities.FromHex(transparentColorHex);
attributes.SetColorKey(transparentColor, transparentColor);
}
}
return attributes;
}
private void DrawSelectedRect(PaintEventArgs e, int midX, int midY, int gridWidth, int gridHeight)
{
var panelCursorPos = pnlSprite.PointToClient(Cursor.Position);
var bitmapCursorPos = MouseToBitmapPosition(panelCursorPos);
if (MouseWithinBitmapBounds(bitmapCursorPos))
{
var selectRect = new Rectangle
{
X = gridWidth * (int)(ScalingX * bitmapCursorPos.X / gridWidth) + midX,
Y = gridHeight * (int)(ScalingY * bitmapCursorPos.Y / gridHeight) + midY,
Width = gridWidth,
Height = gridHeight
};
e.Graphics.FillRectangle(SelectedRectBrush, selectRect);
}
}
private void DrawSourceRect(PaintEventArgs e, int midX, int midY)
{
var currentRect = (Rectangle)selectedFrame.Rectangle;
currentRect.X = (int)(currentRect.X * ScalingX) + midX;
currentRect.Y = (int)(currentRect.Y * ScalingY) + midY;
currentRect.Width = (int)(currentRect.Width * ScalingX);
currentRect.Height = (int)(currentRect.Height * ScalingY);
e.Graphics.FillRectangle(SourceRectBrush, currentRect);
e.Graphics.DrawRectangle(new Pen(Color.Blue), currentRect);
}
private void DrawBoundingBox(PaintEventArgs e, int midX, int midY, Rectangle dest)
{
var box = (Rectangle)selectedPose.BoundingBox;
if (box.X == -1)
{
box.X = 0;
}
if (box.Y == -1)
{
box.Y = 0;
}
if (box.Width == -1)
{
box.Width = dest.Width;
}
else
{
box.Width = (int)(box.Width * ScalingX);
}
if (box.Height == -1)
{
box.Height = dest.Height;
}
else
{
box.Height = (int)(box.Height * ScalingY);
}
box.X = (int)(box.X * ScalingX) + midX;
box.Y = (int)(box.Y * ScalingY) + midY;
e.Graphics.DrawRectangle(new Pen(Color.Green), box);
}
private void DrawBoundingCircle(PaintEventArgs e, int midX, int midY)
{
var circle = selectedPose.BoundingCircle;
if (circle == null) return;
var box = (Rectangle)((Rect)circle);
box.X = (int)(box.X * ScalingX) + midX;
box.Y = (int)(box.Y * ScalingY) + midY;
box.Width = (int)(box.Width * ScalingX);
box.Height = (int)(box.Height * ScalingY);
e.Graphics.DrawEllipse(new Pen(Color.Green), box);
}
private static void DrawGrid(PaintEventArgs e, Rectangle dest, int gridWidth, int gridHeight)
{
var pen = new Pen(Settings.Default.GridColor);
for (int y = dest.Y; y <= dest.Y + dest.Height; y += gridHeight)
{
e.Graphics.DrawLine(pen, dest.X, y, dest.X + dest.Width, y);
}
for (int x = dest.X; x <= dest.X + dest.Width; x += gridWidth)
{
e.Graphics.DrawLine(pen, x, dest.Y, x, dest.Y + dest.Height);
}
}
private Point MouseToBitmapPosition(Point mousePos)
{
var midX = Math.Max(0, pnlSprite.Width / 2 - ScaledWidth / 2);
var midY = Math.Max(0, pnlSprite.Height / 2 - ScaledHeight / 2);
return new Point
{
X = (int)((mousePos.X - midX - pnlSprite.AutoScrollPosition.X) / ScalingX),
Y = (int)((mousePos.Y - midY - pnlSprite.AutoScrollPosition.Y) / ScalingY),
};
}
private bool MouseWithinBitmapBounds(Point bitmapCursorPos)
{
if (lastBitmap == null) return false;
var withinBoundsX = bitmapCursorPos.X >= 0 && bitmapCursorPos.X < lastBitmap.Width;
var withinBoundsY = bitmapCursorPos.Y >= 0 && bitmapCursorPos.Y < lastBitmap.Height;
return withinBoundsX && withinBoundsY;
}
private bool ValidateFrames(List<Frame> frames)
{
for (var i = 0; i < frames.Count; ++i)
{
var frame = frames[i];
if (frame.IsTweenFrame && (i == 0 || i == frames.Count - 1))
{
stlMessage.Text = $"Error: Tween frame {lstFrames.Items[i]}";
return false;
}
}
return true;
}
private void tmrUpdate_Tick(object sender, EventArgs e)
{
if (spriteLogic?.CurrentPose == null ||
!ValidateFrames(spriteLogic.CurrentPose.Frames))
{
return;
}
if (miAnimated.Checked)
{
spriteLogic.Update(Environment.TickCount);
}
else if (lstFrames.SelectedIndex != -1)
{
spriteLogic.SetFrame(lstFrames.SelectedIndex);
}
pnlSprite.Invalidate();
}
private void PopulateSprite(SpriteData spriteData)
{
if (spriteData == null) return;
txtBase.Text = spriteData.BaseDirectory;
txtImage.Text = spriteData.Image;
var transColor = cdTransparentColor.Color;
if (!string.IsNullOrEmpty(spriteData.TransparentColor))
{
transColor = Utilities.FromHex(spriteData.TransparentColor);
}
cdTransparentColor.Color = transColor;
btnTransColor.BackColor = transColor;
var poseNames = spriteData.Poses.Select(x => x.NameWithTags());
lstPoses.Items.Clear();
lstPoses.Items.AddRange(poseNames.ToArray());
cbDefaultPose.Items.Clear();
cbDefaultPose.Items.Add("");
cbDefaultPose.Items.AddRange(spriteData.GetPoseNames().ToArray<object>());
if (!string.IsNullOrEmpty(spriteData.DefaultPose))
{
cbDefaultPose.SelectedItem = spriteData.DefaultPose;
}
}
private void PopulatePose(Pose pose)
{
if (pose == null)
{
ClearPose();
return;
}
txtPoseName.Text = pose.GetName();
txtDuration.Text = pose.DefaultDuration.ToString();
txtRepeats.Text = pose.Repeats.ToString();
chkRequireCompletion.Enabled = pose.Repeats == -1;
chkRequireCompletion.Checked = pose.RequireCompletion;
txtCompletionFrame.Enabled = chkRequireCompletion.Enabled
&& chkRequireCompletion.Checked;
txtCompletionFrame.Text = string.Join(',', pose.CompletionFrames);
if (pose.BoundingCircle != null)
{
txtBoundingBox.Text = pose.BoundingCircle.ToString();
}
else
{
txtBoundingBox.Text = pose.BoundingBox.ToString();
}
txtOrigin.Text = pose.Origin.ToString();
if (pose.Tags.TryGetValue("Direction", out string direction))
{
var index = cbDirection.Items.IndexOf(direction);
cbDirection.SelectedIndex = index;
}
else
{
cbDirection.SelectedIndex = 0;
}
cbState.Text = pose.Tags.TryGetValue("State", out string state) ? state : "";
txtPoseImage.Text = pose.Image;
var transColor = cdTransparentColor.Color;
if (!string.IsNullOrEmpty(pose.TransparentColor))
{
transColor = Utilities.FromHex(pose.TransparentColor);
}
cdTransparentColor.Color = transColor;
btnPoseTransColor.BackColor = transColor;
var frameIndices = pose.Frames
.Select((x, i) => x.GetDisplayName(i + 1));
lstFrames.Items.Clear();
lstFrames.Items.AddRange(frameIndices.ToArray());
}
private void ClearPose()
{
txtPoseName.Text = "";
txtDuration.Text = "";
txtRepeats.Text = "";
chkRequireCompletion.Enabled = false;
chkRequireCompletion.Checked = false;
txtCompletionFrame.Enabled = false;
txtCompletionFrame.Text = "";
txtBoundingBox.Text = "";
txtOrigin.Text = "";
lstFrames.Items.Clear();
}
private static (int?, int?) StringToDuration(string durationString)
{
// Matches patterns like 30-50
const string pattern = @"^\s*(-?\d+)(\s*-\s*(\d+))?\s*$";
var match = Regex.Match(durationString, pattern);
if (!match.Success) return (null, null);
var duration = int.Parse(match.Groups[1].Value);
var maxDuration = string.IsNullOrEmpty(match.Groups[3].Value)
? (int?)null
: int.Parse(match.Groups[3].Value);
if (maxDuration < duration) maxDuration = null;
return new(
duration,
maxDuration
);
}
private static string DurationToString(Frame frame)
{
if (frame.Duration < 0 || !frame.MaxDuration.HasValue)
{
return frame.Duration.ToString();
}
return $"{frame.Duration}-{frame.MaxDuration}";
}
private bool populatingFrame = false;
private void PopulateFrame(Frame frame)
{
if (frame == null)
{
ClearFrame();
return;
}
populatingFrame = true;
txtFrameDuration.Text = DurationToString(frame);
txtFrameMarker.Text = frame.Marker ?? "";
txtRectangle.Text = frame.Rectangle.ToString();
chkTween.Checked = frame.IsTweenFrame;
txtSound.Text = frame.Sound.Filename;
txtPitch.Text = frame.Sound.Pitch.ToString();
txtPitch.Enabled = frame.Sound.Filename != null;
txtVolume.Text = frame.Sound.Volume.ToString();
txtVolume.Enabled = frame.Sound.Filename != null;
txtFrameImage.Text = frame.Image;
if (frame.IsTweenFrame)
{
txtMagnification.Text = "";
txtAngle.Text = "";
txtOpacity.Text = "";
}
else
{
txtMagnification.Text = frame.Magnification.ToString();
txtAngle.Text = frame.Angle.ToString();
txtOpacity.Text = frame.Opacity.ToString();
}
var transColor = cdTransparentColor.Color;
if (!string.IsNullOrEmpty(frame.TransparentColor))
{
transColor = Utilities.FromHex(frame.TransparentColor);
}
cdTransparentColor.Color = transColor;
btnFrameTransColor.BackColor = transColor;
populatingFrame = false;
}
private void ClearFrame()
{
txtFrameDuration.Text = "";
txtFrameMarker.Text = "";
txtMagnification.Text = "";
txtAngle.Text = "";
txtOpacity.Text = "";
txtRectangle.Text = "";
txtSound.Text = "";
txtPitch.Text = "";
txtPitch.Enabled = false;
txtVolume.Text = "";
txtVolume.Enabled = false;
txtFrameImage.Text = "";
chkTween.Checked = false;
}
private void ChangeSelectedPose(int poseIndex)
{
if (poseIndex == -1) return;
var poses = spriteLogic.SpriteData.Poses;
selectedPose = poses[poseIndex];
PopulatePose(selectedPose);
spriteLogic.SetPose(selectedPose.NameWithTags(), Environment.TickCount);
if (lstFrames.Items.Count > 0)
{
changeSelectedFrame(0);
}
else
{
PopulateFrame(null);
}
}
private void lstPoses_SelectedIndexChanged(object sender, EventArgs e)
{
ChangeSelectedPose(lstPoses.SelectedIndex);
}
private void lstPoses_MouseDown(object sender, MouseEventArgs e)
{
if (lstPoses.SelectedItem == null || e.Button != MouseButtons.Left) return;
lstFrames.DoDragDrop(lstPoses.SelectedIndex, DragDropEffects.Move);
if (lstPoses.SelectedIndex != -1)
{
ChangeSelectedPose(lstPoses.SelectedIndex);
}
}
static private int ListDragDrop<T>(ListBox listBox, List<T> list, DragEventArgs e)
{
var point = listBox.PointToClient(new Point(e.X, e.Y));
int toIndex = listBox.IndexFromPoint(point);
if (toIndex < 0)
{
toIndex = listBox.Items.Count - 1;
}
int fromIndex = (int)e.Data.GetData(typeof(int));
var data = listBox.Items[fromIndex].ToString();
listBox.Items.RemoveAt(fromIndex);
listBox.Items.Insert(toIndex, data);
var temp = list[fromIndex];
list.RemoveAt(fromIndex);
list.Insert(toIndex, temp);
return toIndex;
}
private void lstPoses_DragDrop(object sender, DragEventArgs e)
{
var toIndex = ListDragDrop(lstPoses, spriteLogic.SpriteData.Poses, e);
lstPoses.SelectedIndex = toIndex;
stlMessage.Text = "";
}
private void lstPoses_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void chkTween_CheckedChanged(object sender, EventArgs e)
{
txtMagnification.Enabled = !chkTween.Checked;
txtAngle.Enabled = !chkTween.Checked;
txtOpacity.Enabled = !chkTween.Checked;
if (populatingFrame) return;
stlMessage.Text = "";
if (chkTween.Checked)
{
txtMagnification.Text = "";
txtAngle.Text = "";
txtOpacity.Text = "";
}
else
{
txtMagnification.Text = "(1, 1)";
txtAngle.Text = "0";
txtOpacity.Text = "1";
}
if (selectedFrame == null) return;
selectedFrame.IsTweenFrame = chkTween.Checked;
selectedFrame.Magnification = new Vec2(1, 1);
selectedFrame.Angle = 0;
selectedFrame.Opacity = 1;
spriteLogic.Reset(Environment.TickCount);
}
private void lstFrames_MouseDown(object sender, MouseEventArgs e)
{
var valid = selectedPose != null
&& lstFrames.SelectedItem != null
&& e.Button == MouseButtons.Left;
if (!valid) return;
lstFrames.DoDragDrop(lstFrames.SelectedIndex, DragDropEffects.Move);
if (lstFrames.SelectedIndex != -1)
{
changeSelectedFrame(lstFrames.SelectedIndex);
PopulateFrame(selectedFrame);
}
}
private void lstFrames_DragDrop(object sender, DragEventArgs e)
{
var toIndex = ListDragDrop(lstFrames, selectedPose.Frames, e);
changeSelectedFrame(toIndex);
spriteLogic.Reset(Environment.TickCount);
stlMessage.Text = "";
}
private void lstFrames_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void btnBrowseImage_Click(object sender, EventArgs e)
{
var result = ofdImage.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
var fullFilename = ofdImage.FileName;
SetImage(fullFilename, "sprite");
}
private void SetImage(string path, string type)
{
var filename = Path.GetFileName(path);
var bmp = BitmapHelpers.OpenBitmap(spriteLogic, path);
if (bmp != null)
{
var baseDir = spriteLogic.ResolveBaseDir();
var image = path;
if (baseDir != "." && Path.IsPathRooted(path))
{
image = Utilities.MakeRelativePath(baseDir, path);
}
if (type == "frame")
{
txtFrameImage.Text = filename;
selectedFrame.Image = image;
}
else if (type == "pose")
{
txtPoseImage.Text = filename;
selectedPose.Image = image;
}
else
{
txtImage.Text = filename;
spriteLogic.SpriteData.Image = image;
}
stlMessage.Text = "";
}
else
{
stlMessage.Text = "Error: Invalid image";
}
}
private void txtPoseName_TextChanged(object sender, EventArgs e)
{
if (selectedPose == null || selectedPose.GetName() == txtPoseName.Text) return;
var newName = txtPoseName.Text;
if (newName == "")
{
selectedPose.Tags.Remove("Name");
}
else
{
selectedPose.Tags["Name"] = newName;
}
if (tbcSprite.SelectedTab == tabPose)
{
PopulateSprite(spriteLogic.SpriteData);
}
}
private void txtDuration_TextChanged(object sender, EventArgs e)
{
if (selectedPose == null) return;
if (int.TryParse(txtDuration.Text, out int duration))
{
selectedPose.DefaultDuration = duration;
spriteLogic.Reset(Environment.TickCount);
}
}
private void txtRepeats_TextChanged(object sender, EventArgs e)
{
if (selectedPose == null) return;
if (int.TryParse(txtRepeats.Text, out var repeats))
{
selectedPose.Repeats = repeats;
chkRequireCompletion.Enabled = repeats == -1;
txtCompletionFrame.Enabled = chkRequireCompletion.Enabled
&& chkRequireCompletion.Checked;
spriteLogic.Reset(Environment.TickCount);
}
}
private void txtFrameDuration_TextChanged(object sender, EventArgs e)
{
if (selectedFrame == null) return;
var (duration, maxDuration) = StringToDuration(txtFrameDuration.Text);
if (!duration.HasValue) return;
selectedFrame.Duration = duration.Value;
selectedFrame.MaxDuration = maxDuration;
spriteLogic.Reset(Environment.TickCount);
}
private void txtFrameMarker_TextChanged(object sender, EventArgs e)
{
if (selectedFrame == null || selectedFrame.Marker == txtFrameMarker.Text) return;
selectedFrame.Marker = txtFrameMarker.Text;
if (tbcSprite.SelectedTab == tabFrame)
{
var selectedIndex = lstFrames.SelectedIndex;
PopulatePose(selectedPose);
lstFrames.SelectedIndex = selectedIndex;
}
}
private void txtMagnification_TextChanged(object sender, EventArgs e)
{
if (selectedFrame == null) return;
var mag = Vec2.FromString(txtMagnification.Text);
if (mag != null)
{
selectedFrame.Magnification = mag;
spriteLogic.Reset(Environment.TickCount);
}
}
private void txtAngle_TextChanged(object sender, EventArgs e)
{
if (selectedFrame == null) return;
if (int.TryParse(txtAngle.Text, out var angle))
{
selectedFrame.Angle = angle;
spriteLogic.Reset(Environment.TickCount);
}
}
private void txtOpacity_TextChanged(object sender, EventArgs e)
{
if (selectedFrame == null) return;
if (float.TryParse(txtOpacity.Text, out var opacity))
{
selectedFrame.Opacity = opacity;
spriteLogic.Reset(Environment.TickCount);
}
}
private void miAdd_Click(object sender, EventArgs e)
{
if (spriteLogic == null) return;
var poses = spriteLogic.SpriteData.Poses;
var pose = new Pose
{
Tags =
{
["Name"] = "Unnamed Pose " + lstPoses.Items.Count
}
};
poses.Add(pose);
PopulateSprite(spriteLogic.SpriteData);
PopulateFrame(null);
lstPoses.SelectedIndex = lstPoses.Items.Count - 1;
}
private void miRemove_Click(object sender, EventArgs e)
{
if (spriteLogic == null || lstPoses.SelectedIndex == -1) return;
var poses = spriteLogic.SpriteData.Poses;
poses.RemoveAt(lstPoses.SelectedIndex);
PopulateSprite(spriteLogic.SpriteData);
if (lstPoses.Items.Count > 0)
{
lstPoses.SelectedIndex = 0;
}
else
{
ClearPose();
}
}
private void mnuPose_Opening(object sender, CancelEventArgs e)
{
miRemove.Enabled = lstPoses.SelectedIndex != -1;
miCopy.Enabled = miRemove.Enabled;
miPaste.Enabled = copiedPose != null;
}
private void changeSelectedFrame(int newFrameIndex)
{
if (selectedPose == null) return;
btnPrevFrame.Enabled = newFrameIndex > 0;
btnNextFrame.Enabled = newFrameIndex < selectedPose.Frames.Count - 1;
if (newFrameIndex < 0 || newFrameIndex >= selectedPose.Frames.Count) return;
selectedFrame = selectedPose.Frames[newFrameIndex];
PopulateFrame(selectedFrame);
lstFrames.SelectedIndex = newFrameIndex;
lblFrameNumber.Text = $"#{newFrameIndex + 1}";
}
private void btnPrevFrame_Click(object sender, EventArgs e)
{
changeSelectedFrame(lstFrames.SelectedIndex - 1);
spriteLogic.PlaySound(selectedFrame.Sound);
}
private void btnNextFrame_Click(object sender, EventArgs e)
{
changeSelectedFrame(lstFrames.SelectedIndex + 1);
spriteLogic.PlaySound(selectedFrame.Sound);
}
private void miAddFrame_Click(object sender, EventArgs e)
{
if (selectedPose == null) return;
var frames = selectedPose.Frames;
var rect = new Rect(0, 0, 0, 0);
var bmp = BitmapHelpers.OpenBitmap(spriteLogic, spriteLogic.Image);
if (bmp != null)
{
rect = new Rect(0, 0, bmp.Width, bmp.Height);
}
frames.Add(new Frame() { Rectangle = rect });
PopulatePose(selectedPose);
changeSelectedFrame(frames.Count - 1);
spriteLogic.Reset(Environment.TickCount);
}
private void miRemoveFrame_Click(object sender, EventArgs e)
{
if (selectedPose == null || lstFrames.SelectedIndex == -1) return;
var frames = selectedPose.Frames;
frames.RemoveAt(lstFrames.SelectedIndex);
PopulatePose(selectedPose);
if (lstFrames.Items.Count > 0)
{
changeSelectedFrame(0);
}
else
{
ClearFrame();
}
spriteLogic.Reset(Environment.TickCount);