Skip to content

Commit

Permalink
Traktor: Able to edit Scalar nodes inline in shader graph editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed May 29, 2024
1 parent bce1715 commit d8bc87a
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
147 changes: 147 additions & 0 deletions code/Render/Editor/Shader/Facades/ScalarNodeFacade.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* TRAKTOR
* Copyright (c) 2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Core/Misc/String.h"
#include "I18N/Text.h"
#include "Render/Editor/InputPin.h"
#include "Render/Editor/OutputPin.h"
#include "Render/Editor/Shader/Nodes.h"
#include "Render/Editor/Shader/Facades/ScalarNodeFacade.h"
#include "Ui/Application.h"
#include "Ui/Edit.h"
#include "Ui/NumericEditValidator.h"
#include "Ui/Graph/GraphControl.h"
#include "Ui/Graph/Node.h"
#include "Ui/Graph/InputNodeShape.h"

namespace traktor::render
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.render.ScalarNodeFacade", ScalarNodeFacade, INodeFacade)

ScalarNodeFacade::ScalarNodeFacade()
{
m_nodeShape = new ui::InputNodeShape();
}

Ref< Node > ScalarNodeFacade::createShaderNode(
const TypeInfo* nodeType,
editor::IEditor* editor
)
{
return new Scalar();
}

Ref< ui::Node > ScalarNodeFacade::createEditorNode(
editor::IEditor* editor,
ui::GraphControl* graphControl,
ShaderGraph* shaderGraph,
Node* shaderNode
)
{
Ref< ui::Node > editorNode = graphControl->createNode(
i18n::Text(L"SHADERGRAPH_NODE_SCALAR"),
shaderNode->getInformation(),
ui::UnitPoint(
ui::Unit(shaderNode->getPosition().first),
ui::Unit(shaderNode->getPosition().second)
),
m_nodeShape
);

for (int32_t j = 0; j < shaderNode->getOutputPinCount(); ++j)
{
const OutputPin* outputPin = shaderNode->getOutputPin(j);
editorNode->createOutputPin(
outputPin->getName(),
outputPin->getId()
);
}

editorNode->setComment(shaderNode->getComment());

return editorNode;
}

void ScalarNodeFacade::editShaderNode(
editor::IEditor* editor,
ui::GraphControl* graphControl,
ui::Node* editorNode,
ShaderGraph* shaderGraph,
Node* shaderNode
)
{
const ui::Rect rcEditVirtual = graphControl->pixel(editorNode->calculateRect());
const ui::Rect rcEdit(
graphControl->virtualToClient(rcEditVirtual.getTopLeft()),
graphControl->virtualToClient(rcEditVirtual.getBottomRight())
);

m_editEditorNode = editorNode;
m_editShaderNode = mandatory_non_null_type_cast< Scalar* >(shaderNode);

if (m_edit == nullptr)
{
m_edit = new ui::Edit();
m_edit->create(graphControl, L"", ui::WsNone, new ui::NumericEditValidator(true));
m_edit->addEventHandler< ui::FocusEvent >(
[this](ui::FocusEvent* event)
{
if (m_edit->isVisible(false) && event->lostFocus())
{
m_editEditorNode->setInfo(m_edit->getText());
m_editShaderNode->set(parseString< float >(m_edit->getText()));
m_edit->setVisible(false);
}
}
);
m_edit->addEventHandler< ui::KeyDownEvent >(
[this](ui::KeyDownEvent* event)
{
if (event->getVirtualKey() == ui::VkReturn)
{
m_editEditorNode->setInfo(m_edit->getText());
m_editShaderNode->set(parseString< float >(m_edit->getText()));
m_edit->setVisible(false);
}
else if (event->getVirtualKey() == ui::VkEscape)
{
m_edit->setVisible(false);
}
}
);
}

m_edit->setText(toString< float >(m_editShaderNode->get()));
m_edit->setRect(rcEdit);
m_edit->setVisible(true);
m_edit->selectAll();
m_edit->setFocus();
}

void ScalarNodeFacade::refreshEditorNode(
editor::IEditor* editor,
ui::GraphControl* graphControl,
ui::Node* editorNode,
ShaderGraph* shaderGraph,
Node* shaderNode
)
{
editorNode->setComment(shaderNode->getComment());
editorNode->setInfo(shaderNode->getInformation());
}

void ScalarNodeFacade::setValidationIndicator(
ui::Node* editorNode,
bool validationSucceeded
)
{
editorNode->setState(validationSucceeded ? 0 : 1);
}

}
73 changes: 73 additions & 0 deletions code/Render/Editor/Shader/Facades/ScalarNodeFacade.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* TRAKTOR
* Copyright (c) 2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once

#include "Render/Editor/Shader/INodeFacade.h"

namespace traktor::ui
{

class Edit;
class INodeShape;

}

namespace traktor::render
{

class Scalar;

class ScalarNodeFacade : public INodeFacade
{
T_RTTI_CLASS;

public:
ScalarNodeFacade();

virtual Ref< Node > createShaderNode(
const TypeInfo* nodeType,
editor::IEditor* editor
) override final;

virtual Ref< ui::Node > createEditorNode(
editor::IEditor* editor,
ui::GraphControl* graphControl,
ShaderGraph* shaderGraph,
Node* shaderNode
) override final;

virtual void editShaderNode(
editor::IEditor* editor,
ui::GraphControl* graphControl,
ui::Node* editorNode,
ShaderGraph* shaderGraph,
Node* shaderNode
) override final;

virtual void refreshEditorNode(
editor::IEditor* editor,
ui::GraphControl* graphControl,
ui::Node* editorNode,
ShaderGraph* shaderGraph,
Node* shaderNode
) override final;

virtual void setValidationIndicator(
ui::Node* editorNode,
bool validationSucceeded
) override final;

private:
Ref<ui::INodeShape > m_nodeShape;
Ref< ui::Edit > m_edit;
ui::Node* m_editEditorNode = nullptr;
Scalar* m_editShaderNode = nullptr;
};

}
2 changes: 2 additions & 0 deletions code/Render/Editor/Shader/ShaderGraphEditorPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "Render/Editor/Shader/Facades/CommentNodeFacade.h"
#include "Render/Editor/Shader/Facades/ExternalNodeFacade.h"
#include "Render/Editor/Shader/Facades/InterpolatorNodeFacade.h"
#include "Render/Editor/Shader/Facades/ScalarNodeFacade.h"
#include "Render/Editor/Shader/Facades/ScriptNodeFacade.h"
#include "Render/Editor/Shader/Facades/SwizzleNodeFacade.h"
#include "Render/Editor/Shader/Facades/TextureNodeFacade.h"
Expand Down Expand Up @@ -477,6 +478,7 @@ bool ShaderGraphEditorPage::create(ui::Container* parent)
m_nodeFacades[&type_of< Comment >()] = new CommentNodeFacade();
m_nodeFacades[&type_of< External >()] = new ExternalNodeFacade();
m_nodeFacades[&type_of< Interpolator >()] = new InterpolatorNodeFacade();
m_nodeFacades[&type_of< Scalar >()] = new ScalarNodeFacade();
m_nodeFacades[&type_of< Script >()] = new ScriptNodeFacade(this);
m_nodeFacades[&type_of< Swizzle >()] = new SwizzleNodeFacade();
m_nodeFacades[&type_of< Texture >()] = new TextureNodeFacade();
Expand Down

0 comments on commit d8bc87a

Please sign in to comment.