Nelarius / imnodes

A small, dependency-free node editor for dear imgui
MIT License
1.97k stars 241 forks source link

Allow User Defined Callbacks and Custom Header like ImGui #115 #127

Closed kfields closed 2 years ago

kfields commented 2 years ago

Minimally invasive change to allow pybind11 callback and callback data

Here is what it basically looks like in my project if anyone is curious. I had to extend py::object to be assignable from NULL and also extend the bool conversion operator to test for Py_None. I slapped it together over the weekend so I'm sure it can be improved. The upside is not having to make any modifications to ImNodes source except for adding the conditional typedefs/macros.

CMakeLists.txt: target_compile_definitions(${THIS} PRIVATE IMNODES_USER_CONFIG=<imnodes_config.h>)

imnodes_config.h:

#pragma once

#include <pybind11/functional.h>

namespace pybind11 {

inline bool PyWrapper_Check(PyObject *o) { return true; }

class wrapper : public object {
public:
    PYBIND11_OBJECT_DEFAULT(wrapper, object, PyWrapper_Check)
    wrapper(void* x) { m_ptr = (PyObject*)x; }
    explicit operator bool() const { return m_ptr != nullptr && m_ptr != Py_None; }
};

} //namespace pybind11

namespace py = pybind11;

#define ImNodesMiniMapNodeHoveringCallback py::wrapper

#define ImNodesMiniMapNodeHoveringCallbackUserData py::wrapper
kfields commented 2 years ago

Your welcome.

I agree. The next PR I'll add an example.

Nelarius commented 2 years ago

I went ahead and added a quick description to the README: https://github.com/Nelarius/imnodes/commit/9aa6a0f48aeb9ac5e359af1133e3ffe3205e6cbc If you have any improvements, I would be happy to merge 👍