Skip to content

Added zoom feature for node editor #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions EvoEngine_SDK/include/Utilities/NodeGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@ bool NodeGraph<Id, Od, Nd, Ld>::OnInspect(
}
ImNodes::MiniMap();
ImNodes::EndNodeEditor();

if (ImNodes::IsEditorHovered() && ImGui::GetIO().MouseWheel != 0) {
const float zoom = ImNodes::EditorContextGetZoom() + ImGui::GetIO().MouseWheel * 0.1f;
ImNodes::EditorContextSetZoom(zoom, ImGui::GetMousePos());
}

NodeGraphNodeHandle hovered_node_handle = -1;
NodeGraphLinkHandle hovered_link_handle = -1;
NodeGraphInputPinHandle hovered_input_pin_handle = -1;
Expand Down Expand Up @@ -893,6 +899,11 @@ void NodeGraph<Id, Od, Nd, Ld>::Serialize(YAML::Emitter& out,
if (editor_layer) {
prev_editor_context = ImNodes::GetCurrentContext()->EditorCtx;
ImNodes::EditorContextSet(const_cast<ImNodesEditorContext*>(&editor_context_));

out << YAML::Key << "ZoomScale" << YAML::Value << editor_context_.ZoomScale;
out << YAML::Key << "Panning" << YAML::Value << glm::vec2(editor_context_.Panning.x, editor_context_.Panning.y);
out << YAML::Key << "AutoPanningDelta" << YAML::Value
<< glm::vec2(editor_context_.AutoPanningDelta.x, editor_context_.AutoPanningDelta.y);
}

std::unordered_map<NodeGraphOutputPinHandle, int> output_pin_map;
Expand Down Expand Up @@ -1022,6 +1033,17 @@ void NodeGraph<Id, Od, Nd, Ld>::Deserialize(const YAML::Node& in,
if (editor_layer) {
prev_editor_context = ImNodes::GetCurrentContext()->EditorCtx;
ImNodes::EditorContextSet(const_cast<ImNodesEditorContext*>(&editor_context_));
if (in["ZoomScale"]) {
editor_context_.ZoomScale = in["ZoomScale"].as<float>();
}
if (in["Panning"]) {
const auto t = in["Panning"].as<glm::vec2>();
editor_context_.Panning = ImVec2(t.x, t.y);
}
if (in["AutoPanningDelta"]) {
const auto t = in["AutoPanningDelta"].as<glm::vec2>();
editor_context_.Panning = ImVec2(t.x, t.y);
}
}

nodes_.clear();
Expand Down
8 changes: 8 additions & 0 deletions EvoEngine_SDK/include/Utilities/imnodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ ImVec2 EditorContextGetPanning();
void EditorContextResetPanning(const ImVec2& pos);
void EditorContextMoveToNode(const int node_id);

// Get the separate ImGui context that ImNodes uses for zoom functionality
// this is the active context when between BeginNodeEditor and EndNodeEditor
ImGuiContext* GetNodeEditorImGuiContext();

ImNodesIO& GetIO();

// Returns the global style struct. See the struct declaration for default values.
Expand All @@ -265,6 +269,10 @@ void MiniMap(const float minimap_size_fraction = 0.2f,
const ImNodesMiniMapNodeHoveringCallback node_hovering_callback = NULL,
const ImNodesMiniMapNodeHoveringCallbackUserData node_hovering_callback_data = NULL);

// Editor zoom controls
float EditorContextGetZoom();
void EditorContextSetZoom(float zoom_scale, ImVec2 zoom_center);

// Use PushColorStyle and PopColorStyle to modify ImNodesStyle::Colors mid-frame.
void PushColorStyle(ImNodesCol item, unsigned int color);
void PopColorStyle();
Expand Down
8 changes: 7 additions & 1 deletion EvoEngine_SDK/include/Utilities/imnodes_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include "imgui_internal.h"
#include <imgui_internal.h>

#include "imnodes.hpp"

Expand Down Expand Up @@ -248,6 +248,7 @@ struct ImNodesEditorContext {
ImVector<int> NodeDepthOrder;

// ui related fields
float ZoomScale;
ImVec2 Panning;
ImVec2 AutoPanningDelta;
// Minimum and maximum extents of all content in grid space. Valid after final
Expand Down Expand Up @@ -282,6 +283,7 @@ struct ImNodesEditorContext {
: Nodes(),
Pins(),
Links(),
ZoomScale(1.f),
Panning(0.f, 0.f),
SelectedNodeIndices(),
SelectedLinkIndices(),
Expand All @@ -301,13 +303,16 @@ struct ImNodesContext {
ImNodesEditorContext* EditorCtx;

// Canvas draw list and helper state
ImGuiContext* NodeEditorImgCtx;
ImGuiContext* OriginalImgCtx;
ImDrawList* CanvasDrawList;
ImGuiStorage NodeIdxToSubmissionIdx;
ImVector<int> NodeIdxSubmissionOrder;
ImVector<int> NodeIndicesOverlappingWithMouse;
ImVector<int> OccludedPinIndices;

// Canvas extents
ImVec2 CanvasOriginalOrigin;
ImVec2 CanvasOriginScreenSpace;
ImRect CanvasRectScreenSpace;

Expand Down Expand Up @@ -347,6 +352,7 @@ struct ImNodesContext {
// ImGui::IO cache

ImVec2 MousePos;
bool IsHovered;

bool LeftMouseClicked;
bool LeftMouseReleased;
Expand Down
Loading