Persistence.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) - Violent Tulip
  4. //-----------------------------------------------------------------------------
  5. $VerveEditor::FilePath = "sequences";
  6. $VerveEditor::FileSpec = "Verve Sequence Files (*.vsf)|*.vsf|All Files (*.*)|*.*|";
  7. //-----------------------------------------------------------------------------
  8. function VerveEditor::GetFileTarget( %type )
  9. {
  10. %filePath = $VerveEditor::FilePath;
  11. if ( strlen( $Pref::VerveEditor::FilePath ) > 0 )
  12. {
  13. %filePath = $Pref::VerveEditor::FilePath;
  14. }
  15. %fileDialog = new ( %type @ "FileDialog" )()
  16. {
  17. Filters = $VerveEditor::FileSpec;
  18. DefaultPath = %filePath;
  19. ChangePath = false;
  20. MustExist = true;
  21. };
  22. // Record the file name
  23. %filename = "";
  24. if ( %fileDialog.Execute() )
  25. {
  26. %filename = %fileDialog.FileName;
  27. // Store the preference
  28. $Pref::VerveEditor::FilePath = makeRelativePath( filePath( %filename ), getMainDotCsDir() );
  29. }
  30. // Delete the dialog
  31. %fileDialog.delete();
  32. // Return the filename
  33. return %filename;
  34. }
  35. function VerveEditor::NewFile()
  36. {
  37. if ( !isObject( $VerveEditor::Controller ) )
  38. {
  39. return;
  40. }
  41. // Save?
  42. if ( !VerveEditor::SavePrompt() )
  43. {
  44. return;
  45. }
  46. // Clear Sequence Lists.
  47. $VerveEditor::Controller.clear();
  48. // Clear File.
  49. $VerveEditor::Controller.FileName = "";
  50. // Reset Properties.
  51. $VerveEditor::Controller.Time = 0;
  52. $VerveEditor::Controller.Duration = 5000;
  53. $VerveEditor::Controller.TimeScale = 1.0;
  54. // Clear Editor History.
  55. VerveEditor::ClearHistory();
  56. // Refresh Editor.
  57. VerveEditor::Refresh();
  58. }
  59. function VerveEditor::LoadFile( %fileName )
  60. {
  61. if ( !isObject( $VerveEditor::Controller ) )
  62. {
  63. return;
  64. }
  65. // Save?
  66. if ( !VerveEditor::SavePrompt() )
  67. {
  68. return;
  69. }
  70. if ( %fileName $= "" )
  71. {
  72. %fileName = VerveEditor::GetFileTarget( "Open" );
  73. }
  74. // Clear File.
  75. $VerveEditor::Controller.FileName = "";
  76. if ( $VerveEditor::Controller.readFile( %fileName ) )
  77. {
  78. // Pause.
  79. VerveEditor::Pause();
  80. // Store the File.
  81. $VerveEditor::Controller.FileName = %fileName;
  82. // Update File History.
  83. VerveEditor::UpdateFileHistory( %fileName );
  84. // Clear Editor History.
  85. VerveEditor::ClearHistory();
  86. // Refresh Editor.
  87. VerveEditor::Refresh();
  88. return true;
  89. }
  90. // Argh!
  91. // Attempting to load a file which results in failure means the existing
  92. // sequence is messed up, ouch! Do something better than creating a new
  93. // sequence...
  94. VerveEditor::NewFile();
  95. return false;
  96. }
  97. function VerveEditor::SaveFile( %forceSaveAs )
  98. {
  99. if ( !isObject( $VerveEditor::Controller ) )
  100. {
  101. return false;
  102. }
  103. %fileName = $VerveEditor::Controller.FileName;
  104. if ( %forceSaveAs || %fileName $= "" )
  105. {
  106. %fileName = VerveEditor::GetFileTarget( "Save" );
  107. if ( %fileName $= "" )
  108. {
  109. // No Save.
  110. return false;
  111. }
  112. }
  113. if ( fileExt( fileName( %fileName ) ) $= "" )
  114. {
  115. // Add Extension.
  116. %fileName = %fileName @ ".vsf";
  117. }
  118. // Write.
  119. $VerveEditor::Controller.writeFile( %fileName );
  120. // Store the File.
  121. $VerveEditor::Controller.FileName = %fileName;
  122. // Update File History.
  123. VerveEditor::UpdateFileHistory( %fileName );
  124. // Clear Dirty.
  125. VerveEditor::ClearDirty();
  126. // Update Window Title.
  127. VerveEditorWindow.UpdateWindowTitle();
  128. // Valid Save.
  129. return true;
  130. }
  131. function VerveEditor::SavePrompt()
  132. {
  133. if ( !VerveEditor::IsDirty() )
  134. {
  135. return true;
  136. }
  137. %result = toolsMessageBox( "Verve Editor", "Save Changes to your sequence?", "SaveDontSave", "Warning" );
  138. if ( %result $= $MROk )
  139. {
  140. // Save.
  141. return VerveEditor::SaveFile();
  142. }
  143. return true;
  144. }
  145. function VerveEditor::SavePromptCancel()
  146. {
  147. if ( !VerveEditor::IsDirty() )
  148. {
  149. return true;
  150. }
  151. %result = toolsMessageBox( "Verve Editor", "Save Changes to your sequence?", "SaveDontSaveCancel", "Warning" );
  152. if ( %result $= $MRCancel )
  153. {
  154. return false;
  155. }
  156. if ( %result $= $MROk )
  157. {
  158. // Save.
  159. return VerveEditor::SaveFile();
  160. }
  161. return true;
  162. }
  163. function VerveEditor::UpdateFileHistory( %filePath )
  164. {
  165. // Make Relative.
  166. %fileLabel = makeRelativePath( %filePath, getMainDotCsDir() );
  167. // Select an Index.
  168. %initIndex = $Pref::VerveEditor::RecentFileSize;
  169. for ( %i = 0; %i < %initIndex; %i++ )
  170. {
  171. %prefFile = $Pref::VerveEditor::RecentFile[%i];
  172. if ( %prefFile $= %fileLabel )
  173. {
  174. %initIndex = %i;
  175. break;
  176. }
  177. }
  178. // Push Others Down.
  179. for ( %i = %initIndex; %i > 0; %i-- )
  180. {
  181. $Pref::VerveEditor::RecentFile[%i] = $Pref::VerveEditor::RecentFile[%i - 1];
  182. }
  183. // Push to the Front.
  184. $Pref::VerveEditor::RecentFile[0] = %fileLabel;
  185. }