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

Customizable ID type #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JulesFouchy
Copy link
Contributor

Hi 👋
In my application I have a use case where I use 128-bits UUIDs as IDs internally, and I would like to use these same IDs as Node and Link IDs when I use imnodes without having to truncate to int back and forth.

This is why I basically added using ID = int; to imnodes, and added the possibility to override that definition of ID inside "imnodes_config.h".

A user-defined "imnodes_config.h" would look like this:

// imnodes_config.h
#pragma once
#include <limits.h>
#include <string>

namespace IMNODES_NAMESPACE
{
using ID = int;
static constexpr ID INVALID_ID = INT_MIN;

inline void PushID(ID id) { ImGui::PushID(id); }

inline std::string IDToString(ID id) { return std::to_string(id); }

inline ID IDFromString(const std::string& str) { return std::stoi(str); }

} // namespace IMNODES_NAMESPACE

Changes

The two main (and maybe controversial) changes I hade to make are:

  • Having to copy-paste ImGuiStorage's implementation from Dear ImGui into imnodes just to change the type of the ID used to index into that storage. But this also has the benefit of removing the static_casts from int to ImGuiID that were all over imnodes' implementation.
  • Changing the implementation of NodeLineHandler() that is used during serialization of the editor state. We now use std::string manipulations instead of sscanf to read and write the ID to and from strings. That is because we don't know how complex the user ID might be, and we have to let them handle the conversions. Using std::string instead of char[] is the simplest.

leonvictor added a commit to leonvictor/allen_engine that referenced this pull request Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant