Browse Source

Hook up preview image & create save button

The save button is currently non-functional.
tags/v0.1
Starbeamrainbowlabs 2 years ago
parent
commit
b4eab747a4

+ 40
- 1
SpritePacker-GUI/MainWindow.cs View File

@@ -6,6 +6,9 @@ using Gtk;
6 6
 using Ext.SilentorBit;
7 7
 using System.Drawing.Imaging;
8 8
 using System.Runtime.ExceptionServices;
9
+using System.Drawing;
10
+using System.IO;
11
+using System.Text;
9 12
 
10 13
 namespace SpritePacker.GUI
11 14
 {
@@ -19,6 +22,9 @@ namespace SpritePacker.GUI
19 22
 
20 23
 		private Button openImageButton;
21 24
 		private Button removeImageButton;
25
+		private Button saveResultButton;
26
+
27
+		private Gtk.Image previewImage;
22 28
 
23 29
 		private SpritePacker spritePacker = new SpritePacker();
24 30
 
@@ -62,6 +68,7 @@ namespace SpritePacker.GUI
62 68
 			VBox rightPanel = new VBox(false, 0) { MarginLeft = 5 };
63 69
 			Frame leftPanelFrame = new Frame("Sprites") { Child = leftPanel, Margin = 10, MarginRight = 5 };
64 70
 			Frame rightPanelFrame = new Frame("Preview") { Child = rightPanel, Margin = 10, MarginLeft = 5 };
71
+			previewImage = new Gtk.Image() { Margin = 10 };
65 72
 
66 73
 			// Create the file selector filter
67 74
 			spriteListSelectorFilter = new FileFilter() { Name = "Images" };
@@ -84,6 +91,7 @@ namespace SpritePacker.GUI
84 91
 
85 92
 			openImageButton = new Button("Add Sprites") { Margin = 10, MarginTop = 0, MarginBottom = 0 };
86 93
 			removeImageButton = new Button("Remove Selected Sprites") { Margin = 10, MarginTop = 0, MarginBottom = 0 };
94
+			saveResultButton = new Button("Save Result") { Margin = 10 };
87 95
 			openImageButton.Released += (object sender, EventArgs e) => {
88 96
 				ResponseType response = (ResponseType)spriteListSelector.Run();
89 97
 				spriteListSelector.Hide();
@@ -107,6 +115,11 @@ namespace SpritePacker.GUI
107 115
 				foreach(Sprite currentSprite in spritePacker.CurrentSprites)
108 116
 					spriteListDisplay.AddItem(currentSprite);
109 117
 			};
118
+			saveResultButton.Released += (object sender, EventArgs e) => {
119
+				Console.WriteLine("Save image button clicked");
120
+			};
121
+
122
+			updatePreviewImage();
110 123
 
111 124
 			// Populate the control button row
112 125
 			controlButtonRow.PackStart(openImageButton, true, false, 0);
@@ -116,6 +129,10 @@ namespace SpritePacker.GUI
116 129
 			leftPanel.PackStart(controlButtonRow, false, false, 5);
117 130
 			leftPanel.PackStart(spriteListDisplay, true, true, 0);
118 131
 
132
+			// Populate the right panel
133
+			rightPanel.PackStart(previewImage, true, false, 10);
134
+			rightPanel.PackStart(saveResultButton, false, false, 10);
135
+
119 136
 			// Pack the master container
120 137
 			masterContainer.PackStart(leftPanelFrame, true, true, 0);
121 138
 			masterContainer.PackStart(rightPanelFrame, true, true, 0);
@@ -124,7 +141,7 @@ namespace SpritePacker.GUI
124 141
 			Add(masterContainer);
125 142
 		}
126 143
 
127
-		void Action_AddSprite(string[] selectedFilenames)
144
+		private void Action_AddSprite(string[] selectedFilenames)
128 145
 		{
129 146
 			foreach (string filename in selectedFilenames)
130 147
 			{
@@ -132,6 +149,28 @@ namespace SpritePacker.GUI
132 149
 				spritePacker.Add(nextSprite);
133 150
 				spriteListDisplay.AddItem(nextSprite);
134 151
 			}
152
+			updatePreviewImage();
153
+		}
154
+
155
+		private void updatePreviewImage()
156
+		{
157
+			// TODO: Add an option to call this method manually instead of automatically
158
+			spritePacker.Arrange();
159
+
160
+			using(Bitmap newPreviewImage = spritePacker.GenerateImage())
161
+			using(MemoryStream newPreviewImageData = new MemoryStream())
162
+			{
163
+				// If the new preview image is null, then we shouldn't bother continuing
164
+				if (newPreviewImage == null)
165
+					return;
166
+
167
+				Bitmap resizedPreviewImage = SBRLUtilities.ImageTools.ResizeImage(newPreviewImage, new Size(512, 512));
168
+
169
+				resizedPreviewImage.Save(newPreviewImageData, ImageFormat.Png);
170
+				previewImage.Pixbuf = new Gdk.Pixbuf(newPreviewImageData.ToArray());
171
+			}
172
+
173
+			Console.WriteLine("Updated preview image");
135 174
 		}
136 175
 	}
137 176
 }

+ 41
- 0
SpritePacker/SBRLUtilities/ResizeImage.cs View File

@@ -0,0 +1,41 @@
1
+using System;
2
+using System.Drawing;
3
+using System.Drawing.Drawing2D;
4
+
5
+namespace SpritePacker.SBRLUtilities
6
+{
7
+	public static partial class ImageTools
8
+	{
9
+		/// <summary>
10
+		/// Resizes a <c>Bitmap</c> such that it fits within the target dimensions.
11
+		/// </summary>
12
+		/// <param name="sourceImage">The image to resize.</param>
13
+		/// <param name="targetBox">The target dimensions.</param>
14
+		/// <returns>The resized image.</returns>
15
+		public static Bitmap ResizeImage(Bitmap sourceImage, Size targetBox)
16
+		{
17
+			float scaleFactor = Math.Min(
18
+				targetBox.Width / (float)sourceImage.Width,
19
+				targetBox.Height / (float)sourceImage.Height
20
+			);
21
+			Size thumbnailSize = new Size(
22
+				(int)(sourceImage.Width * scaleFactor),
23
+				(int)(sourceImage.Height * scaleFactor)
24
+			);
25
+
26
+			Bitmap resultImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height);
27
+
28
+			using (Graphics context = Graphics.FromImage(resultImage)) {
29
+				context.CompositingMode = CompositingMode.SourceCopy;
30
+				context.InterpolationMode = InterpolationMode.HighQualityBicubic;
31
+				context.DrawImage(sourceImage, new Rectangle(
32
+					Point.Empty,
33
+					thumbnailSize
34
+				));
35
+			}
36
+
37
+			return resultImage;
38
+		}
39
+	}
40
+}
41
+

+ 4
- 0
SpritePacker/SpritePacker.cs View File

@@ -197,6 +197,7 @@ namespace SpritePacker
197 197
 		/// <summary>
198 198
 		/// Generates an image that contains all the currently added sprites.
199 199
 		/// You probably want to call Arrage() before calling this method.
200
+		/// Returns <c>null</c> if you haven't added any sprites to the SpritePacker yet.
200 201
 		/// </summary>
201 202
 		/// <returns>The generated image.</returns>
202 203
 		public Bitmap GenerateImage()
@@ -211,6 +212,9 @@ namespace SpritePacker
211 212
 					imageSize.X = spr.Right;
212 213
 			}
213 214
 
215
+			if (imageSize.IsEmpty)
216
+				return null;
217
+
214 218
 			Bitmap finalImage = new Bitmap(imageSize.X, imageSize.Y, PixelFormat.Format32bppArgb);
215 219
 			finalImage.MakeTransparent();
216 220
 			using (Graphics context = Graphics.FromImage(finalImage))

+ 4
- 0
SpritePacker/SpritePacker.csproj View File

@@ -36,6 +36,10 @@
36 36
     <Compile Include="Sprite.cs" />
37 37
     <Compile Include="SpritePacker.cs" />
38 38
     <Compile Include="Utilities.cs" />
39
+    <Compile Include="SBRLUtilities\ResizeImage.cs" />
39 40
   </ItemGroup>
40 41
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
42
+  <ItemGroup>
43
+    <Folder Include="SBRLUtilities\" />
44
+  </ItemGroup>
41 45
 </Project>

Loading…
Cancel
Save