1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-29 01:44:37 +01:00

SelectableEx: use flags internally (similar to ButtonBehavior)

This commit is contained in:
ocornut 2015-05-26 16:12:40 +01:00
parent 4eeba016f6
commit cdb5e16f70

View File

@ -501,8 +501,9 @@ struct ImGuiTextEditState;
struct ImGuiIniData; struct ImGuiIniData;
struct ImGuiState; struct ImGuiState;
struct ImGuiWindow; struct ImGuiWindow;
typedef int ImGuiLayoutType; // enum ImGuiLayoutType_ typedef int ImGuiLayoutType; // enum ImGuiLayoutType_
typedef int ImGuiButtonFlags; // enum ImGuiButtonFlags_ typedef int ImGuiButtonFlags; // enum ImGuiButtonFlags_
typedef int ImGuiSelectableFlags; // enum ImGuiSelectableFlags_
static bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, ImGuiButtonFlags flags = 0); static bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, bool allow_key_modifiers, ImGuiButtonFlags flags = 0);
static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end = NULL); static void LogText(const ImVec2& ref_pos, const char* text, const char* text_end = NULL);
@ -959,6 +960,14 @@ enum ImGuiButtonFlags_
ImGuiButtonFlags_Disabled = (1 << 3) ImGuiButtonFlags_Disabled = (1 << 3)
}; };
enum ImGuiSelectableFlags_
{
ImGuiSelectableFlags_MenuItem = (1 << 0),
ImGuiSelectableFlags_DontClosePopups = (1 << 1),
ImGuiSelectableFlags_Disabled = (1 << 2)
};
struct ImGuiColMod // Color modifier, backup of modified data so we can restore it struct ImGuiColMod // Color modifier, backup of modified data so we can restore it
{ {
ImGuiCol Col; ImGuiCol Col;
@ -7288,7 +7297,7 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi
return value_changed; return value_changed;
} }
static bool SelectableEx(const char* label, bool selected, const ImVec2& size_arg, const ImVec2 size_draw_arg, bool menu_item = false, bool enabled = true, bool close_popups = true) static bool SelectableEx(const char* label, bool selected, const ImVec2& size_arg, const ImVec2 size_draw_arg, ImGuiSelectableFlags flags)
{ {
ImGuiState& g = *GImGui; ImGuiState& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
@ -7325,7 +7334,7 @@ static bool SelectableEx(const char* label, bool selected, const ImVec2& size_ar
return false; return false;
bool hovered, held; bool hovered, held;
bool pressed = ButtonBehavior(bb_with_spacing, id, &hovered, &held, true, (menu_item ? ImGuiButtonFlags_PressedOnClick : 0) | (enabled ? 0 : ImGuiButtonFlags_Disabled)); bool pressed = ButtonBehavior(bb_with_spacing, id, &hovered, &held, true, ((flags & ImGuiSelectableFlags_MenuItem) ? ImGuiButtonFlags_PressedOnClick : 0) | ((flags & ImGuiSelectableFlags_Disabled) ? ImGuiButtonFlags_Disabled : 0));
// Render // Render
if (hovered || selected) if (hovered || selected)
@ -7333,14 +7342,14 @@ static bool SelectableEx(const char* label, bool selected, const ImVec2& size_ar
const ImU32 col = window->Color((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); const ImU32 col = window->Color((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
RenderFrame(bb_with_spacing.Min, bb_with_spacing.Max, col, false, style.FrameRounding); RenderFrame(bb_with_spacing.Min, bb_with_spacing.Max, col, false, style.FrameRounding);
} }
if (!enabled) ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); if (flags & ImGuiSelectableFlags_Disabled) ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
RenderTextClipped(bb.Min, label, NULL, &label_size, bb_with_spacing.Max); RenderTextClipped(bb.Min, label, NULL, &label_size, bb_with_spacing.Max);
if (!enabled) ImGui::PopStyleColor(); if (flags & ImGuiSelectableFlags_Disabled) ImGui::PopStyleColor();
// Automatically close popups // Automatically close popups
if (close_popups && pressed && (window->Flags & ImGuiWindowFlags_ChildMenu)) if (pressed && (window->Flags & ImGuiWindowFlags_ChildMenu) && !(flags & ImGuiSelectableFlags_DontClosePopups))
CloseCurrentMenus(); CloseCurrentMenus();
else if (close_popups && pressed && (window->Flags & ImGuiWindowFlags_Popup)) else if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups))
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
return pressed; return pressed;
} }
@ -7349,12 +7358,12 @@ static bool SelectableEx(const char* label, bool selected, const ImVec2& size_ar
// But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID. // But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID.
bool ImGui::Selectable(const char* label, bool selected, const ImVec2& size_arg) bool ImGui::Selectable(const char* label, bool selected, const ImVec2& size_arg)
{ {
return SelectableEx(label, selected, size_arg, size_arg); return SelectableEx(label, selected, size_arg, size_arg, 0);
} }
bool ImGui::Selectable(const char* label, bool* p_selected, const ImVec2& size_arg) bool ImGui::Selectable(const char* label, bool* p_selected, const ImVec2& size_arg)
{ {
if (SelectableEx(label, *p_selected, size_arg, size_arg)) if (SelectableEx(label, *p_selected, size_arg, size_arg, 0))
{ {
*p_selected = !*p_selected; *p_selected = !*p_selected;
return true; return true;
@ -7470,7 +7479,7 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo
float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame
float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w); float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w);
bool pressed = SelectableEx(label, false, ImVec2(w, 0.0f), ImVec2(0.0f, 0.0f), true, enabled); bool pressed = SelectableEx(label, false, ImVec2(w, 0.0f), ImVec2(0.0f, 0.0f), ImGuiSelectableFlags_MenuItem | (!enabled ? ImGuiSelectableFlags_Disabled : 0));
if (shortcut_size.x > 0.0f) if (shortcut_size.x > 0.0f)
{ {
ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
@ -7583,7 +7592,7 @@ bool ImGui::BeginMenu(const char* label)
window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing * 2.0f); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing * 2.0f);
float w = label_size.x; float w = label_size.x;
pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(w, 0.0f), true, true, false); pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(w, 0.0f), ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DontClosePopups);
ImGui::PopStyleVar(); ImGui::PopStyleVar();
ImGui::SameLine(); ImGui::SameLine();
window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);
@ -7593,7 +7602,7 @@ bool ImGui::BeginMenu(const char* label)
popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y);
float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame
float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w); float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w);
pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(0.0f, 0.0f), true, true, false); pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(0.0f, 0.0f), ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DontClosePopups);
RenderCollapseTriangle(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.20f, 0.0f), false); RenderCollapseTriangle(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.20f, 0.0f), false);
} }