Browse Source

Tweaks. Combo default height_in_items is -1 at compilation time to match incoming ListSelect() behavior

pull/135/head
ocornut 10 years ago
parent
commit
c26af284db
  1. 35
      imgui.cpp
  2. 6
      imgui.h

35
imgui.cpp

@ -3609,8 +3609,8 @@ void ImGui::TextUnformatted(const char* text, const char* text_end)
{ {
// Long text! // Long text!
// Perform manual coarse clipping to optimize for long multi-line text // Perform manual coarse clipping to optimize for long multi-line text
// From this point we will only compute the width of lines that are visible. // From this point we will only compute the width of lines that are visible. Optimization only available when word-wrapping is disabled.
// Optimization only available when word-wrapping is disabled. // We also don't vertically center the text within the line full height, which is unlikely to matter because we are likely the biggest and only item on the line.
const char* line = text; const char* line = text;
const float line_height = ImGui::GetTextLineHeight(); const float line_height = ImGui::GetTextLineHeight();
const ImVec2 start_pos = window->DC.CursorPos; const ImVec2 start_pos = window->DC.CursorPos;
@ -5612,14 +5612,14 @@ static bool Items_SingleStringGetter(void* data, int idx, const char** out_text)
} }
// Combo box helper allowing to pass an array of strings. // Combo box helper allowing to pass an array of strings.
bool ImGui::Combo(const char* label, int* current_item, const char** items, int items_count, int popup_height_items) bool ImGui::Combo(const char* label, int* current_item, const char** items, int items_count, int height_in_items)
{ {
const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, popup_height_items); const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_in_items);
return value_changed; return value_changed;
} }
// Combo box helper allowing to pass all items in a single string. // Combo box helper allowing to pass all items in a single string.
bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_height_items) bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items)
{ {
int items_count = 0; int items_count = 0;
const char* p = items_separated_by_zeros; // FIXME-OPT: Avoid computing this const char* p = items_separated_by_zeros; // FIXME-OPT: Avoid computing this
@ -5628,12 +5628,12 @@ bool ImGui::Combo(const char* label, int* current_item, const char* items_separa
p += strlen(p) + 1; p += strlen(p) + 1;
items_count++; items_count++;
} }
bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, popup_height_items); bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, height_in_items);
return value_changed; return value_changed;
} }
// Combo box function. // Combo box function.
bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_height_items) bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items)
{ {
ImGuiState& g = *GImGui; ImGuiState& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow(); ImGuiWindow* window = GetCurrentWindow();
@ -5690,9 +5690,13 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi
if (g.ActiveComboID == id) if (g.ActiveComboID == id)
{ {
// Size default to hold ~7 items
if (height_in_items < 0)
height_in_items = 7;
const ImVec2 backup_pos = ImGui::GetCursorPos(); const ImVec2 backup_pos = ImGui::GetCursorPos();
const float popup_off_x = 0.0f;//style.ItemInnerSpacing.x; const float popup_off_x = 0.0f;//style.ItemInnerSpacing.x;
const float popup_height = (text_size.y + style.ItemSpacing.y) * ImMin(items_count, popup_height_items) + style.WindowPadding.y; const float popup_height = (text_size.y + style.ItemSpacing.y) * ImMin(items_count, height_in_items) + style.WindowPadding.y;
const ImGuiAabb popup_aabb(ImVec2(frame_bb.Min.x+popup_off_x, frame_bb.Max.y), ImVec2(frame_bb.Max.x+popup_off_x, frame_bb.Max.y + popup_height)); const ImGuiAabb popup_aabb(ImVec2(frame_bb.Min.x+popup_off_x, frame_bb.Max.y), ImVec2(frame_bb.Max.x+popup_off_x, frame_bb.Max.y + popup_height));
ImGui::SetCursorPos(popup_aabb.Min - window->Pos); ImGui::SetCursorPos(popup_aabb.Min - window->Pos);
@ -5718,11 +5722,8 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi
value_changed = true; value_changed = true;
*current_item = i; *current_item = i;
} }
if (item_selected) if (item_selected && menu_toggled)
{
if (menu_toggled)
ImGui::SetScrollPosHere(); ImGui::SetScrollPosHere();
}
combo_item_active |= ImGui::IsItemActive(); combo_item_active |= ImGui::IsItemActive();
ImGui::PopID(); ImGui::PopID();
} }
@ -5756,16 +5757,16 @@ bool ImGui::Selectable(const char* label, bool selected, const ImVec2& size_arg)
const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + size); const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + size);
ItemSize(bb); ItemSize(bb);
// Selectables are meant to be tightly packed together. But for both rendering and collision we extend to compensate for spacing. // Selectables are meant to be tightly packed together. So for both rendering and collision we extend to compensate for spacing.
ImGuiAabb bb_with_spacing = bb; ImGuiAabb bb_with_spacing = bb;
const float spacing_U = (float)(int)(style.ItemSpacing.y * 0.5f);
const float spacing_D = style.ItemSpacing.y - spacing_U;
const float spacing_L = (float)(int)(style.ItemSpacing.x * 0.5f); const float spacing_L = (float)(int)(style.ItemSpacing.x * 0.5f);
const float spacing_U = (float)(int)(style.ItemSpacing.y * 0.5f);
const float spacing_R = style.ItemSpacing.x - spacing_L; const float spacing_R = style.ItemSpacing.x - spacing_L;
bb_with_spacing.Min.y -= spacing_U; const float spacing_D = style.ItemSpacing.y - spacing_U;
bb_with_spacing.Min.x -= spacing_L; bb_with_spacing.Min.x -= spacing_L;
bb_with_spacing.Max.y += spacing_D; bb_with_spacing.Min.y -= spacing_U;
bb_with_spacing.Max.x += spacing_R; bb_with_spacing.Max.x += spacing_R;
bb_with_spacing.Max.y += spacing_D;
if (!ItemAdd(bb_with_spacing, &id)) if (!ItemAdd(bb_with_spacing, &id))
return false; return false;

6
imgui.h

@ -281,9 +281,9 @@ namespace ImGui
IMGUI_API bool InputFloat3(const char* label, float v[3], int decimal_precision = -1); IMGUI_API bool InputFloat3(const char* label, float v[3], int decimal_precision = -1);
IMGUI_API bool InputFloat4(const char* label, float v[4], int decimal_precision = -1); IMGUI_API bool InputFloat4(const char* label, float v[4], int decimal_precision = -1);
IMGUI_API bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags extra_flags = 0); IMGUI_API bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags extra_flags = 0);
IMGUI_API bool Combo(const char* label, int* current_item, const char** items, int items_count, int popup_height_items = 7); IMGUI_API bool Combo(const char* label, int* current_item, const char** items, int items_count, int height_in_items = -1);
IMGUI_API bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_height_items = 7); // separate items with \0, end item-list with \0\0 IMGUI_API bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items = -1); // separate items with \0, end item-list with \0\0
IMGUI_API bool Combo(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_height_items = 7); IMGUI_API bool Combo(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1);
IMGUI_API bool Selectable(const char* label, bool selected, const ImVec2& size = ImVec2(0,0)); IMGUI_API bool Selectable(const char* label, bool selected, const ImVec2& size = ImVec2(0,0));
IMGUI_API bool Selectable(const char* label, bool* p_selected, const ImVec2& size = ImVec2(0,0)); IMGUI_API bool Selectable(const char* label, bool* p_selected, const ImVec2& size = ImVec2(0,0));
IMGUI_API bool ColorButton(const ImVec4& col, bool small_height = false, bool outline_border = true); IMGUI_API bool ColorButton(const ImVec4& col, bool small_height = false, bool outline_border = true);

Loading…
Cancel
Save