Add option to keep original sprite sizes. Close #107

- Enabled by default
- [GUI] Added "Keep original sprite sizes" option to the Export options
- [CLI] Added "--sprite-cropping" option to the Advanced section
This commit is contained in:
VaDiM
2026-03-14 20:21:42 +03:00
parent 61d9bc8f15
commit 79d192ab70
10 changed files with 142 additions and 24 deletions
+57 -8
View File
@@ -54,6 +54,8 @@ namespace AssetStudioGUI
#region SpriteControl
private SpriteMaskMode spriteMaskVisibleMode = SpriteMaskMode.Preview;
private bool spriteRenderWithOrigSize = true;
private bool spriteRednerWithBorder;
#endregion
#region TexControl
@@ -441,15 +443,24 @@ namespace AssetStudioGUI
break;
}
}
else if (lastSelectedItem?.Type == ClassIDType.Sprite && new SpriteConverter((Sprite)lastSelectedItem.Asset).m_AlphaTexture2D != null)
else if (lastSelectedItem?.Asset is Sprite m_Sprite)
{
var spriteConverter = new SpriteConverter(m_Sprite);
switch (e.KeyCode)
{
case Keys.A:
case Keys.R:
spriteRenderWithOrigSize = !spriteRenderWithOrigSize;
need = true;
break;
case Keys.B:
spriteRednerWithBorder = !spriteRednerWithBorder;
need = true;
break;
case Keys.A when spriteConverter.m_AlphaTexture2D != null:
spriteMaskVisibleMode = spriteMaskVisibleMode == SpriteMaskMode.Preview ? SpriteMaskMode.Off : SpriteMaskMode.Preview;
need = true;
break;
case Keys.M:
case Keys.M when spriteConverter.m_AlphaTexture2D != null:
spriteMaskVisibleMode = spriteMaskVisibleMode == SpriteMaskMode.MaskOnly ? SpriteMaskMode.Preview : SpriteMaskMode.MaskOnly;
need = true;
break;
@@ -1487,19 +1498,29 @@ namespace AssetStudioGUI
private void PreviewSprite(AssetItem assetItem, Sprite m_Sprite)
{
var converter = new SpriteConverter(m_Sprite);
var converter = new SpriteConverter(m_Sprite, spriteRenderWithOrigSize);
if (converter.TryConvertToImage(out var image, maskMode: spriteMaskVisibleMode))
{
var bitmap = new DirectBitmap(image);
image.Dispose();
assetItem.InfoText = $"Width: {bitmap.Width}\nHeight: {bitmap.Height}\n";
PreviewTexture(bitmap);
assetItem.InfoText =
$"Width: {bitmap.Width}\n" +
$"Height: {bitmap.Height}\n" +
$"Keep original size: {spriteRenderWithOrigSize}\n" +
$"Show border: {spriteRednerWithBorder}\n";
if (spriteRednerWithBorder)
PreviewTextureWithBorder(bitmap);
else
PreviewTexture(bitmap);
var statusStrip = "'Ctrl'+'R' - Switch cropping method | 'Ctrl'+'B' - Show sprite border";
if (converter.m_AlphaTexture2D != null)
{
assetItem.InfoText += $"Alpha Mask: {spriteMaskVisibleMode}\n";
StatusStripUpdate("'Ctrl'+'A' - Enable/Disable alpha mask usage. 'Ctrl'+'M' - Show alpha mask only.");
assetItem.InfoText += $"Alpha Mask: {(spriteMaskVisibleMode == SpriteMaskMode.Preview ? "On" : spriteMaskVisibleMode.ToString())}\n";
statusStrip += "\n'Ctrl'+'A' - Enable/Disable alpha mask usage | 'Ctrl'+'M' - Show alpha mask only";
}
StatusStripUpdate(statusStrip);
}
else
{
@@ -1520,6 +1541,34 @@ namespace AssetStudioGUI
previewPanel.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void PreviewTextureWithBorder(DirectBitmap bitmap)
{
previewPanel.Image.Dispose();
imageTexture?.Dispose();
imageTexture = null;
var borderSize = 1;
var newWidth = bitmap.Width + borderSize * 2;
var newHeight = bitmap.Height + borderSize * 2;
var newImage = new Bitmap(newWidth, newHeight);
using (var gfx = Graphics.FromImage(newImage))
{
using (var border = new Pen(System.Drawing.Color.Black, borderSize))
{
gfx.DrawRectangle(border, 0, 0, newWidth - borderSize, newHeight - borderSize);
}
gfx.DrawImage(bitmap.Bitmap, new Point(borderSize, borderSize));
bitmap.Dispose();
previewPanel.Image = newImage;
}
if (newWidth > previewPanel.Width || newHeight > previewPanel.Height)
previewPanel.SizeMode = PictureBoxSizeMode.Zoom;
else
previewPanel.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void PreviewText(string text)
{
textPreviewBox.Text = text;
+19 -2
View File
@@ -84,6 +84,7 @@
this.generalTabPage = new System.Windows.Forms.TabPage();
this.panel2 = new System.Windows.Forms.Panel();
this.imagesTabPage = new System.Windows.Forms.TabPage();
this.keepSpriteSizeCheckBox = new System.Windows.Forms.CheckBox();
this.fbxTabPage = new System.Windows.Forms.TabPage();
this.live2DTabPage = new System.Windows.Forms.TabPage();
this.Cancel = new System.Windows.Forms.Button();
@@ -222,9 +223,9 @@
this.exportSpriteWithAlphaMask.CheckState = System.Windows.Forms.CheckState.Checked;
this.exportSpriteWithAlphaMask.Location = new System.Drawing.Point(7, 70);
this.exportSpriteWithAlphaMask.Name = "exportSpriteWithAlphaMask";
this.exportSpriteWithAlphaMask.Size = new System.Drawing.Size(228, 19);
this.exportSpriteWithAlphaMask.Size = new System.Drawing.Size(278, 19);
this.exportSpriteWithAlphaMask.TabIndex = 2;
this.exportSpriteWithAlphaMask.Text = "Export sprites with alpha mask applied";
this.exportSpriteWithAlphaMask.Text = "Export sprites with alpha mask applied (if exists)";
this.exportSpriteWithAlphaMask.UseVisualStyleBackColor = true;
//
// openAfterExport
@@ -809,6 +810,7 @@
// imagesTabPage
//
this.imagesTabPage.BackColor = System.Drawing.SystemColors.Menu;
this.imagesTabPage.Controls.Add(this.keepSpriteSizeCheckBox);
this.imagesTabPage.Controls.Add(this.panel1);
this.imagesTabPage.Controls.Add(this.converttexture);
this.imagesTabPage.Controls.Add(this.exportSpriteWithAlphaMask);
@@ -818,6 +820,20 @@
this.imagesTabPage.TabIndex = 3;
this.imagesTabPage.Text = "Images";
//
// keepSpriteSizeCheckBox
//
this.keepSpriteSizeCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.keepSpriteSizeCheckBox.AutoSize = true;
this.keepSpriteSizeCheckBox.Checked = true;
this.keepSpriteSizeCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.keepSpriteSizeCheckBox.Location = new System.Drawing.Point(7, 95);
this.keepSpriteSizeCheckBox.Name = "keepSpriteSizeCheckBox";
this.keepSpriteSizeCheckBox.Size = new System.Drawing.Size(154, 19);
this.keepSpriteSizeCheckBox.TabIndex = 3;
this.keepSpriteSizeCheckBox.Text = "Keep original sprite sizes";
this.keepSpriteSizeCheckBox.UseVisualStyleBackColor = true;
//
// fbxTabPage
//
this.fbxTabPage.BackColor = System.Drawing.SystemColors.Menu;
@@ -981,5 +997,6 @@
private System.Windows.Forms.TabPage imagesTabPage;
private System.Windows.Forms.Button Cancel;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.CheckBox keepSpriteSizeCheckBox;
}
}
+2
View File
@@ -19,6 +19,7 @@ namespace AssetStudioGUI
restoreExtensionName.Checked = Properties.Settings.Default.restoreExtensionName;
converttexture.Checked = Properties.Settings.Default.convertTexture;
exportSpriteWithAlphaMask.Checked = Properties.Settings.Default.exportSpriteWithMask;
keepSpriteSizeCheckBox.Checked = Properties.Settings.Default.keepSpriteSize;
convertAudio.Checked = Properties.Settings.Default.convertAudio;
var defaultImageType = Properties.Settings.Default.convertType.ToString();
((RadioButton)panel1.Controls.Cast<Control>().First(x => x.Text == defaultImageType)).Checked = true;
@@ -48,6 +49,7 @@ namespace AssetStudioGUI
Properties.Settings.Default.restoreExtensionName = restoreExtensionName.Checked;
Properties.Settings.Default.convertTexture = converttexture.Checked;
Properties.Settings.Default.exportSpriteWithMask = exportSpriteWithAlphaMask.Checked;
Properties.Settings.Default.keepSpriteSize = keepSpriteSizeCheckBox.Checked;
Properties.Settings.Default.convertAudio = convertAudio.Checked;
var checkedImageType = (RadioButton)panel1.Controls.Cast<Control>().First(x => ((RadioButton)x).Checked);
Properties.Settings.Default.convertType = (ImageFormat)Enum.Parse(typeof(ImageFormat), checkedImageType.Text);
+1 -1
View File
@@ -76,7 +76,7 @@ namespace AssetStudioGUI
{
debugLog = "";
var type = Properties.Settings.Default.convertType;
var converter = new SpriteConverter((Sprite)item.Asset);
var converter = new SpriteConverter((Sprite)item.Asset, Properties.Settings.Default.keepSpriteSize);
if (Properties.Settings.Default.filenameFormat == 3) //asset name@hash
item.Hash = converter.GetSpriteHash();
if (!TryExportFile(exportPath, item, $".{type}", out var exportFullPath))
+12
View File
@@ -382,5 +382,17 @@ namespace AssetStudioGUI.Properties {
this["cachePath"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool keepSpriteSize {
get {
return ((bool)(this["keepSpriteSize"]));
}
set {
this["keepSpriteSize"] = value;
}
}
}
}
@@ -92,5 +92,8 @@
<Setting Name="cachePath" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="keepSpriteSize" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>