Skip to content

Commit

Permalink
User can now override button indices for various actions (#88)
Browse files Browse the repository at this point in the history
        New API in Config:
            int                     DragButtonIndex;        // Mouse button index drag action will react to (0-left, 1-right, 2-middle)
            int                     SelectButtonIndex;      // Mouse button index select action will react to (0-left, 1-right, 2-middle)
            int                     NavigateButtonIndex;    // Mouse button index navigate action will react to (0-left, 1-right, 2-middle)
            int                     ContextMenuButtonIndex; // Mouse button index context menu action will react to (0-left, 1-right, 2-middle)
  • Loading branch information
thedmd committed Dec 25, 2020
1 parent adb21da commit 7b7ca1e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ v0.9.1 (WIP):

NEW: Reintegrate Widgets example from @crolando (#77)

NEW: User can now override button indices for various actions (#88)
New API in Config:
int DragButtonIndex; // Mouse button index drag action will react to (0-left, 1-right, 2-middle)
int SelectButtonIndex; // Mouse button index select action will react to (0-left, 1-right, 2-middle)
int NavigateButtonIndex; // Mouse button index navigate action will react to (0-left, 1-right, 2-middle)
int ContextMenuButtonIndex; // Mouse button index context menu action will react to (0-left, 1-right, 2-middle)

BUGFIX: Avoid crash while destroying editor.

BUGFIX: Save draw list used by editor between Begin() and End()
Expand Down
34 changes: 15 additions & 19 deletions imgui_node_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ static const float c_LinkSelectThickness = 5.0f; // canvas pixels
static const float c_NavigationZoomMargin = 0.1f; // percentage of visible bounds
static const float c_MouseZoomDuration = 0.15f; // seconds
static const float c_SelectionFadeOutDuration = 0.15f; // seconds
static const auto c_DragButtonIndex = 0;
static const auto c_SelectButtonIndex = 0;
static const auto c_ScrollButtonIndex = 1;
static const auto c_ContextMenuButtonIndex = 1;

static const auto c_MaxMoveOverEdgeSpeed = 10.0f;
static const auto c_MaxMoveOverEdgeDistance = 300.0f;
Expand Down Expand Up @@ -2177,11 +2173,11 @@ ed::Control ed::EditorContext::BuildControl(bool allowOffscreen)
};

// Check input interactions over area.
auto checkInteractionsInArea = [&emitInteractiveArea, &hotObject, &activeObject, &clickedObject, &doubleClickedObject](ObjectId id, const ImRect& rect, Object* object)
auto checkInteractionsInArea = [this, &emitInteractiveArea, &hotObject, &activeObject, &clickedObject, &doubleClickedObject](ObjectId id, const ImRect& rect, Object* object)
{
if (emitInteractiveArea(id, rect))
clickedObject = object;
if (!doubleClickedObject && ImGui::IsMouseDoubleClicked(c_DragButtonIndex) && ImGui::IsItemHovered())
if (!doubleClickedObject && ImGui::IsMouseDoubleClicked(m_Config.DragButtonIndex) && ImGui::IsItemHovered())
doubleClickedObject = object;

if (!hotObject && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
Expand Down Expand Up @@ -2253,7 +2249,7 @@ ed::Control ed::EditorContext::BuildControl(bool allowOffscreen)

// Check for interaction with background.
auto backgroundClicked = emitInteractiveArea(NodeId(0), editorRect);
auto backgroundDoubleClicked = !doubleClickedObject && ImGui::IsItemHovered() ? ImGui::IsMouseDoubleClicked(c_DragButtonIndex) : false;
auto backgroundDoubleClicked = !doubleClickedObject && ImGui::IsItemHovered() ? ImGui::IsMouseDoubleClicked(m_Config.DragButtonIndex) : false;
auto isBackgroundActive = ImGui::IsItemActive();
auto isBackgroundHot = !hotObject;
auto isDragging = ImGui::IsMouseDragging(0, 1) || ImGui::IsMouseDragging(1, 1) || ImGui::IsMouseDragging(2, 1);
Expand Down Expand Up @@ -3026,11 +3022,11 @@ ed::EditorAction::AcceptResult ed::NavigateAction::Accept(const Control& control
if (m_IsActive)
return False;

if (ImGui::IsWindowHovered() /*&& !ImGui::IsAnyItemActive()*/ && ImGui::IsMouseDragging(c_ScrollButtonIndex, 0.0f))
if (ImGui::IsWindowHovered() /*&& !ImGui::IsAnyItemActive()*/ && ImGui::IsMouseDragging(Editor->GetConfig().NavigateButtonIndex, 0.0f))
{
m_IsActive = true;
m_ScrollStart = m_Scroll;
m_ScrollDelta = ImGui::GetMouseDragDelta(c_ScrollButtonIndex);
m_ScrollDelta = ImGui::GetMouseDragDelta(Editor->GetConfig().NavigateButtonIndex);
m_Scroll = m_ScrollStart - m_ScrollDelta * m_Zoom;
}

Expand Down Expand Up @@ -3112,9 +3108,9 @@ bool ed::NavigateAction::Process(const Control& control)
if (!m_IsActive)
return false;

if (ImGui::IsMouseDragging(c_ScrollButtonIndex, 0.0f))
if (ImGui::IsMouseDragging(Editor->GetConfig().NavigateButtonIndex, 0.0f))
{
m_ScrollDelta = ImGui::GetMouseDragDelta(c_ScrollButtonIndex);
m_ScrollDelta = ImGui::GetMouseDragDelta(Editor->GetConfig().NavigateButtonIndex);
m_Scroll = m_ScrollStart - m_ScrollDelta * m_Zoom;
m_VisibleRect = GetViewRect();
// if (IsActive && Animation.IsPlaying())
Expand Down Expand Up @@ -3578,7 +3574,7 @@ ed::EditorAction::AcceptResult ed::DragAction::Accept(const Control& control)
if (m_IsActive)
return False;

if (control.ActiveObject && ImGui::IsMouseDragging(c_DragButtonIndex))
if (control.ActiveObject && ImGui::IsMouseDragging(Editor->GetConfig().DragButtonIndex))
{
if (!control.ActiveObject->AcceptDrag())
return False;
Expand Down Expand Up @@ -3646,7 +3642,7 @@ bool ed::DragAction::Process(const Control& control)

if (control.ActiveObject == m_DraggedObject)
{
auto dragOffset = ImGui::GetMouseDragDelta(c_DragButtonIndex, 0.0f);
auto dragOffset = ImGui::GetMouseDragDelta(Editor->GetConfig().DragButtonIndex, 0.0f);

auto draggedOrigin = m_DraggedObject->DragStartLocation();
auto alignPivot = ImVec2(0, 0);
Expand Down Expand Up @@ -3753,7 +3749,7 @@ ed::EditorAction::AcceptResult ed::SelectAction::Accept(const Control& control)

m_SelectedObjectsAtStart.clear();

if (control.BackgroundHot && ImGui::IsMouseDragging(c_SelectButtonIndex, 1))
if (control.BackgroundHot && ImGui::IsMouseDragging(Editor->GetConfig().SelectButtonIndex, 1))
{
m_IsActive = true;
m_StartPoint = ImGui::GetMousePos();
Expand Down Expand Up @@ -3817,7 +3813,7 @@ bool ed::SelectAction::Process(const Control& control)
if (!m_IsActive)
return false;

if (ImGui::IsMouseDragging(c_SelectButtonIndex, 0))
if (ImGui::IsMouseDragging(Editor->GetConfig().SelectButtonIndex, 0))
{
m_EndPoint = ImGui::GetMousePos();

Expand Down Expand Up @@ -3918,9 +3914,9 @@ ed::ContextMenuAction::ContextMenuAction(EditorContext* editor):

ed::EditorAction::AcceptResult ed::ContextMenuAction::Accept(const Control& control)
{
const auto isPressed = ImGui::IsMouseClicked(c_ContextMenuButtonIndex);
const auto isReleased = ImGui::IsMouseReleased(c_ContextMenuButtonIndex);
const auto isDragging = ImGui::IsMouseDragging(c_ContextMenuButtonIndex);
const auto isPressed = ImGui::IsMouseClicked(Editor->GetConfig().ContextMenuButtonIndex);
const auto isReleased = ImGui::IsMouseReleased(Editor->GetConfig().ContextMenuButtonIndex);
const auto isDragging = ImGui::IsMouseDragging(Editor->GetConfig().ContextMenuButtonIndex);

if (isPressed || isReleased || isDragging)
{
Expand Down Expand Up @@ -4286,7 +4282,7 @@ ed::EditorAction::AcceptResult ed::CreateItemAction::Accept(const Control& contr
if (m_IsActive)
return EditorAction::False;

if (control.ActivePin && ImGui::IsMouseDragging(c_DragButtonIndex))
if (control.ActivePin && ImGui::IsMouseDragging(Editor->GetConfig().DragButtonIndex))
{
m_DraggedPin = control.ActivePin;
DragStart(m_DraggedPin);
Expand Down
8 changes: 8 additions & 0 deletions imgui_node_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ struct Config
ConfigSaveNodeSettings SaveNodeSettings;
ConfigLoadNodeSettings LoadNodeSettings;
void* UserPointer;
int DragButtonIndex; // Mouse button index drag action will react to (0-left, 1-right, 2-middle)
int SelectButtonIndex; // Mouse button index select action will react to (0-left, 1-right, 2-middle)
int NavigateButtonIndex; // Mouse button index navigate action will react to (0-left, 1-right, 2-middle)
int ContextMenuButtonIndex; // Mouse button index context menu action will react to (0-left, 1-right, 2-middle)

Config()
: SettingsFile("NodeEditor.json")
Expand All @@ -73,6 +77,10 @@ struct Config
, SaveNodeSettings(nullptr)
, LoadNodeSettings(nullptr)
, UserPointer(nullptr)
, DragButtonIndex(0)
, SelectButtonIndex(0)
, NavigateButtonIndex(1)
, ContextMenuButtonIndex(1)
{
}
};
Expand Down
2 changes: 2 additions & 0 deletions imgui_node_editor_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ struct EditorContext
EditorContext(const ax::NodeEditor::Config* config = nullptr);
~EditorContext();

const Config& GetConfig() const { return m_Config; }

Style& GetStyle() { return m_Style; }

void Begin(const char* id, const ImVec2& size = ImVec2(0, 0));
Expand Down

0 comments on commit 7b7ca1e

Please sign in to comment.