Skip to content

Commit

Permalink
Add a clear button to the file selector
Browse files Browse the repository at this point in the history
  • Loading branch information
kythyria committed Jul 18, 2024
1 parent 498f93c commit e798a66
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 99 deletions.
25 changes: 25 additions & 0 deletions Backup/PD2ModelParser.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PD2ModelParser", "PD2ModelParser\PD2ModelParser.csproj", "{C73A2D85-EFB5-43F3-9637-378A0C478A83}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C73A2D85-EFB5-43F3-9637-378A0C478A83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C73A2D85-EFB5-43F3-9637-378A0C478A83}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C73A2D85-EFB5-43F3-9637-378A0C478A83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C73A2D85-EFB5-43F3-9637-378A0C478A83}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {74EE267C-4976-41E9-BFEC-478C736742C4}
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions PD2ModelParser.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
# Visual Studio Version 17
VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PD2ModelParser", "PD2ModelParser\PD2ModelParser.csproj", "{C73A2D85-EFB5-43F3-9637-378A0C478A83}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PD2ModelParser", "PD2ModelParser\PD2ModelParser.csproj", "{C73A2D85-EFB5-43F3-9637-378A0C478A83}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
1 change: 1 addition & 0 deletions PD2ModelParser/PD2ModelParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
<PreBuildEvent Condition="'$(OS)' != 'Windows_NT' ">"$(SolutionDir)gen-version.sh" "$(ConfigurationName)"</PreBuildEvent>
<ApplicationIcon />
<StartupObject />
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
</Project>
73 changes: 44 additions & 29 deletions PD2ModelParser/UI/FileBrowserControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 82 additions & 42 deletions PD2ModelParser/UI/FileBrowserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
namespace PD2ModelParser.UI {
[DefaultEvent("FileSelected")]
[Designer(typeof(FileBrowserDesigner))]
public partial class FileBrowserControl : UserControl {
public partial class FileBrowserControl : UserControl
{
// See https://stackoverflow.com/a/8531166
private static Regex filterRegex = new Regex(@"(?<Name>[^|]*)\|(?<Extension>[^|]*)\|?");

public FileBrowserControl() {
public FileBrowserControl()
{
InitializeComponent();

AllowDrop = true;
Expand All @@ -35,11 +37,14 @@ public FileBrowserControl() {

private bool _saveMode;

public bool SaveMode {
get {
public bool SaveMode
{
get
{
return _saveMode;
}
set {
set
{
_saveMode = value;

// If we're in save mode, the user can edit the output box
Expand All @@ -51,44 +56,59 @@ public bool SaveMode {

public string Filter { get; set; }

public string Selected {
get {
if (AllSelected.Count > 0) {
public string Selected
{
get
{
if (AllSelected.Count > 0)
{
return AllSelected[0];
}
return null;
}
set {
set
{
AllSelected.Clear();
AllSelected.Add(value);
}
}

private List<string> _allSelected = new List<string>();
public List<string> AllSelected {
get {
public List<string> AllSelected
{
get
{
string text = inputFileBox.Text;
if (text == "") {
if (text == "")
{
_allSelected.Clear();
return _allSelected;
}

if (SaveMode) {
if (SaveMode)
{
DirectoryInfo info = Directory.GetParent(text);
if (!info.Exists) {
if (!info.Exists)
{
_allSelected.Clear();
return _allSelected;
}
} else {
if (!MultiFile && !File.Exists(text)) {
}
else
{
if (!MultiFile && !File.Exists(text))
{
_allSelected.Clear();
return _allSelected;
}

if (MultiFile) {
if (MultiFile)
{
_allSelected = inputFileBox.Text.Split(';').ToList();
foreach (string filepath in _allSelected) {
if (!File.Exists(filepath)) {
foreach (string filepath in _allSelected)
{
if (!File.Exists(filepath))
{
_allSelected.Clear();
return _allSelected;
}
Expand All @@ -98,8 +118,10 @@ public List<string> AllSelected {

return _allSelected;
}
private set {
if (value == null) {
private set
{
if (value == null)
{
value = new List<string>();
}

Expand All @@ -109,15 +131,20 @@ private set {
}
}

private void browseBttn_Click(object sender, EventArgs e) {
private void browseBttn_Click(object sender, EventArgs e)
{
FileDialog fileDialog;
if (SaveMode) {
if (SaveMode)
{
fileDialog = new SaveFileDialog();
} else {
}
else
{
fileDialog = new OpenFileDialog();
fileDialog.CheckFileExists = true;

if (MultiFile) {
if (MultiFile)
{
((OpenFileDialog)fileDialog).Multiselect = true;
}
}
Expand All @@ -129,7 +156,8 @@ private void browseBttn_Click(object sender, EventArgs e) {
AllSelected = fileDialog.FileNames.ToList();
}

private void HandleDragEnter(object sender, DragEventArgs e) {
private void HandleDragEnter(object sender, DragEventArgs e)
{
// Can't drag a file into a save box, since I can't be bothered to
// write the 'are you sure you want to overwrite this file?' code.
if (SaveMode)
Expand All @@ -144,8 +172,8 @@ private void HandleDragEnter(object sender, DragEventArgs e) {

if (!MultiFile && files.Length != 1)
return;
if(!files.All(File.Exists)) { return; }

if (!files.All(File.Exists)) { return; }

// Check the file against the set filters.
//
Expand All @@ -158,18 +186,20 @@ private void HandleDragEnter(object sender, DragEventArgs e) {
.Select(ext => ext.Length == 2 ? ext[1] : "")
.Where(ext => ext != "")
.ToList();

var success = extensions.Contains("*");
success |= files.Select(f => f.Split('.'))
.All(p => p.Length >= 2 && extensions.Contains(p.Last()));

if(success) {

if (success)
{
// We are effectively copying the file in
e.Effect = DragDropEffects.Copy;
}
}

private void HandleDragDrop(object sender, DragEventArgs e) {
private void HandleDragDrop(object sender, DragEventArgs e)
{
// This is only called if we set the effect in HandleDragEnter, so
// this won't be called if the user is dragging in a directory or anything.
String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);
Expand All @@ -180,23 +210,30 @@ private void HandleDragDrop(object sender, DragEventArgs e) {
AllSelected = files.ToList();
}

private void ClearFileSelected(object sender, EventArgs e) {
AllSelected = null;
private void ClearFileSelected(object sender, EventArgs e)
{
AllSelected.Clear();
this.inputFileBox.Clear();
}

protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, Math.Max(inputFileBox.Height, browseBttn.Height), specified);
}

class FileBrowserDesigner : ControlDesigner {
public FileBrowserDesigner() {
class FileBrowserDesigner : ControlDesigner
{
public FileBrowserDesigner()
{
base.AutoResizeHandles = true;
}

public override SelectionRules SelectionRules => SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable;

public override IList SnapLines {
get {
public override IList SnapLines
{
get
{
// https://stackoverflow.com/questions/93541/baseline-snaplines-in-custom-winforms-controls
// This isn't pretty, but it works.

Expand All @@ -207,12 +244,15 @@ public override IList SnapLines {
var designer = TypeDescriptor.CreateDesigner(fbc.inputFileBox, typeof(IDesigner));
designer.Initialize(fbc.inputFileBox);

using (designer) {
using (designer)
{
var boxDesigner = designer as ControlDesigner;
if (boxDesigner == null) { return snaplines; }

foreach (SnapLine i in boxDesigner.SnapLines) {
if (i.SnapLineType == SnapLineType.Baseline) {
foreach (SnapLine i in boxDesigner.SnapLines)
{
if (i.SnapLineType == SnapLineType.Baseline)
{
snaplines.Add(new SnapLine(SnapLineType.Baseline, i.Offset + fbc.inputFileBox.Top, i.Filter, i.Priority));
}
}
Expand Down
Loading

0 comments on commit e798a66

Please sign in to comment.