Skip to content

Commit 738cbe4

Browse files
committed
functions for scaling textures
1 parent 6fe3640 commit 738cbe4

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

DolphinDynamicInputTexture/Data/DynamicInputPack.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public string GameID
121121
/// <param name="location">export directory</param>
122122
public void ExportToLocation(string location)
123123
{
124-
WriteJson(Path.Combine(location, GeneratedJsonName + ".json"));
125124
WriteImages(location);
125+
WriteJson(Path.Combine(location, GeneratedJsonName + ".json"));
126126
WriteGameID(location);
127127
}
128128

@@ -153,6 +153,7 @@ private void WriteImages(string location)
153153
{
154154
//exports the images for the output_textures
155155
WriteImage(location, texture);
156+
156157
foreach (HostDevice device in texture.HostDevices)
157158
{
158159
foreach (HostKey key in device.HostKeys)
@@ -164,7 +165,7 @@ private void WriteImages(string location)
164165
}
165166
}
166167

167-
private void WriteImage(string location, Interfaces.IImage image)
168+
private void WriteImage(string location, IImage image)
168169
{
169170
// Unlikely that we get here but skip textures that don't exist
170171
if (!File.Exists(image.TexturePath))
@@ -174,13 +175,29 @@ private void WriteImage(string location, Interfaces.IImage image)
174175
image.RelativeTexturePath = CheckRelativeTexturePath(image);
175176
string output_location = Path.Combine(location, image.RelativeTexturePath);
176177

178+
//create the directory when it does not exist.
179+
Directory.CreateDirectory(Path.GetDirectoryName(output_location));
180+
181+
//Use External Scaling if necessary.
182+
if (image is DynamicInputTexture texture)
183+
{
184+
if (DynamicInputTextureEvents.DynamicInputTextureExportProcessor != null)
185+
{
186+
DynamicInputTextureEvents.DynamicInputTextureExportProcessor.Invoke(output_location, texture);
187+
if (File.Exists(output_location))
188+
{
189+
texture.TexturePath = output_location;
190+
return;
191+
}
192+
}
193+
}
194+
177195
// Prevents the file from trying to overwrite itself.
178196
if (Path.GetFullPath(output_location) == Path.GetFullPath(image.TexturePath))
179197
return;
180198

181199
//write the image
182200
const bool overwrite = true;
183-
Directory.CreateDirectory(Path.GetDirectoryName(output_location));
184201
File.Copy(image.TexturePath, output_location, overwrite);
185202
}
186203

DolphinDynamicInputTexture/Data/DynamicInputTexture.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DolphinDynamicInputTexture.Interfaces;
22
using Newtonsoft.Json;
3+
using System;
34
using System.Collections.ObjectModel;
45
using System.Collections.Specialized;
56
using System.Drawing;
@@ -191,14 +192,40 @@ public double ImageHeightScaling
191192

192193
#region Update
193194

195+
/// <summary>
196+
/// reads the image size and adjusts the regions if necessary.
197+
/// </summary>
194198
private void UpdateImageWidthHeight()
195199
{
196200
if (File.Exists(_texture_path))
197201
{
198202
using (var bmp = new Bitmap(_texture_path))
199203
{
200-
ImageHeight = bmp.Height;
201-
ImageWidth = bmp.Width;
204+
if (ImageHeight > 0 && ImageWidth > 0)
205+
{
206+
double width_scale = (double)bmp.Width / ImageWidth;
207+
double height_scale = (double)bmp.Height / ImageHeight;
208+
209+
//When scaling up, the scale must be set directly.
210+
if (width_scale >= 1) ImageWidth = bmp.Width;
211+
if (height_scale >= 1) ImageHeight = bmp.Height;
212+
213+
foreach (InputRegion region in Regions)
214+
{
215+
region.RegionRect.X *= width_scale;
216+
region.RegionRect.Width *= width_scale;
217+
region.RegionRect.Y *= height_scale;
218+
region.RegionRect.Height *= height_scale;
219+
}
220+
221+
ImageWidth = bmp.Width;
222+
ImageHeight = bmp.Height;
223+
}
224+
else
225+
{
226+
ImageHeight = bmp.Height;
227+
ImageWidth = bmp.Width;
228+
}
202229
}
203230
}
204231
}

DolphinDynamicInputTexture/Data/DynamicInputTextureEvents.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,18 @@ public static class DynamicInputTextureEvents
1111

1212
public delegate bool ImageNotExistAction(Interfaces.IImage image, string details);
1313

14+
/// <summary>
15+
/// Allows editing the terxtures during exporting.
16+
/// </summary>
17+
/// <param name="savepath">Path where the new texture should be saved</param>
18+
/// <param name="dynamicinputtexture">texture that should be scaled</param>
19+
/// <returns></returns>
20+
public delegate bool ExternalScalingProcesses(string savepath, DynamicInputTexture dynamicinputtexture);
21+
22+
/// <summary>
23+
/// Allows editing the DynamicInputTextures during exporting.
24+
/// </summary>
25+
public static ExternalScalingProcesses DynamicInputTextureExportProcessor { get; set; }
26+
1427
}
1528
}

0 commit comments

Comments
 (0)