Skip to content

Commit

Permalink
Merge pull request #993 from morganwillcock/import-use-selection
Browse files Browse the repository at this point in the history
Add sprite property 'ImportAsTile'
  • Loading branch information
ivan-mogilko authored Nov 26, 2019
2 parents b9a7be4 + 9b5a200 commit e1ab6bf
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Editor/AGS.Editor/AGSEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class AGSEditor : ISourceControlIntegration
* 21: 3.5.0.15 - AudioClip ID.
* 22: 3.5.0.18 - Settings.ScaleMovementSpeedWithMaskResolution.
*/
public const int LATEST_XML_VERSION_INDEX = 23;
public const int LATEST_XML_VERSION_INDEX = 24;
/*
* LATEST_USER_DATA_VERSION is the last version of the user data file that used a
* 4-point-4-number string to identify the version of AGS that saved the file.
Expand Down
4 changes: 2 additions & 2 deletions Editor/AGS.Editor/Panes/SpriteImportWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private void DoReplace()
try
{
SpriteTools.ReplaceSprite(replace, image, UseAlphaChannel, RemapToGamePalette,
UseBackgroundSlots, SpriteImportMethod, spritesheet, 0, filename);
UseBackgroundSlots, SpriteImportMethod, filename, 0, spritesheet);
}
catch (AGSEditorException ex)
{
Expand Down Expand Up @@ -435,7 +435,7 @@ private bool DoImport()
{
// in the interest of speed, import the existing bitmap if the file has a single frame
SpriteTools.ImportNewSprites(folder, image, UseAlphaChannel, RemapToGamePalette,
UseBackgroundSlots, SpriteImportMethod, spritesheet, 0, filename);
UseBackgroundSlots, SpriteImportMethod, filename, 0, spritesheet);
}
else
{
Expand Down
11 changes: 10 additions & 1 deletion Editor/AGS.Editor/Panes/SpriteSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,16 @@ private void ReplaceSpritesFromSource()

try
{
SpriteSheet spritesheet = new SpriteSheet(new Point(spr.OffsetX, spr.OffsetY), new Size(spr.ImportWidth, spr.ImportHeight));
SpriteSheet spritesheet;

if (spr.ImportAsTile)
{
spritesheet = new SpriteSheet(new Point(spr.OffsetX, spr.OffsetY), new Size(spr.ImportWidth, spr.ImportHeight));
}
else
{
spritesheet = null;
}

// take the alpha channel preference from the specified import option
// (instead of using whether the old sprite has an alpha channel)
Expand Down
50 changes: 50 additions & 0 deletions Editor/AGS.Editor/Tasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,56 @@ private void SetDefaultValuesForNewFeatures(Game game)
}
}

if (xmlVersionIndex < 24)
{
// get all known source images and their largest known size
// (avoiding System.Drawing / GDI as a dependency to load the project)
Dictionary<string, Tuple<int, int>> sourceMaxSize = new Dictionary<string, Tuple<int, int>>(StringComparer.OrdinalIgnoreCase);

foreach (Sprite sprite in game.RootSpriteFolder.GetAllSpritesFromAllSubFolders())
{
if (!string.IsNullOrWhiteSpace(sprite.SourceFile))
{
int currentX = sprite.OffsetX + sprite.ImportWidth;
int currentY = sprite.OffsetY + sprite.ImportHeight;

if (sourceMaxSize.ContainsKey(sprite.SourceFile))
{
int maxX = sourceMaxSize[sprite.SourceFile].Item1;
int maxY = sourceMaxSize[sprite.SourceFile].Item2;
if (maxX < currentX) maxX = currentX;
if (maxY < currentY) maxY = currentY;
sourceMaxSize[sprite.SourceFile] = Tuple.Create(maxX, maxY);
}
else
{
sourceMaxSize.Add(sprite.SourceFile, Tuple.Create(currentX, currentY));
}
}
}

// Set the tiled image flag for existing imports - the only misdetection would be
// a single import from a source image that starts at 0,0, but wasn't for the
// entire image
foreach (Sprite sprite in game.RootSpriteFolder.GetAllSpritesFromAllSubFolders())
{
if (sprite.OffsetX > 0 || sprite.OffsetY > 0)
{
sprite.ImportAsTile = true;
}
else if (sourceMaxSize.ContainsKey(sprite.SourceFile))
{
int maxX = sourceMaxSize[sprite.SourceFile].Item1;
int maxY = sourceMaxSize[sprite.SourceFile].Item2;
sprite.ImportAsTile = sprite.ImportWidth < maxX || sprite.ImportHeight < maxY;
}
else
{
sprite.ImportAsTile = false;
}
}
}

System.Version editorVersion = new System.Version(AGS.Types.Version.AGS_EDITOR_VERSION);
System.Version projectVersion = game.SavedXmlEditorVersion != null ? Types.Utilities.TryParseVersion(game.SavedXmlEditorVersion) : null;
if (projectVersion < editorVersion)
Expand Down
51 changes: 29 additions & 22 deletions Editor/AGS.Editor/Utils/SpriteTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private static void FindSpriteUsageInViews(int spriteNumber, StringBuilder repor
}

public static void ReplaceSprite(Sprite sprite, Bitmap bmp, bool alpha, bool remapColours, bool useRoomBackground,
SpriteImportTransparency transparency, string filename = "", int frame = 0, int offsetX = 0, int offsetY = 0)
SpriteImportTransparency transparency, string filename, int frame, Rectangle selection, bool tile)
{
// ignore alpha channel if not 32 bit ARGB
bool useAlphaChannel = bmp.PixelFormat != PixelFormat.Format32bppArgb ||
Expand All @@ -221,25 +221,28 @@ public static void ReplaceSprite(Sprite sprite, Bitmap bmp, bool alpha, bool rem
sprite.RemapToRoomPalette = useRoomBackground;
sprite.SourceFile = Utilities.GetRelativeToProjectPath(filename);
sprite.Frame = frame;
sprite.OffsetX = offsetX;
sprite.OffsetY = offsetY;
sprite.ImportWidth = sprite.Width;
sprite.ImportHeight = sprite.Height;
sprite.OffsetX = selection.Left;
sprite.OffsetY = selection.Top;
sprite.ImportWidth = selection.Width;
sprite.ImportHeight = selection.Height;
sprite.ImportAlphaChannel = alpha;
sprite.ImportAsTile = tile;
}

public static void ReplaceSprite(Sprite sprite, Bitmap bmp, bool alpha, bool remapColours, bool useRoomBackground,
SpriteImportTransparency transparency, SpriteSheet spritesheet = null, int frame = 0, string filename = "")
SpriteImportTransparency transparency, string filename, int frame, SpriteSheet spritesheet)
{
if (spritesheet != null)
bool tiled = spritesheet != null;

if (tiled)
{
Rectangle selection = spritesheet.GetFirstSpriteSelection(new Size(bmp.Width, bmp.Height));

if (!selection.IsEmpty)
{
Bitmap replacement = bmp.Clone(selection, bmp.PixelFormat);
ReplaceSprite(sprite, replacement, alpha, remapColours, useRoomBackground, transparency, filename,
frame, selection.Left, selection.Top);
frame, selection, tiled);
replacement.Dispose();
}
else
Expand All @@ -250,20 +253,21 @@ public static void ReplaceSprite(Sprite sprite, Bitmap bmp, bool alpha, bool rem
}
else
{
ReplaceSprite(sprite, bmp, alpha, remapColours, useRoomBackground, transparency, filename, frame);
Rectangle selection = new Rectangle(0, 0, bmp.Width, bmp.Height);
ReplaceSprite(sprite, bmp, alpha, remapColours, useRoomBackground, transparency, filename, frame, selection, tiled);
}
}

public static void ReplaceSprite(Sprite sprite, string filename, int frame, bool alpha, bool remapColours, bool useRoomBackground,
SpriteImportTransparency transparency, SpriteSheet spritesheet = null)
SpriteImportTransparency transparency, SpriteSheet spritesheet)
{
Bitmap bmp = LoadFrameImageFromFile(filename, frame);
ReplaceSprite(sprite, bmp, alpha, remapColours, useRoomBackground, transparency, spritesheet, frame, filename);
ReplaceSprite(sprite, bmp, alpha, remapColours, useRoomBackground, transparency, filename, frame, spritesheet);
bmp.Dispose();
}

public static void ImportNewSprite(SpriteFolder folder, Bitmap bmp, bool alpha, bool remapColours, bool useRoomBackground,
SpriteImportTransparency transparency, string filename = "", int frame = 0, int offsetX = 0, int offsetY = 0)
SpriteImportTransparency transparency, string filename, int frame, Rectangle selection, bool tile)
{
// ignore alpha channel if not 32 bit ARGB
bool useAlphaChannel = bmp.PixelFormat != PixelFormat.Format32bppArgb ||
Expand All @@ -284,31 +288,34 @@ public static void ImportNewSprite(SpriteFolder folder, Bitmap bmp, bool alpha,
sprite.RemapToRoomPalette = useRoomBackground;
sprite.SourceFile = Utilities.GetRelativeToProjectPath(filename);
sprite.Frame = frame;
sprite.OffsetX = offsetX;
sprite.OffsetY = offsetY;
sprite.ImportWidth = sprite.Width;
sprite.ImportHeight = sprite.Height;
sprite.OffsetX = selection.Left;
sprite.OffsetY = selection.Top;
sprite.ImportWidth = selection.Width;
sprite.ImportHeight = selection.Height;
sprite.ImportAlphaChannel = alpha;
sprite.ImportAsTile = tile;

folder.Sprites.Add(sprite);
}

public static void ImportNewSprites(SpriteFolder folder, Bitmap bmp, bool alpha, bool remapColours, bool useRoomBackground,
SpriteImportTransparency transparency, SpriteSheet spritesheet = null, int frame = 0, string filename = "")
SpriteImportTransparency transparency, string filename, int frame, SpriteSheet spritesheet)
{
if (spritesheet != null)
bool tiled = spritesheet != null;

if (tiled)
{
foreach (Rectangle selection in spritesheet.GetSpriteSelections(new Size(bmp.Width, bmp.Height)))
{
Bitmap import = bmp.Clone(selection, bmp.PixelFormat);
ImportNewSprite(folder, import, alpha, remapColours, useRoomBackground, transparency,
filename, frame, selection.Left, selection.Top);
ImportNewSprite(folder, import, alpha, remapColours, useRoomBackground, transparency, filename, frame, selection, tiled);
import.Dispose();
}
}
else
{
ImportNewSprite(folder, bmp, alpha, remapColours, useRoomBackground, transparency, filename, frame);
Rectangle selection = new Rectangle(0, 0, bmp.Width, bmp.Height);
ImportNewSprite(folder, bmp, alpha, remapColours, useRoomBackground, transparency, filename, frame, selection, tiled);
}
}

Expand All @@ -322,7 +329,7 @@ public static void ImportNewSprites(SpriteFolder folder, string filename, bool a
foreach (Bitmap bmp in LoadSpritesFromFile(filename))
{
progress.SetProgressValue(frame);
ImportNewSprites(folder, bmp, alpha, remapColours, useRoomBackground, transparency, spritesheet, frame, filename);
ImportNewSprites(folder, bmp, alpha, remapColours, useRoomBackground, transparency, filename, frame, spritesheet);
bmp.Dispose();
frame ++;
}
Expand Down
Loading

0 comments on commit e1ab6bf

Please sign in to comment.