Browse Source

Save Dialog for GuiEditor

This adds a save dialog to the GuiEditor and the functionality needed to save a Gui. Obviously a key feature.
Peter Robinson 2 years ago
parent
commit
d2070ffd3e
2 changed files with 231 additions and 6 deletions
  1. 20 6
      editor/GuiEditor/GuiEditor.cs
  2. 211 0
      editor/GuiEditor/scripts/GuiEditorSaveGuiDialog.cs

+ 20 - 6
editor/GuiEditor/GuiEditor.cs

@@ -28,6 +28,7 @@ function GuiEditor::create( %this )
 	exec("./scripts/GuiEditorInspectorWindow.cs");
 	exec("./scripts/GuiEditorExplorerWindow.cs");
     exec("./scripts/GuiEditorExplorerTree.cs");
+    exec("./scripts/GuiEditorSaveGuiDialog.cs");
 
 	%this.guiPage = EditorCore.RegisterEditor("Gui Editor", %this);
 
@@ -152,7 +153,7 @@ function GuiEditor::close(%this)
 
 function GuiEditor::onNewGui(%this)
 {
-    %this.blankGui = new GuiControl()
+    %this.rootGui = new GuiControl()
     {
         HorizSizing = "width";
         VertSizing = "height";
@@ -160,10 +161,10 @@ function GuiEditor::onNewGui(%this)
         Extent = %this.guiPage.getExtent();
         Profile = GuiDefaultProfile;
     };
-    %this.content.add(%this.blankGui);
-    %this.brain.setRoot(%this.blankGui);
-    %this.brain.root = %this.blankGui;
-    %this.explorerWindow.open(%this.blankGui);
+    %this.content.add(%this.rootGui);
+    %this.brain.setRoot(%this.rootGui);
+    %this.brain.root = %this.rootGui;
+    %this.explorerWindow.open(%this.rootGui);
 }
 
 //MENU FUNCTIONS---------------------------------------------------------------
@@ -179,7 +180,20 @@ function GuiEditor::OpenGui(%this)
 
 function GuiEditor::SaveGui(%this)
 {
-    
+    %width = 700;
+	%height = 340;
+	%dialog = new GuiControl()
+	{
+		class = "GuiEditorSaveGuiDialog";
+		superclass = "EditorDialog";
+		dialogSize = (%width + 8) SPC (%height + 8);
+		dialogCanClose = true;
+		dialogText = "Save Gui";
+	};
+	%dialog.init(%width, %height);
+	%this.startListening(%dialog);
+
+	Canvas.pushDialog(%dialog);
 }
 
 function GuiEditor::Undo(%this)

+ 211 - 0
editor/GuiEditor/scripts/GuiEditorSaveGuiDialog.cs

@@ -0,0 +1,211 @@
+
+function GuiEditorSaveGuiDialog::init(%this, %width, %height)
+{
+	//Get the dialog contents
+	%window = %this.getObject(0);
+	%content = %window.getObject(0);
+
+	//Create the file text box
+	%form = new GuiGridCtrl()
+	{
+		class = "EditorForm";
+		extent = %width SPC %height;
+		cellSizeX = %width;
+		cellSizeY = 50;
+	};
+	%form.addListener(%this);
+
+	%item = %form.addFormItem("Gui Name", %width SPC 30);
+	%this.guiNameBox = %form.createTextEditItem(%item);
+	%this.guiNameBox.Command = %this.getId() @ ".Validate();";
+
+	if(GuiEditor.rootGui.getName() !$= "")
+	{
+		%name = %this.blankGui.getName() @ ".gui";
+	}
+	else
+	{
+		%name = "untitled.gui";
+	}
+	%this.guiNameBox.text = %name;
+
+	%item = %form.addFormItem("Target Folder", %width SPC 30);
+	%this.folderBox = %form.createFolderOpenItem(%item, "Select Target Folder");
+	%this.folderBox.Command = %this.getId() @ ".Validate();";
+
+	%item = %form.addFormItem("Target Module", %width SPC 30);
+	%this.moduleNameBox = %form.createTextEditItem(%item);
+	%this.moduleNameBox.setActive(false);
+
+	%content.add(%form);
+
+	%this.feedback = new GuiControl()
+	{
+		HorizSizing = "right";
+		VertSizing = "bottom";
+		Position = "12 170";
+		Extent = (%width - 24) SPC 80;
+		text = "Select a name and target folder!";
+		textWrap = true;
+		textExtend = true;
+	};
+	ThemeManager.setProfile(%this.feedback, "infoProfile");
+
+	%this.cancelButton = new GuiButtonCtrl()
+	{
+		HorizSizing = "right";
+		VertSizing = "bottom";
+		Position = "478 270";
+		Extent = "100 30";
+		Text = "Cancel";
+		Command = %this.getID() @ ".onClose();";
+	};
+	ThemeManager.setProfile(%this.cancelButton, "buttonProfile");
+
+	%this.saveButton = new GuiButtonCtrl()
+	{
+		HorizSizing = "right";
+		VertSizing = "bottom";
+		Position = "588 268";
+		Extent = "100 34";
+		Text = "Save";
+		Command = %this.getID() @ ".onSave();";
+	};
+	ThemeManager.setProfile(%this.saveButton, "primaryButtonProfile");
+
+	%content.add(%this.feedback);
+	%content.add(%this.cancelButton);
+	%content.add(%this.saveButton);
+
+	%this.prevFolder = "";
+}
+
+function GuiEditorSaveGuiDialog::Validate(%this)
+{
+	%this.createButton.active = false;
+
+	%folderPath = %this.getFolderPath();
+	%guiName = %this.guiNameBox.getText();
+
+	if(%this.folderBox.getText() $= "")
+	{
+		%this.folderBox.setText(mgetMainDotCsDir());
+	}
+
+	if(%folderPath !$= %this.prevFolder)
+	{
+		%modSig = EditorCore.findModuleOfPath(%folderPath @ "a.txt");
+		%this.moduleNameBox.setText(%modSig);
+		%this.prevFolder = %folderPath;
+	}
+	%moduleName = getUnit(%this.moduleNameBox.getText(), 0, "_");
+	%moduleVersion = getUnit(%this.moduleNameBox.getText(), 1, "_");
+
+	if(%folderPath $= "")
+	{
+		%this.feedback.setText("Please select a target folder.");
+		return false;
+	}
+
+	if(%guiName $= "")
+	{
+		%this.feedback.setText("A Gui file must have a name.");
+		return false;
+	}
+
+	if(%moduleName $= "")
+	{
+		%this.feedback.setText("You can only save a Gui file inside of a module.");
+		return false;
+	}
+	else
+	{
+		%module = ModuleDatabase.findModule(%moduleName, %moduleVersion);
+		if(!isObject(%module))
+		{
+			%this.feedback.setText("There was a problem finding the module for this file.");
+			return false;
+		}
+		else if(%module.Synchronized)
+		{
+			%this.feedback.setText("You cannot add files to a library module. Updates to the module would remove your files. Instead, create your own module and add files to it.");
+			return false;
+		}
+	}
+
+	%filePath = %this.getFilePath();
+	if(isFile(%filePath) && !isWriteableFileName(%filePath))
+	{
+		%this.feedback.setText("A file by this name already exists and it is read-only.");
+		return false;
+	}
+	if(isFile(%filePath))
+	{
+		%this.createButton.active = true;
+		%this.feedback.setText("A file by this name already exists. It will be overwritten.");
+		return true;
+	}
+
+	%this.createButton.active = true;
+	%this.feedback.setText("A new Gui file will be created!");
+	return true;
+}
+
+function GuiEditorSaveGuiDialog::onClose(%this)
+{
+	Canvas.popDialog(%this);
+	%this.postEvent("DialogClosed", %this);
+}
+
+function GuiEditorSaveGuiDialog::onSave(%this)
+{
+	if(%this.validate())
+	{
+		%filePath = %this.getFilePath();
+		%fo = new FileObject();
+		%fo.openForWrite(%filePath);
+		%fo.writeLine("//--- Created with the GuiEditor ---//");
+		%fo.writeObject(GuiEditor.rootGui, "%guiContent = ");
+		%fo.writeLine("//--- GuiEditor End ---//");
+		%fo.close();
+		%fo.delete();
+
+		%this.onClose();
+	}
+}
+
+function GuiEditorSaveGuiDialog::onFolderOpened(%this, %textBox)
+{
+	%this.Validate();
+}
+
+function GuiEditorSaveGuiDialog::getFilePath(%this)
+{
+	%folderPath = %this.getFolderPath();
+	%fileName = %this.guiNameBox.getText();
+	%filePath = pathConcat(%folderPath, %fileName);
+	%length = strlen(%filePath);
+	%sub = "";
+	if(%length >= 4)
+	{
+		%sub = getSubStr(%filePath, %length - 4, 4);
+	}
+	if(%sub !$= ".gui")
+	{
+		%filePath = %filePath @ ".gui";
+	}
+
+	return %filePath;
+}
+
+function GuiEditorSaveGuiDialog::getFolderPath(%this)
+{
+	%folderPath = stripTrailingSpaces(makeFullPath(%this.folderBox.getText()));
+	%length = strlen(%folderPath);
+	%lastChar = getSubStr(%folderPath, %length - 1, 1);
+	if(%lastChar $= "/")
+	{
+		return %folderPath;
+	}
+	return %folderPath @ "/";
+}