Drag and Drop: Moved internal fields out of public sight. (#143)

This commit is contained in:
omar 2017-11-06 20:02:56 +01:00
parent 8b725c94cb
commit de1e7dc088
3 changed files with 25 additions and 18 deletions

View File

@ -662,6 +662,7 @@ static bool DataTypeApplyOpFromText(const char* buf, const char* ini
namespace ImGui namespace ImGui
{ {
static void ClearDragDrop();
static void FocusPreviousWindow(); static void FocusPreviousWindow();
} }
@ -2250,15 +2251,14 @@ void ImGui::NewFrame()
g.ScalarAsInputTextId = 0; g.ScalarAsInputTextId = 0;
// Elapse drag & drop payload // Elapse drag & drop payload
g.DragDropPayload.AcceptIdPrev = g.DragDropPayload.AcceptIdCurr;
g.DragDropPayload.AcceptIdCurr = 0;
if (g.DragDropActive && g.DragDropPayload.DataFrameCount + 1 < g.FrameCount) if (g.DragDropActive && g.DragDropPayload.DataFrameCount + 1 < g.FrameCount)
{ {
g.DragDropActive = false; ClearDragDrop();
g.DragDropPayload.Clear();
g.DragDropPayloadBufHeap.clear(); g.DragDropPayloadBufHeap.clear();
memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal));
} }
g.DragDropAcceptIdPrev = g.DragDropAcceptIdCurr;
g.DragDropAcceptIdCurr = 0;
// Update keyboard input state // Update keyboard input state
memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));
@ -10631,6 +10631,15 @@ void ImGui::Value(const char* prefix, float v, const char* float_format)
// DRAG AND DROP // DRAG AND DROP
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void ImGui::ClearDragDrop()
{
ImGuiContext& g = *GImGui;
g.DragDropActive = false;
g.DragDropPayload.Clear();
g.DragDropAcceptIdCurr = g.DragDropAcceptIdPrev = 0;
g.DragDropAcceptFrameCount = -1;
}
// Call when current ID is active. // Call when current ID is active.
// When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource() // When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource()
bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags, int mouse_button) bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags, int mouse_button)
@ -10677,8 +10686,8 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags, int mouse_button)
if (!g.DragDropActive) if (!g.DragDropActive)
{ {
IM_ASSERT(id != 0); IM_ASSERT(id != 0);
ClearDragDrop();
ImGuiPayload& payload = g.DragDropPayload; ImGuiPayload& payload = g.DragDropPayload;
payload.Clear();
payload.SourceId = id; payload.SourceId = id;
payload.SourceParentId = window->IDStack.back(); payload.SourceParentId = window->IDStack.back();
g.DragDropActive = true; g.DragDropActive = true;
@ -10718,10 +10727,7 @@ void ImGui::EndDragDropSource()
// Discard the drag if have not called SetDragDropPayload() // Discard the drag if have not called SetDragDropPayload()
if (g.DragDropPayload.DataFrameCount == -1) if (g.DragDropPayload.DataFrameCount == -1)
{ ClearDragDrop();
g.DragDropActive = false;
g.DragDropPayload.Clear();
}
} }
// Use 'cond' to choose to submit payload on drag start or every frame // Use 'cond' to choose to submit payload on drag start or every frame
@ -10765,7 +10771,7 @@ bool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_s
} }
payload.DataFrameCount = g.FrameCount; payload.DataFrameCount = g.FrameCount;
return (payload.AcceptFrameCount == g.FrameCount) || (payload.AcceptFrameCount == g.FrameCount - 1); return (g.DragDropAcceptFrameCount == g.FrameCount) || (g.DragDropAcceptFrameCount == g.FrameCount - 1);
} }
bool ImGui::BeginDragDropTarget() bool ImGui::BeginDragDropTarget()
@ -10795,9 +10801,8 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop
return NULL; return NULL;
// NB: We currently accept NULL id however, overlapping targets required unique ID to function // NB: We currently accept NULL id however, overlapping targets required unique ID to function
const bool was_accepted_previously = (payload.AcceptIdPrev == window->DC.LastItemId); const bool was_accepted_previously = (g.DragDropAcceptIdPrev == window->DC.LastItemId);
//if (window->DC.LastItemId) g.DragDropAcceptIdCurr = window->DC.LastItemId;
payload.AcceptIdCurr = window->DC.LastItemId;
// Render drop visuals // Render drop visuals
if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && was_accepted_previously) if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && was_accepted_previously)
@ -10807,7 +10812,7 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop
window->DrawList->AddRectFilled(r.Min, r.Max, IM_COL32(255, 255, 0, 20), 0.0f); // FIXME-DRAG FIXME-STYLE window->DrawList->AddRectFilled(r.Min, r.Max, IM_COL32(255, 255, 0, 20), 0.0f); // FIXME-DRAG FIXME-STYLE
window->DrawList->AddRect(r.Min, r.Max, IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); // FIXME-DRAG FIXME-STYLE window->DrawList->AddRect(r.Min, r.Max, IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); // FIXME-DRAG FIXME-STYLE
} }
payload.AcceptFrameCount = g.FrameCount; g.DragDropAcceptFrameCount = g.FrameCount;
payload.Delivery = was_accepted_previously && IsMouseReleased(g.DragDropMouseButton); payload.Delivery = was_accepted_previously && IsMouseReleased(g.DragDropMouseButton);
if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery)) if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery))
return NULL; return NULL;

View File

@ -1153,15 +1153,12 @@ struct ImGuiPayload
// [Internal] // [Internal]
ImGuiID SourceId; // Source item id ImGuiID SourceId; // Source item id
ImGuiID SourceParentId; // Source parent id (if available) ImGuiID SourceParentId; // Source parent id (if available)
ImGuiID AcceptIdCurr; // Target item id (set at the time of accepting the payload)
ImGuiID AcceptIdPrev; // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets)
int AcceptFrameCount; // Last time a target expressed a desire to accept the source
int DataFrameCount; // Data timestamp int DataFrameCount; // Data timestamp
char DataType[8 + 1]; // Data type tag (short user-supplied string) char DataType[8 + 1]; // Data type tag (short user-supplied string)
bool Delivery; // Set when AcceptDragDropPayload() was called and the mouse button is released over the target item bool Delivery; // Set when AcceptDragDropPayload() was called and the mouse button is released over the target item
ImGuiPayload() { Clear(); } ImGuiPayload() { Clear(); }
void Clear() { SourceId = SourceParentId = AcceptIdCurr = AcceptIdPrev = 0; AcceptFrameCount = -1; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Delivery = false; } void Clear() { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Delivery = false; }
bool IsDataType(const char* type) const { return DataFrameCount != -1 && strcmp(type, DataType) == 0; } bool IsDataType(const char* type) const { return DataFrameCount != -1 && strcmp(type, DataType) == 0; }
bool IsDelivery() const { return Delivery; } bool IsDelivery() const { return Delivery; }
}; };

View File

@ -480,6 +480,9 @@ struct ImGuiContext
ImGuiDragDropFlags DragDropSourceFlags; ImGuiDragDropFlags DragDropSourceFlags;
int DragDropMouseButton; int DragDropMouseButton;
ImGuiPayload DragDropPayload; ImGuiPayload DragDropPayload;
ImGuiID DragDropAcceptIdCurr; // Target item id (set at the time of accepting the payload)
ImGuiID DragDropAcceptIdPrev; // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets)
int DragDropAcceptFrameCount; // Last time a target expressed a desire to accept the source
ImVector<unsigned char> DragDropPayloadBufHeap; // We don't expose the ImVector<> directly ImVector<unsigned char> DragDropPayloadBufHeap; // We don't expose the ImVector<> directly
unsigned char DragDropPayloadBufLocal[8]; unsigned char DragDropPayloadBufLocal[8];
@ -563,6 +566,8 @@ struct ImGuiContext
DragDropActive = false; DragDropActive = false;
DragDropSourceFlags = 0; DragDropSourceFlags = 0;
DragDropMouseButton = -1; DragDropMouseButton = -1;
DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0;
DragDropAcceptFrameCount = -1;
memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal));
ScalarAsInputTextId = 0; ScalarAsInputTextId = 0;