Skip to content

Commit

Permalink
More functionality on the FileDialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
se5a committed Jun 1, 2024
1 parent 292572c commit bf1a38a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 13 deletions.
54 changes: 51 additions & 3 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public abstract class BluePrintsUI

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

protected BluePrintsUI(ModDataStore modDataStore)
{
Expand Down Expand Up @@ -65,6 +66,8 @@ protected BluePrintsUI(ModDataStore modDataStore)

public abstract void Refresh();

protected abstract void Save();

public void Display(string label)
{
int i = 0;
Expand Down Expand Up @@ -111,7 +114,10 @@ public void Display(string label)

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

}
}
}

Expand Down Expand Up @@ -175,8 +181,20 @@ public sealed override void Refresh()
newEmpty.Name = "New Blueprint";
_newEmpty = newEmpty;
}



protected override void Save()
{
using (StreamWriter outputFile = new StreamWriter(Path.Combine(_fileDialogPath, _fileName)))
{
foreach (TechCategoryBlueprint blueprint in _itemBlueprints)
{
var json = JsonConvert.SerializeObject(blueprint, Formatting.Indented);
outputFile.WriteLine(json);
}
}
}


public override void DisplayEditorWindow(int selectedIndex)
{
if(!_isActive[selectedIndex])
Expand Down Expand Up @@ -234,6 +252,11 @@ public override void Refresh()
_newEmpty = newEmpty;
}

protected override void Save()
{
throw new NotImplementedException();
}

public override void DisplayEditorWindow(int selectedIndex)
{

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

protected override void Save()
{
throw new NotImplementedException();
}


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

protected override void Save()
{
throw new NotImplementedException();
}


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

protected override void Save()
{
throw new NotImplementedException();
}


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

protected override void Save()
{
throw new NotImplementedException();
}


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

protected override void Save()
{
throw new NotImplementedException();
}


public void Display()
{
Expand Down
53 changes: 43 additions & 10 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public enum SaveOrLoad
public static SaveOrLoad DialogType = SaveOrLoad.Save;


public static void Display(ref string path, ref bool IsActive)
public static bool Display(ref string path, ref string fileName, ref bool IsActive)
{
bool isok = false;
if (string.IsNullOrEmpty(path))
_pathString = _curDir;
else
Expand All @@ -32,7 +33,10 @@ public static void Display(ref string path, ref bool IsActive)
ImGui.Begin("File Dialog", ref IsActive);
ImGui.Text("Name:");
ImGui.SameLine();
ImGui.InputText("##Name", _strInputBuffer, 128);
if (ImGui.InputText("##Name", _strInputBuffer, 128))
{
fileName = ImGuiSDL2CSHelper.StringFromBytes(_strInputBuffer);
}

ImGui.Columns(2);
ImGui.SetColumnWidth(0,128);
Expand Down Expand Up @@ -92,15 +96,15 @@ public static void Display(ref string path, ref bool IsActive)
_i = 0;
foreach (var dir in dirs)
{
DirectoryInfo fi = new DirectoryInfo(dir);
DirectoryInfo di = new DirectoryInfo(dir);

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

Expand All @@ -109,10 +113,10 @@ public static void Display(ref string path, ref bool IsActive)
ImGui.Text("");
ImGui.TableNextColumn();

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

ImGui.Text(fi.LastWriteTime.ToString());
ImGui.Text(di.LastWriteTime.ToString());
ImGui.TableNextColumn();
_i++;
}
Expand All @@ -124,10 +128,17 @@ public static void Display(ref string path, ref bool IsActive)
FileInfo fi = new FileInfo(file);

_b = _i == _selectedIndex;
if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns))
if (ImGui.Selectable(fi.Name, _b, ImGuiSelectableFlags.SpanAllColumns | ImGuiSelectableFlags.AllowDoubleClick))
{
_selectedIndex = _i;
_strInputBuffer = ImGuiSDL2CSHelper.BytesFromString(fi.Name);
fileName = fi.Name;

if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
{
isok = true;
IsActive = false;
}
}
_i++;
ImGui.TableNextColumn();
Expand All @@ -141,13 +152,35 @@ public static void Display(ref string path, ref bool IsActive)
ImGui.Text(fi.LastWriteTime.ToString());
ImGui.TableNextColumn();


}

ImGui.EndTable();

ImGui.Columns(1);
if (DialogType == SaveOrLoad.Load)
{
if (ImGui.Button("Load"))
{
isok = true;
IsActive = false;
}
}
else
{
if (ImGui.Button("Save"))
{
isok = true;
IsActive = false;
}
}
ImGui.SameLine();
if (ImGui.Button("Cancel"))
{
IsActive = false;
isok = false;
}

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

0 comments on commit bf1a38a

Please sign in to comment.