Skip to content

Commit

Permalink
Improvements to file I/O options in graph editor (AcademySoftwareFoun…
Browse files Browse the repository at this point in the history
  • Loading branch information
kwokcb authored Aug 31, 2023
1 parent 1027afb commit 34a27d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
47 changes: 36 additions & 11 deletions source/MaterialXGraphEditor/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ mx::DocumentPtr Graph::loadDocument(mx::FilePath filename)
std::cerr << "*** Validation warnings for " << filename.asString() << " ***" << std::endl;
std::cerr << message << std::endl;
}

// Cache the currently loaded file
_materialFilename = filename;
}
}
catch (mx::Exception& e)
Expand Down Expand Up @@ -2961,7 +2964,7 @@ void Graph::clearGraph()
_renderer->updateMaterials(nullptr);
}

void Graph::loadGraphFromFile()
void Graph::loadGraphFromFile(bool prompt)
{
// deselect node before loading new file
if (_currUiNode != nullptr)
Expand All @@ -2970,9 +2973,25 @@ void Graph::loadGraphFromFile()
_currUiNode = nullptr;
}

_fileDialog.setTitle("Open File");
_fileDialog.setTypeFilters(_mtlxFilter);
_fileDialog.open();
if (prompt || _materialFilename.isEmpty())
{
_fileDialog.setTitle("Open File");
_fileDialog.setTypeFilters(_mtlxFilter);
_fileDialog.open();
}
else
{
_graphDoc = loadDocument(_materialFilename);

// Rebuild the UI
_initial = true;
buildUiBaseGraph(_graphDoc);
_currGraphElem = _graphDoc;
_prevUiNode = nullptr;

_renderer->setDocument(_graphDoc);
_renderer->updateMaterials(nullptr);
}
}

void Graph::saveGraphToFile()
Expand Down Expand Up @@ -3006,7 +3025,11 @@ void Graph::graphButtons()
}
else if (ImGui::MenuItem("Open", "Ctrl-O"))
{
loadGraphFromFile();
loadGraphFromFile(true);
}
else if (ImGui::MenuItem("Reload", "Ctrl-R"))
{
loadGraphFromFile(false);
}
else if (ImGui::MenuItem("Save", "Ctrl-S"))
{
Expand Down Expand Up @@ -3052,12 +3075,16 @@ void Graph::graphButtons()
{
if (ImGui::IsKeyReleased(ImGuiKey_O))
{
loadGraphFromFile();
loadGraphFromFile(true);
}
else if (ImGui::IsKeyReleased(ImGuiKey_N))
{
clearGraph();
}
else if (ImGui::IsKeyReleased(ImGuiKey_R))
{
loadGraphFromFile(false);
}
else if (ImGui::IsKeyReleased(ImGuiKey_S))
{
saveGraphToFile();
Expand Down Expand Up @@ -4094,19 +4121,17 @@ void Graph::drawGraph(ImVec2 mousePos)
// saving file
if (_fileDialogSave.hasSelected())
{

std::string message;
if (!_graphDoc->validate(&message))
{
std::cerr << "*** Validation warnings for " << _materialFilename.getBaseName() << " ***" << std::endl;
std::cerr << message;
}
std::string fileName = _fileDialogSave.getSelected();
mx::FilePath name = _fileDialogSave.getSelected();
_materialFilename = _fileDialogSave.getSelected();
ed::Resume();
savePosition();

writeText(fileName, name);
saveDocument(_materialFilename);
_fileDialogSave.clearSelected();
}
else
Expand Down Expand Up @@ -4257,7 +4282,7 @@ void Graph::savePosition()
}
}
}
void Graph::writeText(std::string fileName, mx::FilePath filePath)
void Graph::saveDocument(mx::FilePath filePath)
{
if (filePath.getExtension() != mx::MTLX_EXTENSION)
{
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXGraphEditor/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Graph
void createEdge(UiNodePtr upNode, UiNodePtr downNode, mx::InputPtr connectingInput);
void removeEdge(int downNode, int upNode, UiPinPtr pin);

void writeText(std::string filename, mx::FilePath filePath);
void saveDocument(mx::FilePath filePath);
void savePosition();
bool checkPosition(UiNodePtr node);

Expand Down Expand Up @@ -148,7 +148,7 @@ class Graph

// File I/O
void clearGraph();
void loadGraphFromFile();
void loadGraphFromFile(bool prompt);
void saveGraphToFile();
void loadGeometry();

Expand Down

0 comments on commit 34a27d0

Please sign in to comment.