Skip to content

Commit

Permalink
File Dialog taking shape.
Browse files Browse the repository at this point in the history
  • Loading branch information
se5a committed Jun 1, 2024
1 parent 853229f commit 292572c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 54 deletions.
58 changes: 11 additions & 47 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public abstract class BluePrintsUI
private protected string[] _guiHints;

private protected Vector2 _childSize = new Vector2(640, 200);

private protected bool _showFileDialog = false;
private protected string _fileDialogPath = "";

protected BluePrintsUI(ModDataStore modDataStore)
{
_modDataStore = modDataStore;
Expand Down Expand Up @@ -61,9 +65,6 @@ protected BluePrintsUI(ModDataStore modDataStore)

public abstract void Refresh();

public abstract void SaveAs(string path);


public void Display(string label)
{
int i = 0;
Expand All @@ -73,8 +74,7 @@ public void Display(string label)
ImGui.SameLine();
if (ImGui.Button("SaveAs"))
{
SaveAs("foo");
//FileDialog.Display();
_showFileDialog = true;
}
ImGui.SameLine();
ImGui.Button("SaveToMemory");
Expand Down Expand Up @@ -108,6 +108,11 @@ public void Display(string label)
ImGui.EndChild();
ImGui.TreePop();
}

if (_showFileDialog)
{
FileDialog.Display(ref _fileDialogPath, ref _showFileDialog);
}
}

public abstract void DisplayEditorWindow(int index);
Expand Down Expand Up @@ -170,23 +175,7 @@ public sealed override void Refresh()
newEmpty.Name = "New Blueprint";
_newEmpty = newEmpty;
}

public override void SaveAs(string path)
{

path = "Data/basemod";
FileDialog.Display(path);
/*
using (StreamWriter outputFile = new StreamWriter(Path.Combine(path, "tcb.json")))
{
foreach (TechCategoryBlueprint blueprint in _itemBlueprints)
{
var json = JsonConvert.SerializeObject(blueprint, Formatting.Indented);
outputFile.WriteLine(json);
}
}
*/
}


public override void DisplayEditorWindow(int selectedIndex)
{
Expand Down Expand Up @@ -245,11 +234,6 @@ public override void Refresh()
_newEmpty = newEmpty;
}

public override void SaveAs(string path)
{
throw new NotImplementedException();
}

public override void DisplayEditorWindow(int selectedIndex)
{

Expand Down Expand Up @@ -366,10 +350,6 @@ public sealed override void Refresh()
_newEmpty = newEmpty;
}

public override void SaveAs(string path)
{
throw new NotImplementedException();
}

public override void DisplayEditorWindow(int selectedIndex)
{
Expand Down Expand Up @@ -494,10 +474,6 @@ public sealed override void Refresh()
_newEmpty = newEmpty;
}

public override void SaveAs(string path)
{
throw new NotImplementedException();
}

public override void DisplayEditorWindow(int selectedIndex)
{
Expand Down Expand Up @@ -569,10 +545,6 @@ public sealed override void Refresh()
_newEmpty = newEmpty;
}

public override void SaveAs(string path)
{
throw new NotImplementedException();
}

public override void DisplayEditorWindow(int selectedIndex)
{
Expand Down Expand Up @@ -733,10 +705,6 @@ public override void Refresh()
_newEmpty = newEmpty;
}

public override void SaveAs(string path)
{
throw new NotImplementedException();
}

public override void DisplayEditorWindow(int selectedIndex)
{
Expand Down Expand Up @@ -869,10 +837,6 @@ public sealed override void Refresh()
}
}

public override void SaveAs(string path)
{
throw new NotImplementedException();
}

public void Display()
{
Expand Down
115 changes: 108 additions & 7 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.IO;
using ImGuiNET;
using ImGuiSDL2CS;

namespace Pulsar4X.SDL2UI.ModFileEditing;


public static class FileDialog
{
public enum SaveOrLoad
Expand All @@ -11,24 +15,62 @@ public enum SaveOrLoad
}
private static byte[] _strInputBuffer = new byte[128];
private static string _pathString;

private static string _curDir = Directory.GetCurrentDirectory();
private static int _selectedIndex = -1;
private static int _i = 0;
private static bool _b = false;
public static SaveOrLoad DialogType = SaveOrLoad.Save;

public static void Display(string path)

public static void Display(ref string path, ref bool IsActive)
{
_pathString = path;
ImGui.Begin("File Dialog");
if (string.IsNullOrEmpty(path))
_pathString = _curDir;
else
_pathString = path;

ImGui.Begin("File Dialog", ref IsActive);
ImGui.Text("Name:");
ImGui.SameLine();
ImGui.InputText("##Name", _strInputBuffer, 128);

ImGui.Columns(2);
ImGui.SetColumnWidth(0,128);


if (ImGui.Button("Docs"))
{
_pathString = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

}

if (ImGui.Button("Desktop"))
{
_pathString = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}

//this is Editor specific TODO: add a way to add specific dir to the LH colomn
if (ImGui.Button("Data/basemod"))
{
_pathString = "Data/basemod";
}

//this is Editor specific TODO: add a way to add specific dir to the LH colomn
if (ImGui.Button("GameEngine/Data/basemod"))
{
var dir = new DirectoryInfo(_curDir);
while (dir.Name != "Pulsar4X")
{
dir = Directory.GetParent(dir.FullName);
}
_pathString = Path.Combine(dir.FullName, "GameEngine/Data/basemod");
}

ImGui.NextColumn();

ImGui.BeginTable("", 4);
ImGui.NextColumn();

ImGui.BeginTable("table", 4);
ImGui.TableNextColumn();
ImGui.TableHeader("Name");
ImGui.TableNextColumn();
ImGui.TableHeader("Size");
Expand All @@ -38,15 +80,74 @@ public static void Display(string path)
ImGui.TableHeader("Modified");
ImGui.TableNextColumn();

var files = System.IO.Directory.EnumerateFiles(_pathString);
if (ImGui.Selectable("..", _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick))
{
if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
{
_pathString = Directory.GetParent(_pathString).FullName;
}
}

var dirs = Directory.EnumerateDirectories(_pathString);
_i = 0;
foreach (var dir in dirs)
{
DirectoryInfo fi = new DirectoryInfo(dir);

_b = _i == _selectedIndex;
if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick))
{
_selectedIndex = _i;
if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
{
_pathString = fi.FullName;
}
}

ImGui.TableNextColumn();

ImGui.Text("");
ImGui.TableNextColumn();

ImGui.Text(fi.Extension);
ImGui.TableNextColumn();

ImGui.Text(fi.LastWriteTime.ToString());
ImGui.TableNextColumn();
_i++;
}

var files = Directory.EnumerateFiles(_pathString);
foreach (var file in files)
{

FileInfo fi = new FileInfo(file);

_b = _i == _selectedIndex;
if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns))
{
_selectedIndex = _i;
_strInputBuffer = ImGuiSDL2CSHelper.BytesFromString(fi.Name);
}
_i++;
ImGui.TableNextColumn();

ImGui.Text(fi.Length.ToString());
ImGui.TableNextColumn();

ImGui.Text(fi.Extension);
ImGui.TableNextColumn();

ImGui.Text(fi.LastWriteTime.ToString());
ImGui.TableNextColumn();


}

ImGui.EndTable();


ImGui.End();
path = _pathString;
}
}

0 comments on commit 292572c

Please sign in to comment.