Skip to content

Commit

Permalink
Re-loading brand new project now maintains the NineSlice image
Browse files Browse the repository at this point in the history
fixes #409
  • Loading branch information
vchelaru committed Dec 24, 2024
1 parent 1b0c869 commit 0b696ef
Showing 3 changed files with 90 additions and 53 deletions.
1 change: 1 addition & 0 deletions Gum/Gum.csproj
Original file line number Diff line number Diff line change
@@ -250,6 +250,7 @@
<Compile Include="Plugins\InternalPlugins\LoadRecentFilesPlugin\Views\RecentFileItem.xaml.cs">
<DependentUpon>RecentFileItem.xaml</DependentUpon>
</Compile>
<Compile Include="Plugins\InternalPlugins\NineSlicePlugin\MainNineSlicePlugin.cs" />
<Compile Include="Plugins\InternalPlugins\Output\MainOutputPlugin.cs" />
<Compile Include="Plugins\InternalPlugins\ParentPlugin\MainParentPlugin.cs" />
<Compile Include="Plugins\InternalPlugins\ScreenshotPlugin\MainScreenshotPlugin.cs" />
86 changes: 33 additions & 53 deletions Gum/Managers/ProjectManager.cs
Original file line number Diff line number Diff line change
@@ -591,12 +591,6 @@ private void FillWithNecessaryInstances(ComponentSave component, List<InstanceSa
}
}

public static string GetExecutingDirectory()
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
return path;
}

internal void SaveProject(bool forceSaveContainedElements = false)
{
bool succeeded = false;
@@ -614,34 +608,7 @@ internal void SaveProject(bool forceSaveContainedElements = false)
{
PluginManager.Self.BeforeProjectSave(GumProjectSave);

foreach (var elementSave in GumProjectSave.Screens)
{
foreach (var stateSave in elementSave.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
foreach (var elementSave in GumProjectSave.Components)
{
foreach (var stateSave in elementSave.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
foreach (var elementSave in GumProjectSave.StandardElements)
{
foreach (var stateSave in elementSave.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
foreach (var behavior in GumProjectSave.Behaviors)
{
foreach (var stateSave in behavior.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
SortVariables();

bool saveContainedElements = isNewProject || forceSaveContainedElements;

@@ -668,25 +635,6 @@ internal void SaveProject(bool forceSaveContainedElements = false)
FileWatchLogic.Self.IgnoreNextChangeOn(GumProjectSave.FullFileName);

GumCommands.Self.TryMultipleTimes(() => GumProjectSave.Save(GumProjectSave.FullFileName, saveContainedElements));

if (isNewProject)
{

var sourceFile = Path.Combine(GetExecutingDirectory(), "Content\\ExampleSpriteFrame.png");
var destinationFile = FileManager.GetDirectory(GumProjectSave.FullFileName) + "ExampleSpriteFrame.png";
try
{
System.IO.File.Copy(sourceFile, destinationFile);

var nineSliceStandard = GumProjectSave.StandardElements.Find(item => item.Name == "NineSlice");
nineSliceStandard.DefaultState.SetValue("SourceFile", "ExampleSpriteFrame.png", "string");
}
catch (Exception e)
{
GumCommands.Self.GuiCommands.PrintOutput($"Error copying ExampleSpriteFrame.png: {e}");
}
}

succeeded = true;

if (succeeded && saveContainedElements)
@@ -735,6 +683,38 @@ internal void SaveProject(bool forceSaveContainedElements = false)
}
}

private void SortVariables()
{
foreach (var elementSave in GumProjectSave.Screens)
{
foreach (var stateSave in elementSave.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
foreach (var elementSave in GumProjectSave.Components)
{
foreach (var stateSave in elementSave.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
foreach (var elementSave in GumProjectSave.StandardElements)
{
foreach (var stateSave in elementSave.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
foreach (var behavior in GumProjectSave.Behaviors)
{
foreach (var stateSave in behavior.AllStates)
{
stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
}
}
}

private static string TryGetFileNameFromException(UnauthorizedAccessException exception)
{
string message = exception.Message;
56 changes: 56 additions & 0 deletions Gum/Plugins/InternalPlugins/NineSlicePlugin/MainNineSlicePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Gum.DataTypes;
using Gum.Managers;
using Gum.Plugins.BaseClasses;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ToolsUtilities;

namespace Gum.Plugins.InternalPlugins.NineSlicePlugin;

[Export(typeof(PluginBase))]
internal class MainNineSlicePlugin : InternalPlugin
{
public override void StartUp()
{
AssignEvents();
}

private void AssignEvents()
{
this.ProjectLocationSet += HandleProjectLocationSet;
}

private void HandleProjectLocationSet(FilePath path)
{
var gumProject = ObjectFinder.Self.GumProjectSave;

var sourceFile = Path.Combine(GetExecutingDirectory(), "Content\\ExampleSpriteFrame.png");
var destinationFile = FileManager.GetDirectory(gumProject.FullFileName) + "ExampleSpriteFrame.png";
try
{
System.IO.File.Copy(sourceFile, destinationFile);

var nineSliceStandard = gumProject.StandardElements.Find(item => item.Name == "NineSlice");
nineSliceStandard.DefaultState.SetValue("SourceFile", "ExampleSpriteFrame.png", "string");

GumCommands.Self.FileCommands.TryAutoSaveElement(nineSliceStandard);
}
catch (Exception e)
{
GumCommands.Self.GuiCommands.PrintOutput($"Error copying ExampleSpriteFrame.png: {e}");
}
}

static string GetExecutingDirectory()
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
return path;
}

}

0 comments on commit 0b696ef

Please sign in to comment.