Skip to content
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

Add a way to specify custom id types #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED YES)
macro(add_example_executable name)
project(${name})

set(options NOLINK)
set(oneValueArgs)
set(multiValueArgs SOURCES)
cmake_parse_arguments(ADD_EXAMPLE "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})

set(_Example_Sources
${ARGN}
${ADD_EXAMPLE_SOURCES}
)

#source_group("" FILES ${_Example_Sources})
Expand Down Expand Up @@ -69,7 +75,10 @@ macro(add_example_executable name)

find_package(imgui REQUIRED)
find_package(imgui_node_editor REQUIRED)
target_link_libraries(${name} PRIVATE imgui imgui_node_editor application)
target_link_libraries(${name} PRIVATE imgui application)
if (NOT ADD_EXAMPLE_NOLINK)
target_link_libraries(${name} PRIVATE imgui_node_editor)
endif()

set(_ExampleBinDir ${CMAKE_BINARY_DIR}/bin)

Expand Down Expand Up @@ -132,3 +141,4 @@ add_subdirectory(simple-example)
add_subdirectory(widgets-example)
add_subdirectory(basic-interaction-example)
add_subdirectory(blueprints-example)
add_subdirectory(idtypes-example)
1 change: 1 addition & 0 deletions examples/application/source/renderer_ogl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# include "platform.h"
# include <algorithm>
# include <cstdint>

# if PLATFORM(WINDOWS)
# define NOMINMAX
Expand Down
4 changes: 2 additions & 2 deletions examples/basic-interaction-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_example_executable(basic-interaction-example
basic-interaction-example.cpp
)
SOURCES basic-interaction-example.cpp
)
2 changes: 1 addition & 1 deletion examples/blueprints-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_example_executable(blueprints-example
add_example_executable(blueprints-example SOURCES
blueprints-example.cpp
utilities/builders.h
utilities/drawing.h
Expand Down
4 changes: 2 additions & 2 deletions examples/canvas-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_example_executable(canvas-example
add_example_executable(canvas-example SOURCES
canvas-example.cpp
)

#target_link_libraries(Canvas PRIVATE imgui_canvas)
#target_link_libraries(Canvas PRIVATE imgui_canvas)
19 changes: 19 additions & 0 deletions examples/idtypes-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
add_example_executable(idtypes-example NOLINK
SOURCES idtypes-example.cpp
)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_library(imgui_node_editor_customids STATIC
${IMGUI_NODE_EDITOR_SOURCES}
)

target_include_directories(imgui_node_editor_customids PUBLIC
${IMGUI_NODE_EDITOR_ROOT_DIR}
)

target_link_libraries(imgui_node_editor_customids PUBLIC imgui)

target_compile_definitions(imgui_node_editor_customids PUBLIC IMGUI_NODE_EDITOR_USER_CONFIG="${CMAKE_CURRENT_SOURCE_DIR}/edconfig.h")
target_link_libraries(idtypes-example PRIVATE imgui_node_editor_customids)
67 changes: 67 additions & 0 deletions examples/idtypes-example/edconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef EDCONFIG_H
#define EDCONFIG_H

#include <string>

struct CustomNodeId
{
std::string name;

CustomNodeId(const CustomNodeId &id) = default;
CustomNodeId &operator=(const CustomNodeId &id) = default;

bool operator<(const CustomNodeId &id) const;
bool operator==(const CustomNodeId &id) const;

std::string AsString() const;
static CustomNodeId FromString(const char *str, const char *end);
bool IsValid() const;

static const CustomNodeId Invalid;
};

struct CustomPinId
{
enum class Direction {
In,
Out
};
std::string nodeName;
Direction direction;
int pinIndex;

CustomPinId(const CustomPinId &id) = default;
CustomPinId &operator=(const CustomPinId &id) = default;

bool operator<(const CustomPinId &id) const;
bool operator==(const CustomPinId &id) const;

std::string AsString() const;
static CustomPinId FromString(const char *str, const char *end);
bool IsValid() const;

static const CustomPinId Invalid;
};

struct CustomLinkId
{
CustomPinId in, out;

CustomLinkId(const CustomLinkId &id) = default;
CustomLinkId &operator=(const CustomLinkId &id) = default;

bool operator<(const CustomLinkId &id) const;
bool operator==(const CustomLinkId &id) const;

std::string AsString() const;
static CustomLinkId FromString(const char *str, const char *end);
bool IsValid() const;

static const CustomLinkId Invalid;
};

#define IMGUI_NODE_EDITOR_CUSTOM_NODEID CustomNodeId
#define IMGUI_NODE_EDITOR_CUSTOM_PINID CustomPinId
#define IMGUI_NODE_EDITOR_CUSTOM_LINKID CustomLinkId

#endif
212 changes: 212 additions & 0 deletions examples/idtypes-example/idtypes-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
# include <imgui.h>
# include <imgui_node_editor.h>
# include <application.h>
# include <vector>
# include <unordered_map>

#include "edconfig.h"

namespace ed = ax::NodeEditor;

struct Pin
{
};

struct Node
{
std::vector<Pin> InPins;
std::vector<Pin> OutPins;
};

bool CustomNodeId::operator<(const CustomNodeId &id) const
{
return name < id.name;
}

bool CustomNodeId::operator==(const CustomNodeId &id) const
{
return name == id.name;
}

std::string CustomNodeId::AsString() const
{
return name;
}

CustomNodeId CustomNodeId::FromString(const char *str, const char *end)
{
return CustomNodeId{ std::string(str, end) };
}

bool CustomNodeId::IsValid() const
{
return !name.empty();
}

const CustomNodeId CustomNodeId::Invalid = {};

bool CustomPinId::operator<(const CustomPinId &id) const
{
if (nodeName < id.nodeName) return true;
else if (nodeName > id.nodeName) return false;

if (int(direction) < int(id.direction)) return true;
else if (int(direction) > int(id.direction)) return false;

return pinIndex < id.pinIndex;
}

bool CustomPinId::operator==(const CustomPinId &id) const
{
return nodeName == id.nodeName && direction == id.direction && pinIndex == id.pinIndex;
}

std::string CustomPinId::AsString() const
{
return nodeName + ";" + std::to_string(int(direction)) + ";" + std::to_string(pinIndex);
}

CustomPinId CustomPinId::FromString(const char *str, const char *end)
{
std::string string(str, end);
auto sep1 = string.find(';');

if (sep1 == 0 || sep1 == std::string::npos) {
return Invalid;
}

auto sep2 = string.find(';', sep1 + 1);
if (sep2 == sep1 + 1 || sep2 == std::string::npos) {
return Invalid;
}

char *e;
auto nodeName = std::string(string.data(), sep1);
int dir = std::strtol(string.data() + sep1 + 1, &e, 10);
if (dir > 1) {
return Invalid;
}

int pin = std::strtol(string.data() + sep2 + 1, &e, 10);
return CustomPinId{ nodeName, CustomPinId::Direction(dir), pin };
}

bool CustomPinId::IsValid() const
{
return !nodeName.empty();
}

const CustomPinId CustomPinId::Invalid = {};


bool CustomLinkId::operator<(const CustomLinkId &id) const
{
if (in < id.in) return true;
if (in == id.in) return out < id.out;
return false;
}

bool CustomLinkId::operator==(const CustomLinkId &id) const
{
return in == id.in && out == id.out;
}

std::string CustomLinkId::AsString() const
{
return in.AsString() + "->" + out.AsString();
}

CustomLinkId CustomLinkId::FromString(const char *str, const char *end)
{
std::string string(str, end);
auto sep = string.find("->");
if (sep == 0 || sep == std::string::npos) {
return Invalid;
}

auto in = CustomPinId::FromString(str, str + sep);
auto out = CustomPinId::FromString(str + sep + 2, end);
return { in, out };
}

bool CustomLinkId::IsValid() const
{
return in.IsValid() && out.IsValid();
}

const CustomLinkId CustomLinkId::Invalid = {};

struct Example:
public Application
{
using Application::Application;

void OnStart() override
{
ed::Config config;
config.SettingsFile = "IdTypes.json";
m_Context = ed::CreateEditor(&config);

m_Nodes.insert({ "foo", Node{ { {}, {} }, { {} } } });
m_Nodes.insert({ "bar", Node{ { {} }, { {}, {}, {} } } });
}

void OnStop() override
{
ed::DestroyEditor(m_Context);
}

void OnFrame(float deltaTime) override
{
auto& io = ImGui::GetIO();

ImGui::Text("FPS: %.2f (%.2gms)", io.Framerate, io.Framerate ? 1000.0f / io.Framerate : 0.0f);

ImGui::Separator();

ed::SetCurrentEditor(m_Context);
ed::Begin("My Editor", ImVec2(0.0, 0.0f));
// Start drawing nodes.
for (auto &n: m_Nodes) {
ed::BeginNode(CustomNodeId{ n.first });
ImGui::TextUnformatted(n.first.c_str());

ImGui::BeginGroup();
for (int i = 0; i < n.second.InPins.size(); ++i) {
ed::BeginPin(CustomPinId{ n.first, CustomPinId::Direction::In, i }, ed::PinKind::Input);
ImGui::Text("-> In");
ed::EndPin();
}
ImGui::EndGroup();

ImGui::SameLine();

ImGui::BeginGroup();
for (int i = 0; i < n.second.OutPins.size(); ++i) {
ed::BeginPin(CustomPinId{ n.first, CustomPinId::Direction::Out, i }, ed::PinKind::Output);
ImGui::Text("Out ->");
ed::EndPin();
}
ImGui::EndGroup();

ed::EndNode();
}
ed::End();
ed::SetCurrentEditor(nullptr);

//ImGui::ShowMetricsWindow();
}

ed::EditorContext* m_Context = nullptr;
std::unordered_map<std::string, Node> m_Nodes;
};

int Main(int argc, char** argv)
{
Example exampe("IdTypes", argc, argv);

if (exampe.Create())
return exampe.Run();

return 0;
}
4 changes: 2 additions & 2 deletions examples/simple-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_example_executable(simple-example
add_example_executable(simple-example SOURCES
simple-example.cpp
)
)
4 changes: 2 additions & 2 deletions examples/widgets-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_example_executable(widgets-example
add_example_executable(widgets-example SOURCES
widgets-example.cpp
)
)
Loading