Skip to content

Commit

Permalink
More cosmetic changes. Also TreeNode now copies string passed to it i…
Browse files Browse the repository at this point in the history
…nstead of taking ownership of passed string.
  • Loading branch information
rokups committed Oct 3, 2019
1 parent 2f092de commit 0113a67
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 54 deletions.
12 changes: 6 additions & 6 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ ImGuiStyle::ImGuiStyle()
ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right.
ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text.
SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text when button is larger than text.
DragDropAcceptPadding = ImVec2(3.5f, 3.5f);// Inner padding of rectangle drawn around area that accepts drag&drop payload. FIXME-DRAG: Settle on a proper default visuals for drop target.
DragDropTargetPadding = ImVec2(3.5f, 3.5f);// Inner padding of rectangle drawn around area that accepts drag&drop payload. FIXME-DRAG: Settle on a proper default visuals for drop target.
DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows.
DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows.
MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.
Expand Down Expand Up @@ -1191,7 +1191,7 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor)
DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor);
DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor);
MouseCursorScale = ImFloor(MouseCursorScale * scale_factor);
DragDropAcceptPadding = ImFloor(DragDropAcceptPadding * scale_factor);
DragDropTargetPadding = ImFloor(DragDropTargetPadding * scale_factor);
}

ImGuiIO::ImGuiIO()
Expand Down Expand Up @@ -2509,15 +2509,15 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con

float ellipsis_glyph_width = glyph->X1; // Width of the glyph with no padding on either side
float ellipsis_total_width = ellipsis_glyph_width; // Full width of entire ellipsis

if (ellipsis_char_count > 1)
{
// Full ellipsis size without free spacing after it.
const float spacing_between_dots = 1.0f * (draw_list->_Data->FontSize / font->FontSize);
ellipsis_glyph_width = glyph->X1 - glyph->X0 + spacing_between_dots;
ellipsis_total_width = ellipsis_glyph_width * (float)ellipsis_char_count - spacing_between_dots;
}

// We can now claim the space between pos_max.x and ellipsis_max.x
const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_total_width) - pos_min.x, 1.0f);
float text_size_clipped_x = font->CalcTextSizeA(font_size, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x;
Expand Down Expand Up @@ -6385,7 +6385,7 @@ static const ImGuiStyleVarInfo GStyleVarInfo[] =
{ ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, DragDropAcceptPadding) },// ImGuiStyleVar_DragDropAcceptPadding
{ ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, DragDropTargetPadding) },// ImGuiStyleVar_DragDropTargetPadding
};

static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx)
Expand Down Expand Up @@ -9140,7 +9140,7 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop
if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview)
{
if (!(flags & ImGuiDragDropFlags_AcceptNoPadding))
r.Expand(g.Style.DragDropAcceptPadding);
r.Expand(g.Style.DragDropTargetPadding);
bool push_clip_rect = !window->ClipRect.Contains(r);
if (push_clip_rect) window->DrawList->PushClipRect(r.Min-ImVec2(1,1), r.Max+ImVec2(1,1));
window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, ~0, 2.0f);
Expand Down
4 changes: 2 additions & 2 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ enum ImGuiStyleVar_
ImGuiStyleVar_TabRounding, // float TabRounding
ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign
ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign
ImGuiStyleVar_DragDropAcceptPadding,// ImVec2 DragDropAcceptPadding
ImGuiStyleVar_DragDropTargetPadding,// ImVec2 DragDropTargetPadding
ImGuiStyleVar_COUNT

// Obsolete names (will be removed)
Expand Down Expand Up @@ -1321,7 +1321,7 @@ struct ImGuiStyle
ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right.
ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered).
ImVec2 SelectableTextAlign; // Alignment of selectable text when selectable is larger than text. Defaults to (0.0f, 0.0f) (top-left aligned).
ImVec2 DragDropAcceptPadding; // Inner padding of rectangle drawn around area that accepts drag&drop payload.
ImVec2 DragDropTargetPadding; // Inner padding of rectangle drawn around area that accepts drag&drop payload.
ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows.
ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly!
float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.
Expand Down
91 changes: 45 additions & 46 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,36 +192,38 @@ static void ShowDemoWindowMisc();

struct TreeNode
{
const char* name;
TreeNode* parent;
ImVector<TreeNode*> children;
const char* Name;
TreeNode* Parent;
ImVector<TreeNode*> Children;

TreeNode(const char* nodeName, TreeNode* parentNode=NULL)
TreeNode(const char* node_name, TreeNode* parent_node=NULL)
{
name = nodeName;
parent = parentNode;
char* name = (char*)IM_ALLOC(strlen(node_name));
strcpy(name, node_name);
Name = name;
Parent = parent_node;
}

~TreeNode()
{
free((void*)name);
name = NULL;
parent = NULL;
for (int i = 0; i < children.Size; i++)
delete children[i];
children.clear();
IM_FREE((void*)Name);
Name = NULL;
Parent = NULL;
for (int i = 0; i < Children.Size; i++)
IM_DELETE(Children[i]);
Children.clear();
}

TreeNode* AddChild(const char* name)
{
children.push_back(new TreeNode(name, this));
return children.back();
Children.push_back(IM_NEW(TreeNode(name, this)));
return Children.back();
}

bool IsDescendantOf(TreeNode* node)
{
bool is_descendant = false;
for (TreeNode* n = this; !is_descendant && n->parent != NULL; n = n->parent)
for (TreeNode* n = this; !is_descendant && n->Parent != NULL; n = n->Parent)
is_descendant = n == node;
return is_descendant;
}
Expand All @@ -232,18 +234,18 @@ struct TreeNode
static void RenderReorderTree(TreeNode* node)
{
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
bool open = ImGui::TreeNode(node->name);
bool open = ImGui::TreeNode(node->Name);

if (node->parent != NULL) // Can not move root node itself
if (node->Parent != NULL) // Can not move root node itself
{
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID))
{
ImGui::SetDragDropPayload("animal", &node, sizeof(node));
ImGui::TextUnformatted(node->name);
ImGui::TextUnformatted(node->Name);
ImGui::EndDragDropSource();
}
}

// Prevent dropping a parent node on to one of it's children.
bool acceptable = false;
TreeNode* dropped = NULL;
Expand All @@ -259,10 +261,10 @@ static void RenderReorderTree(TreeNode* node)
// Drop directly on to node and append to the end of it's children list.
if (ImGui::AcceptDragDropPayload("animal"))
{
if (dropped->parent->children.find_erase(dropped))
if (dropped->Parent->Children.find_erase(dropped))
{
node->children.push_back(dropped);
dropped->parent = node;
node->Children.push_back(dropped);
dropped->Parent = node;
}
}
ImGui::EndDragDropTarget();
Expand All @@ -271,27 +273,27 @@ static void RenderReorderTree(TreeNode* node)
if (open)
{
// Drop at specific position within a tree.
for (int i = -1; i < node->children.Size; i++)
for (int i = -1; i < node->Children.Size; i++)
{
if (i >= 0)
RenderReorderTree(node->children[i]);
RenderReorderTree(node->Children[i]);

if (acceptable && ImGui::AcceptReorderDropPayload("animal"))
{
// This offset compensates for i starting at -1, however if we are moving a node within same parent
// and it is located at or before our destination position, deletion of this node does compensate
// for this offset already.
// and it is located at or before our destination position, deletion of this node does compensate
// for this offset already.
int offset = 1;
if (node->children.contains(dropped))
if (node->Children.contains(dropped))
{
int prev_i = node->children.index_from_ptr(node->children.find(dropped));
int prev_i = node->Children.index_from_ptr(node->Children.find(dropped));
if (prev_i <= i)
offset = 0;
}

dropped->parent->children.find_erase(dropped);
node->children.insert(node->children.begin() + i + offset, dropped);
dropped->parent = node;
dropped->Parent->Children.find_erase(dropped);
node->Children.insert(node->Children.begin() + i + offset, dropped);
dropped->Parent = node;
}
}
ImGui::TreePop();
Expand Down Expand Up @@ -1771,20 +1773,17 @@ static void ShowDemoWindowWidgets()
if (root == NULL)
{
// Initialize tree with sample data
const char* colors[] = {"Red", "Green", "Blue", "Black"};
const char* animals[] = {"Seal", "Wolf", "Crow", "Horse"};
char name_buffer[32];
const char* colors[] = { "Red", "Green", "Blue", "Black" };
const char* animals[] = { "Seal", "Wolf", "Crow", "Horse" };
root = new TreeNode("Root");
for (int i = 0; i < 4; i++)
{
TreeNode* parent = root->AddChild(colors[i]);
for (int j = 0; j < 4; j++)
{
char* name = (char*)malloc(strlen(colors[i]) + strlen(animals[j]) + 1);
*name = 0;
strcat(name, colors[i]);
strcat(name, " ");
strcat(name, animals[j]);
parent->AddChild(name);
sprintf(name_buffer, "%s %s", colors[i], animals[j]);
parent->AddChild(name_buffer);
}
}
}
Expand Down Expand Up @@ -1819,10 +1818,10 @@ static void ShowDemoWindowWidgets()
if (item_type == 10){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy.
if (item_type == 11){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", &current, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); }

// Display the value of IsItemHovered() and other common item state functions.
// Display the value of IsItemHovered() and other common item state functions.
// Note that the ImGuiHoveredFlags_XXX flags can be combined.
// Because BulletText is an item itself and that would affect the output of IsItemXXX functions,
// we query every state in a single call to avoid storing them and to simplify the code
// Because BulletText is an item itself and that would affect the output of IsItemXXX functions,
// we query every state in a single call to avoid storing them and to simplify the code
ImGui::BulletText(
"Return value = %d\n"
"IsItemFocused() = %d\n"
Expand Down Expand Up @@ -1865,7 +1864,7 @@ static void ShowDemoWindowWidgets()
if (embed_all_inside_a_child_window)
ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20), true);

// Testing IsWindowFocused() function with its various flags.
// Testing IsWindowFocused() function with its various flags.
// Note that the ImGuiFocusedFlags_XXX flags can be combined.
ImGui::BulletText(
"IsWindowFocused() = %d\n"
Expand Down

0 comments on commit 0113a67

Please sign in to comment.