1- using System . Collections . ObjectModel ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Collections . ObjectModel ;
4+ using System . IO ;
5+ using System . Linq ;
6+ using System . Threading . Tasks ;
27using Avalonia . Controls ;
38using Avalonia . Layout ;
49using HaruhiChokuretsuLib . Archive . Event ;
10+ using HaruhiChokuretsuLib . Save ;
11+ using HaruhiChokuretsuLib . Util ;
512using ReactiveUI ;
13+ using SerialLoops . Assets ;
14+ using SerialLoops . Lib . Items ;
615using SerialLoops . Lib . Script ;
16+ using SerialLoops . Lib . Script . Parameters ;
17+ using SerialLoops . Lib . Util ;
718using SerialLoops . Utility ;
19+ using SerialLoops . ViewModels . Dialogs ;
20+ using SerialLoops . Views ;
21+ using SerialLoops . Views . Dialogs ;
22+ using SoftCircuits . Collections ;
823
924namespace SerialLoops . Models ;
1025
@@ -23,13 +38,83 @@ public class ScriptCommandTreeItem : ITreeItem, IViewFor<ScriptItemCommand>
2338 public ObservableCollection < ITreeItem > Children { get ; set ; } = null ;
2439 public bool IsExpanded { get ; set ; } = false ;
2540
26- public ScriptCommandTreeItem ( ScriptItemCommand command )
41+ public ScriptCommandTreeItem ( ScriptItemCommand command , ILogger log , MainWindow window )
2742 {
2843 ViewModel = command ;
2944 this . OneWayBind ( ViewModel , vm => vm . Display , v => v . _textBlock . Text ) ;
3045 _textBlock . VerticalAlignment = VerticalAlignment . Center ;
3146 _panel . Children . Add ( _textBlock ) ;
3247 _panel [ ToolTip . TipProperty ] = Shared . GetScriptVerbHelp ( ViewModel ? . Verb ?? EventFile . CommandVerb . NOOP1 ) ;
48+ _panel . ContextMenu = new ( ) ;
49+ _panel . ContextMenu . Items . Add ( new MenuItem
50+ {
51+ Header = Strings . ScriptCommandSetQuickSaveText ,
52+ Command = ReactiveCommand . Create ( SetQuickSaveHere ) ,
53+ } ) ;
54+ _panel . ContextMenu . Items . Add ( new MenuItem
55+ {
56+ Header = Strings . ScriptCommandSetQuickSaveAdvancedText ,
57+ Command = ReactiveCommand . CreateFromTask ( async Task ( ) =>
58+ {
59+ if ( ! SetQuickSaveHere ( ) )
60+ {
61+ return ;
62+ }
63+ SaveSlotEditorDialogViewModel quickSaveEditorDialogVm = new ( ViewModel . Project . ProjectSaveFile ,
64+ ViewModel . Project . ProjectSaveFile ! . Save . QuickSaveSlot ,
65+ ViewModel . Project . ProjectSaveFile . Name , Strings . Quick_Save , ViewModel . Project , log , dontTreatAsQuickSave : true ) ;
66+ await new SaveSlotEditorDialog { DataContext = quickSaveEditorDialogVm } . ShowDialog ( window ) ;
67+ } ) ,
68+ } ) ;
69+
70+ return ;
71+
72+ bool SetQuickSaveHere ( )
73+ {
74+ if ( ViewModel ? . Project . ProjectSaveFile is null )
75+ {
76+ if ( ! ( ViewModel ? . Project . LoadProjectSave ( ) ?? false ) )
77+ {
78+ log . LogError ( "" ) ;
79+ return false ;
80+ }
81+ }
82+
83+ ScriptItem script = ( ScriptItem ) ViewModel . Project . Items . First ( i =>
84+ i . Type == ItemDescription . ItemType . Script && ( ( ScriptItem ) i ) . Event . Index == ViewModel . Script . Index ) ;
85+ List < ItemDescription > references = script . GetReferencesTo ( ViewModel . Project ) ;
86+ ScenarioItem scenarioRef = ( ScenarioItem ) references . FirstOrDefault ( i => i . Type == ItemDescription . ItemType . Scenario ) ;
87+ GroupSelectionItem groupSelectionRef = ( GroupSelectionItem ) references . FirstOrDefault ( i => i . Type == ItemDescription . ItemType . Group_Selection ) ;
88+
89+ QuickSaveSlotData quickSave = ViewModel . Project . ProjectSaveFile ! . Save . QuickSaveSlot ;
90+ quickSave . SaveTime = DateTimeOffset . Now ;
91+
92+ if ( scenarioRef is not null )
93+ {
94+ quickSave . ScenarioPosition = ( short ) ( scenarioRef . Scenario . Commands . FindIndex ( c =>
95+ c . Verb == ScenarioCommand . ScenarioVerb . LOAD_SCENE && c . Parameter == ViewModel . Script . Index ) + 1 ) ;
96+ }
97+ else if ( groupSelectionRef is not null )
98+ {
99+ quickSave . ScenarioPosition = ( short ) ( ViewModel . Project . Scenario . Commands . FindIndex ( c =>
100+ c . Verb == ScenarioCommand . ScenarioVerb . ROUTE_SELECT && c . Parameter == groupSelectionRef . Index ) + 1 ) ;
101+ }
102+ else
103+ {
104+ log . LogWarning ( $ "Unable to find references to current script '{ ViewModel . Script . Name } ' (0x{ ViewModel . Script . Index : X3} ' within other scenario. Could be dangerous!") ;
105+ quickSave . ScenarioPosition = 1 ;
106+ }
107+ quickSave . EpisodeNumber = 1 ;
108+ quickSave . CurrentScript = ViewModel . Script . Index ;
109+ quickSave . CurrentScriptBlock = ViewModel . Script . ScriptSections . IndexOf ( ViewModel . Section ) ;
110+ quickSave . CurrentScriptCommand = ViewModel . Index ;
111+
112+ quickSave . ApplyScriptPreview ( ViewModel . CurrentPreview , script , ViewModel . Index , ViewModel . Project , log ) ;
113+
114+ File . WriteAllBytes ( ViewModel . Project . ProjectSaveFile . SaveLoc , ViewModel . Project . ProjectSaveFile . Save . GetBytes ( ) ) ;
115+
116+ return true ;
117+ }
33118 }
34119
35120 public Control GetDisplay ( )
0 commit comments