Skip to content

Commit edff67c

Browse files
authored
Merge pull request #57 from PipeRift/feature/graph-context-improvements
Graph context menu improvements
2 parents 3dd2f69 + 156f0de commit edff67c

14 files changed

Lines changed: 366 additions & 273 deletions

File tree

CMake/LLVM.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ message(STATUS "LLVM_INCLUDE_DIRS: ${LLVM_INCLUDE_DIRS}")
3131
message(STATUS "LLVM_DEFINITIONS: ${LLVM_DEFINITIONS_LIST}")
3232

3333

34-
add_library(LLVM INTERFACE)
35-
target_include_directories(LLVM INTERFACE ${LLVM_INCLUDE_DIRS})
34+
add_library(RiftLLVM INTERFACE)
35+
target_include_directories(RiftLLVM INTERFACE ${LLVM_INCLUDE_DIRS})
3636
llvm_map_components_to_libnames(llvm_libs core x86asmparser x86codegen)
37-
target_link_libraries(LLVM INTERFACE ${LLVM_AVAILABLE_LIBS})
38-
target_compile_definitions(LLVM INTERFACE ${LLVM_DEFINITIONS_LIST} -DNOMINMAX)
37+
target_link_libraries(RiftLLVM INTERFACE ${LLVM_AVAILABLE_LIBS})
38+
target_compile_definitions(RiftLLVM INTERFACE ${LLVM_DEFINITIONS_LIST} -DNOMINMAX)
3939
# rift_target_disable_all_warnings(LLVM INTERFACE)
4040

Libs/Backends/LLVM/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rift_target_shared_output_directory(RiftBackendLLVM)
1212

1313
target_link_libraries(RiftBackendLLVM PUBLIC Rift::Rift)
1414

15-
target_link_libraries(RiftBackendLLVM PRIVATE LLVM)
15+
target_link_libraries(RiftBackendLLVM PRIVATE RiftLLVM)
1616

1717

1818
install(TARGETS RiftBackendLLVM

Libs/Editor/Include/Utils/FunctionGraph.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace Rift::Graph
3232

3333
float GetSpaceHeight(u32 height) const;
3434
v2 GetContentPadding() const;
35+
v2 GetGridPosition(v2 screenPosition) const;
3536
};
3637
inline Settings settings{};
3738

@@ -46,16 +47,13 @@ namespace Rift::Graph
4647
void Init();
4748
void Shutdown();
4849

49-
5050
void PushNodeStyle();
5151
void PopNodeStyle();
5252
void PushInnerNodeStyle();
5353
void PopInnerNodeStyle();
5454

55-
void DrawContextMenu(AST::Tree& ast);
5655
void DrawTypeGraph(AST::Tree& ast, AST::Id typeId, CTypeEditor& typeEditor);
5756

58-
v2 GetGridPosition(v2 screenPosition);
5957
void SetNodePosition(AST::Id id, v2 position);
6058
v2 GetNodePosition(AST::Id id);
6159

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2015-2022 Piperift - All rights reserved
2+
#pragma once
3+
4+
#include <AST/Filtering.h>
5+
#include <AST/Tree.h>
6+
7+
8+
namespace Rift::Graph
9+
{
10+
void DrawContextMenu(AST::Tree& ast, AST::Id typeId, AST::Id hoveredNodeId);
11+
} // namespace Rift::Graph

Libs/Editor/Include/Utils/Nodes.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,16 @@ namespace Rift::Nodes
439439
// Use the following functions to query a change of state for an existing link, or new link.
440440
// Call these after EndNodeEditor().
441441

442-
// Did the user start dragging a new link from a pin?
443-
bool IsLinkStarted(Id* outputPinId);
442+
bool IsDraggingLink();
443+
TPair<Id, PinType> GetDraggedOriginPin();
444444
// Did the user drop the dragged link before attaching it to a pin?
445445
// There are two different kinds of situations to consider when handling this event:
446446
// 1) a link which is created at a pin and then dropped
447447
// 2) an existing link which is detached from a pin and then dropped
448448
// Use the including_detached_links flag to control whether this function triggers when the
449449
// user detaches a link and drops it.
450-
bool IsLinkDropped(Id* outputPinId = nullptr, bool includingDetachedLinks = true);
450+
bool IsLinkDropped(
451+
Id* outputId = nullptr, Id* inputId = nullptr, bool includingDetachedLinks = true);
451452
// Did the user finish creating a new link?
452453
bool IsLinkCreated(Id& outputPinId, Id& inputPinId, bool* createdFromSnap = nullptr);
453454
bool IsLinkCreated(AST::Id& outputNodeId, Id& outputPinId, AST::Id& inputNodeId, Id& inputPinId,

Libs/Editor/Include/Utils/NodesInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ namespace Rift::Nodes
122122

123123
T& Get(AST::Id id)
124124
{
125-
Check(Contains(id));
126125
return *GetByIndex(AST::GetIndex(id));
127126
}
128127

@@ -186,6 +185,7 @@ namespace Rift::Nodes
186185
private:
187186
T* GetByIndex(u32 index)
188187
{
188+
Check(data.IsValidIndex(index));
189189
return data.Data() + index;
190190
}
191191
};

Libs/Editor/Src/Utils/FunctionGraph.cpp

Lines changed: 12 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "Components/CTypeEditor.h"
66
#include "DockSpaceLayout.h"
77
#include "Utils/EditorStyle.h"
8+
#include "Utils/FunctionGraphContextMenu.h"
89
#include "Utils/FunctionUtils.h"
9-
#include "Utils/TypeUtils.h"
1010

1111
#include <AST/Components/CDeclFunction.h>
1212
#include <AST/Components/CDeclVariable.h>
@@ -26,9 +26,9 @@
2626
#include <AST/Components/CStmtInput.h>
2727
#include <AST/Components/CStmtOutputs.h>
2828
#include <AST/Components/CStmtReturn.h>
29+
#include <AST/Components/CType.h>
2930
#include <AST/Components/Views/CGraphTransform.h>
3031
#include <AST/Filtering.h>
31-
#include <AST/Statics/STypes.h>
3232
#include <AST/Utils/Expressions.h>
3333
#include <AST/Utils/Hierarchy.h>
3434
#include <AST/Utils/Statements.h>
@@ -71,6 +71,11 @@ namespace Rift::Graph
7171
return {0.f, settings.verticalMargin + settings.verticalPadding};
7272
}
7373

74+
v2 Settings::GetGridPosition(v2 screenPosition) const
75+
{
76+
return Nodes::ScreenToGridPosition(screenPosition) * GetInvGridSize();
77+
}
78+
7479
void BeginNode(TAccessRef<TWrite<CGraphTransform>> access, AST::Id id)
7580
{
7681
currentNodeTransform = &access.GetOrAdd<CGraphTransform>(id);
@@ -465,223 +470,6 @@ namespace Rift::Graph
465470
{
466471
ImGui::PopStyleVar(2);
467472
}
468-
void DrawNodeContextMenu(AST::Tree& ast, AST::Id typeId, TSpan<AST::Id> nodeIds)
469-
{
470-
Check(!nodeIds.IsEmpty());
471-
472-
AST::Id firstNodeId = nodeIds[0];
473-
474-
if (nodeIds.Size() == 1 && ast.Has<CDeclFunction>(firstNodeId))
475-
{
476-
if (UI::MenuItem("Add return node"))
477-
{
478-
AST::Id newId = Functions::AddReturn({ast, typeId});
479-
if (!IsNone(newId))
480-
{
481-
v2 position = ast.Get<CGraphTransform>(firstNodeId).position;
482-
ast.Add<CGraphTransform>(newId, position + v2{10.f, 0.f});
483-
484-
AST::Statements::TryConnect(ast, firstNodeId, newId);
485-
}
486-
}
487-
UI::Separator();
488-
}
489-
if (UI::MenuItem("Delete"))
490-
{
491-
Functions::RemoveNodes(ast, nodeIds);
492-
}
493-
}
494-
495-
void DrawGraphContextMenu(AST::Tree& ast, AST::Id typeId)
496-
{
497-
static ImGuiTextFilter filter;
498-
if (UI::IsWindowAppearing())
499-
{
500-
UI::SetKeyboardFocusHere();
501-
}
502-
filter.Draw("##Filter");
503-
const v2 clickPos = UI::GetMousePosOnOpeningCurrentPopup();
504-
const v2 gridPos = GetGridPosition(clickPos).Floor();
505-
506-
if (filter.IsActive() || UI::TreeNode("Flow"))
507-
{
508-
if (filter.PassFilter("Return") && UI::MenuItem("Return"))
509-
{
510-
AST::Id newId = Functions::AddReturn({ast, typeId});
511-
if (!IsNone(newId))
512-
{
513-
ast.Add<CGraphTransform>(newId, gridPos);
514-
}
515-
}
516-
if (filter.PassFilter("If") && UI::MenuItem("If"))
517-
{
518-
AST::Id newId = Functions::AddIf({ast, typeId});
519-
if (!IsNone(newId))
520-
{
521-
ast.Add<CGraphTransform>(newId, gridPos);
522-
}
523-
}
524-
525-
if (!filter.IsActive())
526-
{
527-
UI::TreePop();
528-
}
529-
}
530-
531-
if (filter.IsActive() || UI::TreeNode("Operators"))
532-
{
533-
static String name;
534-
// Unary operators
535-
for (auto type : Refl::GetEnumValues<UnaryOperatorType>())
536-
{
537-
StringView shortName = Functions::GetUnaryOperatorName(type);
538-
StringView longName = Functions::GetUnaryOperatorLongName(type);
539-
name.clear();
540-
Strings::FormatTo(name, "{} ({})", shortName, longName);
541-
if (filter.PassFilter(name.data(), name.data() + name.size())
542-
&& UI::MenuItem(name.data()))
543-
{
544-
AST::Id newId = Functions::AddUnaryOperator({ast, typeId}, type);
545-
if (!IsNone(newId))
546-
{
547-
ast.Add<CGraphTransform>(newId, gridPos);
548-
}
549-
}
550-
}
551-
// Binary operators
552-
for (auto type : Refl::GetEnumValues<BinaryOperatorType>())
553-
{
554-
StringView shortName = Functions::GetBinaryOperatorName(type);
555-
StringView longName = Functions::GetBinaryOperatorLongName(type);
556-
name.clear();
557-
Strings::FormatTo(name, "{} ({})", shortName, longName);
558-
if (filter.PassFilter(name.data(), name.data() + name.size())
559-
&& UI::MenuItem(name.data()))
560-
{
561-
AST::Id newId = Functions::AddBinaryOperator({ast, typeId}, type);
562-
if (!IsNone(newId))
563-
{
564-
ast.Add<CGraphTransform>(newId, gridPos);
565-
}
566-
}
567-
}
568-
569-
if (!filter.IsActive())
570-
{
571-
UI::TreePop();
572-
}
573-
}
574-
575-
if (filter.IsActive() || UI::TreeNode("Constructors"))
576-
{
577-
String makeStr{};
578-
auto& typeList = ast.GetStatic<STypes>();
579-
auto types = ast.Filter<CType>();
580-
for (const auto& it : typeList.typesByName)
581-
{
582-
if (auto* type = types.TryGet<CType>(it.second))
583-
{
584-
makeStr.clear();
585-
Strings::FormatTo(makeStr, "Make {}", type->name);
586-
if (filter.PassFilter(makeStr.c_str(), makeStr.c_str() + makeStr.size()))
587-
{
588-
if (UI::MenuItem(makeStr.c_str()))
589-
{
590-
AST::Id newId = Functions::AddLiteral({ast, typeId}, it.second);
591-
if (!IsNone(newId))
592-
{
593-
ast.Add<CGraphTransform>(newId, gridPos);
594-
}
595-
}
596-
}
597-
}
598-
}
599-
600-
if (!filter.IsActive())
601-
{
602-
UI::TreePop();
603-
}
604-
}
605-
606-
if (filter.IsActive() || UI::TreeNode("Variables"))
607-
{
608-
auto variables = ast.Filter<CDeclVariable>();
609-
auto identifiers = ast.Filter<CIdentifier>();
610-
for (AST::Id variableId : variables)
611-
{
612-
if (auto* iden = identifiers.TryGet<CIdentifier>(variableId))
613-
{
614-
const String& name = iden->name.ToString();
615-
if (filter.PassFilter(name.c_str(), name.c_str() + name.size()))
616-
{
617-
if (UI::MenuItem(name.c_str()))
618-
{
619-
AST::Id newId =
620-
Functions::AddDeclarationReference({ast, typeId}, variableId);
621-
if (!IsNone(newId))
622-
{
623-
ast.Add<CGraphTransform>(newId, gridPos);
624-
}
625-
}
626-
}
627-
}
628-
}
629-
630-
if (!filter.IsActive())
631-
{
632-
UI::TreePop();
633-
}
634-
}
635-
636-
if (filter.IsActive() || UI::TreeNode("Functions"))
637-
{
638-
auto functions = ast.Filter<CDeclFunction>();
639-
auto identifiers = ast.Filter<CIdentifier>();
640-
for (AST::Id functionId : functions)
641-
{
642-
if (auto* iden = identifiers.TryGet<CIdentifier>(functionId))
643-
{
644-
const String& name = iden->name.ToString();
645-
if (filter.PassFilter(name.c_str(), name.c_str() + name.size()))
646-
{
647-
if (UI::MenuItem(name.c_str()))
648-
{
649-
AST::Id newId = Functions::AddCall({ast, typeId}, functionId);
650-
if (!IsNone(newId))
651-
{
652-
ast.Add<CGraphTransform>(newId, gridPos);
653-
}
654-
}
655-
}
656-
}
657-
}
658-
659-
if (!filter.IsActive())
660-
{
661-
ImGui::TreePop();
662-
}
663-
}
664-
}
665-
666-
void DrawContextMenu(AST::Tree& ast, AST::Id typeId, AST::Id hoveredNodeId)
667-
{
668-
if (ImGui::BeginPopup("GraphContextMenu"))
669-
{
670-
if (IsNone(hoveredNodeId))
671-
{
672-
DrawGraphContextMenu(ast, typeId);
673-
}
674-
else if (Nodes::IsNodeSelected(hoveredNodeId))
675-
{
676-
DrawNodeContextMenu(ast, typeId, Nodes::GetSelectedNodes());
677-
}
678-
else
679-
{
680-
DrawNodeContextMenu(ast, typeId, hoveredNodeId);
681-
}
682-
ImGui::EndPopup();
683-
}
684-
}
685473

686474
void DrawFunctionDecls(AST::Tree& ast, const TArray<AST::Id>& functionDecls)
687475
{
@@ -1065,15 +853,15 @@ namespace Rift::Graph
1065853
// Links
1066854
DrawStatementLinks(ast, *children);
1067855
DrawExpressionLinks(ast, *children);
1068-
1069-
if (UI::IsKeyReleased(GLFW_KEY_DELETE))
1070-
{
1071-
Functions::RemoveNodes(ast, Nodes::GetSelectedNodes());
1072-
}
1073856
}
1074857

1075858
Nodes::DrawMiniMap(0.2f, Nodes::MiniMapCorner::TopRight);
1076859
PopNodeStyle();
860+
861+
if (UI::IsKeyReleased(GLFW_KEY_DELETE))
862+
{
863+
Functions::RemoveNodes(ast, Nodes::GetSelectedNodes());
864+
}
1077865
Nodes::EndNodeEditor();
1078866

1079867
Nodes::Id outputPin;
@@ -1102,12 +890,6 @@ namespace Rift::Graph
1102890
}
1103891
}
1104892

1105-
1106-
v2 GetGridPosition(v2 screenPosition)
1107-
{
1108-
return Nodes::ScreenToGridPosition(screenPosition) * settings.GetInvGridSize();
1109-
}
1110-
1111893
void SetNodePosition(AST::Id id, v2 position)
1112894
{
1113895
position *= settings.GetGridSize();

0 commit comments

Comments
 (0)