Skip to content

Instantly share code, notes, and snippets.

@thennequin
Created October 5, 2016 09:19
Show Gist options
  • Save thennequin/e4804573cdb3e56cab72e0f9d7027945 to your computer and use it in GitHub Desktop.
Save thennequin/e4804573cdb3e56cab72e0f9d7027945 to your computer and use it in GitHub Desktop.
int BeginCombo(const char* pLabel, const char* pValue)
{
ImGuiWindow* pWindow = ImGui::GetCurrentWindow();
ImGuiState& oState = *GImGui;
const ImGuiStyle& oStyle = oState.Style;
const ImGuiID oId = pWindow->GetID(pLabel);
const float fWidth = ImGui::CalcItemWidth();
const ImVec2 oLabelSize = ImGui::CalcTextSize(pLabel, NULL, true);
const ImRect oFrameRect(pWindow->DC.CursorPos, pWindow->DC.CursorPos + ImVec2(fWidth, oLabelSize.y) + oStyle.FramePadding * 2.0f);
const ImRect oTotalRect(oFrameRect.Min, oFrameRect.Max + ImVec2(oLabelSize.x > 0.0f ? oStyle.ItemInnerSpacing.x + oLabelSize.x : 0.0f, 0.0f));
ImGui::ItemSize(oTotalRect, oStyle.FramePadding.y);
if (!ImGui::ItemAdd(oTotalRect, &oId))
return false;
const float fArrowSize = (oState.FontSize + oStyle.FramePadding.x * 2.0f);
const bool bHovered = ImGui::IsHovered(oFrameRect, oId);
const ImRect oValueRect(oFrameRect.Min, oFrameRect.Max - ImVec2(fArrowSize, 0.0f));
ImGui::RenderFrame(oFrameRect.Min, oFrameRect.Max, pWindow->Color(ImGuiCol_FrameBg), true, oStyle.FrameRounding);
ImGui::RenderFrame(ImVec2(oFrameRect.Max.x-fArrowSize, oFrameRect.Min.y), oFrameRect.Max, pWindow->Color(bHovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button), true, oStyle.FrameRounding); // FIXME-ROUNDING
ImGui::RenderCollapseTriangle(ImVec2(oFrameRect.Max.x-fArrowSize, oFrameRect.Min.y) + oStyle.FramePadding, true);
ImGui::RenderTextClipped(oFrameRect.Min + oStyle.FramePadding, oValueRect.Max, pValue, NULL, NULL);
if (oLabelSize.x > 0)
ImGui::RenderText(ImVec2(oFrameRect.Max.x + oStyle.ItemInnerSpacing.x, oFrameRect.Min.y + oStyle.FramePadding.y), pLabel);
bool bJustOpened = false;
if (bHovered)
{
ImGui::SetHoveredID(oId);
if (oState.IO.MouseClicked[0])
{
ImGui::SetActiveID(0, pWindow);
ImGui::OpenPopup(pLabel);
ImGui::SetNextWindowPos(ImVec2(oTotalRect.Min.x, oTotalRect.Max.y));
bJustOpened = true;
}
}
//TODO: Use BeginPopupEx but in next version of ImGui
return ImGui::BeginPopup(pLabel) ? bJustOpened ? 2 : 1 : 0;
}
void EndCombo()
{
ImGui::EndPopup();
}
/* Exemple
if (BeginCombo("My combobox", "Value"))
{
ImGui::MenuItem("Value");
ImGui::MenuItem("Value2");
ImGui::MenuItem("Value3");
EndCombo();
}
*/
static int BeginCombo(const char* pLabel, const char* pValue);
static void EndCombo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment