IMGUI_API bool TabLabels(const char ** tabLabels, int tabSize, int & tabIndex, int * tabOrder)
{
ImGuiStyle & style = ImGui::GetStyle();
const ImVec2 itemSpacing = style.ItemSpacing;
const ImVec4 color = style.Colors[ImGuiCol_Button];
const ImVec4 colorActive = style.Colors[ImGuiCol_ButtonActive];
const ImVec4 colorHover = style.Colors[ImGuiCol_ButtonHovered];
const ImVec4 colorText = style.Colors[ImGuiCol_Text];
style.ItemSpacing.x = 1;
style.ItemSpacing.y = 1;
style.ButtonTextAlign.y = 0.5;
const ImVec4 colorSelectedTab = ImVec4(color.x, color.y, color.z, color.w * 0.5f);
const ImVec4 colorSelectedTabHovered = ImVec4(colorHover.x, colorHover.y, colorHover.z, colorHover.w * 0.5f);
const ImVec4 colorSelectedTabText = ImVec4(colorText.x * 0.8f, colorText.y * 0.8f, colorText.z * 0.8f, colorText.w * 0.8f);
if (tabSize> 0 && (tabIndex <0 || tabIndex >= tabSize))
{
if (!tabOrder)
{
tabIndex = 0;
}
else
{
tabIndex = -1;
}
}
float windowWidth = 0.f, sumX = 0.f;
windowWidth = ImGui::GetWindowWidth() - style.WindowPadding.x - (ImGui::GetScrollMaxY()> 0 ? style.ScrollbarSize : 0.f);
static int draggingTabIndex = -1; int draggingTabTargetIndex = -1;
static ImVec2 draggingtabSize(0, 0);
static ImVec2 draggingTabOffset(0, 0);
const bool isMMBreleased = ImGui::IsMouseReleased(2);
const bool isMouseDragging = ImGui::IsMouseDragging(0, 2.f);
int justClosedTabIndex = -1, newtabIndex = tabIndex;
bool selection_changed = false; bool noButtonDrawn = true;
for (int j = 0, i; j <tabSize; j++)
{
i = tabOrder ? tabOrder[j] : j;
if (i == -1) continue;
if (sumX> 0.f)
{
sumX += style.ItemSpacing.x;
sumX += ImGui::CalcTextSize(tabLabels[i]).x;
if (sumX> windowWidth)
{
sumX = 0.f;
}
else
{
}
}
if (i != tabIndex)
{
// Push the style
style.Colors[ImGuiCol_Button] = colorSelectedTab;
style.Colors[ImGuiCol_ButtonActive] = colorSelectedTab;
style.Colors[ImGuiCol_ButtonHovered] = colorSelectedTabHovered;
style.Colors[ImGuiCol_Text] = colorSelectedTabText;
}
// Draw the button
ImGui::PushID(i); // otherwise two tabs with the same name would clash.
if (ImGui::Button(tabLabels[i], ImVec2(114.91f, 28.14f))) { selection_changed = (tabIndex != i); newtabIndex = i; } // if you want to change the button width and hegiht x = width, y = height .;
ImGui::PopID();
if (i != tabIndex)
{
// Reset the style
style.Colors[ImGuiCol_Button] = color;
style.Colors[ImGuiCol_ButtonActive] = colorActive;
style.Colors[ImGuiCol_ButtonHovered] = colorHover;
style.Colors[ImGuiCol_Text] = colorText;
}
noButtonDrawn = false;
if (sumX == 0.f) sumX = style.WindowPadding.x + ImGui::GetItemRectSize().x; // First element of a line
if (ImGui::IsItemHoveredRect())
{
if (tabOrder)
{
// tab reordering
if (isMouseDragging)
{
if (draggingTabIndex == -1)
{
draggingTabIndex = j;
draggingtabSize = ImGui::GetItemRectSize();
const ImVec2 & mp = ImGui::GetIO().MousePos;
const ImVec2 draggingTabCursorPos = ImGui::GetCursorPos();
draggingTabOffset = ImVec2(
mp.x + draggingtabSize.x * 0.5f - sumX + ImGui::GetScrollX(),
mp.y + draggingtabSize.y * 0.5f - draggingTabCursorPos.y + ImGui::GetScrollY()
);
}
}
else if (draggingTabIndex >= 0 && draggingTabIndex <tabSize && draggingTabIndex != j)
{
draggingTabTargetIndex = j; // For some odd reasons this seems to get called only when draggingTabIndex < i ! (Probably during mouse dragging ImGui owns the mouse someway and sometimes ImGui::IsItemHovered() is not getting called)
}
}
}
}
tabIndex = newtabIndex;
// Restore the style
style.Colors[ImGuiCol_Button] = color;
style.Colors[ImGuiCol_ButtonActive] = colorActive;
style.Colors[ImGuiCol_ButtonHovered] = colorHover;
style.Colors[ImGuiCol_Text] = colorText;
style.ItemSpacing = itemSpacing;
return selection_changed;
}