Skip to content

Commit

Permalink
Updated Notepad sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Jul 25, 2018
1 parent 05ea263 commit 0797671
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 46 deletions.
56 changes: 44 additions & 12 deletions Source/Myra.Editor/UI/File/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class FileDialog : Dialog

private readonly List<string> _paths = new List<string>();
private readonly List<string> _history = new List<string>();
private int _historyPosition = 0;
private int _historyPosition;
private readonly FileDialogMode _mode;
private bool _firstRender = true;

Expand Down Expand Up @@ -58,13 +58,13 @@ public string FilePath
{
get
{
return System.IO.Path.Combine(Folder, FileName);
return Path.Combine(Folder, FileName);
}

set
{
Folder = System.IO.Path.GetDirectoryName(value);
FileName = System.IO.Path.GetFileName(value);
Folder = Path.GetDirectoryName(value);
FileName = Path.GetFileName(value);

if (!string.IsNullOrEmpty(FileName))
{
Expand All @@ -87,6 +87,8 @@ public string FilePath
}
}

public bool AutoAddFilterExtension { get; set; }

public FileDialog(FileDialogMode mode)
{
_mode = mode;
Expand All @@ -106,6 +108,8 @@ public FileDialog(FileDialogMode mode)
break;
}

AutoAddFilterExtension = true;

if (mode == FileDialogMode.ChooseFolder)
{
_textBlockFileName.Visible = false;
Expand Down Expand Up @@ -456,21 +460,49 @@ protected override bool CanCloseByOk()
return true;
}

var dlg = Dialog.CreateMessageBox("Confirm Replace",
string.Format("File named '{0}' already exists. Do you want to replace it?", FileName));
var fileName = FileName;

dlg.Closed += (s, a) =>
if (AutoAddFilterExtension && !string.IsNullOrEmpty(Filter))
{
if (dlg.ModalResult == (int)Window.DefaultModalResult.Cancel)
var idx = Filter.LastIndexOf('.');
if (idx != -1)
{
return;
var ext = Filter.Substring(idx);

if (!fileName.EndsWith(ext))
{
fileName += ext;
}
}
}

if (System.IO.File.Exists(Path.Combine(Folder, fileName)))
{
var dlg = CreateMessageBox("Confirm Replace",
string.Format("File named '{0}' already exists. Do you want to replace it?", fileName));

dlg.Closed += (s, a) =>
{
if (dlg.ModalResult == (int) DefaultModalResult.Cancel)
{
return;
}

FileName = fileName;

ModalResult = (int) DefaultModalResult.Ok;
Close();
};

dlg.ShowModal(Desktop);
}
else
{
FileName = fileName;

ModalResult = (int)DefaultModalResult.Ok;
Close();
};

dlg.ShowModal(Desktop);
}

return false;
}
Expand Down
16 changes: 10 additions & 6 deletions Source/Myra.UIEditor/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,10 @@ private void NewItemOnClicked(object sender, EventArgs eventArgs)

private void OpenItemOnClicked(object sender, EventArgs eventArgs)
{
var dlg = new FileDialog(FileDialogMode.OpenFile);
dlg.Filter = "*.ui";
var dlg = new FileDialog(FileDialogMode.OpenFile)
{
Filter = "*.ui"
};

if (!string.IsNullOrEmpty(FilePath))
{
Expand All @@ -709,7 +711,7 @@ private void OpenItemOnClicked(object sender, EventArgs eventArgs)

dlg.Closed += (s, a) =>
{
if (dlg.ModalResult != (int)Myra.Graphics2D.UI.Window.DefaultModalResult.Ok)
if (dlg.ModalResult != (int)Graphics2D.UI.Window.DefaultModalResult.Ok)
{
return;
}
Expand Down Expand Up @@ -810,8 +812,10 @@ private void Save(bool setFileName)
{
if (string.IsNullOrEmpty(FilePath) || setFileName)
{
var dlg = new FileDialog(FileDialogMode.SaveFile);
dlg.Filter = "*.ui";
var dlg = new FileDialog(FileDialogMode.SaveFile)
{
Filter = "*.ui"
};

if (!string.IsNullOrEmpty(FilePath))
{
Expand All @@ -826,7 +830,7 @@ private void Save(bool setFileName)

dlg.Closed += (s, a) =>
{
if (dlg.ModalResult == (int)Myra.Graphics2D.UI.Window.DefaultModalResult.Ok)
if (dlg.ModalResult == (int)Graphics2D.UI.Window.DefaultModalResult.Ok)
{
ProcessSave(dlg.FilePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Myra.Editor\Myra.Editor.csproj">
<Project>{f18bd428-25f7-4183-beb7-7c96f21ae2d1}</Project>
<Name>Myra.Editor</Name>
</ProjectReference>
<ProjectReference Include="..\..\Myra\Myra.csproj">
<Project>{4dcbb001-9c79-4f6b-996d-d654062d93e0}</Project>
<Name>Myra</Name>
Expand Down
80 changes: 52 additions & 28 deletions Source/Samples/Myra.Samples.NotepadSample/NotepadGame.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Microsoft.Xna.Framework;
using Myra.Editor.UI.File;
using Myra.Graphics2D.UI;

namespace Myra.Samples.NotepadSample
Expand Down Expand Up @@ -47,9 +48,6 @@ public bool Dirty
}
}

public Func<string> OpenFileHandler { get; set; }
public Func<string> SaveFileHandler { get; set; }

public NotepadGame()
{
graphics = new GraphicsDeviceManager(this);
Expand Down Expand Up @@ -126,31 +124,47 @@ private void TextFieldOnTextChanged(object sender, EventArgs eventArgs)
Dirty = true;
}

private void ProcessSave(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}

File.WriteAllText(filePath, _textField.Text);

FilePath = filePath;
Dirty = false;
}

private void Save(bool setFileName)
{
if (string.IsNullOrEmpty(FilePath) || setFileName)
{
var h = SaveFileHandler;
if (h == null)
var dlg = new FileDialog(FileDialogMode.SaveFile)
{
return;
}
Filter = "*.txt"
};

var f = h();
if (string.IsNullOrEmpty(f))
if (!string.IsNullOrEmpty(FilePath))
{
return;
dlg.FilePath = FilePath;
}

FilePath = f;
}
dlg.Closed += (s, a) =>
{
if (dlg.ModalResult == (int) Graphics2D.UI.Window.DefaultModalResult.Ok)
{
ProcessSave(dlg.FilePath);
}
};

using (var writer = new StreamWriter(_filePath))
dlg.ShowModal(_host);
}
else
{
writer.Write(_textField.Text);
ProcessSave(FilePath);
}

Dirty = false;
}

private void AboutItemOnDown(object sender, EventArgs eventArgs)
Expand All @@ -171,25 +185,35 @@ private void SaveItemOnDown(object sender, EventArgs eventArgs)

private void OpenItemOnDown(object sender, EventArgs eventArgs)
{
var h = OpenFileHandler;
if (h == null)
var dlg = new FileDialog(FileDialogMode.OpenFile)
{
return;
}
Filter = "*.txt"
};

var filePath = h();
if (string.IsNullOrEmpty(filePath))
if (!string.IsNullOrEmpty(FilePath))
{
return;
dlg.FilePath = FilePath;
}

using (var reader = new StreamReader(filePath))
dlg.Closed += (s, a) =>
{
_textField.Text = reader.ReadToEnd();
}
if (dlg.ModalResult != (int) Graphics2D.UI.Window.DefaultModalResult.Ok)
{
return;
}

FilePath = _filePath;
Dirty = false;
var filePath = dlg.FilePath;
if (string.IsNullOrEmpty(filePath))
{
return;
}

_textField.Text = File.ReadAllText(filePath);
FilePath = filePath;
Dirty = false;
};

dlg.ShowModal(_host);
}

private void NewItemOnDown(object sender, EventArgs eventArgs)
Expand Down

0 comments on commit 0797671

Please sign in to comment.