Skip to content

Commit

Permalink
Make the save editor work as a standalone feature as well (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonko0493 authored Dec 23, 2024
1 parent 5d251f9 commit b6c8024
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 23 deletions.
47 changes: 47 additions & 0 deletions src/SerialLoops/Assets/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/SerialLoops/Assets/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,18 @@ This action is irreversible.</value>
<data name="Archive_Filename" xml:space="preserve">
<value>Archive Filename</value>
</data>
<data name="Temporary Project Already Exists!" xml:space="preserve">
<value>Temporary Project Already Exists!</value>
</data>
<data name="In order to edit this save file, Serial Loops needs to make a temporary project. However, a project called &quot;{0}&quot; already exists. Would you like to overwrite this project?" xml:space="preserve">
<value>In order to edit this save file, Serial Loops needs to make a temporary project. However, a project called "{0}" already exists. Would you like to overwrite this project?</value>
</data>
<data name="Save Save File" xml:space="preserve">
<value>Save Save File</value>
</data>
<data name="Close Save File" xml:space="preserve">
<value>Close Save File</value>
</data>
<data name="Ignore Hash?" xml:space="preserve">
<value>Ignore Hash?</value><comment>This is in the project import dialog and indicates the ROM hash should be ignored</comment>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,4 @@ await dialog.ShowMessageBoxAsync(Strings.Successfully_applied_hacks_, string.For

dialog.Close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void OpenItem(TreeDataGrid viewer)

private ObservableCollection<ITreeItem> GetSections()
{
return new ObservableCollection<ITreeItem>(Items.GroupBy(i => i.Type)
return new(Items.GroupBy(i => i.Type)
.OrderBy(g => ControlGenerator.LocalizeItemTypes(g.Key))
.Select(g => new SectionTreeItem(
ControlGenerator.LocalizeItemTypes(g.Key),
Expand Down
131 changes: 116 additions & 15 deletions src/SerialLoops/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using SerialLoops.ViewModels.Panels;
using SerialLoops.Views;
using SerialLoops.Views.Dialogs;
using SerialLoops.Views.Editors;
using SerialLoops.Views.Panels;
using SkiaSharp;

Expand Down Expand Up @@ -227,6 +228,13 @@ public async Task<bool> CloseProject_Executed(WindowClosingEventArgs e)
bool cancel = false;
if (OpenProject is not null)
{
if (EditorTabs is null)
{
// If there are no editor tabs, we're in standalone save editor mode
Directory.Delete(OpenProject.MainDirectory, true); // Clean up after ourselves
return false;
}

// Warn against unsaved items
IEnumerable<ItemDescription> unsavedItems = OpenProject.Items.Where(i => i.UnsavedChanges);
if (unsavedItems.Any())
Expand Down Expand Up @@ -272,6 +280,10 @@ public async Task<bool> CloseProject_Executed(WindowClosingEventArgs e)
ProjectsCache.HadProjectOpenOnLastClose = true;
ProjectsCache.Save(Log);
}
else
{
cancel = true;
}
return cancel;
}

Expand Down Expand Up @@ -307,12 +319,21 @@ public async Task CloseProjectView()
ToolBar.Items.Clear();

NativeMenu menu = NativeMenu.GetMenu(Window);
menu.Items.Remove(WindowMenu[MenuHeader.PROJECT]);
WindowMenu.Remove(MenuHeader.PROJECT);
menu.Items.Remove(WindowMenu[MenuHeader.TOOLS]);
WindowMenu.Remove(MenuHeader.TOOLS);
menu.Items.Remove(WindowMenu[MenuHeader.BUILD]);
WindowMenu.Remove(MenuHeader.BUILD);
if (WindowMenu.ContainsKey(MenuHeader.PROJECT))
{
menu.Items.Remove(WindowMenu[MenuHeader.PROJECT]);
WindowMenu.Remove(MenuHeader.PROJECT);
}
if (WindowMenu.ContainsKey(MenuHeader.TOOLS))
{
menu.Items.Remove(WindowMenu[MenuHeader.TOOLS]);
WindowMenu.Remove(MenuHeader.TOOLS);
}
if (WindowMenu.ContainsKey(MenuHeader.BUILD))
{
menu.Items.Remove(WindowMenu[MenuHeader.BUILD]);
WindowMenu.Remove(MenuHeader.BUILD);
}
ProjectsCache.HadProjectOpenOnLastClose = false;
UpdateRecentProjects();
}
Expand Down Expand Up @@ -518,23 +539,103 @@ public async Task PreferencesCommand_Executed()

public async Task EditSaveFileCommand_Executed()
{
if (OpenProject is not null)
IStorageFile saveFile = await Window.ShowOpenFilePickerAsync(Strings.Open_Chokuretsu_Save_File,
[new(Strings.Chokuretsu_Save_File) { Patterns = ["*.sav"] }]);
if (saveFile is null)
{
IStorageFile saveFile = await Window.ShowOpenFilePickerAsync(Strings.Open_Chokuretsu_Save_File,
[new(Strings.Chokuretsu_Save_File) { Patterns = ["*.sav"] }]);
if (saveFile is null)
{
return;
}
return;
}
string savePath = saveFile.TryGetLocalPath();

string path = saveFile.TryGetLocalPath();
SaveItem saveItem = new SaveItem(path, Path.GetFileNameWithoutExtension(path));
if (OpenProject is not null)
{
SaveItem saveItem = new(savePath, Path.GetFileNameWithoutExtension(savePath));
OpenProject.Items.Add(saveItem);
EditorTabs.OpenTab(saveItem);
}
else
{
string rom = Path.Combine(Path.GetDirectoryName(savePath) ?? string.Empty, $"{Path.GetFileNameWithoutExtension(savePath)}.nds");
if (!File.Exists(rom))
{
IStorageFile romFile = await Window.ShowOpenFilePickerAsync(Strings.Open_ROM,
[new(Strings.NDS_ROM) { Patterns = ["*.nds"] }]);
if (romFile is null)
{
return;
}
rom = romFile.TryGetLocalPath();
}

string projectName = $"{Path.GetFileNameWithoutExtension(savePath)}_Temp";
string tempProjectDirectory = Path.Combine(CurrentConfig.ProjectsDirectory, projectName);
if (Directory.Exists(tempProjectDirectory))
{
if (await Window.ShowMessageBoxAsync(Strings.Temporary_Project_Already_Exists_,
string.Format(
Strings.In_order_to_edit_this_save_file__Serial_Loops_needs_to_make_a_temporary_project__However__a_project_called___0___already_exists__Would_you_like_to_overwrite_this_project_,
projectName),
ButtonEnum.YesNo, Icon.Warning, Log) == ButtonResult.Yes)
{
Directory.Delete(tempProjectDirectory, true);
}
else
{
return;
}
}
OpenProject = new(projectName, "en", CurrentConfig, Strings.ResourceManager.GetString, Log);
LoopyProgressTracker tracker = new();
await new ProgressDialog(() =>
{
((IProgressTracker)tracker).Focus(Strings.Creating_Project, 1);
IO.OpenRom(OpenProject, rom, Log, tracker);
tracker.Finished++;
OpenProject.Load(CurrentConfig, Log, tracker);
}, () =>
{
SaveItem saveItem = new(savePath, Path.GetFileNameWithoutExtension(savePath));
OpenProject.Items.Add(saveItem);
Window.MainContent.Content = new SaveEditorView
{
DataContext =
new SaveEditorViewModel(saveItem, this, Log, null),
};
if (WindowMenu.ContainsKey(MenuHeader.TOOLS))
{
// Skip adding the new menu items if they're already here
return;
}

// Add a few commands to the menu
NativeMenu menu = NativeMenu.GetMenu(Window);
int insertionPoint = menu.Items.Count;
if (((NativeMenuItem)menu.Items.Last()).Header.Equals(Strings._Help))
{
insertionPoint--;
}

// PROJECT
WindowMenu.Add(MenuHeader.PROJECT, new(Strings._Project));
WindowMenu[MenuHeader.PROJECT].Menu =
[
new NativeMenuItem
{
Header = Strings.Save_Save_File,
Command = SaveProjectCommand,
Icon = ControlGenerator.GetIcon("Save", Log),
Gesture = SaveHotKey,
},
new NativeMenuItem
{
Header = Strings.Close_Save_File,
Command = CloseProjectCommand,
Icon = ControlGenerator.GetIcon("Close", Log),
Gesture = CloseProjectKey,
},
];
menu.Items.Insert(insertionPoint, WindowMenu[MenuHeader.PROJECT]);
}, tracker, Strings.Creating_Project).ShowDialog(Window);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void OpenItem(TreeDataGrid viewer)

private ObservableCollection<ITreeItem> GetSections()
{
return new ObservableCollection<ITreeItem>(Items
return new(Items
.Where(i => i.Type != ItemDescription.ItemType.Save)
.GroupBy(i => i.Type)
.OrderBy(g => ControlGenerator.LocalizeItemTypes(g.Key))
Expand Down
13 changes: 8 additions & 5 deletions src/SerialLoops/Views/Dialogs/SaveSlotEditorDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
Header="{x:Static assets:Strings.Script_Position}">
<ScrollViewer>
<Grid RowDefinitions="Auto,Auto,Auto,*" ColumnDefinitions="Auto,*">
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<StackPanel Margin="10,5" Grid.Row="0" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<TextBlock Margin="0,7,0,0" Text="{x:Static assets:Strings.Script_}"/>
<ComboBox ItemsSource="{Binding ScriptItems}" SelectedItem="{Binding SelectedScriptItem}">
<ComboBox.ItemTemplate>
Expand All @@ -312,10 +312,11 @@
</ControlTheme>
</ComboBox.ItemContainerTheme>
</ComboBox>
<controls:ItemLink Item="{Binding SelectedScriptItem}" Tabs="{Binding Tabs}"/>
<controls:ItemLink Item="{Binding SelectedScriptItem}" Tabs="{Binding Tabs}"
IsVisible="{Binding Tabs, Converter={x:Static ObjectConverters.IsNotNull}}"/>
</StackPanel>

<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<StackPanel Margin="10,5" Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<TextBlock Margin="0,7,0,0" Text="{x:Static assets:Strings.Script_Section}"/>
<ComboBox ItemsSource="{Binding ScriptSections}" SelectedItem="{Binding SelectedScriptSection}">
<ComboBox.ItemTemplate>
Expand All @@ -331,17 +332,19 @@
</ComboBox>
</StackPanel>

<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<StackPanel Margin="10,5" Grid.Row="2" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<TextBlock Margin="0,7,0,0" Text="{x:Static assets:Strings.Command_Index_}"/>
<NumericUpDown Value="{Binding ScriptCommandIndex}"
Minimum="0" Maximum="{Binding SelectedScriptSection.Objects.Count, Converter={StaticResource SubtractionConverter}, ConverterParameter=1}"
FormatString="N0" Increment="1" ParsingNumberStyle="Integer"/>
</StackPanel>

<Grid Grid.Row="0" Grid.Column="1" Grid.RowSpan="4">
<Grid Grid.Row="0" Grid.Column="1" Grid.RowSpan="4" Margin="20,0">
<Image IsVisible="{Binding ScriptPreview, Converter={x:Static ObjectConverters.IsNotNull}}"
Width="256" Height="384"
Source="{Binding ScriptPreview, Converter={x:Static utility:SLConverters.SKBitmapToAvaloniaConverter}}"/>
<Image IsVisible="{Binding ScriptPreview, Converter={x:Static ObjectConverters.IsNull}}"
Width="256" Height="384"
Source="{Binding ErrorImagePath}"/>
</Grid>
</Grid>
Expand Down

0 comments on commit b6c8024

Please sign in to comment.