From 81935c1050ef00654e4a3c43d32b833e7b3c5aba Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 3 Aug 2018 20:43:05 +0100 Subject: [PATCH] Add preset option, but it's not working just right yet. --- .gitignore | 339 +++++++++++++++++++++++ SpritePacker-CLI/Program.cs | 49 +++- SpritePacker-CLI/Resources/HelpText.txt | 1 + SpritePacker-CLI/SpritePacker-CLI.csproj | 3 + SpritePacker/Sprite.cs | 2 + SpritePacker/SpritePacker.cs | 14 +- 6 files changed, 396 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 0eb3cb4..67c541c 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,342 @@ _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML + +# Created by https://www.gitignore.io/api/visualstudio + +### VisualStudio ### +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + + +# End of https://www.gitignore.io/api/visualstudio diff --git a/SpritePacker-CLI/Program.cs b/SpritePacker-CLI/Program.cs index a7d6f70..9e2837d 100644 --- a/SpritePacker-CLI/Program.cs +++ b/SpritePacker-CLI/Program.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text.RegularExpressions; +using System.Linq; +using System.Drawing; namespace SpritePacker { @@ -32,6 +35,7 @@ namespace SpritePacker private static OutputFormat outputFormat = OutputFormat.Text; private static string outputFilename = "spritesheet.png"; private static List values = new List(); + private static string rawSourceLocations = null; public static bool Verbose = false; @@ -53,12 +57,14 @@ namespace SpritePacker case "--verbose": Verbose = true; break; + case "--preset": + rawSourceLocations = File.ReadAllText(args[++i]); + break; case "--csv": outputFormat = OutputFormat.CSV; break; default: - if(args[i].StartsWith("-")) - { + if(args[i].StartsWith("-")) { Console.Error.WriteLine("Error: Unrecognised flag '{0}'.", args[i]); return 1; } @@ -104,7 +110,25 @@ namespace SpritePacker public static void RunNormal() { spritePacker = new SpritePacker(Verbose); - addSprites(values); + List sprites = new List(); + addSprites(values, sprites); + + // Preset some locations if they were specified + if (rawSourceLocations != null) { + foreach (string[] line in parseCSV(rawSourceLocations).Skip(1)) { // Skip the header + // index,filename,x,y,width,height + Sprite currentSprite = sprites.Find((Sprite item) => item.Filename == line[1]); + currentSprite.Area = new Rectangle( + int.Parse(line[2]), + int.Parse(line[3]), + int.Parse(line[4]), + int.Parse(line[5]) + ); + currentSprite.Placed = true; + } + } + spritePacker.Add(sprites); + spritePacker.Arrange(); spritePacker.Output(outputFilename); switch(outputFormat) @@ -121,19 +145,17 @@ namespace SpritePacker } } - private static void addSprites(List filenames) + private static void addSprites(List filenames, List outputSpriteList) { - foreach(string filename in values) + foreach(string filename in filenames) { // Make sure that the file actually exists (we don't want the packer to throw a tantrum) if (File.Exists(filename)) { - try - { - spritePacker.Add(new Sprite(filename)); + try { + outputSpriteList.Add(new Sprite(filename)); } - catch(Exception error) - { + catch(Exception error) { Console.Error.WriteLine("Error reading {0}: {1}", filename, error.Message); } @@ -141,7 +163,7 @@ namespace SpritePacker { // This filename is a directory - recurse over all the files in the directory string[] dirFiles = Directory.GetFiles(filename); - addSprites(new List(dirFiles)); + addSprites(new List(dirFiles), outputSpriteList); } } else @@ -151,5 +173,10 @@ namespace SpritePacker } } + + private static IEnumerable parseCSV(string source) + { + return Regex.Split(source.Trim(), @"\r?\n").Select((string line) => line.Split(',')); + } } } diff --git a/SpritePacker-CLI/Resources/HelpText.txt b/SpritePacker-CLI/Resources/HelpText.txt index b1b9bf4..cae1b45 100644 --- a/SpritePacker-CLI/Resources/HelpText.txt +++ b/SpritePacker-CLI/Resources/HelpText.txt @@ -11,5 +11,6 @@ Option Meaning --help Shows this help message. --version Shows the version information. --csv Outputs the final sprite positions in CSV. +--preset Specifies a csv file of previous location information to use when packing. This will ensure that sprites previously packed don't change their location. More information can be found at https://git.starbeamrainbowlabs.com/sbrl/SpritePacker diff --git a/SpritePacker-CLI/SpritePacker-CLI.csproj b/SpritePacker-CLI/SpritePacker-CLI.csproj index 9a293c9..a6990f3 100644 --- a/SpritePacker-CLI/SpritePacker-CLI.csproj +++ b/SpritePacker-CLI/SpritePacker-CLI.csproj @@ -35,6 +35,9 @@ + + ..\..\..\..\..\..\..\usr\lib\mono\4.6.2-api\System.Drawing.dll + diff --git a/SpritePacker/Sprite.cs b/SpritePacker/Sprite.cs index 5340699..bbd55ee 100644 --- a/SpritePacker/Sprite.cs +++ b/SpritePacker/Sprite.cs @@ -11,6 +11,8 @@ namespace SpritePacker private string filename; private Image image; + public bool Placed { get; set; } = false; + public Rectangle Area { get { return area; } diff --git a/SpritePacker/SpritePacker.cs b/SpritePacker/SpritePacker.cs index 9ae3da6..3002151 100644 --- a/SpritePacker/SpritePacker.cs +++ b/SpritePacker/SpritePacker.cs @@ -51,6 +51,11 @@ namespace SpritePacker if(Verbose) Console.WriteLine("Adding {0}.", sprite); sprites.Add(sprite); } + public void Add(IEnumerable sprites) + { + foreach (Sprite sprite in sprites) + Add(sprite); + } /// /// Adds a sprite to the sprite packer. /// @@ -78,6 +83,11 @@ namespace SpritePacker List arrangedSprites = new List(); foreach(Sprite cspr in sprites) { + if (cspr.Placed) { + if(Verbose) Console.WriteLine($"Skipping placement of {cspr} as it has been preset"); + continue; + } + if(Verbose) Console.WriteLine("Attempting to place {0}.", cspr); Point scanLines = Point.Empty; Point nextScanLines = new Point(int.MaxValue, int.MaxValue); @@ -161,8 +171,10 @@ namespace SpritePacker } // If we found a new position, then we don't need to move the scan lines up and try again - if (foundPosition) + if (foundPosition) { + cspr.Placed = true; break; + } if(Verbose) Console.WriteLine("Failed to find a position along the current scan lines."); if(Verbose) Console.WriteLine("Next candidate scan lines: {0}", nextScanLines);