| // dear imgui, v1.75 WIP |
| // (widgets code) |
| |
| /* |
| |
| Index of this file: |
| |
| // [SECTION] Forward Declarations |
| // [SECTION] Widgets: Text, etc. |
| // [SECTION] Widgets: Main (Button, Image, Checkbox, RadioButton, ProgressBar, Bullet, etc.) |
| // [SECTION] Widgets: Low-level Layout helpers (Spacing, Dummy, NewLine, Separator, etc.) |
| // [SECTION] Widgets: ComboBox |
| // [SECTION] Data Type and Data Formatting Helpers |
| // [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc. |
| // [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. |
| // [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. |
| // [SECTION] Widgets: InputText, InputTextMultiline |
| // [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. |
| // [SECTION] Widgets: TreeNode, CollapsingHeader, etc. |
| // [SECTION] Widgets: Selectable |
| // [SECTION] Widgets: ListBox |
| // [SECTION] Widgets: PlotLines, PlotHistogram |
| // [SECTION] Widgets: Value helpers |
| // [SECTION] Widgets: MenuItem, BeginMenu, EndMenu, etc. |
| // [SECTION] Widgets: BeginTabBar, EndTabBar, etc. |
| // [SECTION] Widgets: BeginTabItem, EndTabItem, etc. |
| // [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc. |
| |
| */ |
| |
| #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| #define _CRT_SECURE_NO_WARNINGS |
| #endif |
| |
| #include "imgui.h" |
| #ifndef IMGUI_DISABLE |
| |
| #ifndef IMGUI_DEFINE_MATH_OPERATORS |
| #define IMGUI_DEFINE_MATH_OPERATORS |
| #endif |
| #include "imgui_internal.h" |
| |
| #include <ctype.h> // toupper |
| #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier |
| #include <stddef.h> // intptr_t |
| #else |
| #include <stdint.h> // intptr_t |
| #endif |
| |
| // Visual Studio warnings |
| #ifdef _MSC_VER |
| #pragma warning (disable: 4127) // condition expression is constant |
| #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen |
| #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later |
| #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types |
| #endif |
| #endif |
| |
| // Clang/GCC warnings with -Weverything |
| #if defined(__clang__) |
| #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. |
| #pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. |
| #pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. |
| #pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // |
| #if __has_warning("-Wzero-as-null-pointer-constant") |
| #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 |
| #endif |
| #if __has_warning("-Wdouble-promotion") |
| #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. |
| #endif |
| #elif defined(__GNUC__) |
| #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind |
| #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked |
| #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead |
| #endif |
| |
| //------------------------------------------------------------------------- |
| // Data |
| //------------------------------------------------------------------------- |
| |
| // Those MIN/MAX values are not define because we need to point to them |
| static const signed char IM_S8_MIN = -128; |
| static const signed char IM_S8_MAX = 127; |
| static const unsigned char IM_U8_MIN = 0; |
| static const unsigned char IM_U8_MAX = 0xFF; |
| static const signed short IM_S16_MIN = -32768; |
| static const signed short IM_S16_MAX = 32767; |
| static const unsigned short IM_U16_MIN = 0; |
| static const unsigned short IM_U16_MAX = 0xFFFF; |
| static const ImS32 IM_S32_MIN = INT_MIN; // (-2147483647 - 1), (0x80000000); |
| static const ImS32 IM_S32_MAX = INT_MAX; // (2147483647), (0x7FFFFFFF) |
| static const ImU32 IM_U32_MIN = 0; |
| static const ImU32 IM_U32_MAX = UINT_MAX; // (0xFFFFFFFF) |
| #ifdef LLONG_MIN |
| static const ImS64 IM_S64_MIN = LLONG_MIN; // (-9223372036854775807ll - 1ll); |
| static const ImS64 IM_S64_MAX = LLONG_MAX; // (9223372036854775807ll); |
| #else |
| static const ImS64 IM_S64_MIN = -9223372036854775807LL - 1; |
| static const ImS64 IM_S64_MAX = 9223372036854775807LL; |
| #endif |
| static const ImU64 IM_U64_MIN = 0; |
| #ifdef ULLONG_MAX |
| static const ImU64 IM_U64_MAX = ULLONG_MAX; // (0xFFFFFFFFFFFFFFFFull); |
| #else |
| static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); |
| #endif |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Forward Declarations |
| //------------------------------------------------------------------------- |
| |
| // For InputTextEx() |
| static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data); |
| static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); |
| static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Widgets: Text, etc. |
| //------------------------------------------------------------------------- |
| // - TextEx() [Internal] |
| // - TextUnformatted() |
| // - Text() |
| // - TextV() |
| // - TextColored() |
| // - TextColoredV() |
| // - TextDisabled() |
| // - TextDisabledV() |
| // - TextWrapped() |
| // - TextWrappedV() |
| // - LabelText() |
| // - LabelTextV() |
| // - BulletText() |
| // - BulletTextV() |
| //------------------------------------------------------------------------- |
| |
| void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| IM_ASSERT(text != NULL); |
| const char* text_begin = text; |
| if (text_end == NULL) |
| text_end = text + strlen(text); // FIXME-OPT |
| |
| const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); |
| const float wrap_pos_x = window->DC.TextWrapPos; |
| const bool wrap_enabled = (wrap_pos_x >= 0.0f); |
| if (text_end - text > 2000 && !wrap_enabled) |
| { |
| // Long 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. 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. |
| // - We use memchr(), pay attention that well optimized versions of those str/mem functions are much faster than a casually written loop. |
| const char* line = text; |
| const float line_height = GetTextLineHeight(); |
| ImVec2 text_size(0,0); |
| |
| // Lines to skip (can't skip when logging text) |
| ImVec2 pos = text_pos; |
| if (!g.LogEnabled) |
| { |
| int lines_skippable = (int)((window->ClipRect.Min.y - text_pos.y) / line_height); |
| if (lines_skippable > 0) |
| { |
| int lines_skipped = 0; |
| while (line < text_end && lines_skipped < lines_skippable) |
| { |
| const char* line_end = (const char*)memchr(line, '\n', text_end - line); |
| if (!line_end) |
| line_end = text_end; |
| if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0) |
| text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); |
| line = line_end + 1; |
| lines_skipped++; |
| } |
| pos.y += lines_skipped * line_height; |
| } |
| } |
| |
| // Lines to render |
| if (line < text_end) |
| { |
| ImRect line_rect(pos, pos + ImVec2(FLT_MAX, line_height)); |
| while (line < text_end) |
| { |
| if (IsClippedEx(line_rect, 0, false)) |
| break; |
| |
| const char* line_end = (const char*)memchr(line, '\n', text_end - line); |
| if (!line_end) |
| line_end = text_end; |
| text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); |
| RenderText(pos, line, line_end, false); |
| line = line_end + 1; |
| line_rect.Min.y += line_height; |
| line_rect.Max.y += line_height; |
| pos.y += line_height; |
| } |
| |
| // Count remaining lines |
| int lines_skipped = 0; |
| while (line < text_end) |
| { |
| const char* line_end = (const char*)memchr(line, '\n', text_end - line); |
| if (!line_end) |
| line_end = text_end; |
| if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0) |
| text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); |
| line = line_end + 1; |
| lines_skipped++; |
| } |
| pos.y += lines_skipped * line_height; |
| } |
| text_size.y = (pos - text_pos).y; |
| |
| ImRect bb(text_pos, text_pos + text_size); |
| ItemSize(text_size, 0.0f); |
| ItemAdd(bb, 0); |
| } |
| else |
| { |
| const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f; |
| const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width); |
| |
| ImRect bb(text_pos, text_pos + text_size); |
| ItemSize(text_size, 0.0f); |
| if (!ItemAdd(bb, 0)) |
| return; |
| |
| // Render (we don't hide text after ## in this end-user function) |
| RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width); |
| } |
| } |
| |
| void ImGui::TextUnformatted(const char* text, const char* text_end) |
| { |
| TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); |
| } |
| |
| void ImGui::Text(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| TextV(fmt, args); |
| va_end(args); |
| } |
| |
| void ImGui::TextV(const char* fmt, va_list args) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); |
| TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); |
| } |
| |
| void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| TextColoredV(col, fmt, args); |
| va_end(args); |
| } |
| |
| void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args) |
| { |
| PushStyleColor(ImGuiCol_Text, col); |
| TextV(fmt, args); |
| PopStyleColor(); |
| } |
| |
| void ImGui::TextDisabled(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| TextDisabledV(fmt, args); |
| va_end(args); |
| } |
| |
| void ImGui::TextDisabledV(const char* fmt, va_list args) |
| { |
| PushStyleColor(ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled]); |
| TextV(fmt, args); |
| PopStyleColor(); |
| } |
| |
| void ImGui::TextWrapped(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| TextWrappedV(fmt, args); |
| va_end(args); |
| } |
| |
| void ImGui::TextWrappedV(const char* fmt, va_list args) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| bool need_backup = (window->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set |
| if (need_backup) |
| PushTextWrapPos(0.0f); |
| TextV(fmt, args); |
| if (need_backup) |
| PopTextWrapPos(); |
| } |
| |
| void ImGui::LabelText(const char* label, const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| LabelTextV(label, fmt, args); |
| va_end(args); |
| } |
| |
| // Add a label+text combo aligned to other label+value widgets |
| void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| const float w = CalcItemWidth(); |
| |
| const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2)); |
| const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y*2) + label_size); |
| ItemSize(total_bb, style.FramePadding.y); |
| if (!ItemAdd(total_bb, 0)) |
| return; |
| |
| // Render |
| const char* value_text_begin = &g.TempBuffer[0]; |
| const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); |
| RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f,0.5f)); |
| if (label_size.x > 0.0f) |
| RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label); |
| } |
| |
| void ImGui::BulletText(const char* fmt, ...) |
| { |
| va_list args; |
| va_start(args, fmt); |
| BulletTextV(fmt, args); |
| va_end(args); |
| } |
| |
| // Text with a little bullet aligned to the typical tree node. |
| void ImGui::BulletTextV(const char* fmt, va_list args) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| |
| const char* text_begin = g.TempBuffer; |
| const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); |
| const ImVec2 label_size = CalcTextSize(text_begin, text_end, false); |
| const ImVec2 total_size = ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x * 2) : 0.0f), label_size.y); // Empty text doesn't add padding |
| ImVec2 pos = window->DC.CursorPos; |
| pos.y += window->DC.CurrLineTextBaseOffset; |
| ItemSize(total_size, 0.0f); |
| const ImRect bb(pos, pos + total_size); |
| if (!ItemAdd(bb, 0)) |
| return; |
| |
| // Render |
| ImU32 text_col = GetColorU32(ImGuiCol_Text); |
| RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, g.FontSize*0.5f), text_col); |
| RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x * 2, 0.0f), text_begin, text_end, false); |
| } |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Widgets: Main |
| //------------------------------------------------------------------------- |
| // - ButtonBehavior() [Internal] |
| // - Button() |
| // - SmallButton() |
| // - InvisibleButton() |
| // - ArrowButton() |
| // - CloseButton() [Internal] |
| // - CollapseButton() [Internal] |
| // - ScrollbarEx() [Internal] |
| // - Scrollbar() [Internal] |
| // - Image() |
| // - ImageButton() |
| // - Checkbox() |
| // - CheckboxFlags() |
| // - RadioButton() |
| // - ProgressBar() |
| // - Bullet() |
| //------------------------------------------------------------------------- |
| |
| // The ButtonBehavior() function is key to many interactions and used by many/most widgets. |
| // Because we handle so many cases (keyboard/gamepad navigation, drag and drop) and many specific behavior (via ImGuiButtonFlags_), |
| // this code is a little complex. |
| // By far the most common path is interacting with the Mouse using the default ImGuiButtonFlags_PressedOnClickRelease button behavior. |
| // See the series of events below and the corresponding state reported by dear imgui: |
| //------------------------------------------------------------------------------------------------------------------------------------------------ |
| // with PressedOnClickRelease: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() |
| // Frame N+0 (mouse is outside bb) - - - - - - |
| // Frame N+1 (mouse moves inside bb) - true - - - - |
| // Frame N+2 (mouse button is down) - true true true - true |
| // Frame N+3 (mouse button is down) - true true - - - |
| // Frame N+4 (mouse moves outside bb) - - true - - - |
| // Frame N+5 (mouse moves inside bb) - true true - - - |
| // Frame N+6 (mouse button is released) true true - - true - |
| // Frame N+7 (mouse button is released) - true - - - - |
| // Frame N+8 (mouse moves outside bb) - - - - - - |
| //------------------------------------------------------------------------------------------------------------------------------------------------ |
| // with PressedOnClick: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() |
| // Frame N+2 (mouse button is down) true true true true - true |
| // Frame N+3 (mouse button is down) - true true - - - |
| // Frame N+6 (mouse button is released) - true - - true - |
| // Frame N+7 (mouse button is released) - true - - - - |
| //------------------------------------------------------------------------------------------------------------------------------------------------ |
| // with PressedOnRelease: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() |
| // Frame N+2 (mouse button is down) - true - - - true |
| // Frame N+3 (mouse button is down) - true - - - - |
| // Frame N+6 (mouse button is released) true true - - - - |
| // Frame N+7 (mouse button is released) - true - - - - |
| //------------------------------------------------------------------------------------------------------------------------------------------------ |
| // with PressedOnDoubleClick: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() |
| // Frame N+0 (mouse button is down) - true - - - true |
| // Frame N+1 (mouse button is down) - true - - - - |
| // Frame N+2 (mouse button is released) - true - - - - |
| // Frame N+3 (mouse button is released) - true - - - - |
| // Frame N+4 (mouse button is down) true true true true - true |
| // Frame N+5 (mouse button is down) - true true - - - |
| // Frame N+6 (mouse button is released) - true - - true - |
| // Frame N+7 (mouse button is released) - true - - - - |
| //------------------------------------------------------------------------------------------------------------------------------------------------ |
| // Note that some combinations are supported, |
| // - PressedOnDragDropHold can generally be associated with any flag. |
| // - PressedOnDoubleClick can be associated by PressedOnClickRelease/PressedOnRelease, in which case the second release event won't be reported. |
| //------------------------------------------------------------------------------------------------------------------------------------------------ |
| // The behavior of the return-value changes when ImGuiButtonFlags_Repeat is set: |
| // Repeat+ Repeat+ Repeat+ Repeat+ |
| // PressedOnClickRelease PressedOnClick PressedOnRelease PressedOnDoubleClick |
| //------------------------------------------------------------------------------------------------------------------------------------------------- |
| // Frame N+0 (mouse button is down) - true - true |
| // ... - - - - |
| // Frame N + RepeatDelay true true - true |
| // ... - - - - |
| // Frame N + RepeatDelay + RepeatRate*N true true - true |
| //------------------------------------------------------------------------------------------------------------------------------------------------- |
| |
| bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = GetCurrentWindow(); |
| |
| if (flags & ImGuiButtonFlags_Disabled) |
| { |
| if (out_hovered) *out_hovered = false; |
| if (out_held) *out_held = false; |
| if (g.ActiveId == id) ClearActiveID(); |
| return false; |
| } |
| |
| // Default only reacts to left mouse button |
| if ((flags & ImGuiButtonFlags_MouseButtonMask_) == 0) |
| flags |= ImGuiButtonFlags_MouseButtonDefault_; |
| |
| // Default behavior requires click + release inside bounding box |
| if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0) |
| flags |= ImGuiButtonFlags_PressedOnDefault_; |
| |
| ImGuiWindow* backup_hovered_window = g.HoveredWindow; |
| const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredRootWindow == window; |
| if (flatten_hovered_children) |
| g.HoveredWindow = window; |
| |
| #ifdef IMGUI_ENABLE_TEST_ENGINE |
| if (id != 0 && window->DC.LastItemId != id) |
| ImGuiTestEngineHook_ItemAdd(&g, bb, id); |
| #endif |
| |
| bool pressed = false; |
| bool hovered = ItemHoverable(bb, id); |
| |
| // Drag source doesn't report as hovered |
| if (hovered && g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover)) |
| hovered = false; |
| |
| // Special mode for Drag and Drop where holding button pressed for a long time while dragging another item triggers the button |
| if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) |
| if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) |
| { |
| hovered = true; |
| SetHoveredID(id); |
| if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, 0.70f, 0.00f)) |
| { |
| pressed = true; |
| FocusWindow(window); |
| } |
| } |
| |
| if (flatten_hovered_children) |
| g.HoveredWindow = backup_hovered_window; |
| |
| // AllowOverlap mode (rarely used) requires previous frame HoveredId to be null or to match. This allows using patterns where a later submitted widget overlaps a previous one. |
| if (hovered && (flags & ImGuiButtonFlags_AllowItemOverlap) && (g.HoveredIdPreviousFrame != id && g.HoveredIdPreviousFrame != 0)) |
| hovered = false; |
| |
| // Mouse handling |
| if (hovered) |
| { |
| if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt)) |
| { |
| // Poll buttons |
| int mouse_button_clicked = -1; |
| int mouse_button_released = -1; |
| if ((flags & ImGuiButtonFlags_MouseButtonLeft) && g.IO.MouseClicked[0]) { mouse_button_clicked = 0; } |
| else if ((flags & ImGuiButtonFlags_MouseButtonRight) && g.IO.MouseClicked[1]) { mouse_button_clicked = 1; } |
| else if ((flags & ImGuiButtonFlags_MouseButtonMiddle) && g.IO.MouseClicked[2]) { mouse_button_clicked = 2; } |
| if ((flags & ImGuiButtonFlags_MouseButtonLeft) && g.IO.MouseReleased[0]) { mouse_button_released = 0; } |
| else if ((flags & ImGuiButtonFlags_MouseButtonRight) && g.IO.MouseReleased[1]) { mouse_button_released = 1; } |
| else if ((flags & ImGuiButtonFlags_MouseButtonMiddle) && g.IO.MouseReleased[2]) { mouse_button_released = 2; } |
| |
| if (mouse_button_clicked != -1 && g.ActiveId != id) |
| { |
| if (flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere)) |
| { |
| SetActiveID(id, window); |
| g.ActiveIdMouseButton = mouse_button_clicked; |
| if (!(flags & ImGuiButtonFlags_NoNavFocus)) |
| SetFocusID(id, window); |
| FocusWindow(window); |
| } |
| if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[mouse_button_clicked])) |
| { |
| pressed = true; |
| if (flags & ImGuiButtonFlags_NoHoldingActiveId) |
| ClearActiveID(); |
| else |
| SetActiveID(id, window); // Hold on ID |
| g.ActiveIdMouseButton = mouse_button_clicked; |
| FocusWindow(window); |
| } |
| } |
| if ((flags & ImGuiButtonFlags_PressedOnRelease) && mouse_button_released != -1) |
| { |
| // Repeat mode trumps on release behavior |
| if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay)) |
| pressed = true; |
| ClearActiveID(); |
| } |
| |
| // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). |
| // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. |
| if (g.ActiveId == id && (flags & ImGuiButtonFlags_Repeat)) |
| if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, true)) |
| pressed = true; |
| } |
| |
| if (pressed) |
| g.NavDisableHighlight = true; |
| } |
| |
| // Gamepad/Keyboard navigation |
| // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse. |
| if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover && (g.ActiveId == 0 || g.ActiveId == id || g.ActiveId == window->MoveId)) |
| if (!(flags & ImGuiButtonFlags_NoHoveredOnNav)) |
| hovered = true; |
| if (g.NavActivateDownId == id) |
| { |
| bool nav_activated_by_code = (g.NavActivateId == id); |
| bool nav_activated_by_inputs = IsNavInputTest(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); |
| if (nav_activated_by_code || nav_activated_by_inputs) |
| pressed = true; |
| if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id) |
| { |
| // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. |
| g.NavActivateId = id; // This is so SetActiveId assign a Nav source |
| SetActiveID(id, window); |
| if ((nav_activated_by_code || nav_activated_by_inputs) && !(flags & ImGuiButtonFlags_NoNavFocus)) |
| SetFocusID(id, window); |
| } |
| } |
| |
| bool held = false; |
| if (g.ActiveId == id) |
| { |
| if (g.ActiveIdSource == ImGuiInputSource_Mouse) |
| { |
| if (g.ActiveIdIsJustActivated) |
| g.ActiveIdClickOffset = g.IO.MousePos - bb.Min; |
| |
| const int mouse_button = g.ActiveIdMouseButton; |
| IM_ASSERT(mouse_button >= 0 && mouse_button < ImGuiMouseButton_COUNT); |
| if (g.IO.MouseDown[mouse_button]) |
| { |
| held = true; |
| } |
| else |
| { |
| bool release_in = hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease) != 0; |
| bool release_anywhere = (flags & ImGuiButtonFlags_PressedOnClickReleaseAnywhere) != 0; |
| if ((release_in || release_anywhere) && !g.DragDropActive) |
| { |
| bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDownWasDoubleClick[mouse_button]; |
| bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps <on release> |
| if (!is_double_click_release && !is_repeating_already) |
| pressed = true; |
| } |
| ClearActiveID(); |
| } |
| if (!(flags & ImGuiButtonFlags_NoNavFocus)) |
| g.NavDisableHighlight = true; |
| } |
| else if (g.ActiveIdSource == ImGuiInputSource_Nav) |
| { |
| if (g.NavActivateDownId != id) |
| ClearActiveID(); |
| } |
| if (pressed) |
| g.ActiveIdHasBeenPressedBefore = true; |
| } |
| |
| if (out_hovered) *out_hovered = hovered; |
| if (out_held) *out_held = held; |
| |
| return pressed; |
| } |
| |
| bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags flags) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| const ImGuiID id = window->GetID(label); |
| const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| |
| ImVec2 pos = window->DC.CursorPos; |
| if ((flags & ImGuiButtonFlags_AlignTextBaseLine) && style.FramePadding.y < window->DC.CurrLineTextBaseOffset) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag) |
| pos.y += window->DC.CurrLineTextBaseOffset - style.FramePadding.y; |
| ImVec2 size = CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f); |
| |
| const ImRect bb(pos, pos + size); |
| ItemSize(size, style.FramePadding.y); |
| if (!ItemAdd(bb, id)) |
| return false; |
| |
| if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) |
| flags |= ImGuiButtonFlags_Repeat; |
| bool hovered, held; |
| bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); |
| |
| // Render |
| const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); |
| RenderNavHighlight(bb, id); |
| RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); |
| RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, NULL, &label_size, style.ButtonTextAlign, &bb); |
| |
| // Automatically close popups |
| //if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup)) |
| // CloseCurrentPopup(); |
| |
| IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); |
| return pressed; |
| } |
| |
| bool ImGui::Button(const char* label, const ImVec2& size_arg) |
| { |
| return ButtonEx(label, size_arg, 0); |
| } |
| |
| // Small buttons fits within text without additional vertical spacing. |
| bool ImGui::SmallButton(const char* label) |
| { |
| ImGuiContext& g = *GImGui; |
| float backup_padding_y = g.Style.FramePadding.y; |
| g.Style.FramePadding.y = 0.0f; |
| bool pressed = ButtonEx(label, ImVec2(0, 0), ImGuiButtonFlags_AlignTextBaseLine); |
| g.Style.FramePadding.y = backup_padding_y; |
| return pressed; |
| } |
| |
| // Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack. |
| // Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id) |
| bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| // Cannot use zero-size for InvisibleButton(). Unlike Button() there is not way to fallback using the label size. |
| IM_ASSERT(size_arg.x != 0.0f && size_arg.y != 0.0f); |
| |
| const ImGuiID id = window->GetID(str_id); |
| ImVec2 size = CalcItemSize(size_arg, 0.0f, 0.0f); |
| const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); |
| ItemSize(size); |
| if (!ItemAdd(bb, id)) |
| return false; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(bb, id, &hovered, &held); |
| |
| return pressed; |
| } |
| |
| bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiButtonFlags flags) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiID id = window->GetID(str_id); |
| const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); |
| const float default_size = GetFrameHeight(); |
| ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : -1.0f); |
| if (!ItemAdd(bb, id)) |
| return false; |
| |
| if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) |
| flags |= ImGuiButtonFlags_Repeat; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); |
| |
| // Render |
| const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); |
| const ImU32 text_col = GetColorU32(ImGuiCol_Text); |
| RenderNavHighlight(bb, id); |
| RenderFrame(bb.Min, bb.Max, bg_col, true, g.Style.FrameRounding); |
| RenderArrow(window->DrawList, bb.Min + ImVec2(ImMax(0.0f, (size.x - g.FontSize) * 0.5f), ImMax(0.0f, (size.y - g.FontSize) * 0.5f)), text_col, dir); |
| |
| return pressed; |
| } |
| |
| bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) |
| { |
| float sz = GetFrameHeight(); |
| return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), ImGuiButtonFlags_None); |
| } |
| |
| // Button to close a window |
| bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)//, float size) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| |
| // We intentionally allow interaction when clipped so that a mechanical Alt,Right,Validate sequence close a window. |
| // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible). |
| const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); |
| bool is_clipped = !ItemAdd(bb, id); |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(bb, id, &hovered, &held); |
| if (is_clipped) |
| return pressed; |
| |
| // Render |
| ImU32 col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); |
| ImVec2 center = bb.GetCenter(); |
| if (hovered) |
| window->DrawList->AddCircleFilled(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col, 12); |
| |
| float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; |
| ImU32 cross_col = GetColorU32(ImGuiCol_Text); |
| center -= ImVec2(0.5f, 0.5f); |
| window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), cross_col, 1.0f); |
| window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), cross_col, 1.0f); |
| |
| return pressed; |
| } |
| |
| bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| |
| ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); |
| ItemAdd(bb, id); |
| bool hovered, held; |
| bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None); |
| |
| // Render |
| ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); |
| ImU32 text_col = GetColorU32(ImGuiCol_Text); |
| ImVec2 center = bb.GetCenter(); |
| if (hovered || held) |
| window->DrawList->AddCircleFilled(center/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col, 12); |
| RenderArrow(window->DrawList, bb.Min + g.Style.FramePadding, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); |
| |
| // Switch to moving the window after mouse is moved beyond the initial drag threshold |
| if (IsItemActive() && IsMouseDragging(0)) |
| StartMouseMovingWindow(window); |
| |
| return pressed; |
| } |
| |
| ImGuiID ImGui::GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis) |
| { |
| return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); |
| } |
| |
| // Vertical/Horizontal scrollbar |
| // The entire piece of code below is rather confusing because: |
| // - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab) |
| // - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar |
| // - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal. |
| // Still, the code should probably be made simpler.. |
| bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float size_avail_v, float size_contents_v, ImDrawCornerFlags rounding_corners) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| if (window->SkipItems) |
| return false; |
| |
| const float bb_frame_width = bb_frame.GetWidth(); |
| const float bb_frame_height = bb_frame.GetHeight(); |
| if (bb_frame_width <= 0.0f || bb_frame_height <= 0.0f) |
| return false; |
| |
| // When we are too small, start hiding and disabling the grab (this reduce visual noise on very small window and facilitate using the resize grab) |
| float alpha = 1.0f; |
| if ((axis == ImGuiAxis_Y) && bb_frame_height < g.FontSize + g.Style.FramePadding.y * 2.0f) |
| alpha = ImSaturate((bb_frame_height - g.FontSize) / (g.Style.FramePadding.y * 2.0f)); |
| if (alpha <= 0.0f) |
| return false; |
| |
| const ImGuiStyle& style = g.Style; |
| const bool allow_interaction = (alpha >= 1.0f); |
| const bool horizontal = (axis == ImGuiAxis_X); |
| |
| ImRect bb = bb_frame; |
| bb.Expand(ImVec2(-ImClamp(IM_FLOOR((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp(IM_FLOOR((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); |
| |
| // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar) |
| const float scrollbar_size_v = horizontal ? bb.GetWidth() : bb.GetHeight(); |
| |
| // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount) |
| // But we maintain a minimum size in pixel to allow for the user to still aim inside. |
| IM_ASSERT(ImMax(size_contents_v, size_avail_v) > 0.0f); // Adding this assert to check if the ImMax(XXX,1.0f) is still needed. PLEASE CONTACT ME if this triggers. |
| const float win_size_v = ImMax(ImMax(size_contents_v, size_avail_v), 1.0f); |
| const float grab_h_pixels = ImClamp(scrollbar_size_v * (size_avail_v / win_size_v), style.GrabMinSize, scrollbar_size_v); |
| const float grab_h_norm = grab_h_pixels / scrollbar_size_v; |
| |
| // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar(). |
| bool held = false; |
| bool hovered = false; |
| ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus); |
| |
| float scroll_max = ImMax(1.0f, size_contents_v - size_avail_v); |
| float scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); |
| float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; |
| if (held && allow_interaction && grab_h_norm < 1.0f) |
| { |
| float scrollbar_pos_v = horizontal ? bb.Min.x : bb.Min.y; |
| float mouse_pos_v = horizontal ? g.IO.MousePos.x : g.IO.MousePos.y; |
| |
| // Click position in scrollbar normalized space (0.0f->1.0f) |
| const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); |
| SetHoveredID(id); |
| |
| bool seek_absolute = false; |
| if (g.ActiveIdIsJustActivated) |
| { |
| // On initial click calculate the distance between mouse and the center of the grab |
| seek_absolute = (clicked_v_norm < grab_v_norm || clicked_v_norm > grab_v_norm + grab_h_norm); |
| if (seek_absolute) |
| g.ScrollbarClickDeltaToGrabCenter = 0.0f; |
| else |
| g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; |
| } |
| |
| // Apply scroll |
| // It is ok to modify Scroll here because we are being called in Begin() after the calculation of ContentSize and before setting up our starting position |
| const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); |
| *p_scroll_v = IM_ROUND(scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); |
| |
| // Update values for rendering |
| scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); |
| grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; |
| |
| // Update distance to grab now that we have seeked and saturated |
| if (seek_absolute) |
| g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; |
| } |
| |
| // Render |
| window->DrawList->AddRectFilled(bb_frame.Min, bb_frame.Max, GetColorU32(ImGuiCol_ScrollbarBg), window->WindowRounding, rounding_corners); |
| const ImU32 grab_col = GetColorU32(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab, alpha); |
| ImRect grab_rect; |
| if (horizontal) |
| grab_rect = ImRect(ImLerp(bb.Min.x, bb.Max.x, grab_v_norm), bb.Min.y, ImLerp(bb.Min.x, bb.Max.x, grab_v_norm) + grab_h_pixels, bb.Max.y); |
| else |
| grab_rect = ImRect(bb.Min.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm), bb.Max.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm) + grab_h_pixels); |
| window->DrawList->AddRectFilled(grab_rect.Min, grab_rect.Max, grab_col, style.ScrollbarRounding); |
| |
| return held; |
| } |
| |
| void ImGui::Scrollbar(ImGuiAxis axis) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| |
| const ImGuiID id = GetWindowScrollbarID(window, axis); |
| KeepAliveID(id); |
| |
| // Calculate scrollbar bounding box |
| const ImRect outer_rect = window->Rect(); |
| const ImRect inner_rect = window->InnerRect; |
| const float border_size = window->WindowBorderSize; |
| const float scrollbar_size = window->ScrollbarSizes[axis ^ 1]; |
| IM_ASSERT(scrollbar_size > 0.0f); |
| const float other_scrollbar_size = window->ScrollbarSizes[axis]; |
| ImDrawCornerFlags rounding_corners = (other_scrollbar_size <= 0.0f) ? ImDrawCornerFlags_BotRight : 0; |
| ImRect bb; |
| if (axis == ImGuiAxis_X) |
| { |
| bb.Min = ImVec2(inner_rect.Min.x, ImMax(outer_rect.Min.y, outer_rect.Max.y - border_size - scrollbar_size)); |
| bb.Max = ImVec2(inner_rect.Max.x, outer_rect.Max.y); |
| rounding_corners |= ImDrawCornerFlags_BotLeft; |
| } |
| else |
| { |
| bb.Min = ImVec2(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y); |
| bb.Max = ImVec2(outer_rect.Max.x, window->InnerRect.Max.y); |
| rounding_corners |= ((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) ? ImDrawCornerFlags_TopRight : 0; |
| } |
| ScrollbarEx(bb, id, axis, &window->Scroll[axis], inner_rect.Max[axis] - inner_rect.Min[axis], window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f, rounding_corners); |
| } |
| |
| void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); |
| if (border_col.w > 0.0f) |
| bb.Max += ImVec2(2, 2); |
| ItemSize(bb); |
| if (!ItemAdd(bb, 0)) |
| return; |
| |
| if (border_col.w > 0.0f) |
| { |
| window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f); |
| window->DrawList->AddImage(user_texture_id, bb.Min + ImVec2(1, 1), bb.Max - ImVec2(1, 1), uv0, uv1, GetColorU32(tint_col)); |
| } |
| else |
| { |
| window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col)); |
| } |
| } |
| |
| // frame_padding < 0: uses FramePadding from style (default) |
| // frame_padding = 0: no framing |
| // frame_padding > 0: set framing size |
| // The color used are the button colors. |
| bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| |
| // Default to using texture ID as ID. User can still push string/integer prefixes. |
| // We could hash the size/uv to create a unique ID but that would prevent the user from animating UV. |
| PushID((void*)(intptr_t)user_texture_id); |
| const ImGuiID id = window->GetID("#image"); |
| PopID(); |
| |
| const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding; |
| const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2); |
| const ImRect image_bb(window->DC.CursorPos + padding, window->DC.CursorPos + padding + size); |
| ItemSize(bb); |
| if (!ItemAdd(bb, id)) |
| return false; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(bb, id, &hovered, &held); |
| |
| // Render |
| const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); |
| RenderNavHighlight(bb, id); |
| RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, style.FrameRounding)); |
| if (bg_col.w > 0.0f) |
| window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, GetColorU32(bg_col)); |
| window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col)); |
| |
| return pressed; |
| } |
| |
| bool ImGui::Checkbox(const char* label, bool* v) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| const ImGuiID id = window->GetID(label); |
| const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| |
| const float square_sz = GetFrameHeight(); |
| const ImVec2 pos = window->DC.CursorPos; |
| const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); |
| ItemSize(total_bb, style.FramePadding.y); |
| if (!ItemAdd(total_bb, id)) |
| return false; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); |
| if (pressed) |
| { |
| *v = !(*v); |
| MarkItemEdited(id); |
| } |
| |
| const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); |
| RenderNavHighlight(total_bb, id); |
| RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); |
| ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); |
| if (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) |
| { |
| // Undocumented tristate/mixed/indeterminate checkbox (#2644) |
| ImVec2 pad(ImMax(1.0f, IM_FLOOR(square_sz / 3.6f)), ImMax(1.0f, IM_FLOOR(square_sz / 3.6f))); |
| window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); |
| } |
| else if (*v) |
| { |
| const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); |
| RenderCheckMark(check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f); |
| } |
| |
| if (g.LogEnabled) |
| LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]"); |
| if (label_size.x > 0.0f) |
| RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); |
| |
| IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); |
| return pressed; |
| } |
| |
| bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value) |
| { |
| bool v = ((*flags & flags_value) == flags_value); |
| bool pressed = Checkbox(label, &v); |
| if (pressed) |
| { |
| if (v) |
| *flags |= flags_value; |
| else |
| *flags &= ~flags_value; |
| } |
| |
| return pressed; |
| } |
| |
| bool ImGui::RadioButton(const char* label, bool active) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| const ImGuiID id = window->GetID(label); |
| const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| |
| const float square_sz = GetFrameHeight(); |
| const ImVec2 pos = window->DC.CursorPos; |
| const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); |
| const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); |
| ItemSize(total_bb, style.FramePadding.y); |
| if (!ItemAdd(total_bb, id)) |
| return false; |
| |
| ImVec2 center = check_bb.GetCenter(); |
| center.x = IM_ROUND(center.x); |
| center.y = IM_ROUND(center.y); |
| const float radius = (square_sz - 1.0f) * 0.5f; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); |
| if (pressed) |
| MarkItemEdited(id); |
| |
| RenderNavHighlight(total_bb, id); |
| window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16); |
| if (active) |
| { |
| const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); |
| window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark), 16); |
| } |
| |
| if (style.FrameBorderSize > 0.0f) |
| { |
| window->DrawList->AddCircle(center + ImVec2(1,1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize); |
| window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize); |
| } |
| |
| if (g.LogEnabled) |
| LogRenderedText(&total_bb.Min, active ? "(x)" : "( )"); |
| if (label_size.x > 0.0f) |
| RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); |
| |
| return pressed; |
| } |
| |
| // FIXME: This would work nicely if it was a public template, e.g. 'template<T> RadioButton(const char* label, T* v, T v_button)', but I'm not sure how we would expose it.. |
| bool ImGui::RadioButton(const char* label, int* v, int v_button) |
| { |
| const bool pressed = RadioButton(label, *v == v_button); |
| if (pressed) |
| *v = v_button; |
| return pressed; |
| } |
| |
| // size_arg (for each axis) < 0.0f: align to end, 0.0f: auto, > 0.0f: specified size |
| void ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* overlay) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| |
| ImVec2 pos = window->DC.CursorPos; |
| ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), g.FontSize + style.FramePadding.y*2.0f); |
| ImRect bb(pos, pos + size); |
| ItemSize(size, style.FramePadding.y); |
| if (!ItemAdd(bb, 0)) |
| return; |
| |
| // Render |
| fraction = ImSaturate(fraction); |
| RenderFrame(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); |
| bb.Expand(ImVec2(-style.FrameBorderSize, -style.FrameBorderSize)); |
| const ImVec2 fill_br = ImVec2(ImLerp(bb.Min.x, bb.Max.x, fraction), bb.Max.y); |
| RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding); |
| |
| // Default displaying the fraction as percentage string, but user can override it |
| char overlay_buf[32]; |
| if (!overlay) |
| { |
| ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction*100+0.01f); |
| overlay = overlay_buf; |
| } |
| |
| ImVec2 overlay_size = CalcTextSize(overlay, NULL); |
| if (overlay_size.x > 0.0f) |
| RenderTextClipped(ImVec2(ImClamp(fill_br.x + style.ItemSpacing.x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f,0.5f), &bb); |
| } |
| |
| void ImGui::Bullet() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); |
| const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height)); |
| ItemSize(bb); |
| if (!ItemAdd(bb, 0)) |
| { |
| SameLine(0, style.FramePadding.x*2); |
| return; |
| } |
| |
| // Render and stay on same line |
| ImU32 text_col = GetColorU32(ImGuiCol_Text); |
| RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), text_col); |
| SameLine(0, style.FramePadding.x * 2.0f); |
| } |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Widgets: Low-level Layout helpers |
| //------------------------------------------------------------------------- |
| // - Spacing() |
| // - Dummy() |
| // - NewLine() |
| // - AlignTextToFramePadding() |
| // - SeparatorEx() [Internal] |
| // - Separator() |
| // - SplitterBehavior() [Internal] |
| // - ShrinkWidths() [Internal] |
| //------------------------------------------------------------------------- |
| |
| void ImGui::Spacing() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| ItemSize(ImVec2(0,0)); |
| } |
| |
| void ImGui::Dummy(const ImVec2& size) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); |
| ItemSize(size); |
| ItemAdd(bb, 0); |
| } |
| |
| void ImGui::NewLine() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiLayoutType backup_layout_type = window->DC.LayoutType; |
| window->DC.LayoutType = ImGuiLayoutType_Vertical; |
| if (window->DC.CurrLineSize.y > 0.0f) // In the event that we are on a line with items that is smaller that FontSize high, we will preserve its height. |
| ItemSize(ImVec2(0,0)); |
| else |
| ItemSize(ImVec2(0.0f, g.FontSize)); |
| window->DC.LayoutType = backup_layout_type; |
| } |
| |
| void ImGui::AlignTextToFramePadding() |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| window->DC.CurrLineSize.y = ImMax(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y * 2); |
| window->DC.CurrLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, g.Style.FramePadding.y); |
| } |
| |
| // Horizontal/vertical separating line |
| void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return; |
| |
| ImGuiContext& g = *GImGui; |
| IM_ASSERT(ImIsPowerOfTwo(flags & (ImGuiSeparatorFlags_Horizontal | ImGuiSeparatorFlags_Vertical))); // Check that only 1 option is selected |
| |
| float thickness_draw = 1.0f; |
| float thickness_layout = 0.0f; |
| if (flags & ImGuiSeparatorFlags_Vertical) |
| { |
| // Vertical separator, for menu bars (use current line height). Not exposed because it is misleading and it doesn't have an effect on regular layout. |
| float y1 = window->DC.CursorPos.y; |
| float y2 = window->DC.CursorPos.y + window->DC.CurrLineSize.y; |
| const ImRect bb(ImVec2(window->DC.CursorPos.x, y1), ImVec2(window->DC.CursorPos.x + thickness_draw, y2)); |
| ItemSize(ImVec2(thickness_layout, 0.0f)); |
| if (!ItemAdd(bb, 0)) |
| return; |
| |
| // Draw |
| window->DrawList->AddLine(ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Min.x, bb.Max.y), GetColorU32(ImGuiCol_Separator)); |
| if (g.LogEnabled) |
| LogText(" |"); |
| } |
| else if (flags & ImGuiSeparatorFlags_Horizontal) |
| { |
| // Horizontal Separator |
| float x1 = window->Pos.x; |
| float x2 = window->Pos.x + window->Size.x; |
| if (!window->DC.GroupStack.empty()) |
| x1 += window->DC.Indent.x; |
| |
| ImGuiColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; |
| if (columns) |
| PushColumnsBackground(); |
| |
| // We don't provide our width to the layout so that it doesn't get feed back into AutoFit |
| const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness_draw)); |
| ItemSize(ImVec2(0.0f, thickness_layout)); |
| const bool item_visible = ItemAdd(bb, 0); |
| if (item_visible) |
| { |
| // Draw |
| window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x, bb.Min.y), GetColorU32(ImGuiCol_Separator)); |
| if (g.LogEnabled) |
| LogRenderedText(&bb.Min, "--------------------------------"); |
| } |
| if (columns) |
| { |
| PopColumnsBackground(); |
| columns->LineMinY = window->DC.CursorPos.y; |
| } |
| } |
| } |
| |
| void ImGui::Separator() |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| if (window->SkipItems) |
| return; |
| |
| // Those flags should eventually be overridable by the user |
| ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal; |
| flags |= ImGuiSeparatorFlags_SpanAllColumns; |
| SeparatorEx(flags); |
| } |
| |
| // Using 'hover_visibility_delay' allows us to hide the highlight and mouse cursor for a short time, which can be convenient to reduce visual noise. |
| bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend, float hover_visibility_delay) |
| { |
| ImGuiContext& g = *GImGui; |
| ImGuiWindow* window = g.CurrentWindow; |
| |
| const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; |
| window->DC.ItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus; |
| bool item_add = ItemAdd(bb, id); |
| window->DC.ItemFlags = item_flags_backup; |
| if (!item_add) |
| return false; |
| |
| bool hovered, held; |
| ImRect bb_interact = bb; |
| bb_interact.Expand(axis == ImGuiAxis_Y ? ImVec2(0.0f, hover_extend) : ImVec2(hover_extend, 0.0f)); |
| ButtonBehavior(bb_interact, id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap); |
| if (g.ActiveId != id) |
| SetItemAllowOverlap(); |
| |
| if (held || (g.HoveredId == id && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay)) |
| SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW); |
| |
| ImRect bb_render = bb; |
| if (held) |
| { |
| ImVec2 mouse_delta_2d = g.IO.MousePos - g.ActiveIdClickOffset - bb_interact.Min; |
| float mouse_delta = (axis == ImGuiAxis_Y) ? mouse_delta_2d.y : mouse_delta_2d.x; |
| |
| // Minimum pane size |
| float size_1_maximum_delta = ImMax(0.0f, *size1 - min_size1); |
| float size_2_maximum_delta = ImMax(0.0f, *size2 - min_size2); |
| if (mouse_delta < -size_1_maximum_delta) |
| mouse_delta = -size_1_maximum_delta; |
| if (mouse_delta > size_2_maximum_delta) |
| mouse_delta = size_2_maximum_delta; |
| |
| // Apply resize |
| if (mouse_delta != 0.0f) |
| { |
| if (mouse_delta < 0.0f) |
| IM_ASSERT(*size1 + mouse_delta >= min_size1); |
| if (mouse_delta > 0.0f) |
| IM_ASSERT(*size2 - mouse_delta >= min_size2); |
| *size1 += mouse_delta; |
| *size2 -= mouse_delta; |
| bb_render.Translate((axis == ImGuiAxis_X) ? ImVec2(mouse_delta, 0.0f) : ImVec2(0.0f, mouse_delta)); |
| MarkItemEdited(id); |
| } |
| } |
| |
| // Render |
| const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : (hovered && g.HoveredIdTimer >= hover_visibility_delay) ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); |
| window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, 0.0f); |
| |
| return held; |
| } |
| |
| static int IMGUI_CDECL ShrinkWidthItemComparer(const void* lhs, const void* rhs) |
| { |
| const ImGuiShrinkWidthItem* a = (const ImGuiShrinkWidthItem*)lhs; |
| const ImGuiShrinkWidthItem* b = (const ImGuiShrinkWidthItem*)rhs; |
| if (int d = (int)(b->Width - a->Width)) |
| return d; |
| return (b->Index - a->Index); |
| } |
| |
| // Shrink excess width from a set of item, by removing width from the larger items first. |
| void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess) |
| { |
| if (count == 1) |
| { |
| items[0].Width = ImMax(items[0].Width - width_excess, 1.0f); |
| return; |
| } |
| ImQsort(items, (size_t)count, sizeof(ImGuiShrinkWidthItem), ShrinkWidthItemComparer); |
| int count_same_width = 1; |
| while (width_excess > 0.0f && count_same_width < count) |
| { |
| while (count_same_width < count && items[0].Width <= items[count_same_width].Width) |
| count_same_width++; |
| float max_width_to_remove_per_item = (count_same_width < count) ? (items[0].Width - items[count_same_width].Width) : (items[0].Width - 1.0f); |
| float width_to_remove_per_item = ImMin(width_excess / count_same_width, max_width_to_remove_per_item); |
| for (int item_n = 0; item_n < count_same_width; item_n++) |
| items[item_n].Width -= width_to_remove_per_item; |
| width_excess -= width_to_remove_per_item * count_same_width; |
| } |
| |
| // Round width and redistribute remainder left-to-right (could make it an option of the function?) |
| // Ensure that e.g. the right-most tab of a shrunk tab-bar always reaches exactly at the same distance from the right-most edge of the tab bar separator. |
| width_excess = 0.0f; |
| for (int n = 0; n < count; n++) |
| { |
| float width_rounded = ImFloor(items[n].Width); |
| width_excess += items[n].Width - width_rounded; |
| items[n].Width = width_rounded; |
| } |
| if (width_excess > 0.0f) |
| for (int n = 0; n < count; n++) |
| if (items[n].Index < (int)(width_excess + 0.01f)) |
| items[n].Width += 1.0f; |
| } |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Widgets: ComboBox |
| //------------------------------------------------------------------------- |
| // - BeginCombo() |
| // - EndCombo() |
| // - Combo() |
| //------------------------------------------------------------------------- |
| |
| static float CalcMaxPopupHeightFromItemCount(int items_count) |
| { |
| ImGuiContext& g = *GImGui; |
| if (items_count <= 0) |
| return FLT_MAX; |
| return (g.FontSize + g.Style.ItemSpacing.y) * items_count - g.Style.ItemSpacing.y + (g.Style.WindowPadding.y * 2); |
| } |
| |
| bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags) |
| { |
| // Always consume the SetNextWindowSizeConstraint() call in our early return paths |
| ImGuiContext& g = *GImGui; |
| bool has_window_size_constraint = (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) != 0; |
| g.NextWindowData.Flags &= ~ImGuiNextWindowDataFlags_HasSizeConstraint; |
| |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together |
| |
| const ImGuiStyle& style = g.Style; |
| const ImGuiID id = window->GetID(label); |
| |
| const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight(); |
| const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| const float expected_w = CalcItemWidth(); |
| const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : expected_w; |
| const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); |
| const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); |
| ItemSize(total_bb, style.FramePadding.y); |
| if (!ItemAdd(total_bb, id, &frame_bb)) |
| return false; |
| |
| bool hovered, held; |
| bool pressed = ButtonBehavior(frame_bb, id, &hovered, &held); |
| bool popup_open = IsPopupOpen(id); |
| |
| const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); |
| const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size); |
| RenderNavHighlight(frame_bb, id); |
| if (!(flags & ImGuiComboFlags_NoPreview)) |
| window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Left); |
| if (!(flags & ImGuiComboFlags_NoArrowButton)) |
| { |
| ImU32 bg_col = GetColorU32((popup_open || hovered) ? ImGuiCol_ButtonHovered : ImGuiCol_Button); |
| ImU32 text_col = GetColorU32(ImGuiCol_Text); |
| window->DrawList->AddRectFilled(ImVec2(value_x2, frame_bb.Min.y), frame_bb.Max, bg_col, style.FrameRounding, (w <= arrow_size) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Right); |
| if (value_x2 + arrow_size - style.FramePadding.x <= frame_bb.Max.x) |
| RenderArrow(window->DrawList, ImVec2(value_x2 + style.FramePadding.y, frame_bb.Min.y + style.FramePadding.y), text_col, ImGuiDir_Down, 1.0f); |
| } |
| RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding); |
| if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview)) |
| RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f,0.0f)); |
| if (label_size.x > 0) |
| RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); |
| |
| if ((pressed || g.NavActivateId == id) && !popup_open) |
| { |
| if (window->DC.NavLayerCurrent == 0) |
| window->NavLastIds[0] = id; |
| OpenPopupEx(id); |
| popup_open = true; |
| } |
| |
| if (!popup_open) |
| return false; |
| |
| if (has_window_size_constraint) |
| { |
| g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasSizeConstraint; |
| g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w); |
| } |
| else |
| { |
| if ((flags & ImGuiComboFlags_HeightMask_) == 0) |
| flags |= ImGuiComboFlags_HeightRegular; |
| IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one |
| int popup_max_height_in_items = -1; |
| if (flags & ImGuiComboFlags_HeightRegular) popup_max_height_in_items = 8; |
| else if (flags & ImGuiComboFlags_HeightSmall) popup_max_height_in_items = 4; |
| else if (flags & ImGuiComboFlags_HeightLarge) popup_max_height_in_items = 20; |
| SetNextWindowSizeConstraints(ImVec2(w, 0.0f), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); |
| } |
| |
| char name[16]; |
| ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth |
| |
| // Peak into expected window size so we can position it |
| if (ImGuiWindow* popup_window = FindWindowByName(name)) |
| if (popup_window->WasActive) |
| { |
| ImVec2 size_expected = CalcWindowExpectedSize(popup_window); |
| if (flags & ImGuiComboFlags_PopupAlignLeft) |
| popup_window->AutoPosLastDirection = ImGuiDir_Left; |
| ImRect r_outer = GetWindowAllowedExtentRect(popup_window); |
| ImVec2 pos = FindBestWindowPosForPopupEx(frame_bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, frame_bb, ImGuiPopupPositionPolicy_ComboBox); |
| SetNextWindowPos(pos); |
| } |
| |
| // We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx() |
| ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove; |
| |
| // Horizontally align ourselves with the framed text |
| PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(style.FramePadding.x, style.WindowPadding.y)); |
| bool ret = Begin(name, NULL, window_flags); |
| PopStyleVar(); |
| if (!ret) |
| { |
| EndPopup(); |
| IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above |
| return false; |
| } |
| return true; |
| } |
| |
| void ImGui::EndCombo() |
| { |
| EndPopup(); |
| } |
| |
| // Getter for the old Combo() API: const char*[] |
| static bool Items_ArrayGetter(void* data, int idx, const char** out_text) |
| { |
| const char* const* items = (const char* const*)data; |
| if (out_text) |
| *out_text = items[idx]; |
| return true; |
| } |
| |
| // Getter for the old Combo() API: "item1\0item2\0item3\0" |
| static bool Items_SingleStringGetter(void* data, int idx, const char** out_text) |
| { |
| // FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited. |
| const char* items_separated_by_zeros = (const char*)data; |
| int items_count = 0; |
| const char* p = items_separated_by_zeros; |
| while (*p) |
| { |
| if (idx == items_count) |
| break; |
| p += strlen(p) + 1; |
| items_count++; |
| } |
| if (!*p) |
| return false; |
| if (out_text) |
| *out_text = p; |
| return true; |
| } |
| |
| // Old API, prefer using BeginCombo() nowadays if you can. |
| bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_max_height_in_items) |
| { |
| ImGuiContext& g = *GImGui; |
| |
| // Call the getter to obtain the preview string which is a parameter to BeginCombo() |
| const char* preview_value = NULL; |
| if (*current_item >= 0 && *current_item < items_count) |
| items_getter(data, *current_item, &preview_value); |
| |
| // The old Combo() API exposed "popup_max_height_in_items". The new more general BeginCombo() API doesn't have/need it, but we emulate it here. |
| if (popup_max_height_in_items != -1 && !(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint)) |
| SetNextWindowSizeConstraints(ImVec2(0,0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); |
| |
| if (!BeginCombo(label, preview_value, ImGuiComboFlags_None)) |
| return false; |
| |
| // Display items |
| // FIXME-OPT: Use clipper (but we need to disable it on the appearing frame to make sure our call to SetItemDefaultFocus() is processed) |
| bool value_changed = false; |
| for (int i = 0; i < items_count; i++) |
| { |
| PushID((void*)(intptr_t)i); |
| const bool item_selected = (i == *current_item); |
| const char* item_text; |
| if (!items_getter(data, i, &item_text)) |
| item_text = "*Unknown item*"; |
| if (Selectable(item_text, item_selected)) |
| { |
| value_changed = true; |
| *current_item = i; |
| } |
| if (item_selected) |
| SetItemDefaultFocus(); |
| PopID(); |
| } |
| |
| EndCombo(); |
| return value_changed; |
| } |
| |
| // Combo box helper allowing to pass an array of strings. |
| bool ImGui::Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items) |
| { |
| const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_in_items); |
| return value_changed; |
| } |
| |
| // Combo box helper allowing to pass all items in a single string literal holding multiple zero-terminated items "item1\0item2\0" |
| bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items) |
| { |
| int items_count = 0; |
| const char* p = items_separated_by_zeros; // FIXME-OPT: Avoid computing this, or at least only when combo is open |
| while (*p) |
| { |
| p += strlen(p) + 1; |
| items_count++; |
| } |
| bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, height_in_items); |
| return value_changed; |
| } |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Data Type and Data Formatting Helpers [Internal] |
| //------------------------------------------------------------------------- |
| // - PatchFormatStringFloatToInt() |
| // - DataTypeGetInfo() |
| // - DataTypeFormatString() |
| // - DataTypeApplyOp() |
| // - DataTypeApplyOpFromText() |
| // - GetMinimumStepAtDecimalPrecision |
| // - RoundScalarWithFormat<>() |
| //------------------------------------------------------------------------- |
| |
| static const ImGuiDataTypeInfo GDataTypeInfo[] = |
| { |
| { sizeof(char), "%d", "%d" }, // ImGuiDataType_S8 |
| { sizeof(unsigned char), "%u", "%u" }, |
| { sizeof(short), "%d", "%d" }, // ImGuiDataType_S16 |
| { sizeof(unsigned short), "%u", "%u" }, |
| { sizeof(int), "%d", "%d" }, // ImGuiDataType_S32 |
| { sizeof(unsigned int), "%u", "%u" }, |
| #ifdef _MSC_VER |
| { sizeof(ImS64), "%I64d","%I64d" }, // ImGuiDataType_S64 |
| { sizeof(ImU64), "%I64u","%I64u" }, |
| #else |
| { sizeof(ImS64), "%lld", "%lld" }, // ImGuiDataType_S64 |
| { sizeof(ImU64), "%llu", "%llu" }, |
| #endif |
| { sizeof(float), "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) |
| { sizeof(double), "%f", "%lf" }, // ImGuiDataType_Double |
| }; |
| IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); |
| |
| // FIXME-LEGACY: Prior to 1.61 our DragInt() function internally used floats and because of this the compile-time default value for format was "%.0f". |
| // Even though we changed the compile-time default, we expect users to have carried %f around, which would break the display of DragInt() calls. |
| // To honor backward compatibility we are rewriting the format string, unless IMGUI_DISABLE_OBSOLETE_FUNCTIONS is enabled. What could possibly go wrong?! |
| static const char* PatchFormatStringFloatToInt(const char* fmt) |
| { |
| if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '0' && fmt[3] == 'f' && fmt[4] == 0) // Fast legacy path for "%.0f" which is expected to be the most common case. |
| return "%d"; |
| const char* fmt_start = ImParseFormatFindStart(fmt); // Find % (if any, and ignore %%) |
| const char* fmt_end = ImParseFormatFindEnd(fmt_start); // Find end of format specifier, which itself is an exercise of confidence/recklessness (because snprintf is dependent on libc or user). |
| if (fmt_end > fmt_start && fmt_end[-1] == 'f') |
| { |
| #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| if (fmt_start == fmt && fmt_end[0] == 0) |
| return "%d"; |
| ImGuiContext& g = *GImGui; |
| ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%.*s%%d%s", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision. |
| return g.TempBuffer; |
| #else |
| IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d" |
| #endif |
| } |
| return fmt; |
| } |
| |
| const ImGuiDataTypeInfo* ImGui::DataTypeGetInfo(ImGuiDataType data_type) |
| { |
| IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); |
| return &GDataTypeInfo[data_type]; |
| } |
| |
| int ImGui::DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format) |
| { |
| // Signedness doesn't matter when pushing integer arguments |
| if (data_type == ImGuiDataType_S32 || data_type == ImGuiDataType_U32) |
| return ImFormatString(buf, buf_size, format, *(const ImU32*)p_data); |
| if (data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) |
| return ImFormatString(buf, buf_size, format, *(const ImU64*)p_data); |
| if (data_type == ImGuiDataType_Float) |
| return ImFormatString(buf, buf_size, format, *(const float*)p_data); |
| if (data_type == ImGuiDataType_Double) |
| return ImFormatString(buf, buf_size, format, *(const double*)p_data); |
| if (data_type == ImGuiDataType_S8) |
| return ImFormatString(buf, buf_size, format, *(const ImS8*)p_data); |
| if (data_type == ImGuiDataType_U8) |
| return ImFormatString(buf, buf_size, format, *(const ImU8*)p_data); |
| if (data_type == ImGuiDataType_S16) |
| return ImFormatString(buf, buf_size, format, *(const ImS16*)p_data); |
| if (data_type == ImGuiDataType_U16) |
| return ImFormatString(buf, buf_size, format, *(const ImU16*)p_data); |
| IM_ASSERT(0); |
| return 0; |
| } |
| |
| void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg1, const void* arg2) |
| { |
| IM_ASSERT(op == '+' || op == '-'); |
| switch (data_type) |
| { |
| case ImGuiDataType_S8: |
| if (op == '+') { *(ImS8*)output = ImAddClampOverflow(*(const ImS8*)arg1, *(const ImS8*)arg2, IM_S8_MIN, IM_S8_MAX); } |
| if (op == '-') { *(ImS8*)output = ImSubClampOverflow(*(const ImS8*)arg1, *(const ImS8*)arg2, IM_S8_MIN, IM_S8_MAX); } |
| return; |
| case ImGuiDataType_U8: |
| if (op == '+') { *(ImU8*)output = ImAddClampOverflow(*(const ImU8*)arg1, *(const ImU8*)arg2, IM_U8_MIN, IM_U8_MAX); } |
| if (op == '-') { *(ImU8*)output = ImSubClampOverflow(*(const ImU8*)arg1, *(const ImU8*)arg2, IM_U8_MIN, IM_U8_MAX); } |
| return; |
| case ImGuiDataType_S16: |
| if (op == '+') { *(ImS16*)output = ImAddClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); } |
| if (op == '-') { *(ImS16*)output = ImSubClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); } |
| return; |
| case ImGuiDataType_U16: |
| if (op == '+') { *(ImU16*)output = ImAddClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); } |
| if (op == '-') { *(ImU16*)output = ImSubClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); } |
| return; |
| case ImGuiDataType_S32: |
| if (op == '+') { *(ImS32*)output = ImAddClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); } |
| if (op == '-') { *(ImS32*)output = ImSubClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); } |
| return; |
| case ImGuiDataType_U32: |
| if (op == '+') { *(ImU32*)output = ImAddClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); } |
| if (op == '-') { *(ImU32*)output = ImSubClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); } |
| return; |
| case ImGuiDataType_S64: |
| if (op == '+') { *(ImS64*)output = ImAddClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); } |
| if (op == '-') { *(ImS64*)output = ImSubClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); } |
| return; |
| case ImGuiDataType_U64: |
| if (op == '+') { *(ImU64*)output = ImAddClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); } |
| if (op == '-') { *(ImU64*)output = ImSubClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); } |
| return; |
| case ImGuiDataType_Float: |
| if (op == '+') { *(float*)output = *(const float*)arg1 + *(const float*)arg2; } |
| if (op == '-') { *(float*)output = *(const float*)arg1 - *(const float*)arg2; } |
| return; |
| case ImGuiDataType_Double: |
| if (op == '+') { *(double*)output = *(const double*)arg1 + *(const double*)arg2; } |
| if (op == '-') { *(double*)output = *(const double*)arg1 - *(const double*)arg2; } |
| return; |
| case ImGuiDataType_COUNT: break; |
| } |
| IM_ASSERT(0); |
| } |
| |
| // User can input math operators (e.g. +100) to edit a numerical values. |
| // NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess.. |
| bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format) |
| { |
| while (ImCharIsBlankA(*buf)) |
| buf++; |
| |
| // We don't support '-' op because it would conflict with inputing negative value. |
| // Instead you can use +-100 to subtract from an existing value |
| char op = buf[0]; |
| if (op == '+' || op == '*' || op == '/') |
| { |
| buf++; |
| while (ImCharIsBlankA(*buf)) |
| buf++; |
| } |
| else |
| { |
| op = 0; |
| } |
| if (!buf[0]) |
| return false; |
| |
| // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all. |
| IM_ASSERT(data_type < ImGuiDataType_COUNT); |
| int data_backup[2]; |
| const ImGuiDataTypeInfo* type_info = ImGui::DataTypeGetInfo(data_type); |
| IM_ASSERT(type_info->Size <= sizeof(data_backup)); |
| memcpy(data_backup, p_data, type_info->Size); |
| |
| if (format == NULL) |
| format = type_info->ScanFmt; |
| |
| // FIXME-LEGACY: The aim is to remove those operators and write a proper expression evaluator at some point.. |
| int arg1i = 0; |
| if (data_type == ImGuiDataType_S32) |
| { |
| int* v = (int*)p_data; |
| int arg0i = *v; |
| float arg1f = 0.0f; |
| if (op && sscanf(initial_value_buf, format, &arg0i) < 1) |
| return false; |
| // Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision |
| if (op == '+') { if (sscanf(buf, "%d", &arg1i)) *v = (int)(arg0i + arg1i); } // Add (use "+-" to subtract) |
| else if (op == '*') { if (sscanf(buf, "%f", &arg1f)) *v = (int)(arg0i * arg1f); } // Multiply |
| else if (op == '/') { if (sscanf(buf, "%f", &arg1f) && arg1f != 0.0f) *v = (int)(arg0i / arg1f); } // Divide |
| else { if (sscanf(buf, format, &arg1i) == 1) *v = arg1i; } // Assign constant |
| } |
| else if (data_type == ImGuiDataType_Float) |
| { |
| // For floats we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in |
| format = "%f"; |
| float* v = (float*)p_data; |
| float arg0f = *v, arg1f = 0.0f; |
| if (op && sscanf(initial_value_buf, format, &arg0f) < 1) |
| return false; |
| if (sscanf(buf, format, &arg1f) < 1) |
| return false; |
| if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) |
| else if (op == '*') { *v = arg0f * arg1f; } // Multiply |
| else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide |
| else { *v = arg1f; } // Assign constant |
| } |
| else if (data_type == ImGuiDataType_Double) |
| { |
| format = "%lf"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis |
| double* v = (double*)p_data; |
| double arg0f = *v, arg1f = 0.0; |
| if (op && sscanf(initial_value_buf, format, &arg0f) < 1) |
| return false; |
| if (sscanf(buf, format, &arg1f) < 1) |
| return false; |
| if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) |
| else if (op == '*') { *v = arg0f * arg1f; } // Multiply |
| else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide |
| else { *v = arg1f; } // Assign constant |
| } |
| else if (data_type == ImGuiDataType_U32 || data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) |
| { |
| // All other types assign constant |
| // We don't bother handling support for legacy operators since they are a little too crappy. Instead we will later implement a proper expression evaluator in the future. |
| sscanf(buf, format, p_data); |
| } |
| else |
| { |
| // Small types need a 32-bit buffer to receive the result from scanf() |
| int v32; |
| sscanf(buf, format, &v32); |
| if (data_type == ImGuiDataType_S8) |
| *(ImS8*)p_data = (ImS8)ImClamp(v32, (int)IM_S8_MIN, (int)IM_S8_MAX); |
| else if (data_type == ImGuiDataType_U8) |
| *(ImU8*)p_data = (ImU8)ImClamp(v32, (int)IM_U8_MIN, (int)IM_U8_MAX); |
| else if (data_type == ImGuiDataType_S16) |
| *(ImS16*)p_data = (ImS16)ImClamp(v32, (int)IM_S16_MIN, (int)IM_S16_MAX); |
| else if (data_type == ImGuiDataType_U16) |
| *(ImU16*)p_data = (ImU16)ImClamp(v32, (int)IM_U16_MIN, (int)IM_U16_MAX); |
| else |
| IM_ASSERT(0); |
| } |
| |
| return memcmp(data_backup, p_data, type_info->Size) != 0; |
| } |
| |
| static float GetMinimumStepAtDecimalPrecision(int decimal_precision) |
| { |
| static const float min_steps[10] = { 1.0f, 0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 0.000000001f }; |
| if (decimal_precision < 0) |
| return FLT_MIN; |
| return (decimal_precision < IM_ARRAYSIZE(min_steps)) ? min_steps[decimal_precision] : ImPow(10.0f, (float)-decimal_precision); |
| } |
| |
| template<typename TYPE> |
| static const char* ImAtoi(const char* src, TYPE* output) |
| { |
| int negative = 0; |
| if (*src == '-') { negative = 1; src++; } |
| if (*src == '+') { src++; } |
| TYPE v = 0; |
| while (*src >= '0' && *src <= '9') |
| v = (v * 10) + (*src++ - '0'); |
| *output = negative ? -v : v; |
| return src; |
| } |
| |
| template<typename TYPE, typename SIGNEDTYPE> |
| TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, TYPE v) |
| { |
| const char* fmt_start = ImParseFormatFindStart(format); |
| if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string |
| return v; |
| char v_str[64]; |
| ImFormatString(v_str, IM_ARRAYSIZE(v_str), fmt_start, v); |
| const char* p = v_str; |
| while (*p == ' ') |
| p++; |
| if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) |
| v = (TYPE)ImAtof(p); |
| else |
| ImAtoi(p, (SIGNEDTYPE*)&v); |
| return v; |
| } |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc. |
| //------------------------------------------------------------------------- |
| // - DragBehaviorT<>() [Internal] |
| // - DragBehavior() [Internal] |
| // - DragScalar() |
| // - DragScalarN() |
| // - DragFloat() |
| // - DragFloat2() |
| // - DragFloat3() |
| // - DragFloat4() |
| // - DragFloatRange2() |
| // - DragInt() |
| // - DragInt2() |
| // - DragInt3() |
| // - DragInt4() |
| // - DragIntRange2() |
| //------------------------------------------------------------------------- |
| |
| // This is called by DragBehavior() when the widget is active (held by mouse or being manipulated with Nav controls) |
| template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE> |
| bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiDragFlags flags) |
| { |
| ImGuiContext& g = *GImGui; |
| const ImGuiAxis axis = (flags & ImGuiDragFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; |
| const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); |
| const bool is_clamped = (v_min < v_max); |
| const bool is_power = (power != 1.0f && is_decimal && is_clamped && (v_max - v_min < FLT_MAX)); |
| const bool is_locked = (v_min > v_max); |
| if (is_locked) |
| return false; |
| |
| // Default tweak speed |
| if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX)) |
| v_speed = (float)((v_max - v_min) * g.DragSpeedDefaultRatio); |
| |
| // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings |
| float adjust_delta = 0.0f; |
| if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f*1.0f) |
| { |
| adjust_delta = g.IO.MouseDelta[axis]; |
| if (g.IO.KeyAlt) |
| adjust_delta *= 1.0f / 100.0f; |
| if (g.IO.KeyShift) |
| adjust_delta *= 10.0f; |
| } |
| else if (g.ActiveIdSource == ImGuiInputSource_Nav) |
| { |
| int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; |
| adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis]; |
| v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); |
| } |
| adjust_delta *= v_speed; |
| |
| // For vertical drag we currently assume that Up=higher value (like we do with vertical sliders). This may become a parameter. |
| if (axis == ImGuiAxis_Y) |
| adjust_delta = -adjust_delta; |
| |
| // Clear current value on activation |
| // Avoid altering values and clamping when we are _already_ past the limits and heading in the same direction, so e.g. if range is 0..255, current value is 300 and we are pushing to the right side, keep the 300. |
| bool is_just_activated = g.ActiveIdIsJustActivated; |
| bool is_already_past_limits_and_pushing_outward = is_clamped && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f)); |
| bool is_drag_direction_change_with_power = is_power && ((adjust_delta < 0 && g.DragCurrentAccum > 0) || (adjust_delta > 0 && g.DragCurrentAccum < 0)); |
| if (is_just_activated || is_already_past_limits_and_pushing_outward || is_drag_direction_change_with_power) |
| { |
| g.DragCurrentAccum = 0.0f; |
| g.DragCurrentAccumDirty = false; |
| } |
| else if (adjust_delta != 0.0f) |
| { |
| g.DragCurrentAccum += adjust_delta; |
| g.DragCurrentAccumDirty = true; |
| } |
| |
| if (!g.DragCurrentAccumDirty) |
| return false; |
| |
| TYPE v_cur = *v; |
| FLOATTYPE v_old_ref_for_accum_remainder = (FLOATTYPE)0.0f; |
| |
| if (is_power) |
| { |
| // Offset + round to user desired precision, with a curve on the v_min..v_max range to get more precision on one side of the range |
| FLOATTYPE v_old_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); |
| FLOATTYPE v_new_norm_curved = v_old_norm_curved + (g.DragCurrentAccum / (v_max - v_min)); |
| v_cur = v_min + (SIGNEDTYPE)ImPow(ImSaturate((float)v_new_norm_curved), power) * (v_max - v_min); |
| v_old_ref_for_accum_remainder = v_old_norm_curved; |
| } |
| else |
| { |
| v_cur += (SIGNEDTYPE)g.DragCurrentAccum; |
| } |
| |
| // Round to user desired precision based on format string |
| v_cur = RoundScalarWithFormatT<TYPE, SIGNEDTYPE>(format, data_type, v_cur); |
| |
| // Preserve remainder after rounding has been applied. This also allow slow tweaking of values. |
| g.DragCurrentAccumDirty = false; |
| if (is_power) |
| { |
| FLOATTYPE v_cur_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); |
| g.DragCurrentAccum -= (float)(v_cur_norm_curved - v_old_ref_for_accum_remainder); |
| } |
| else |
| { |
| g.DragCurrentAccum -= (float)((SIGNEDTYPE)v_cur - (SIGNEDTYPE)*v); |
| } |
| |
| // Lose zero sign for float/double |
| if (v_cur == (TYPE)-0) |
| v_cur = (TYPE)0; |
| |
| // Clamp values (+ handle overflow/wrap-around for integer types) |
| if (*v != v_cur && is_clamped) |
| { |
| if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_decimal)) |
| v_cur = v_min; |
| if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_decimal)) |
| v_cur = v_max; |
| } |
| |
| // Apply result |
| if (*v == v_cur) |
| return false; |
| *v = v_cur; |
| return true; |
| } |
| |
| bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, float power, ImGuiDragFlags flags) |
| { |
| ImGuiContext& g = *GImGui; |
| if (g.ActiveId == id) |
| { |
| if (g.ActiveIdSource == ImGuiInputSource_Mouse && !g.IO.MouseDown[0]) |
| ClearActiveID(); |
| else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) |
| ClearActiveID(); |
| } |
| if (g.ActiveId != id) |
| return false; |
| |
| switch (data_type) |
| { |
| case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = DragBehaviorT<ImS32, ImS32, float>(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS8*) p_min : IM_S8_MIN, p_max ? *(const ImS8*)p_max : IM_S8_MAX, format, power, flags); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } |
| case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = DragBehaviorT<ImU32, ImS32, float>(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU8*) p_min : IM_U8_MIN, p_max ? *(const ImU8*)p_max : IM_U8_MAX, format, power, flags); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } |
| case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = DragBehaviorT<ImS32, ImS32, float>(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS16*)p_min : IM_S16_MIN, p_max ? *(const ImS16*)p_max : IM_S16_MAX, format, power, flags); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } |
| case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = DragBehaviorT<ImU32, ImS32, float>(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU16*)p_min : IM_U16_MIN, p_max ? *(const ImU16*)p_max : IM_U16_MAX, format, power, flags); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } |
| case ImGuiDataType_S32: return DragBehaviorT<ImS32, ImS32, float >(data_type, (ImS32*)p_v, v_speed, p_min ? *(const ImS32* )p_min : IM_S32_MIN, p_max ? *(const ImS32* )p_max : IM_S32_MAX, format, power, flags); |
| case ImGuiDataType_U32: return DragBehaviorT<ImU32, ImS32, float >(data_type, (ImU32*)p_v, v_speed, p_min ? *(const ImU32* )p_min : IM_U32_MIN, p_max ? *(const ImU32* )p_max : IM_U32_MAX, format, power, flags); |
| case ImGuiDataType_S64: return DragBehaviorT<ImS64, ImS64, double>(data_type, (ImS64*)p_v, v_speed, p_min ? *(const ImS64* )p_min : IM_S64_MIN, p_max ? *(const ImS64* )p_max : IM_S64_MAX, format, power, flags); |
| case ImGuiDataType_U64: return DragBehaviorT<ImU64, ImS64, double>(data_type, (ImU64*)p_v, v_speed, p_min ? *(const ImU64* )p_min : IM_U64_MIN, p_max ? *(const ImU64* )p_max : IM_U64_MAX, format, power, flags); |
| case ImGuiDataType_Float: return DragBehaviorT<float, float, float >(data_type, (float*)p_v, v_speed, p_min ? *(const float* )p_min : -FLT_MAX, p_max ? *(const float* )p_max : FLT_MAX, format, power, flags); |
| case ImGuiDataType_Double: return DragBehaviorT<double,double,double>(data_type, (double*)p_v, v_speed, p_min ? *(const double*)p_min : -DBL_MAX, p_max ? *(const double*)p_max : DBL_MAX, format, power, flags); |
| case ImGuiDataType_COUNT: break; |
| } |
| IM_ASSERT(0); |
| return false; |
| } |
| |
| // Note: p_data, p_min and p_max are _pointers_ to a memory address holding the data. For a Drag widget, p_min and p_max are optional. |
| // Read code of e.g. SliderFloat(), SliderInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. |
| bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| if (power != 1.0f) |
| IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds |
| |
| ImGuiContext& g = *GImGui; |
| const ImGuiStyle& style = g.Style; |
| const ImGuiID id = window->GetID(label); |
| const float w = CalcItemWidth(); |
| const ImVec2 label_size = CalcTextSize(label, NULL, true); |
| const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); |
| const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); |
| |
| ItemSize(total_bb, style.FramePadding.y); |
| if (!ItemAdd(total_bb, id, &frame_bb)) |
| return false; |
| |
| // Default format string when passing NULL |
| if (format == NULL) |
| format = DataTypeGetInfo(data_type)->PrintFmt; |
| else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) |
| format = PatchFormatStringFloatToInt(format); |
| |
| // Tabbing or CTRL-clicking on Drag turns it into an input box |
| const bool hovered = ItemHoverable(frame_bb, id); |
| bool temp_input_is_active = TempInputTextIsActive(id); |
| bool temp_input_start = false; |
| if (!temp_input_is_active) |
| { |
| const bool focus_requested = FocusableItemRegister(window, id); |
| const bool clicked = (hovered && g.IO.MouseClicked[0]); |
| const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); |
| if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id) |
| { |
| SetActiveID(id, window); |
| SetFocusID(id, window); |
| FocusWindow(window); |
| g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); |
| if (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id) |
| { |
| temp_input_start = true; |
| FocusableItemUnregister(window); |
| } |
| } |
| } |
| if (temp_input_is_active || temp_input_start) |
| return TempInputTextScalar(frame_bb, id, label, data_type, p_data, format); |
| |
| // Draw frame |
| const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); |
| RenderNavHighlight(frame_bb, id); |
| RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); |
| |
| // Drag behavior |
| const bool value_changed = DragBehavior(id, data_type, p_data, v_speed, p_min, p_max, format, power, ImGuiDragFlags_None); |
| if (value_changed) |
| MarkItemEdited(id); |
| |
| // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. |
| char value_buf[64]; |
| const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); |
| RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); |
| |
| if (label_size.x > 0.0f) |
| RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); |
| |
| IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); |
| return value_changed; |
| } |
| |
| bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| bool value_changed = false; |
| BeginGroup(); |
| PushID(label); |
| PushMultiItemsWidths(components, CalcItemWidth()); |
| size_t type_size = GDataTypeInfo[data_type].Size; |
| for (int i = 0; i < components; i++) |
| { |
| PushID(i); |
| if (i > 0) |
| SameLine(0, g.Style.ItemInnerSpacing.x); |
| value_changed |= DragScalar("", data_type, p_data, v_speed, p_min, p_max, format, power); |
| PopID(); |
| PopItemWidth(); |
| p_data = (void*)((char*)p_data + type_size); |
| } |
| PopID(); |
| |
| const char* label_end = FindRenderedTextEnd(label); |
| if (label != label_end) |
| { |
| SameLine(0, g.Style.ItemInnerSpacing.x); |
| TextEx(label, label_end); |
| } |
| |
| EndGroup(); |
| return value_changed; |
| } |
| |
| bool ImGui::DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) |
| { |
| return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); |
| } |
| |
| bool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) |
| { |
| return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); |
| } |
| |
| bool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) |
| { |
| return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); |
| } |
| |
| bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) |
| { |
| return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); |
| } |
| |
| bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* format, const char* format_max, float power) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| PushID(label); |
| BeginGroup(); |
| PushMultiItemsWidths(2, CalcItemWidth()); |
| |
| bool value_changed = DragFloat("##min", v_current_min, v_speed, (v_min >= v_max) ? -FLT_MAX : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format, power); |
| PopItemWidth(); |
| SameLine(0, g.Style.ItemInnerSpacing.x); |
| value_changed |= DragFloat("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? FLT_MAX : v_max, format_max ? format_max : format, power); |
| PopItemWidth(); |
| SameLine(0, g.Style.ItemInnerSpacing.x); |
| |
| TextEx(label, FindRenderedTextEnd(label)); |
| EndGroup(); |
| PopID(); |
| return value_changed; |
| } |
| |
| // NB: v_speed is float to allow adjusting the drag speed with more precision |
| bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format) |
| { |
| return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format); |
| } |
| |
| bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format) |
| { |
| return DragScalarN(label, ImGuiDataType_S32, v, 2, v_speed, &v_min, &v_max, format); |
| } |
| |
| bool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* format) |
| { |
| return DragScalarN(label, ImGuiDataType_S32, v, 3, v_speed, &v_min, &v_max, format); |
| } |
| |
| bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* format) |
| { |
| return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format); |
| } |
| |
| bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max) |
| { |
| ImGuiWindow* window = GetCurrentWindow(); |
| if (window->SkipItems) |
| return false; |
| |
| ImGuiContext& g = *GImGui; |
| PushID(label); |
| BeginGroup(); |
| PushMultiItemsWidths(2, CalcItemWidth()); |
| |
| bool value_changed = DragInt("##min", v_current_min, v_speed, (v_min >= v_max) ? INT_MIN : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format); |
| PopItemWidth(); |
| SameLine(0, g.Style.ItemInnerSpacing.x); |
| value_changed |= DragInt("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? INT_MAX : v_max, format_max ? format_max : format); |
| PopItemWidth(); |
| SameLine(0, g.Style.ItemInnerSpacing.x); |
| |
| TextEx(label, FindRenderedTextEnd(label)); |
| EndGroup(); |
| PopID(); |
| |
| return value_changed; |
| } |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. |
| //------------------------------------------------------------------------- |
| // - SliderBehaviorT<>() [Internal] |
| // - SliderBehavior() [Internal] |
| // - SliderScalar() |
| // - SliderScalarN() |
| // - SliderFloat() |
| // - SliderFloat2() |
| // - SliderFloat3() |
| // - SliderFloat4() |
| // - SliderAngle() |
| // - SliderInt() |
| // - SliderInt2() |
| // - SliderInt3() |
| // - SliderInt4() |
| // - VSliderScalar() |
| // - VSliderFloat() |
| // - VSliderInt() |
| //------------------------------------------------------------------------- |
| |
| template<typename TYPE, typename FLOATTYPE> |
| float ImGui::SliderCalcRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, float power, float linear_zero_pos) |
| { |
| if (v_min == v_max) |
| return 0.0f; |
| |
| const bool is_power = (power != 1.0f) && (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double); |
| const TYPE v_clamped = (v_min < v_max) ? ImClamp(v, v_min, v_max) : ImClamp(v, v_max, v_min); |
| if (is_power) |
| { |
| if (v_clamped < 0.0f) |
| { |
| const float f = 1.0f - (float)((v_clamped - v_min) / (ImMin((TYPE)0, v_max) - v_min)); |
| return (1.0f - ImPow(f, 1.0f/power)) * linear_zero_pos; |
| } |
| else |
| { |
| const float f = (float)((v_clamped - ImMax((TYPE)0, v_min)) / (v_max - ImMax((TYPE)0, v_min))); |
| return linear_zero_pos + ImPow(f, 1.0f/power) * |