Skip to content

Commit 3729004

Browse files
feat: file picker dialog
1 parent cb86bc6 commit 3729004

9 files changed

Lines changed: 377 additions & 279 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,16 @@ namespace FreeFrame.Components
1010
{
1111
public static class Importer
1212
{
13-
static public void Import()
14-
{
15-
//TODO: find a solution for opening a crossplatform file dialog. Do I need a crossplatform dialog?
16-
//using (OpenFileDialog openFileDialog = new OpenFileDialog())
17-
//{
18-
//}
19-
}
20-
static public List<Shape> ImportFromStream(Stream pStream)
13+
static public (List<Shape>, bool) ImportFromStream(Stream pStream)
2114
{
2215
List<Shape> shapes = new List<Shape>();
16+
bool compatibilityFlag = false;
2317

2418
using (XmlReader reader = XmlReader.Create(pStream))
2519
{
2620
while (reader.Read())
2721
{
28-
if (reader.HasAttributes)
22+
//if (reader.HasAttributes)
2923
{
3024
//Console.WriteLine("Attributes of <" + reader.Name + ">");
3125
switch (reader.Name)
@@ -46,15 +40,15 @@ static public List<Shape> ImportFromStream(Stream pStream)
4640
shapes.Add(new SVGCircle(reader));
4741
break;
4842
default:
49-
// TODO: show all the elemnt are not valid, be careful
43+
compatibilityFlag = true; // If an element is unknow, the flag is trigger
5044
break;
5145
}
5246
}
5347
}
5448
}
55-
return (shapes);
49+
return (shapes, compatibilityFlag);
5650
}
57-
static public List<Shape> ImportFromFile(string pFilename)
51+
static public (List<Shape>, bool) ImportFromFile(string pFilename)
5852
{
5953
if (!File.Exists(pFilename))
6054
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
@@ -63,7 +57,7 @@ static public List<Shape> ImportFromFile(string pFilename)
6357

6458
return ImportFromStream(new MemoryStream(byteArray));
6559
}
66-
static public List<Shape> ImportFromString(string pString)
60+
static public (List<Shape>, bool) ImportFromString(string pString)
6761
{
6862
byte[] byteArray = Encoding.UTF8.GetBytes(pString);
6963

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
3939
_height = height;
4040
_rx = rx;
4141
_ry = ry;
42-
43-
UpdateProperties();
4442
}
4543
public override void UpdateProperties()
4644
{

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public abstract class Shape
1818
private int _indexCount;
1919

2020
public Shape()
21+
{
22+
23+
}
24+
public void GenerateObjects()
2125
{
2226
_vertexArrayObject = GL.GenVertexArray();
2327
_vertexBufferObject = GL.GenBuffer();
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* Original Author: prime31
3+
* Source: https://gist.github.com/prime31/91d1582624eb2635395417393018016e
4+
*/
5+
using ImGuiNET;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using Num = System.Numerics;
10+
11+
namespace FreeFrame.Lib.FilePicker
12+
{
13+
public class FilePicker
14+
{
15+
static readonly Dictionary<object, FilePicker> _filePickers = new Dictionary<object, FilePicker>();
16+
17+
public string RootFolder;
18+
public string CurrentFolder;
19+
public string SelectedFile;
20+
public List<string> AllowedExtensions;
21+
public bool OnlyAllowFolders;
22+
23+
public static FilePicker GetFolderPicker(object o, string startingPath)
24+
=> GetFilePicker(o, startingPath, null, true);
25+
26+
public static FilePicker GetFilePicker(object o, string startingPath, string searchFilter = null, bool onlyAllowFolders = false)
27+
{
28+
if (File.Exists(startingPath))
29+
{
30+
startingPath = new FileInfo(startingPath).DirectoryName;
31+
}
32+
else if (string.IsNullOrEmpty(startingPath) || !Directory.Exists(startingPath))
33+
{
34+
startingPath = Environment.CurrentDirectory;
35+
if (string.IsNullOrEmpty(startingPath))
36+
startingPath = AppContext.BaseDirectory;
37+
}
38+
39+
if (!_filePickers.TryGetValue(o, out FilePicker fp))
40+
{
41+
fp = new FilePicker
42+
{
43+
RootFolder = startingPath,
44+
CurrentFolder = startingPath,
45+
OnlyAllowFolders = onlyAllowFolders
46+
};
47+
48+
if (searchFilter != null)
49+
{
50+
if (fp.AllowedExtensions != null)
51+
fp.AllowedExtensions.Clear();
52+
else
53+
fp.AllowedExtensions = new List<string>();
54+
55+
fp.AllowedExtensions.AddRange(searchFilter.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
56+
}
57+
58+
_filePickers.Add(o, fp);
59+
}
60+
61+
return fp;
62+
}
63+
64+
public static void RemoveFilePicker(object o) => _filePickers.Remove(o);
65+
66+
public bool Draw()
67+
{
68+
if (CurrentFolder.Length >= 50)
69+
ImGui.Text("Current Folder: " + CurrentFolder.Replace(CurrentFolder[10..(CurrentFolder.Length - 40)], "..."));
70+
else
71+
ImGui.Text("Current Folder: " + CurrentFolder);
72+
bool result = false;
73+
74+
if (ImGui.BeginChildFrame(1, new Num.Vector2(400, 400)))
75+
{
76+
var di = new DirectoryInfo(CurrentFolder);
77+
if (di.Exists)
78+
{
79+
if (di.Parent != null) // && CurrentFolder != RootFolder
80+
if (ImGui.Selectable("../", false, ImGuiSelectableFlags.DontClosePopups))
81+
CurrentFolder = di.Parent.FullName;
82+
83+
var fileSystemEntries = GetFileSystemEntries(di.FullName);
84+
foreach (var fse in fileSystemEntries)
85+
{
86+
if (Directory.Exists(fse))
87+
{
88+
var name = Path.GetFileName(fse);
89+
ImGui.PushStyleColor(ImGuiCol.Text, new Num.Vector4(0, 125, 255, 255));
90+
if (ImGui.Selectable($"[D] {name}/", false, ImGuiSelectableFlags.DontClosePopups))
91+
CurrentFolder = fse;
92+
ImGui.PopStyleColor();
93+
}
94+
else
95+
{
96+
var name = Path.GetFileName(fse);
97+
bool isSelected = SelectedFile == fse;
98+
if (ImGui.Selectable($"[F] {name}", isSelected, ImGuiSelectableFlags.DontClosePopups))
99+
SelectedFile = fse;
100+
101+
if (ImGui.IsMouseDoubleClicked(0))
102+
{
103+
result = true;
104+
ImGui.CloseCurrentPopup();
105+
}
106+
}
107+
}
108+
}
109+
}
110+
ImGui.EndChildFrame();
111+
112+
113+
if (ImGui.Button("Cancel"))
114+
{
115+
result = false;
116+
ImGui.CloseCurrentPopup();
117+
}
118+
119+
if (OnlyAllowFolders)
120+
{
121+
ImGui.SameLine();
122+
if (ImGui.Button("Open"))
123+
{
124+
result = true;
125+
SelectedFile = CurrentFolder;
126+
ImGui.CloseCurrentPopup();
127+
}
128+
}
129+
else if (SelectedFile != null)
130+
{
131+
ImGui.SameLine();
132+
if (ImGui.Button("Open"))
133+
{
134+
result = true;
135+
ImGui.CloseCurrentPopup();
136+
}
137+
}
138+
139+
return result;
140+
}
141+
List<string> GetFileSystemEntries(string fullName)
142+
{
143+
var files = new List<string>();
144+
var dirs = new List<string>();
145+
146+
foreach (var fse in Directory.GetFileSystemEntries(fullName, ""))
147+
{
148+
if (Directory.Exists(fse))
149+
{
150+
dirs.Add(fse);
151+
}
152+
else if (!OnlyAllowFolders)
153+
{
154+
if (AllowedExtensions != null)
155+
{
156+
if (AllowedExtensions.Contains(Path.GetExtension(fse)))
157+
files.Add(fse);
158+
}
159+
else
160+
files.Add(fse);
161+
}
162+
}
163+
164+
var ret = new List<string>(dirs);
165+
ret.AddRange(files);
166+
167+
return ret;
168+
}
169+
170+
}
171+
}

FreeFrame/Object.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

FreeFrame/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ static void Main()
1919
};
2020
using Window window = new(GameWindowSettings.Default, nativeWindowSettings); // Create window context (GLFW, OpenGL)
2121

22-
23-
2422
window.Run();
2523

2624
}

0 commit comments

Comments
 (0)