1+ using Avalonia ;
12using Avalonia . Controls ;
23using Avalonia . Platform . Storage ;
34using AvaloniaEdit . Utils ;
89using System . Linq ;
910using System . Text . RegularExpressions ;
1011using System . Threading . Tasks ;
12+ using ZXBasicStudio . Classes ;
13+ using ZXBasicStudio . DebuggingTools . Registers . Binding ;
1114using ZXBasicStudio . DocumentEditors . ZXRamDisk . Classes ;
1215using ZXBasicStudio . DocumentEditors . ZXTapeBuilder . Classes ;
1316using ZXBasicStudio . DocumentModel . Classes ;
@@ -18,12 +21,14 @@ namespace ZXBasicStudio.DocumentEditors.ZXRamDisk.Controls
1821{
1922 public partial class ZXRamDiskEditor : ZXDocumentEditorBase
2023 {
24+ public static StyledProperty < ObservableCollection < ZXRamDiskContainedFile > > FilesProperty = AvaloniaProperty . Register < ZXRamDiskEditor , ObservableCollection < ZXRamDiskContainedFile > > ( "Files" ) ;
25+
2126 #region Private variables
2227 string _docName ;
2328 string _docPath ;
2429 bool _modified ;
2530 bool _internalUpdate ;
26- ObservableCollection < ZXRamDiskContainedFile > _files = new ObservableCollection < ZXRamDiskContainedFile > ( ) ;
31+ ObservableCollection < ZXRamDiskContainedFile > [ ] _files = new ObservableCollection < ZXRamDiskContainedFile > [ 5 ] ;
2732 #endregion
2833
2934 #region Events
@@ -34,7 +39,11 @@ public partial class ZXRamDiskEditor : ZXDocumentEditorBase
3439 #endregion
3540
3641 #region Binding properties
37- public ObservableCollection < ZXRamDiskContainedFile > Files => _files ;
42+ public ObservableCollection < ZXRamDiskContainedFile > Files
43+ {
44+ get { return GetValue ( FilesProperty ) ; }
45+ set { SetValue ( FilesProperty , value ) ; }
46+ }
3847 #endregion
3948
4049 #region ZXDocumentBase properties
@@ -44,16 +53,31 @@ public partial class ZXRamDiskEditor : ZXDocumentEditorBase
4453 #endregion
4554 public ZXRamDiskEditor ( )
4655 {
56+ for ( int buc = 0 ; buc < 5 ; buc ++ )
57+ _files [ buc ] = new ObservableCollection < ZXRamDiskContainedFile > ( ) ;
58+
59+ //Order: 4, 6, 1, 3, 7
60+ Files = _files [ 0 ] ;
61+
4762 DataContext = this ;
4863 InitializeComponent ( ) ;
4964 }
5065
5166 public ZXRamDiskEditor ( string DocumentPath )
5267 {
68+ for ( int buc = 0 ; buc < 5 ; buc ++ )
69+ _files [ buc ] = new ObservableCollection < ZXRamDiskContainedFile > ( ) ;
70+
71+ //Order: 4, 6, 1, 3, 7
72+ Files = _files [ 0 ] ;
73+
5374 DataContext = this ;
5475 InitializeComponent ( ) ;
5576
56- txtDiskName . TextChanged += DocumentChanged ;
77+ ckIndirect . IsCheckedChanged += DocumentChanged ;
78+ ckRelocate . IsCheckedChanged += DocumentChanged ;
79+ nudIndSize . ValueChanged += DocumentChanged ;
80+ cbBank . SelectionChanged += ChangeSelectedBank ;
5781
5882 btnSelectFile . Click += BtnSelectFile_Click ;
5983 btnAddFile . Click += BtnAddFile_Click ;
@@ -67,6 +91,11 @@ public ZXRamDiskEditor(string DocumentPath)
6791 throw new Exception ( "Error opening document" ) ;
6892 }
6993
94+ private void ChangeSelectedBank ( object ? sender , SelectionChangedEventArgs e )
95+ {
96+ int bank = cbBank . SelectedIndex ;
97+ Files = _files [ bank ] ;
98+ }
7099
71100 private async void BtnAddFile_Click ( object ? sender , Avalonia . Interactivity . RoutedEventArgs e )
72101 {
@@ -76,7 +105,9 @@ private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.Route
76105 return ;
77106 }
78107
79- if ( ! File . Exists ( txtFilePath . Text ) )
108+ string path = Path . Combine ( ZXProjectManager . Current . ProjectPath , txtFilePath . Text ) ;
109+
110+ if ( ! File . Exists ( path ) )
80111 {
81112 await Window . GetTopLevel ( this ) . ShowError ( "File not found" , "Cannot find the specified file." ) ;
82113 return ;
@@ -88,9 +119,9 @@ private async void BtnAddFile_Click(object? sender, Avalonia.Interactivity.Route
88119 return ;
89120 }
90121
91- ZXRamDiskContainedFile file = new ZXRamDiskContainedFile { Name = txtFileName . Text , SourcePath = txtFilePath . Text , Content = File . ReadAllBytes ( txtFilePath . Text ) } ;
122+ ZXRamDiskContainedFile file = new ZXRamDiskContainedFile { Name = txtFileName . Text , SourcePath = txtFilePath . Text } ;
92123
93- _files . Add ( file ) ;
124+ Files . Add ( file ) ;
94125
95126 DocumentChanged ( this , e ) ;
96127 }
@@ -121,6 +152,8 @@ private async void BtnSelectFile_Click(object? sender, Avalonia.Interactivity.Ro
121152 if ( string . IsNullOrWhiteSpace ( file ) )
122153 return ;
123154
155+ file = Path . GetRelativePath ( ZXProjectManager . Current . ProjectPath , file ) ;
156+
124157 txtFilePath . Text = file ;
125158
126159 string fileName = Path . GetFileNameWithoutExtension ( txtFilePath . Text ) ;
@@ -132,7 +165,7 @@ private void BtnRemoveFile_Click(object? sender, Avalonia.Interactivity.RoutedEv
132165 {
133166 if ( lstFiles . SelectedItem != null )
134167 {
135- _files . Remove ( ( ZXRamDiskContainedFile ) lstFiles . SelectedItem ) ;
168+ Files . Remove ( ( ZXRamDiskContainedFile ) lstFiles . SelectedItem ) ;
136169 DocumentChanged ( this , e ) ;
137170 }
138171 }
@@ -183,30 +216,18 @@ bool UpdateFileName(string NewPath)
183216
184217 _internalUpdate = true ;
185218
186- txtDiskName . Text = fileContent . DiskName ;
219+ ckIndirect . IsChecked = fileContent . EnableIndirect ;
220+ ckRelocate . IsChecked = fileContent . RelocateStack ;
221+ nudIndSize . Value = fileContent . IndirectBufferSize ;
187222
188- switch ( fileContent . Bank )
223+ for ( int buc = 0 ; buc < 5 ; buc ++ )
189224 {
190- case RamDiskBank . Bank1 :
191- cbBank . SelectedIndex = 2 ;
192- break ;
193- case RamDiskBank . Bank3 :
194- cbBank . SelectedIndex = 3 ;
195- break ;
196- case RamDiskBank . Bank4 :
197- cbBank . SelectedIndex = 0 ;
198- break ;
199- case RamDiskBank . Bank6 :
200- cbBank . SelectedIndex = 1 ;
201- break ;
202- case RamDiskBank . Bank7 :
203- cbBank . SelectedIndex = 4 ;
204- break ;
225+ _files [ buc ] . Clear ( ) ;
226+ _files [ buc ] . AddRange ( fileContent . Banks [ buc ] . Files ) ;
205227 }
206228
207- _files . Clear ( ) ;
208- _files . AddRange ( fileContent . Files ) ;
209-
229+ cbBank . SelectedIndex = 0 ;
230+
210231 Task . Run ( async ( ) =>
211232 {
212233 await Task . Delay ( 100 ) ;
@@ -219,47 +240,26 @@ bool UpdateFileName(string NewPath)
219240 #region ZXDocumentBase implementation
220241 public override bool SaveDocument ( TextWriter OutputLog )
221242 {
222- if ( string . IsNullOrWhiteSpace ( txtDiskName . Text ) )
223- {
224- OutputLog . WriteLine ( "Missing disk name, aborting..." ) ;
225- return false ;
226- }
227-
228- if ( _files . Count == 0 )
243+ if ( _files . Sum ( f => f . Count ) == 0 )
229244 {
230245 OutputLog . WriteLine ( "No files on disk, aborting..." ) ;
231246 return false ;
232247 }
233248
234- if ( _files . Sum ( f => f . Size ) > 16 * 1024 )
249+ if ( _files . Any ( f => f . Sum ( ff => ff . Size ) > 16 * 1024 ) )
235250 {
236- OutputLog . WriteLine ( "Total size exceeds 16Kb, aborting..." ) ;
251+ OutputLog . WriteLine ( "Bank size exceeds 16Kb, aborting..." ) ;
237252 return false ;
238253 }
239254
240- RamDiskBank selBank = new RamDiskBank ( ) ;
255+ ZXRamDiskFile fileContent = new ZXRamDiskFile ( ) ;
241256
242- switch ( cbBank . SelectedIndex )
243- {
244- case 2 :
245- selBank = RamDiskBank . Bank1 ;
246- break ;
247- case 3 :
248- selBank = RamDiskBank . Bank3 ;
249- break ;
250- case 0 :
251- selBank = RamDiskBank . Bank4 ;
252- break ;
253- case 1 :
254- selBank = RamDiskBank . Bank6 ;
255- break ;
256- case 4 :
257- selBank = RamDiskBank . Bank7 ;
258- break ;
259- }
257+ for ( int buc = 0 ; buc < 5 ; buc ++ )
258+ fileContent . Banks [ buc ] . Files . AddRange ( _files [ buc ] ) ;
260259
261- //TODO: Make paths relative to project
262- ZXRamDiskFile fileContent = new ZXRamDiskFile { DiskName = txtDiskName . Text , Bank = selBank , Files = _files . ToList ( ) } ;
260+ fileContent . IndirectBufferSize = ( int ) ( nudIndSize . Value ?? 0 ) ;
261+ fileContent . EnableIndirect = ckIndirect . IsChecked ?? false ;
262+ fileContent . RelocateStack = ckRelocate . IsChecked ?? false ;
263263
264264 string content = JsonConvert . SerializeObject ( fileContent , Formatting . Indented ) ;
265265 try
0 commit comments