Skip to content

Commit

Permalink
Make use of extra keys in node editor optional thedmd#25
Browse files Browse the repository at this point in the history
ImGuiKey_D and ImGuiKey_F are used in node editor only if they
are defined in ImGuiKey_ enum.

This commit can be removed when/if ocornut/imgui#2625
PR will be merged.
  • Loading branch information
thedmd authored and fdfxalex committed Dec 31, 2019
1 parent 7563605 commit 5496d83
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions NodeEditor/Source/imgui_node_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,61 @@
# include <algorithm>
# include <sstream>
# include <streambuf>
# include <type_traits>

// https://stackoverflow.com/a/8597498
# define DECLARE_HAS_NESTED(Name, Member) \
\
template<class T> \
struct has_nested_ ## Name \
{ \
typedef char yes; \
typedef yes(&no)[2]; \
\
template<class U> static yes test(decltype(U::Member)*); \
template<class U> static no test(...); \
\
static bool const value = sizeof(test<T>(0)) == sizeof(yes); \
};


namespace ax {
namespace NodeEditor {
namespace Detail {

# define DECLARE_KEY_TESTER(Key) \
DECLARE_HAS_NESTED(Key, Key) \
struct KeyTester_ ## Key \
{ \
template <typename T> \
static int Get(typename std::enable_if<has_nested_ ## Key<ImGuiKey_>::value, T>::type*) \
{ \
return ImGui::GetKeyIndex(T::Key); \
} \
\
template <typename T> \
static int Get(typename std::enable_if<!has_nested_ ## Key<ImGuiKey_>::value, T>::type*) \
{ \
return -1; \
} \
}

DECLARE_KEY_TESTER(ImGuiKey_F);
DECLARE_KEY_TESTER(ImGuiKey_D);

static inline int GetKeyIndexForF()
{
return KeyTester_ImGuiKey_F::Get<ImGuiKey_>(nullptr);
}

static inline int GetKeyIndexForD()
{
return KeyTester_ImGuiKey_D::Get<ImGuiKey_>(nullptr);
}

} // namespace Detail
} // namespace NodeEditor
} // namespace ax


//------------------------------------------------------------------------------
Expand Down Expand Up @@ -2791,7 +2846,7 @@ ed::EditorAction::AcceptResult ed::NavigateAction::Accept(const Control& control

auto& io = ImGui::GetIO();

if (ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_F)) && Editor->AreShortcutsEnabled())
if (ImGui::IsWindowFocused() && ImGui::IsKeyPressed(GetKeyIndexForF()) && Editor->AreShortcutsEnabled())
{
const auto allowZoomIn = io.KeyShift;

Expand Down Expand Up @@ -3791,7 +3846,7 @@ ed::EditorAction::AcceptResult ed::ShortcutAction::Accept(const Control& control
candidateAction = Copy;
if (io.KeyCtrl && !io.KeyShift && !io.KeyAlt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_V)))
candidateAction = Paste;
if (io.KeyCtrl && !io.KeyShift && !io.KeyAlt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_D)))
if (io.KeyCtrl && !io.KeyShift && !io.KeyAlt && ImGui::IsKeyPressed(GetKeyIndexForD()))
candidateAction = Duplicate;
if (!io.KeyCtrl && !io.KeyShift && !io.KeyAlt && ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space)))
candidateAction = CreateNode;
Expand Down

0 comments on commit 5496d83

Please sign in to comment.