Repository navigation
Open
Labels
Description
This is more a proof of concept that a finished api.
#include <imgui_internal.h>
// Return >= 0 on mouse release
// Optional int* p_selected display and update a currently selected item
int PiePopupSelectMenu(const ImVec2& center, const char* popup_id, const char** items, int items_count, int* p_selected)
{
int ret = -1;
// FIXME: Missing a call to query if Popup is open so we can move the PushStyleColor inside the BeginPopupBlock (e.g. IsPopupOpen() in imgui.cpp)
// FIXME: Our PathFill function only handle convex polygons, so we can't have items spanning an arc too large else inner concave edge artifact is too visible, hence the ImMax(7,items_count)
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0,0,0,0));
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0,0,0,0));
if (ImGui::BeginPopup(popup_id))
{
const ImVec2 drag_delta = ImVec2(ImGui::GetIO().MousePos.x - center.x, ImGui::GetIO().MousePos.y - center.y);
const float drag_dist2 = drag_delta.x*drag_delta.x + drag_delta.y*drag_delta.y;
const ImGuiStyle& style = ImGui::GetStyle();
const float RADIUS_MIN = 30.0f;
const float RADIUS_MAX = 120.0f;
const float RADIUS_INTERACT_MIN = 20.0f;
const int ITEMS_MIN = 6;
ImDrawList* draw_list = ImGui::GetWindowDrawList();
//ImGuiWindow* window = ImGui::GetCurrentWindow();
draw_list->PushClipRectFullScreen();
draw_list->PathArcTo(center, (RADIUS_MIN + RADIUS_MAX)*0.5f, 0.0f, IM_PI*2.0f*0.99f, 32); // FIXME: 0.99f look like full arc with closed thick stroke has a bug now
draw_list->PathStroke(ImColor(0,0,0), true, RADIUS_MAX - RADIUS_MIN);
const float item_arc_span = 2*IM_PI / ImMax(ITEMS_MIN, items_count);
float drag_angle = atan2f(drag_delta.y, drag_delta.x);
if (drag_angle < -0.5f*item_arc_span)
drag_angle += 2.0f*IM_PI;
//ImGui::Text("%f", drag_angle); // [Debug]
int item_hovered = -1;
for (int item_n = 0; item_n < items_count; item_n++)
{
const char* item_label = items[item_n];
const float item_ang_min = item_arc_span * (item_n+0.02f) - item_arc_span*0.5f; // FIXME: Could calculate padding angle based on how many pixels they'll take
const float item_ang_max = item_arc_span * (item_n+0.98f) - item_arc_span*0.5f;
bool hovered = false;
if (drag_dist2 >= RADIUS_INTERACT_MIN*RADIUS_INTERACT_MIN)
{
if (drag_angle >= item_ang_min && drag_angle < item_ang_max)
hovered = true;
}
bool selected = p_selected && (*p_selected == item_n);
int arc_segments = (int)(32 * item_arc_span / (2*IM_PI)) + 1;
draw_list->PathArcTo(center, RADIUS_MAX - style.ItemInnerSpacing.x, item_ang_min, item_ang_max, arc_segments);
draw_list->PathArcTo(center, RADIUS_MIN + style.ItemInnerSpacing.x, item_ang_max, item_ang_min, arc_segments);
//draw_list->PathFill(window->Color(hovered ? ImGuiCol_HeaderHovered : ImGuiCol_FrameBg));
draw_list->PathFill(hovered ? ImColor(100,100,150) : selected ? ImColor(120,120,140) : ImColor(70,70,70));
ImVec2 text_size = ImGui::GetWindowFont()->CalcTextSizeA(ImGui::GetWindowFontSize(), FLT_MAX, 0.0f, item_label);
ImVec2 text_pos = ImVec2(
center.x + cosf((item_ang_min + item_ang_max) * 0.5f) * (RADIUS_MIN + RADIUS_MAX) * 0.5f - text_size.x * 0.5f,
center.y + sinf((item_ang_min + item_ang_max) * 0.5f) * (RADIUS_MIN + RADIUS_MAX) * 0.5f - text_size.y * 0.5f);
draw_list->AddText(text_pos, ImColor(255,255,255), item_label);
if (hovered)
item_hovered = item_n;
}
draw_list->PopClipRect();
if (ImGui::IsMouseReleased(0))
{
ImGui::CloseCurrentPopup();
ret = item_hovered;
if (p_selected)
*p_selected = item_hovered;
}
ImGui::EndPopup();
}
ImGui::PopStyleColor(2);
return ret;
}
{
static const char* test_data = "Menu";
const char* items[] = { "Orange", "Blue", "Purple", "Gray", "Yellow", "Las Vegas" };
int items_count = sizeof(items)/sizeof(*items);
static int selected = -1;
ImGui::Button(selected >= 0 ? items[selected] : "Menu", ImVec2(50,50));
if (ImGui::IsItemActive()) // Don't wait for button release to activate the pie menu
ImGui::OpenPopup("##piepopup");
ImVec2 pie_menu_center = ImGui::GetIO().MouseClickedPos[0];
int n = PiePopupSelectMenu(pie_menu_center, "##piepopup", items, items_count, &selected);
if (n >= 0)
printf("returned %d\n", n);
Reacted by Gabriel Tavares, Bulat-Ziganshin, Naiyang Lin, well.james, fromasmtodisasm, Delphinidae84, Toivo Kallio, LunaUI and Djuned Djusdek

Activity
lundmark commentedon Dec 13, 2015
Looking really good! Will you be adding it to the lib or will it just be a snippet?
ocornut commentedon Dec 13, 2015
Eventually it should be added but it's not complete nor good enough yet. In order to support submenus the API would likely need to be changed?
lundmark commentedon Dec 13, 2015
What would submenues within a pie-popup look like?
ocornut commentedon Dec 13, 2015
I'm not sure. Open a second pie-popup over the center of the previous button? That would suggest that merely holding mouse would open the sub-menu (like normal menu), which in turn ask the question of how we can get back to the parent menu.
I haven't spend time to think about it in details, just implemented something as fast as I can for someone, which is essentially why this is a proof of concept rather than a feature.
lundmark commentedon Dec 14, 2015
Another idea would perhaps be to expand the pie outwards, presenting a top-down menu. The amount of choices could be increased by increasing the size of the extended pie-slice upwards/downwards?
Horrowind commentedon Dec 14, 2015
It always bothered me, that the border between the items increased from the inner ring to the outer, so here is a fix for that (only listing the inner rendering for-loop):
Furthermore here are inspirational videos from other pie menu implementations:
https://www.youtube.com/watch?v=nXh1Tm24kTE&t=50s
the submenu implementation from this one is probably not advisable, but I like how it adds a second ring if there are more then six items.
https://www.youtube.com/watch?v=Job4Rg-sbDo
this is the one in OneNote; it has some kind of submenu implementation, but from the video, I am unable to understand how it works.
ocornut commentedon Dec 14, 2015
Thanks @Horrowind, really useful!
ocornut commentedon Dec 17, 2015
You should be able to, the pie-menu is its own window. Krys please provide more detailed information and standalone repro code with your questions when possible! Lots of your questions are really unclear, if you can't clarify the question then please provide the smallest set of code that demonstrate the issue you have. Thanks!
jarikomppa commentedon Dec 17, 2015
What he means is, start a new, tiny application that just reproduces a
single problem.
On Thu, Dec 17, 2015 at 12:25 PM, krys-spectralpixel <
notifications@github.com> wrote:
ocornut commentedon Dec 17, 2015
It is your job to provide a repro to ensure that your request is well formed and thought up. It also in many cases helps you understand the issue better. Your code above is not a repro I can paste.
Have you noticed that my pie code above close automatically if mouse is released?
This works without a window for me:
ocornut commentedon Dec 29, 2015
I did that intentionally to avoid false negatives and also considering that pie menus have a "gesture" feel to them. e.g. click and drag left, release, by not worrying about distance travelled you can accomplish faster actions. Maybe it isn't as important with mouse controls. Both ways have legit uses, it could be a global setting of the pie menus potentially.
8 remaining items
ocornut commentedon Dec 24, 2017
@thennequin
There's a helper ImGuiTextBuffer which is close to what you are doing.
Maybe a stripped down version of https://github.com/ocornut/Str would generally be useful, but if you can use a single buffer like ImGuiTextBuffer it helps.
I've been considering it, perhaps a way for a external subsystem to register a slot instead an array of pointers stored in ImGuiContext, so you can store one PieMenuContext per ImGuiContext. Is that were you were thinking of?
thennequin commentedon Dec 25, 2017
I use an ImVector< char > for storing strings.
Sorry for the misunderstanding of the first question, I just wanted a way to add strings (like bool/int/float/ptr) in the ImGuiStorage for avoiding the use of a new external struct because I store bool/float/int and string.
Yes, I want to store one PieMenuContext per ImGuiContext.
Stanlyhalo commentedon Nov 5, 2021
@thennequin You probably haven't updated this in a while nor remember how you wrote it, but it's worth the shot. I tried your implementation, and I've seen only 1 issue, and a weird quirk I wanted to see if I can change. The issue is that there is no background color, and yes, I've tried to change the color, didn't budge, and the quirk is that if it goes a little offscreen, it pushes itself away from the border, if I wanted to remove this quirk, how would I go about doing that?
thennequin commentedon Nov 5, 2021
I updated the gist to fix the "quirk" (if you talk about the black rectangle) and I fixed the background (wrong UV).
tmsrise commentedon Jul 14, 2022
does this work with controller navigation?
ocornut commentedon Jul 14, 2022
tmsrise commentedon Jul 19, 2022
For those of you that want to implement controller support:
There is no ImGuiKey_GamepadLStickX, but there is ImGuiKey_GamepadLStickUp, down, left, right, etc, so you need to subtract.
Implement hovered memory so that releasing the joystick keeps the highlight and subsequent return when you close the menu.
Mind the inner_spacing gaps. For controllers it creates awkward locations inbetween elements that results in no selection. Either remove it or create separate floats for the actual selectable and visual representation of segments if you want a clean look without usability sacrifice.
Implement the controller equivalent to the minimum drag distance (deadzone) so that the kickback from releasing a thumbstick doesn't inadvertently select a different element. I've found |x| or |y| greater than 0.6 works well.
berthubert commentedon Jan 20, 2023
This could maybe better live within ImPlot? https://github.com/epezent/implot
D7ry commentedon Jun 3, 2023
Hi! just dropping by to say thanks for the initial idea. I've managed to make sth cool with it
tmsrise commentedon Jun 3, 2023
Nice! I used it to make a combat art + prosthetic wheel in Sekiro. Yours seems much more polished though. I was considering doing a rewrite that would fix the bugs, crashes, and terrible spaghetti code. Mind if I yoink parts of that with credit? (if I even get to it lol)
D7ry commentedon Jun 4, 2023
@tmsrise It's MIT licensed so take anything you'd like :)
ocornut commentedon Apr 16, 2024
Posting a minor update to the 2015 version (some code simplification) note however that it is functionally the same.
I reckon Thibault's version may be better suited: #434 (comment)
Usage:
mumin161616 commentedon Mar 23, 2025
ugly arrow draw in center in linux.
hippyau commentedon Apr 11, 2025
Yeah, that's weird. I commented out
But it now has no background.