Skip to content
Open
76 changes: 68 additions & 8 deletions source/MaterialXGraphEditor/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ Graph::Graph(const std::string& materialFilename,
const mx::FileSearchPath& searchPath,
const mx::FilePathVec& libraryFolders,
int viewWidth,
int viewHeight) :
int viewHeight,
float previewWidth) :
_materialFilename(materialFilename),
_searchPath(searchPath),
_libraryFolders(libraryFolders),
Expand All @@ -143,6 +144,7 @@ Graph::Graph(const std::string& materialFilename,
_autoLayout(false),
_frameCount(INT_MIN),
_fontScale(1.0f),
_previewSize(previewWidth),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A size of 0 will turn on previews.

_saveNodePositions(true)
{
loadStandardLibraries();
Expand Down Expand Up @@ -903,12 +905,60 @@ void Graph::showPropertyEditorValue(UiNodePtr node, mx::InputPtr input, const mx
}
else if (input->getType() == "filename")
{
mx::ValuePtr val = input->getValue();
mx::ValuePtr val = input->getResolvedValue();

if (val && val->isA<std::string>())
{
std::string prev, temp;
prev = temp = val->asA<std::string>();
mx::FilePath filePath(temp);

bool drawPreview = _previewSize > 0;
if (drawPreview)
{
float previewSize = _previewSize;
// Clamp preview size to width of panel
float panelWidth = ImGui::GetContentRegionAvail().x;
if (previewSize > panelWidth)
{
previewSize = panelWidth;
}

ImGui::BeginChild("imagePreview", ImVec2(previewSize, previewSize), false,
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);

// Show image preview if file exists and is an image
if (!temp.empty())
{
mx::ImageHandlerPtr imageHandler = _renderer ? _renderer->getImageHandler() : nullptr;
if (imageHandler)
{
unsigned int textureId = 0;
int width = 0, height = 0;
mx::ImagePtr image = imageHandler->acquireImage(filePath);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will reusage images from the handler's cache if found.

if (image)
{
textureId = image->getResourceId();
width = image->getWidth();
height = image->getHeight();
}
else
{
std::cout << "Image file not loaded: " << temp << std::endl;
}
if (textureId)
{
float aspect = (height > 0) ? (float)width / (float)height : 1.0f;
ImVec2 imagePreviewSize(previewSize, previewSize / aspect);

ImGui::Image((void*)(intptr_t)textureId, imagePreviewSize);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just preview scaling not actual image pixels resizing.

}
}
}
ImGui::EndChild();
//ImGui::PopItemWidth();
}

ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(.15f, .15f, .15f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(.2f, .4f, .6f, 1.0f));

Expand All @@ -923,7 +973,8 @@ void Graph::showPropertyEditorValue(UiNodePtr node, mx::InputPtr input, const mx
}
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::Text("%s", mx::FilePath(temp).getBaseName().c_str());
ImGui::Text("%s", filePath.getBaseName().c_str());

ImGui::PopStyleColor();
ImGui::PopStyleColor();

Expand Down Expand Up @@ -3382,23 +3433,32 @@ void Graph::propertyEditor()
{
_currUiNode->setShowAllInputs(showAllInputs);
}
bool showOutputsInEditor = _currUiNode->getShowOutputsInEditor();
if (ImGui::Checkbox("Show output connections", &showOutputsInEditor))

int count = 0;
float totalImagePadding = 0.0f;
float imagePadding = 0.0f;
if (_previewSize > 0.0f)
{
_currUiNode->setShowOutputsInEditor(showOutputsInEditor);
imagePadding = (_previewSize > availableWidth) ? availableWidth : _previewSize;
}

int count = 0;
for (UiPinPtr input : _currUiNode->getInputPins())
{
if (_currUiNode->getShowAllInputs() || (input->getConnected() || _currUiNode->getNode()->getInput(input->getName())))
{
count++;

// Add space for image previews
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are using a fixed height, we need to take into consideration space for image previews.

if (imagePadding > 0.0f && input->getInput()->getType() == "filename")
{
totalImagePadding += imagePadding;
}
}
}
if (count)
{
ImVec2 tableSize(0.0f, TEXT_BASE_HEIGHT * std::min(SCROLL_LINE_COUNT, count));
float baseHeight = TEXT_BASE_HEIGHT * std::min(SCROLL_LINE_COUNT, count);
ImVec2 tableSize(0.0f, baseHeight + totalImagePadding);
bool haveTable = ImGui::BeginTable("inputs_node_table", 2, tableFlags, tableSize);
if (haveTable)
{
Expand Down
6 changes: 5 additions & 1 deletion source/MaterialXGraphEditor/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class Graph
const mx::FileSearchPath& searchPath,
const mx::FilePathVec& libraryFolders,
int viewWidth,
int viewHeight);
int viewHeight,
float previewWidth);
~Graph() = default;

mx::DocumentPtr loadDocument(const mx::FilePath& filename);
Expand Down Expand Up @@ -344,6 +345,9 @@ class Graph
// Layout engine
Layout _layout;

// Preview area size
float _previewSize;

// Options
bool _saveNodePositions;
};
Expand Down
9 changes: 8 additions & 1 deletion source/MaterialXGraphEditor/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const std::string options =
" --font [FILENAME] Specify the name of the custom font file to use. If not specified the default font will be used.\n"
" --fontSize [SIZE] Specify font size to use for the custom font. If not specified a default of 18 will be used.\n"
" --captureFilename [FILENAME] Specify the filename to which the first rendered frame should be written\n"
" --previewWidth [WIDTH] Specify the width for image previews\n"
" --help Display the complete list of command-line options\n";

template <class T> void parseToken(std::string token, std::string type, T& res)
Expand Down Expand Up @@ -72,6 +73,7 @@ int main(int argc, char* const argv[])
float uiScale = 0.0f;
std::string fontFilename;
int fontSize = 18;
float previewWidth = 256.0f;
std::string captureFilename;

for (size_t i = 0; i < tokens.size(); i++)
Expand Down Expand Up @@ -115,6 +117,10 @@ int main(int argc, char* const argv[])
{
parseToken(nextToken, "integer", fontSize);
}
else if (token == "--previewWidth")
{
parseToken(nextToken, "float", previewWidth);
}
else if (token == "--captureFilename")
{
parseToken(nextToken, "string", captureFilename);
Expand Down Expand Up @@ -224,7 +230,8 @@ int main(int argc, char* const argv[])
searchPath,
libraryFolders,
viewWidth,
viewHeight);
viewHeight,
previewWidth);
if (!captureFilename.empty())
{
graph->getRenderer()->requestFrameCapture(captureFilename);
Expand Down