| // dear imgui, v1.90 WIP |
| // (drawing and font code) |
| |
| /* |
| |
| Index of this file: |
| |
| // [SECTION] STB libraries implementation |
| // [SECTION] Style functions |
| // [SECTION] ImDrawList |
| // [SECTION] ImDrawListSplitter |
| // [SECTION] ImDrawData |
| // [SECTION] Helpers ShadeVertsXXX functions |
| // [SECTION] ImFontConfig |
| // [SECTION] ImFontAtlas |
| // [SECTION] ImFontAtlas glyph ranges helpers |
| // [SECTION] ImFontGlyphRangesBuilder |
| // [SECTION] ImFont |
| // [SECTION] ImGui Internal Render Helpers |
| // [SECTION] Decompression code |
| // [SECTION] Default font data (ProggyClean.ttf) |
| |
| */ |
| |
| #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| #define _CRT_SECURE_NO_WARNINGS |
| #endif |
| |
| #ifndef IMGUI_DEFINE_MATH_OPERATORS |
| #define IMGUI_DEFINE_MATH_OPERATORS |
| #endif |
| |
| #include "imgui.h" |
| #ifndef IMGUI_DISABLE |
| #include "imgui_internal.h" |
| #ifdef IMGUI_ENABLE_FREETYPE |
| #include "misc/freetype/imgui_freetype.h" |
| #endif |
| |
| #include <stdio.h> // vsnprintf, sscanf, printf |
| |
| // Visual Studio warnings |
| #ifdef _MSC_VER |
| #pragma warning (disable: 4127) // condition expression is constant |
| #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) |
| #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen |
| #pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). |
| #pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer) |
| #endif |
| |
| // Clang/GCC warnings with -Weverything |
| #if defined(__clang__) |
| #if __has_warning("-Wunknown-warning-option") |
| #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! |
| #endif |
| #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' |
| #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 ok. |
| #pragma clang diagnostic ignored "-Wglobal-constructors" // warning: declaration requires a global destructor // similar to above, not sure what the exact difference is. |
| #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness |
| #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 |
| #pragma clang diagnostic ignored "-Wcomma" // warning: possible misuse of comma operator here |
| #pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier |
| #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. |
| #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision |
| #pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter |
| #elif defined(__GNUC__) |
| #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind |
| #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used |
| #pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function |
| #pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value |
| #pragma GCC diagnostic ignored "-Wstack-protector" // warning: stack protector not protecting local variables: variable length buffer |
| #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 |
| |
| //------------------------------------------------------------------------- |
| // [SECTION] STB libraries implementation (for stb_truetype and stb_rect_pack) |
| //------------------------------------------------------------------------- |
| |
| // Compile time options: |
| //#define IMGUI_STB_NAMESPACE ImStb |
| //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" |
| //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" |
| //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION |
| //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION |
| |
| #ifdef IMGUI_STB_NAMESPACE |
| namespace IMGUI_STB_NAMESPACE |
| { |
| #endif |
| |
| #ifdef _MSC_VER |
| #pragma warning (push) |
| #pragma warning (disable: 4456) // declaration of 'xx' hides previous local declaration |
| #pragma warning (disable: 6011) // (stb_rectpack) Dereferencing NULL pointer 'cur->next'. |
| #pragma warning (disable: 6385) // (stb_truetype) Reading invalid data from 'buffer': the readable size is '_Old_3`kernel_width' bytes, but '3' bytes may be read. |
| #pragma warning (disable: 28182) // (stb_rectpack) Dereferencing NULL pointer. 'cur' contains the same NULL value as 'cur->next' did. |
| #endif |
| |
| #if defined(__clang__) |
| #pragma clang diagnostic push |
| #pragma clang diagnostic ignored "-Wunused-function" |
| #pragma clang diagnostic ignored "-Wmissing-prototypes" |
| #pragma clang diagnostic ignored "-Wimplicit-fallthrough" |
| #pragma clang diagnostic ignored "-Wcast-qual" // warning: cast from 'const xxxx *' to 'xxx *' drops const qualifier |
| #endif |
| |
| #if defined(__GNUC__) |
| #pragma GCC diagnostic push |
| #pragma GCC diagnostic ignored "-Wtype-limits" // warning: comparison is always true due to limited range of data type [-Wtype-limits] |
| #pragma GCC diagnostic ignored "-Wcast-qual" // warning: cast from type 'const xxxx *' to type 'xxxx *' casts away qualifiers |
| #endif |
| |
| #ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) |
| #ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in another compilation unit |
| #define STBRP_STATIC |
| #define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0) |
| #define STBRP_SORT ImQsort |
| #define STB_RECT_PACK_IMPLEMENTATION |
| #endif |
| #ifdef IMGUI_STB_RECT_PACK_FILENAME |
| #include IMGUI_STB_RECT_PACK_FILENAME |
| #else |
| #include "imstb_rectpack.h" |
| #endif |
| #endif |
| |
| #ifdef IMGUI_ENABLE_STB_TRUETYPE |
| #ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) |
| #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in another compilation unit |
| #define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) |
| #define STBTT_free(x,u) ((void)(u), IM_FREE(x)) |
| #define STBTT_assert(x) do { IM_ASSERT(x); } while(0) |
| #define STBTT_fmod(x,y) ImFmod(x,y) |
| #define STBTT_sqrt(x) ImSqrt(x) |
| #define STBTT_pow(x,y) ImPow(x,y) |
| #define STBTT_fabs(x) ImFabs(x) |
| #define STBTT_ifloor(x) ((int)ImFloor(x)) |
| #define STBTT_iceil(x) ((int)ImCeil(x)) |
| #define STBTT_STATIC |
| #define STB_TRUETYPE_IMPLEMENTATION |
| #else |
| #define STBTT_DEF extern |
| #endif |
| #ifdef IMGUI_STB_TRUETYPE_FILENAME |
| #include IMGUI_STB_TRUETYPE_FILENAME |
| #else |
| #include "imstb_truetype.h" |
| #endif |
| #endif |
| #endif // IMGUI_ENABLE_STB_TRUETYPE |
| |
| #if defined(__GNUC__) |
| #pragma GCC diagnostic pop |
| #endif |
| |
| #if defined(__clang__) |
| #pragma clang diagnostic pop |
| #endif |
| |
| #if defined(_MSC_VER) |
| #pragma warning (pop) |
| #endif |
| |
| #ifdef IMGUI_STB_NAMESPACE |
| } // namespace ImStb |
| using namespace IMGUI_STB_NAMESPACE; |
| #endif |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] Style functions |
| //----------------------------------------------------------------------------- |
| |
| void ImGui::StyleColorsDark(ImGuiStyle* dst) |
| { |
| ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); |
| ImVec4* colors = style->Colors; |
| |
| colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
| colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); |
| colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); |
| colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); |
| colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); |
| colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.29f, 0.48f, 0.54f); |
| colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
| colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
| colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); |
| colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f); |
| colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); |
| colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); |
| colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); |
| colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); |
| colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); |
| colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); |
| colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f); |
| colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
| colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); |
| colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); |
| colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); |
| colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; |
| colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f); |
| colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f); |
| colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.20f); |
| colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
| colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); |
| colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); |
| colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; |
| colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); |
| colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); |
| colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); |
| colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); |
| colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); |
| colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
| colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); |
| colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); |
| colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); // Prefer using Alpha=1.0 here |
| colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); // Prefer using Alpha=1.0 here |
| colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); |
| colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); |
| colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); |
| colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); |
| colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); |
| colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); |
| } |
| |
| void ImGui::StyleColorsClassic(ImGuiStyle* dst) |
| { |
| ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); |
| ImVec4* colors = style->Colors; |
| |
| colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); |
| colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); |
| colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.85f); |
| colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_PopupBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f); |
| colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f); |
| colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_FrameBg] = ImVec4(0.43f, 0.43f, 0.43f, 0.39f); |
| colors[ImGuiCol_FrameBgHovered] = ImVec4(0.47f, 0.47f, 0.69f, 0.40f); |
| colors[ImGuiCol_FrameBgActive] = ImVec4(0.42f, 0.41f, 0.64f, 0.69f); |
| colors[ImGuiCol_TitleBg] = ImVec4(0.27f, 0.27f, 0.54f, 0.83f); |
| colors[ImGuiCol_TitleBgActive] = ImVec4(0.32f, 0.32f, 0.63f, 0.87f); |
| colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f); |
| colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.80f); |
| colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.60f); |
| colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f); |
| colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f); |
| colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); |
| colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f); |
| colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); |
| colors[ImGuiCol_SliderGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); |
| colors[ImGuiCol_Button] = ImVec4(0.35f, 0.40f, 0.61f, 0.62f); |
| colors[ImGuiCol_ButtonHovered] = ImVec4(0.40f, 0.48f, 0.71f, 0.79f); |
| colors[ImGuiCol_ButtonActive] = ImVec4(0.46f, 0.54f, 0.80f, 1.00f); |
| colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f); |
| colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f); |
| colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.53f, 0.87f, 0.80f); |
| colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f); |
| colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f); |
| colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 1.00f); |
| colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f); |
| colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f); |
| colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f); |
| colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); |
| colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; |
| colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); |
| colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); |
| colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); |
| colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
| colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
| colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
| colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); |
| colors[ImGuiCol_TableHeaderBg] = ImVec4(0.27f, 0.27f, 0.38f, 1.00f); |
| colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.45f, 1.00f); // Prefer using Alpha=1.0 here |
| colors[ImGuiCol_TableBorderLight] = ImVec4(0.26f, 0.26f, 0.28f, 1.00f); // Prefer using Alpha=1.0 here |
| colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f); |
| colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); |
| colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); |
| colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; |
| colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); |
| colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); |
| colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); |
| } |
| |
| // Those light colors are better suited with a thicker font than the default one + FrameBorder |
| void ImGui::StyleColorsLight(ImGuiStyle* dst) |
| { |
| ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); |
| ImVec4* colors = style->Colors; |
| |
| colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); |
| colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); |
| colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); |
| colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.98f); |
| colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.30f); |
| colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
| colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
| colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
| colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f); |
| colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f); |
| colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f); |
| colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); |
| colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f); |
| colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f); |
| colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f); |
| colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f); |
| colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); |
| colors[ImGuiCol_SliderGrabActive] = ImVec4(0.46f, 0.54f, 0.80f, 0.60f); |
| colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
| colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); |
| colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); |
| colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); |
| colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
| colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 0.62f); |
| colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f); |
| colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f); |
| colors[ImGuiCol_ResizeGrip] = ImVec4(0.35f, 0.35f, 0.35f, 0.17f); |
| colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
| colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); |
| colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.90f); |
| colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; |
| colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); |
| colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); |
| colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); |
| colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); |
| colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); |
| colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
| colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.45f, 0.00f, 1.00f); |
| colors[ImGuiCol_TableHeaderBg] = ImVec4(0.78f, 0.87f, 0.98f, 1.00f); |
| colors[ImGuiCol_TableBorderStrong] = ImVec4(0.57f, 0.57f, 0.64f, 1.00f); // Prefer using Alpha=1.0 here |
| colors[ImGuiCol_TableBorderLight] = ImVec4(0.68f, 0.68f, 0.74f, 1.00f); // Prefer using Alpha=1.0 here |
| colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
| colors[ImGuiCol_TableRowBgAlt] = ImVec4(0.30f, 0.30f, 0.30f, 0.09f); |
| colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); |
| colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); |
| colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; |
| colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f); |
| colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.20f); |
| colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); |
| } |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] ImDrawList |
| //----------------------------------------------------------------------------- |
| |
| ImDrawListSharedData::ImDrawListSharedData() |
| { |
| memset(this, 0, sizeof(*this)); |
| for (int i = 0; i < IM_ARRAYSIZE(ArcFastVtx); i++) |
| { |
| const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(ArcFastVtx); |
| ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a)); |
| } |
| ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); |
| } |
| |
| void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) |
| { |
| if (CircleSegmentMaxError == max_error) |
| return; |
| |
| IM_ASSERT(max_error > 0.0f); |
| CircleSegmentMaxError = max_error; |
| for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++) |
| { |
| const float radius = (float)i; |
| CircleSegmentCounts[i] = (ImU8)((i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : IM_DRAWLIST_ARCFAST_SAMPLE_MAX); |
| } |
| ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); |
| } |
| |
| // Initialize before use in a new frame. We always have a command ready in the buffer. |
| void ImDrawList::_ResetForNewFrame() |
| { |
| // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory. |
| IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, ClipRect) == 0); |
| IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, TextureId) == sizeof(ImVec4)); |
| IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); |
| if (_Splitter._Count > 1) |
| _Splitter.Merge(this); |
| |
| CmdBuffer.resize(0); |
| IdxBuffer.resize(0); |
| VtxBuffer.resize(0); |
| Flags = _Data->InitialFlags; |
| memset(&_CmdHeader, 0, sizeof(_CmdHeader)); |
| _VtxCurrentIdx = 0; |
| _VtxWritePtr = NULL; |
| _IdxWritePtr = NULL; |
| _ClipRectStack.resize(0); |
| _TextureIdStack.resize(0); |
| _Path.resize(0); |
| _Splitter.Clear(); |
| CmdBuffer.push_back(ImDrawCmd()); |
| _FringeScale = 1.0f; |
| } |
| |
| void ImDrawList::_ClearFreeMemory() |
| { |
| CmdBuffer.clear(); |
| IdxBuffer.clear(); |
| VtxBuffer.clear(); |
| Flags = ImDrawListFlags_None; |
| _VtxCurrentIdx = 0; |
| _VtxWritePtr = NULL; |
| _IdxWritePtr = NULL; |
| _ClipRectStack.clear(); |
| _TextureIdStack.clear(); |
| _Path.clear(); |
| _Splitter.ClearFreeMemory(); |
| } |
| |
| ImDrawList* ImDrawList::CloneOutput() const |
| { |
| ImDrawList* dst = IM_NEW(ImDrawList(_Data)); |
| dst->CmdBuffer = CmdBuffer; |
| dst->IdxBuffer = IdxBuffer; |
| dst->VtxBuffer = VtxBuffer; |
| dst->Flags = Flags; |
| return dst; |
| } |
| |
| void ImDrawList::AddDrawCmd() |
| { |
| ImDrawCmd draw_cmd; |
| draw_cmd.ClipRect = _CmdHeader.ClipRect; // Same as calling ImDrawCmd_HeaderCopy() |
| draw_cmd.TextureId = _CmdHeader.TextureId; |
| draw_cmd.VtxOffset = _CmdHeader.VtxOffset; |
| draw_cmd.IdxOffset = IdxBuffer.Size; |
| |
| IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w); |
| CmdBuffer.push_back(draw_cmd); |
| } |
| |
| // Pop trailing draw command (used before merging or presenting to user) |
| // Note that this leaves the ImDrawList in a state unfit for further commands, as most code assume that CmdBuffer.Size > 0 && CmdBuffer.back().UserCallback == NULL |
| void ImDrawList::_PopUnusedDrawCmd() |
| { |
| while (CmdBuffer.Size > 0) |
| { |
| ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| if (curr_cmd->ElemCount != 0 || curr_cmd->UserCallback != NULL) |
| return;// break; |
| CmdBuffer.pop_back(); |
| } |
| } |
| |
| void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) |
| { |
| IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
| ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| IM_ASSERT(curr_cmd->UserCallback == NULL); |
| if (curr_cmd->ElemCount != 0) |
| { |
| AddDrawCmd(); |
| curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| } |
| curr_cmd->UserCallback = callback; |
| curr_cmd->UserCallbackData = callback_data; |
| |
| AddDrawCmd(); // Force a new command after us (see comment below) |
| } |
| |
| // Compare ClipRect, TextureId and VtxOffset with a single memcmp() |
| #define ImDrawCmd_HeaderSize (IM_OFFSETOF(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) |
| #define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset |
| #define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset |
| #define ImDrawCmd_AreSequentialIdxOffset(CMD_0, CMD_1) (CMD_0->IdxOffset + CMD_0->ElemCount == CMD_1->IdxOffset) |
| |
| // Try to merge two last draw commands |
| void ImDrawList::_TryMergeDrawCmds() |
| { |
| IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
| ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| ImDrawCmd* prev_cmd = curr_cmd - 1; |
| if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL) |
| { |
| prev_cmd->ElemCount += curr_cmd->ElemCount; |
| CmdBuffer.pop_back(); |
| } |
| } |
| |
| // Our scheme may appears a bit unusual, basically we want the most-common calls AddLine AddRect etc. to not have to perform any check so we always have a command ready in the stack. |
| // The cost of figuring out if a new command has to be added or if we can merge is paid in those Update** functions only. |
| void ImDrawList::_OnChangedClipRect() |
| { |
| // If current command is used with different settings we need to add a new command |
| IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
| ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| if (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &_CmdHeader.ClipRect, sizeof(ImVec4)) != 0) |
| { |
| AddDrawCmd(); |
| return; |
| } |
| IM_ASSERT(curr_cmd->UserCallback == NULL); |
| |
| // Try to merge with previous command if it matches, else use current command |
| ImDrawCmd* prev_cmd = curr_cmd - 1; |
| if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL) |
| { |
| CmdBuffer.pop_back(); |
| return; |
| } |
| |
| curr_cmd->ClipRect = _CmdHeader.ClipRect; |
| } |
| |
| void ImDrawList::_OnChangedTextureID() |
| { |
| // If current command is used with different settings we need to add a new command |
| IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
| ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| if (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != _CmdHeader.TextureId) |
| { |
| AddDrawCmd(); |
| return; |
| } |
| IM_ASSERT(curr_cmd->UserCallback == NULL); |
| |
| // Try to merge with previous command if it matches, else use current command |
| ImDrawCmd* prev_cmd = curr_cmd - 1; |
| if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL) |
| { |
| CmdBuffer.pop_back(); |
| return; |
| } |
| |
| curr_cmd->TextureId = _CmdHeader.TextureId; |
| } |
| |
| void ImDrawList::_OnChangedVtxOffset() |
| { |
| // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this. |
| _VtxCurrentIdx = 0; |
| IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
| ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| //IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); // See #3349 |
| if (curr_cmd->ElemCount != 0) |
| { |
| AddDrawCmd(); |
| return; |
| } |
| IM_ASSERT(curr_cmd->UserCallback == NULL); |
| curr_cmd->VtxOffset = _CmdHeader.VtxOffset; |
| } |
| |
| int ImDrawList::_CalcCircleAutoSegmentCount(float radius) const |
| { |
| // Automatic segment count |
| const int radius_idx = (int)(radius + 0.999999f); // ceil to never reduce accuracy |
| if (radius_idx >= 0 && radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) |
| return _Data->CircleSegmentCounts[radius_idx]; // Use cached value |
| else |
| return IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); |
| } |
| |
| // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) |
| void ImDrawList::PushClipRect(const ImVec2& cr_min, const ImVec2& cr_max, bool intersect_with_current_clip_rect) |
| { |
| ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y); |
| if (intersect_with_current_clip_rect) |
| { |
| ImVec4 current = _CmdHeader.ClipRect; |
| if (cr.x < current.x) cr.x = current.x; |
| if (cr.y < current.y) cr.y = current.y; |
| if (cr.z > current.z) cr.z = current.z; |
| if (cr.w > current.w) cr.w = current.w; |
| } |
| cr.z = ImMax(cr.x, cr.z); |
| cr.w = ImMax(cr.y, cr.w); |
| |
| _ClipRectStack.push_back(cr); |
| _CmdHeader.ClipRect = cr; |
| _OnChangedClipRect(); |
| } |
| |
| void ImDrawList::PushClipRectFullScreen() |
| { |
| PushClipRect(ImVec2(_Data->ClipRectFullscreen.x, _Data->ClipRectFullscreen.y), ImVec2(_Data->ClipRectFullscreen.z, _Data->ClipRectFullscreen.w)); |
| } |
| |
| void ImDrawList::PopClipRect() |
| { |
| _ClipRectStack.pop_back(); |
| _CmdHeader.ClipRect = (_ClipRectStack.Size == 0) ? _Data->ClipRectFullscreen : _ClipRectStack.Data[_ClipRectStack.Size - 1]; |
| _OnChangedClipRect(); |
| } |
| |
| void ImDrawList::PushTextureID(ImTextureID texture_id) |
| { |
| _TextureIdStack.push_back(texture_id); |
| _CmdHeader.TextureId = texture_id; |
| _OnChangedTextureID(); |
| } |
| |
| void ImDrawList::PopTextureID() |
| { |
| _TextureIdStack.pop_back(); |
| _CmdHeader.TextureId = (_TextureIdStack.Size == 0) ? (ImTextureID)NULL : _TextureIdStack.Data[_TextureIdStack.Size - 1]; |
| _OnChangedTextureID(); |
| } |
| |
| // Reserve space for a number of vertices and indices. |
| // You must finish filling your reserved data before calling PrimReserve() again, as it may reallocate or |
| // submit the intermediate results. PrimUnreserve() can be used to release unused allocations. |
| void ImDrawList::PrimReserve(int idx_count, int vtx_count) |
| { |
| // Large mesh support (when enabled) |
| IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); |
| if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) |
| { |
| // FIXME: In theory we should be testing that vtx_count <64k here. |
| // In practice, RenderText() relies on reserving ahead for a worst case scenario so it is currently useful for us |
| // to not make that check until we rework the text functions to handle clipping and large horizontal lines better. |
| _CmdHeader.VtxOffset = VtxBuffer.Size; |
| _OnChangedVtxOffset(); |
| } |
| |
| ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| draw_cmd->ElemCount += idx_count; |
| |
| int vtx_buffer_old_size = VtxBuffer.Size; |
| VtxBuffer.resize(vtx_buffer_old_size + vtx_count); |
| _VtxWritePtr = VtxBuffer.Data + vtx_buffer_old_size; |
| |
| int idx_buffer_old_size = IdxBuffer.Size; |
| IdxBuffer.resize(idx_buffer_old_size + idx_count); |
| _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size; |
| } |
| |
| // Release the a number of reserved vertices/indices from the end of the last reservation made with PrimReserve(). |
| void ImDrawList::PrimUnreserve(int idx_count, int vtx_count) |
| { |
| IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); |
| |
| ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
| draw_cmd->ElemCount -= idx_count; |
| VtxBuffer.shrink(VtxBuffer.Size - vtx_count); |
| IdxBuffer.shrink(IdxBuffer.Size - idx_count); |
| } |
| |
| // Fully unrolled with inline call to keep our debug builds decently fast. |
| void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col) |
| { |
| ImVec2 b(c.x, a.y), d(a.x, c.y), uv(_Data->TexUvWhitePixel); |
| ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; |
| _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); |
| _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); |
| _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; |
| _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; |
| _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; |
| _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; |
| _VtxWritePtr += 4; |
| _VtxCurrentIdx += 4; |
| _IdxWritePtr += 6; |
| } |
| |
| void ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col) |
| { |
| ImVec2 b(c.x, a.y), d(a.x, c.y), uv_b(uv_c.x, uv_a.y), uv_d(uv_a.x, uv_c.y); |
| ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; |
| _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); |
| _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); |
| _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; |
| _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; |
| _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; |
| _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; |
| _VtxWritePtr += 4; |
| _VtxCurrentIdx += 4; |
| _IdxWritePtr += 6; |
| } |
| |
| void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col) |
| { |
| ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; |
| _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); |
| _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); |
| _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; |
| _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; |
| _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; |
| _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; |
| _VtxWritePtr += 4; |
| _VtxCurrentIdx += 4; |
| _IdxWritePtr += 6; |
| } |
| |
| // On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superfluous function calls to optimize debug/non-inlined builds. |
| // - Those macros expects l-values and need to be used as their own statement. |
| // - Those macros are intentionally not surrounded by the 'do {} while (0)' idiom because even that translates to runtime with debug compilers. |
| #define IM_NORMALIZE2F_OVER_ZERO(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = ImRsqrt(d2); VX *= inv_len; VY *= inv_len; } } (void)0 |
| #define IM_FIXNORMAL2F_MAX_INVLEN2 100.0f // 500.0f (see #4053, #3366) |
| #define IM_FIXNORMAL2F(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.000001f) { float inv_len2 = 1.0f / d2; if (inv_len2 > IM_FIXNORMAL2F_MAX_INVLEN2) inv_len2 = IM_FIXNORMAL2F_MAX_INVLEN2; VX *= inv_len2; VY *= inv_len2; } } (void)0 |
| |
| // TODO: Thickness anti-aliased lines cap are missing their AA fringe. |
| // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. |
| void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, ImDrawFlags flags, float thickness) |
| { |
| if (points_count < 2 || (col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| const bool closed = (flags & ImDrawFlags_Closed) != 0; |
| const ImVec2 opaque_uv = _Data->TexUvWhitePixel; |
| const int count = closed ? points_count : points_count - 1; // The number of line segments we need to draw |
| const bool thick_line = (thickness > _FringeScale); |
| |
| if (Flags & ImDrawListFlags_AntiAliasedLines) |
| { |
| // Anti-aliased stroke |
| const float AA_SIZE = _FringeScale; |
| const ImU32 col_trans = col & ~IM_COL32_A_MASK; |
| |
| // Thicknesses <1.0 should behave like thickness 1.0 |
| thickness = ImMax(thickness, 1.0f); |
| const int integer_thickness = (int)thickness; |
| const float fractional_thickness = thickness - integer_thickness; |
| |
| // Do we want to draw this line using a texture? |
| // - For now, only draw integer-width lines using textures to avoid issues with the way scaling occurs, could be improved. |
| // - If AA_SIZE is not 1.0f we cannot use the texture path. |
| const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f) && (AA_SIZE == 1.0f); |
| |
| // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoBakedLines is off |
| IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines)); |
| |
| const int idx_count = use_texture ? (count * 6) : (thick_line ? count * 18 : count * 12); |
| const int vtx_count = use_texture ? (points_count * 2) : (thick_line ? points_count * 4 : points_count * 3); |
| PrimReserve(idx_count, vtx_count); |
| |
| // Temporary buffer |
| // The first <points_count> items are normals at each line point, then after that there are either 2 or 4 temp points for each line point |
| _Data->TempBuffer.reserve_discard(points_count * ((use_texture || !thick_line) ? 3 : 5)); |
| ImVec2* temp_normals = _Data->TempBuffer.Data; |
| ImVec2* temp_points = temp_normals + points_count; |
| |
| // Calculate normals (tangents) for each line segment |
| for (int i1 = 0; i1 < count; i1++) |
| { |
| const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; |
| float dx = points[i2].x - points[i1].x; |
| float dy = points[i2].y - points[i1].y; |
| IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
| temp_normals[i1].x = dy; |
| temp_normals[i1].y = -dx; |
| } |
| if (!closed) |
| temp_normals[points_count - 1] = temp_normals[points_count - 2]; |
| |
| // If we are drawing a one-pixel-wide line without a texture, or a textured line of any width, we only need 2 or 3 vertices per point |
| if (use_texture || !thick_line) |
| { |
| // [PATH 1] Texture-based lines (thick or non-thick) |
| // [PATH 2] Non texture-based lines (non-thick) |
| |
| // The width of the geometry we need to draw - this is essentially <thickness> pixels for the line itself, plus "one pixel" for AA. |
| // - In the texture-based path, we don't use AA_SIZE here because the +1 is tied to the generated texture |
| // (see ImFontAtlasBuildRenderLinesTexData() function), and so alternate values won't work without changes to that code. |
| // - In the non texture-based paths, we would allow AA_SIZE to potentially be != 1.0f with a patch (e.g. fringe_scale patch to |
| // allow scaling geometry while preserving one-screen-pixel AA fringe). |
| const float half_draw_size = use_texture ? ((thickness * 0.5f) + 1) : AA_SIZE; |
| |
| // If line is not closed, the first and last points need to be generated differently as there are no normals to blend |
| if (!closed) |
| { |
| temp_points[0] = points[0] + temp_normals[0] * half_draw_size; |
| temp_points[1] = points[0] - temp_normals[0] * half_draw_size; |
| temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * half_draw_size; |
| temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * half_draw_size; |
| } |
| |
| // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges |
| // This takes points n and n+1 and writes into n+1, with the first point in a closed line being generated from the final one (as n+1 wraps) |
| // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. |
| unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment |
| for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment |
| { |
| const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; // i2 is the second point of the line segment |
| const unsigned int idx2 = ((i1 + 1) == points_count) ? _VtxCurrentIdx : (idx1 + (use_texture ? 2 : 3)); // Vertex index for end of segment |
| |
| // Average normals |
| float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; |
| float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; |
| IM_FIXNORMAL2F(dm_x, dm_y); |
| dm_x *= half_draw_size; // dm_x, dm_y are offset to the outer edge of the AA area |
| dm_y *= half_draw_size; |
| |
| // Add temporary vertexes for the outer edges |
| ImVec2* out_vtx = &temp_points[i2 * 2]; |
| out_vtx[0].x = points[i2].x + dm_x; |
| out_vtx[0].y = points[i2].y + dm_y; |
| out_vtx[1].x = points[i2].x - dm_x; |
| out_vtx[1].y = points[i2].y - dm_y; |
| |
| if (use_texture) |
| { |
| // Add indices for two triangles |
| _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 1); // Right tri |
| _IdxWritePtr[3] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[4] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Left tri |
| _IdxWritePtr += 6; |
| } |
| else |
| { |
| // Add indexes for four triangles |
| _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); // Right tri 1 |
| _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Right tri 2 |
| _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); // Left tri 1 |
| _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); // Left tri 2 |
| _IdxWritePtr += 12; |
| } |
| |
| idx1 = idx2; |
| } |
| |
| // Add vertexes for each point on the line |
| if (use_texture) |
| { |
| // If we're using textures we only need to emit the left/right edge vertices |
| ImVec4 tex_uvs = _Data->TexUvLines[integer_thickness]; |
| /*if (fractional_thickness != 0.0f) // Currently always zero when use_texture==false! |
| { |
| const ImVec4 tex_uvs_1 = _Data->TexUvLines[integer_thickness + 1]; |
| tex_uvs.x = tex_uvs.x + (tex_uvs_1.x - tex_uvs.x) * fractional_thickness; // inlined ImLerp() |
| tex_uvs.y = tex_uvs.y + (tex_uvs_1.y - tex_uvs.y) * fractional_thickness; |
| tex_uvs.z = tex_uvs.z + (tex_uvs_1.z - tex_uvs.z) * fractional_thickness; |
| tex_uvs.w = tex_uvs.w + (tex_uvs_1.w - tex_uvs.w) * fractional_thickness; |
| }*/ |
| ImVec2 tex_uv0(tex_uvs.x, tex_uvs.y); |
| ImVec2 tex_uv1(tex_uvs.z, tex_uvs.w); |
| for (int i = 0; i < points_count; i++) |
| { |
| _VtxWritePtr[0].pos = temp_points[i * 2 + 0]; _VtxWritePtr[0].uv = tex_uv0; _VtxWritePtr[0].col = col; // Left-side outer edge |
| _VtxWritePtr[1].pos = temp_points[i * 2 + 1]; _VtxWritePtr[1].uv = tex_uv1; _VtxWritePtr[1].col = col; // Right-side outer edge |
| _VtxWritePtr += 2; |
| } |
| } |
| else |
| { |
| // If we're not using a texture, we need the center vertex as well |
| for (int i = 0; i < points_count; i++) |
| { |
| _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; // Center of line |
| _VtxWritePtr[1].pos = temp_points[i * 2 + 0]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col_trans; // Left-side outer edge |
| _VtxWritePtr[2].pos = temp_points[i * 2 + 1]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col_trans; // Right-side outer edge |
| _VtxWritePtr += 3; |
| } |
| } |
| } |
| else |
| { |
| // [PATH 2] Non texture-based lines (thick): we need to draw the solid line core and thus require four vertices per point |
| const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f; |
| |
| // If line is not closed, the first and last points need to be generated differently as there are no normals to blend |
| if (!closed) |
| { |
| const int points_last = points_count - 1; |
| temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE); |
| temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness); |
| temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness); |
| temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE); |
| temp_points[points_last * 4 + 0] = points[points_last] + temp_normals[points_last] * (half_inner_thickness + AA_SIZE); |
| temp_points[points_last * 4 + 1] = points[points_last] + temp_normals[points_last] * (half_inner_thickness); |
| temp_points[points_last * 4 + 2] = points[points_last] - temp_normals[points_last] * (half_inner_thickness); |
| temp_points[points_last * 4 + 3] = points[points_last] - temp_normals[points_last] * (half_inner_thickness + AA_SIZE); |
| } |
| |
| // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges |
| // This takes points n and n+1 and writes into n+1, with the first point in a closed line being generated from the final one (as n+1 wraps) |
| // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. |
| unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment |
| for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment |
| { |
| const int i2 = (i1 + 1) == points_count ? 0 : (i1 + 1); // i2 is the second point of the line segment |
| const unsigned int idx2 = (i1 + 1) == points_count ? _VtxCurrentIdx : (idx1 + 4); // Vertex index for end of segment |
| |
| // Average normals |
| float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; |
| float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; |
| IM_FIXNORMAL2F(dm_x, dm_y); |
| float dm_out_x = dm_x * (half_inner_thickness + AA_SIZE); |
| float dm_out_y = dm_y * (half_inner_thickness + AA_SIZE); |
| float dm_in_x = dm_x * half_inner_thickness; |
| float dm_in_y = dm_y * half_inner_thickness; |
| |
| // Add temporary vertices |
| ImVec2* out_vtx = &temp_points[i2 * 4]; |
| out_vtx[0].x = points[i2].x + dm_out_x; |
| out_vtx[0].y = points[i2].y + dm_out_y; |
| out_vtx[1].x = points[i2].x + dm_in_x; |
| out_vtx[1].y = points[i2].y + dm_in_y; |
| out_vtx[2].x = points[i2].x - dm_in_x; |
| out_vtx[2].y = points[i2].y - dm_in_y; |
| out_vtx[3].x = points[i2].x - dm_out_x; |
| out_vtx[3].y = points[i2].y - dm_out_y; |
| |
| // Add indexes |
| _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); |
| _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 1); |
| _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); |
| _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); |
| _IdxWritePtr[12] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[13] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[14] = (ImDrawIdx)(idx1 + 3); |
| _IdxWritePtr[15] = (ImDrawIdx)(idx1 + 3); _IdxWritePtr[16] = (ImDrawIdx)(idx2 + 3); _IdxWritePtr[17] = (ImDrawIdx)(idx2 + 2); |
| _IdxWritePtr += 18; |
| |
| idx1 = idx2; |
| } |
| |
| // Add vertices |
| for (int i = 0; i < points_count; i++) |
| { |
| _VtxWritePtr[0].pos = temp_points[i * 4 + 0]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col_trans; |
| _VtxWritePtr[1].pos = temp_points[i * 4 + 1]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col; |
| _VtxWritePtr[2].pos = temp_points[i * 4 + 2]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col; |
| _VtxWritePtr[3].pos = temp_points[i * 4 + 3]; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col_trans; |
| _VtxWritePtr += 4; |
| } |
| } |
| _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
| } |
| else |
| { |
| // [PATH 4] Non texture-based, Non anti-aliased lines |
| const int idx_count = count * 6; |
| const int vtx_count = count * 4; // FIXME-OPT: Not sharing edges |
| PrimReserve(idx_count, vtx_count); |
| |
| for (int i1 = 0; i1 < count; i1++) |
| { |
| const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; |
| const ImVec2& p1 = points[i1]; |
| const ImVec2& p2 = points[i2]; |
| |
| float dx = p2.x - p1.x; |
| float dy = p2.y - p1.y; |
| IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
| dx *= (thickness * 0.5f); |
| dy *= (thickness * 0.5f); |
| |
| _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; |
| _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col; |
| _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col; |
| _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col; |
| _VtxWritePtr += 4; |
| |
| _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + 2); |
| _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx + 2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx + 3); |
| _IdxWritePtr += 6; |
| _VtxCurrentIdx += 4; |
| } |
| } |
| } |
| |
| // - We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds. |
| // - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. |
| void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col) |
| { |
| if (points_count < 3 || (col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| const ImVec2 uv = _Data->TexUvWhitePixel; |
| |
| if (Flags & ImDrawListFlags_AntiAliasedFill) |
| { |
| // Anti-aliased Fill |
| const float AA_SIZE = _FringeScale; |
| const ImU32 col_trans = col & ~IM_COL32_A_MASK; |
| const int idx_count = (points_count - 2)*3 + points_count * 6; |
| const int vtx_count = (points_count * 2); |
| PrimReserve(idx_count, vtx_count); |
| |
| // Add indexes for fill |
| unsigned int vtx_inner_idx = _VtxCurrentIdx; |
| unsigned int vtx_outer_idx = _VtxCurrentIdx + 1; |
| for (int i = 2; i < points_count; i++) |
| { |
| _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + ((i - 1) << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx + (i << 1)); |
| _IdxWritePtr += 3; |
| } |
| |
| // Compute normals |
| _Data->TempBuffer.reserve_discard(points_count); |
| ImVec2* temp_normals = _Data->TempBuffer.Data; |
| for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) |
| { |
| const ImVec2& p0 = points[i0]; |
| const ImVec2& p1 = points[i1]; |
| float dx = p1.x - p0.x; |
| float dy = p1.y - p0.y; |
| IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
| temp_normals[i0].x = dy; |
| temp_normals[i0].y = -dx; |
| } |
| |
| for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) |
| { |
| // Average normals |
| const ImVec2& n0 = temp_normals[i0]; |
| const ImVec2& n1 = temp_normals[i1]; |
| float dm_x = (n0.x + n1.x) * 0.5f; |
| float dm_y = (n0.y + n1.y) * 0.5f; |
| IM_FIXNORMAL2F(dm_x, dm_y); |
| dm_x *= AA_SIZE * 0.5f; |
| dm_y *= AA_SIZE * 0.5f; |
| |
| // Add vertices |
| _VtxWritePtr[0].pos.x = (points[i1].x - dm_x); _VtxWritePtr[0].pos.y = (points[i1].y - dm_y); _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; // Inner |
| _VtxWritePtr[1].pos.x = (points[i1].x + dm_x); _VtxWritePtr[1].pos.y = (points[i1].y + dm_y); _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; // Outer |
| _VtxWritePtr += 2; |
| |
| // Add indexes for fringes |
| _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (i0 << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); |
| _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx + (i1 << 1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); |
| _IdxWritePtr += 6; |
| } |
| _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
| } |
| else |
| { |
| // Non Anti-aliased Fill |
| const int idx_count = (points_count - 2)*3; |
| const int vtx_count = points_count; |
| PrimReserve(idx_count, vtx_count); |
| for (int i = 0; i < vtx_count; i++) |
| { |
| _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; |
| _VtxWritePtr++; |
| } |
| for (int i = 2; i < points_count; i++) |
| { |
| _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + i - 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + i); |
| _IdxWritePtr += 3; |
| } |
| _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
| } |
| } |
| |
| void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step) |
| { |
| if (radius < 0.5f) |
| { |
| _Path.push_back(center); |
| return; |
| } |
| |
| // Calculate arc auto segment step size |
| if (a_step <= 0) |
| a_step = IM_DRAWLIST_ARCFAST_SAMPLE_MAX / _CalcCircleAutoSegmentCount(radius); |
| |
| // Make sure we never do steps larger than one quarter of the circle |
| a_step = ImClamp(a_step, 1, IM_DRAWLIST_ARCFAST_TABLE_SIZE / 4); |
| |
| const int sample_range = ImAbs(a_max_sample - a_min_sample); |
| const int a_next_step = a_step; |
| |
| int samples = sample_range + 1; |
| bool extra_max_sample = false; |
| if (a_step > 1) |
| { |
| samples = sample_range / a_step + 1; |
| const int overstep = sample_range % a_step; |
| |
| if (overstep > 0) |
| { |
| extra_max_sample = true; |
| samples++; |
| |
| // When we have overstep to avoid awkwardly looking one long line and one tiny one at the end, |
| // distribute first step range evenly between them by reducing first step size. |
| if (sample_range > 0) |
| a_step -= (a_step - overstep) / 2; |
| } |
| } |
| |
| _Path.resize(_Path.Size + samples); |
| ImVec2* out_ptr = _Path.Data + (_Path.Size - samples); |
| |
| int sample_index = a_min_sample; |
| if (sample_index < 0 || sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) |
| { |
| sample_index = sample_index % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| if (sample_index < 0) |
| sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| } |
| |
| if (a_max_sample >= a_min_sample) |
| { |
| for (int a = a_min_sample; a <= a_max_sample; a += a_step, sample_index += a_step, a_step = a_next_step) |
| { |
| // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more |
| if (sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) |
| sample_index -= IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| |
| const ImVec2 s = _Data->ArcFastVtx[sample_index]; |
| out_ptr->x = center.x + s.x * radius; |
| out_ptr->y = center.y + s.y * radius; |
| out_ptr++; |
| } |
| } |
| else |
| { |
| for (int a = a_min_sample; a >= a_max_sample; a -= a_step, sample_index -= a_step, a_step = a_next_step) |
| { |
| // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more |
| if (sample_index < 0) |
| sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| |
| const ImVec2 s = _Data->ArcFastVtx[sample_index]; |
| out_ptr->x = center.x + s.x * radius; |
| out_ptr->y = center.y + s.y * radius; |
| out_ptr++; |
| } |
| } |
| |
| if (extra_max_sample) |
| { |
| int normalized_max_sample = a_max_sample % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| if (normalized_max_sample < 0) |
| normalized_max_sample += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| |
| const ImVec2 s = _Data->ArcFastVtx[normalized_max_sample]; |
| out_ptr->x = center.x + s.x * radius; |
| out_ptr->y = center.y + s.y * radius; |
| out_ptr++; |
| } |
| |
| IM_ASSERT_PARANOID(_Path.Data + _Path.Size == out_ptr); |
| } |
| |
| void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) |
| { |
| if (radius < 0.5f) |
| { |
| _Path.push_back(center); |
| return; |
| } |
| |
| // Note that we are adding a point at both a_min and a_max. |
| // If you are trying to draw a full closed circle you don't want the overlapping points! |
| _Path.reserve(_Path.Size + (num_segments + 1)); |
| for (int i = 0; i <= num_segments; i++) |
| { |
| const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); |
| _Path.push_back(ImVec2(center.x + ImCos(a) * radius, center.y + ImSin(a) * radius)); |
| } |
| } |
| |
| // 0: East, 3: South, 6: West, 9: North, 12: East |
| void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) |
| { |
| if (radius < 0.5f) |
| { |
| _Path.push_back(center); |
| return; |
| } |
| _PathArcToFastEx(center, radius, a_min_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_max_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, 0); |
| } |
| |
| void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) |
| { |
| if (radius < 0.5f) |
| { |
| _Path.push_back(center); |
| return; |
| } |
| |
| if (num_segments > 0) |
| { |
| _PathArcToN(center, radius, a_min, a_max, num_segments); |
| return; |
| } |
| |
| // Automatic segment count |
| if (radius <= _Data->ArcFastRadiusCutoff) |
| { |
| const bool a_is_reverse = a_max < a_min; |
| |
| // We are going to use precomputed values for mid samples. |
| // Determine first and last sample in lookup table that belong to the arc. |
| const float a_min_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f); |
| const float a_max_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f); |
| |
| const int a_min_sample = a_is_reverse ? (int)ImFloor(a_min_sample_f) : (int)ImCeil(a_min_sample_f); |
| const int a_max_sample = a_is_reverse ? (int)ImCeil(a_max_sample_f) : (int)ImFloor(a_max_sample_f); |
| const int a_mid_samples = a_is_reverse ? ImMax(a_min_sample - a_max_sample, 0) : ImMax(a_max_sample - a_min_sample, 0); |
| |
| const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| const float a_max_segment_angle = a_max_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
| const bool a_emit_start = ImAbs(a_min_segment_angle - a_min) >= 1e-5f; |
| const bool a_emit_end = ImAbs(a_max - a_max_segment_angle) >= 1e-5f; |
| |
| _Path.reserve(_Path.Size + (a_mid_samples + 1 + (a_emit_start ? 1 : 0) + (a_emit_end ? 1 : 0))); |
| if (a_emit_start) |
| _Path.push_back(ImVec2(center.x + ImCos(a_min) * radius, center.y + ImSin(a_min) * radius)); |
| if (a_mid_samples > 0) |
| _PathArcToFastEx(center, radius, a_min_sample, a_max_sample, 0); |
| if (a_emit_end) |
| _Path.push_back(ImVec2(center.x + ImCos(a_max) * radius, center.y + ImSin(a_max) * radius)); |
| } |
| else |
| { |
| const float arc_length = ImAbs(a_max - a_min); |
| const int circle_segment_count = _CalcCircleAutoSegmentCount(radius); |
| const int arc_segment_count = ImMax((int)ImCeil(circle_segment_count * arc_length / (IM_PI * 2.0f)), (int)(2.0f * IM_PI / arc_length)); |
| _PathArcToN(center, radius, a_min, a_max, arc_segment_count); |
| } |
| } |
| |
| void ImDrawList::PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments) |
| { |
| if (num_segments <= 0) |
| num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. |
| |
| _Path.reserve(_Path.Size + (num_segments + 1)); |
| |
| const float cos_rot = ImCos(rot); |
| const float sin_rot = ImSin(rot); |
| for (int i = 0; i <= num_segments; i++) |
| { |
| const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); |
| ImVec2 point(ImCos(a) * radius_x, ImSin(a) * radius_y); |
| const float rel_x = (point.x * cos_rot) - (point.y * sin_rot); |
| const float rel_y = (point.x * sin_rot) + (point.y * cos_rot); |
| point.x = rel_x + center.x; |
| point.y = rel_y + center.y; |
| _Path.push_back(point); |
| } |
| } |
| |
| ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) |
| { |
| float u = 1.0f - t; |
| float w1 = u * u * u; |
| float w2 = 3 * u * u * t; |
| float w3 = 3 * u * t * t; |
| float w4 = t * t * t; |
| return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x, w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y); |
| } |
| |
| ImVec2 ImBezierQuadraticCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, float t) |
| { |
| float u = 1.0f - t; |
| float w1 = u * u; |
| float w2 = 2 * u * t; |
| float w3 = t * t; |
| return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x, w1 * p1.y + w2 * p2.y + w3 * p3.y); |
| } |
| |
| // Closely mimics ImBezierCubicClosestPointCasteljau() in imgui.cpp |
| static void PathBezierCubicCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) |
| { |
| float dx = x4 - x1; |
| float dy = y4 - y1; |
| float d2 = (x2 - x4) * dy - (y2 - y4) * dx; |
| float d3 = (x3 - x4) * dy - (y3 - y4) * dx; |
| d2 = (d2 >= 0) ? d2 : -d2; |
| d3 = (d3 >= 0) ? d3 : -d3; |
| if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) |
| { |
| path->push_back(ImVec2(x4, y4)); |
| } |
| else if (level < 10) |
| { |
| float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; |
| float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; |
| float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f; |
| float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; |
| float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f; |
| float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f; |
| PathBezierCubicCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); |
| PathBezierCubicCurveToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); |
| } |
| } |
| |
| static void PathBezierQuadraticCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float tess_tol, int level) |
| { |
| float dx = x3 - x1, dy = y3 - y1; |
| float det = (x2 - x3) * dy - (y2 - y3) * dx; |
| if (det * det * 4.0f < tess_tol * (dx * dx + dy * dy)) |
| { |
| path->push_back(ImVec2(x3, y3)); |
| } |
| else if (level < 10) |
| { |
| float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; |
| float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; |
| float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; |
| PathBezierQuadraticCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, tess_tol, level + 1); |
| PathBezierQuadraticCurveToCasteljau(path, x123, y123, x23, y23, x3, y3, tess_tol, level + 1); |
| } |
| } |
| |
| void ImDrawList::PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments) |
| { |
| ImVec2 p1 = _Path.back(); |
| if (num_segments == 0) |
| { |
| IM_ASSERT(_Data->CurveTessellationTol > 0.0f); |
| PathBezierCubicCurveToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); // Auto-tessellated |
| } |
| else |
| { |
| float t_step = 1.0f / (float)num_segments; |
| for (int i_step = 1; i_step <= num_segments; i_step++) |
| _Path.push_back(ImBezierCubicCalc(p1, p2, p3, p4, t_step * i_step)); |
| } |
| } |
| |
| void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments) |
| { |
| ImVec2 p1 = _Path.back(); |
| if (num_segments == 0) |
| { |
| IM_ASSERT(_Data->CurveTessellationTol > 0.0f); |
| PathBezierQuadraticCurveToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, _Data->CurveTessellationTol, 0);// Auto-tessellated |
| } |
| else |
| { |
| float t_step = 1.0f / (float)num_segments; |
| for (int i_step = 1; i_step <= num_segments; i_step++) |
| _Path.push_back(ImBezierQuadraticCalc(p1, p2, p3, t_step * i_step)); |
| } |
| } |
| |
| static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) |
| { |
| /* |
| IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); |
| #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| // Obsoleted in 1.82 (from February 2021). This code was stripped/simplified and mostly commented in 1.90 (from September 2023) |
| // - Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) |
| if (flags == ~0) { return ImDrawFlags_RoundCornersAll; } |
| // - Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations). Read details in older version of this code. |
| if (flags >= 0x01 && flags <= 0x0F) { return (flags << 4); } |
| // We cannot support hard coded 0x00 with 'float rounding > 0.0f' --> replace with ImDrawFlags_RoundCornersNone or use 'float rounding = 0.0f' |
| #endif |
| */ |
| // If this assert triggers, please update your code replacing hardcoded values with new ImDrawFlags_RoundCorners* values. |
| // Note that ImDrawFlags_Closed (== 0x01) is an invalid flag for AddRect(), AddRectFilled(), PathRect() etc. anyway. |
| // See details in 1.82 Changelog as well as 2021/03/12 and 2023/09/08 entries in "API BREAKING CHANGES" section. |
| IM_ASSERT((flags & 0x0F) == 0 && "Misuse of legacy hardcoded ImDrawCornerFlags values!"); |
| |
| if ((flags & ImDrawFlags_RoundCornersMask_) == 0) |
| flags |= ImDrawFlags_RoundCornersAll; |
| |
| return flags; |
| } |
| |
| void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawFlags flags) |
| { |
| if (rounding >= 0.5f) |
| { |
| flags = FixRectCornerFlags(flags); |
| rounding = ImMin(rounding, ImFabs(b.x - a.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f); |
| rounding = ImMin(rounding, ImFabs(b.y - a.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f); |
| } |
| if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
| { |
| PathLineTo(a); |
| PathLineTo(ImVec2(b.x, a.y)); |
| PathLineTo(b); |
| PathLineTo(ImVec2(a.x, b.y)); |
| } |
| else |
| { |
| const float rounding_tl = (flags & ImDrawFlags_RoundCornersTopLeft) ? rounding : 0.0f; |
| const float rounding_tr = (flags & ImDrawFlags_RoundCornersTopRight) ? rounding : 0.0f; |
| const float rounding_br = (flags & ImDrawFlags_RoundCornersBottomRight) ? rounding : 0.0f; |
| const float rounding_bl = (flags & ImDrawFlags_RoundCornersBottomLeft) ? rounding : 0.0f; |
| PathArcToFast(ImVec2(a.x + rounding_tl, a.y + rounding_tl), rounding_tl, 6, 9); |
| PathArcToFast(ImVec2(b.x - rounding_tr, a.y + rounding_tr), rounding_tr, 9, 12); |
| PathArcToFast(ImVec2(b.x - rounding_br, b.y - rounding_br), rounding_br, 0, 3); |
| PathArcToFast(ImVec2(a.x + rounding_bl, b.y - rounding_bl), rounding_bl, 3, 6); |
| } |
| } |
| |
| void ImDrawList::AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| PathLineTo(p1 + ImVec2(0.5f, 0.5f)); |
| PathLineTo(p2 + ImVec2(0.5f, 0.5f)); |
| PathStroke(col, 0, thickness); |
| } |
| |
| // Add instructions to draw a rectangle with rounded corners to the draw list |
| // a is the top-left coordinate of the rectangle, b is the bottom-right |
| // flags should contains a mask indicating which corners should be drawn rounded |
| // Returns true if the rectangle was drawn, false for some reason it couldn't |
| // be (in which case the caller should try again with the regular path drawing API) |
| // We are using the textures generated by ImFontAtlasBuildRenderRoundCornersTexData() |
| inline bool AddRoundCornerRect(ImDrawList* draw_list, const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, float thickness, ImDrawFlags flags, bool fill) |
| { |
| if (!(draw_list->Flags & ImDrawListFlags_RoundCornersUseTex)) // Disabled by the draw list flags |
| return false; |
| |
| #if 1 |
| flags = FixRectCornerFlags(flags); |
| rounding = ImMin(rounding, ImFabs(b.x - a.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f); |
| rounding = ImMin(rounding, ImFabs(b.y - a.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f); |
| #endif |
| |
| const ImDrawListSharedData* data = draw_list->_Data; |
| IM_ASSERT_PARANOID(!(data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedRoundCorners)); // No data in font |
| |
| // Filled rectangles have no stroke width |
| const int stroke_width = fill ? 1 : (int)thickness; |
| if (stroke_width <= 0 || (int)stroke_width > ImFontAtlasRoundCornersMaxStrokeWidth) |
| return false; // We can't handle this |
| |
| // If we have a >1 stroke width, we actually need to increase the radius appropriately as well to match how the geometry renderer does things |
| const int rad = (int)rounding + (stroke_width - 1); |
| |
| // We don't support zero radius |
| if (rad <= 0 || rad > ImFontAtlasRoundCornersMaxSize) |
| return false; // We can't handle this |
| |
| // This is a very awkward special case - if two opposing corners are curved *and* the width/height of the rectangle is <= 2x radius, the non-curved corner overlaps with the curved one |
| // Technically this is fixable but it's a major PITA to do so instead we just don't support that (hopefully very rare) case |
| const ImDrawFlags corner_flags = (flags & ImDrawFlags_RoundCornersMask_); |
| if ((((b.x - a.x) <= (rad * 2)) || ((b.y - a.y) <= (rad * 2))) && |
| ((corner_flags == (ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBottomRight)) || (corner_flags == (ImDrawFlags_RoundCornersTopRight | ImDrawFlags_RoundCornersBottomLeft)))) |
| return false; |
| |
| const int square_rad = stroke_width + (stroke_width - 1); // Radius to use for square corners and sides - because increasing stroke width grows the line on both sides, we need to do this slightly odd calculation |
| |
| // Another awkward special case - if rectangle is smaller than the stroke width then we can get bits of one corner poking out from the other at small sizes when we draw a non-filled rect with a mix of rounded and square corners |
| // (technically this test can be refined to check for possible left/right and top/bottom clashes independently, but it's almost certainly not worth the added complexity) |
| if ((((b.x - a.x) <= square_rad) || ((b.y - a.y) <= square_rad)) && (!fill) && |
| (corner_flags != 0) && (corner_flags != ImDrawFlags_RoundCornersAll)) |
| return false; |
| |
| const unsigned int index = (stroke_width - 1) + ((rad - 1) * ImFontAtlasRoundCornersMaxStrokeWidth); |
| ImFontRoundedCornerData& round_corner_data = (*data->TexRoundCornerData)[index]; |
| const unsigned int square_index = (stroke_width - 1) + ((square_rad - 1) * ImFontAtlasRoundCornersMaxStrokeWidth); |
| ImFontRoundedCornerData& square_corner_data = (*data->TexSquareCornerData)[square_index]; |
| |
| if ((round_corner_data.RectId < 0) || (square_corner_data.RectId < 0)) |
| return false; // No data for this configuration |
| |
| IM_ASSERT(data->Font->ContainerAtlas->TexID == draw_list->_TextureIdStack.back()); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. |
| |
| // Calculate UVs for the three points we are interested in from the texture |
| // - corner_uv[0] is the innermost point of the circle (solid for filled circles) |
| // - corner_uv[1] is either straight down or across from it (depending on if we are using the filled or stroked version) |
| // - corner_uv[2] is diagonally across from it |
| // - corner_uv[1] is always solid (either inside the circle or on the line), whilst corner_uv[2] is always blank |
| // This represents a 45 degree "wedge" of circle, which then gets mirrored here to produce a 90 degree curve |
| // See ImFontAtlasBuildRenderRoundCornersTexData() for more details of the texture contents |
| // If use_alternative_uvs is true then this means we are drawing a stroked texture that has been packed into the "filled" |
| // corner of the area on the texture page, so we need to calculate UVs appropriately |
| const ImVec4& round_uvs = fill ? round_corner_data.TexUvFilled : round_corner_data.TexUvStroked; |
| const bool round_use_alternative_uvs = fill | round_corner_data.StrokedUsesAlternateUVs; |
| const ImVec2 round_corner_uv[3] = |
| { |
| ImVec2(round_uvs.x, round_uvs.y), |
| round_use_alternative_uvs ? ImVec2(round_uvs.x, round_uvs.w) : ImVec2(round_uvs.z, round_uvs.y), |
| ImVec2(round_uvs.z, round_uvs.w) |
| }; |
| |
| // Do the same for square corners |
| const ImVec4& square_uvs = fill ? square_corner_data.TexUvFilled : square_corner_data.TexUvStroked; |
| const bool square_use_alternative_uvs = fill | square_corner_data.StrokedUsesAlternateUVs; |
| const ImVec2 square_corner_uv[3] = |
| { |
| ImVec2(square_uvs.x, square_uvs.y), |
| square_use_alternative_uvs ? ImVec2(square_uvs.x, square_uvs.w) : ImVec2(square_uvs.z, square_uvs.y), |
| ImVec2(square_uvs.z, square_uvs.w) |
| }; |
| |
| // In this code A-D represent the four corners of the rectangle, going clockwise from the top-left: |
| // |
| // A---B |
| // | | |
| // D---C |
| |
| const bool ba = (flags & ImDrawFlags_RoundCornersTopLeft) != 0; |
| const bool bb = (flags & ImDrawFlags_RoundCornersTopRight) != 0; |
| const bool bc = (flags & ImDrawFlags_RoundCornersBottomRight) != 0; |
| const bool bd = (flags & ImDrawFlags_RoundCornersBottomLeft) != 0; |
| |
| // Flags indicating which sides should use the rounded texture |
| const bool side_rounded_l = fill && (ba || bd); |
| const bool side_rounded_r = fill && (bb || bc); |
| const bool side_rounded_t = fill && (ba || bb); |
| const bool side_rounded_b = fill && (bd || bc); |
| |
| // UVs to use for each corner and the edges |
| const ImVec2* corner_uv_a = ba ? round_corner_uv : square_corner_uv; |
| const ImVec2* corner_uv_b = bb ? round_corner_uv : square_corner_uv; |
| const ImVec2* corner_uv_c = bc ? round_corner_uv : square_corner_uv; |
| const ImVec2* corner_uv_d = bd ? round_corner_uv : square_corner_uv; |
| const ImVec2* edge_uv_l = side_rounded_l ? round_corner_uv : square_corner_uv; |
| const ImVec2* edge_uv_r = side_rounded_r ? round_corner_uv : square_corner_uv; |
| const ImVec2* edge_uv_t = side_rounded_t ? round_corner_uv : square_corner_uv; |
| const ImVec2* edge_uv_b = side_rounded_b ? round_corner_uv : square_corner_uv; |
| |
| // The base vertices for the rectangle |
| // |
| // C are the corner vertices, I the interior ones, |
| // and M the intermediate points on the edge of each |
| // rounded section, as shown below: |
| // |
| // CA--MAY--------MBY--CB |
| // | | |
| // MAX IA--------IB MBX |
| // | | | | |
| // | | | | |
| // MDX ID--------IC MCX |
| // | | |
| // CD--MDY--------MCY--CC |
| |
| // Adjust size to account for the fact that wider strokes draw "outside the box" |
| const float stroke_width_size_expansion = stroke_width - 1.0f; |
| |
| // The thickness of each edge piece |
| const int left_side_thickness = side_rounded_l ? rad : square_rad; |
| const int right_side_thickness = side_rounded_r ? rad : square_rad; |
| const int top_side_thickness = side_rounded_t ? rad : square_rad; |
| const int bottom_side_thickness = side_rounded_b ? rad : square_rad; |
| |
| // The sizes of the corner pieces |
| const int size_a = ba ? rad : square_rad; |
| const int size_b = bb ? rad : square_rad; |
| const int size_c = bc ? rad : square_rad; |
| const int size_d = bd ? rad : square_rad; |
| |
| const ImVec2 ca(a.x - stroke_width_size_expansion, a.y - stroke_width_size_expansion), cb(b.x + stroke_width_size_expansion, a.y - stroke_width_size_expansion); |
| const ImVec2 may(ca.x + size_a, ca.y), mby(cb.x - size_b, cb.y); |
| const ImVec2 max(ca.x, ca.y + size_a), mbx(cb.x, cb.y + size_b); |
| const ImVec2 ia(ca.x + size_a, ca.y + size_a), ib(cb.x - size_b, cb.y + size_b); |
| |
| const ImVec2 cc(b.x + stroke_width_size_expansion, b.y + stroke_width_size_expansion), cd(a.x - stroke_width_size_expansion, b.y + stroke_width_size_expansion); |
| const ImVec2 mdx(cd.x, cd.y - size_d), mcx(cc.x, cc.y - size_c); |
| const ImVec2 mdy(cd.x + size_d, cd.y), mcy(cc.x - size_c, cc.y); |
| const ImVec2 id(cd.x + size_d, cd.y - size_d), ic(cc.x - size_c, cc.y - size_c); |
| |
| // Positions for edge vertices |
| // |
| // Each letter of the name refers to one of (t)op, (b)ottom, (l)eft or (r)ight |
| // The first letter is the edge, and the second and third are the position on that edge, so for example: |
| // tbr = (t)op edge, (b)ottom (r)ight vertex |
| |
| const ImVec2 ttl = may; |
| const ImVec2 ttr = mby; |
| const ImVec2 tbr(mby.x, mby.y + (fill ? size_b : top_side_thickness)); |
| const ImVec2 tbl(may.x, may.y + (fill ? size_a : top_side_thickness)); |
| |
| const ImVec2 btl(mdy.x, mdy.y - (fill ? size_d : bottom_side_thickness)); |
| const ImVec2 btr(mcy.x, mcy.y - (fill ? size_c : bottom_side_thickness)); |
| const ImVec2 bbr = mcy; |
| const ImVec2 bbl = mdy; |
| |
| const ImVec2 ltl = max; |
| const ImVec2 ltr(max.x + (fill ? size_a : left_side_thickness), max.y); |
| const ImVec2 lbr(mdx.x + (fill ? size_d : left_side_thickness), mdx.y); |
| const ImVec2 lbl = mdx; |
| |
| const ImVec2 rtl(mbx.x - (fill ? size_b : right_side_thickness), mbx.y); |
| const ImVec2 rtr = mbx; |
| const ImVec2 rbr = mcx; |
| const ImVec2 rbl(mcx.x - (fill ? size_c : right_side_thickness), mcx.y); |
| |
| // Reserve enough space for the vertices/indices |
| const int vtcs = fill ? (4 * 9) : (4 * 8); |
| const int idcs = fill ? (6 * 9) : (6 * 8); |
| draw_list->PrimReserve(idcs, vtcs); |
| |
| const ImDrawIdx idx = (ImDrawIdx)draw_list->_VtxCurrentIdx; |
| |
| // Snap a position to the nearest pixel to ensure correct rasterisation |
| #define SNAP_POS(vec) (ImVec2(ImTrunc((vec).x + 0.5f), ImTrunc((vec).y + 0.5f))) |
| |
| // Write a corner vertex to the draw list, with d being the vertex index, |
| // p the position, c is the corner and i the index into the UV list |
| #define VTX_WRITE_CORNER(d, p, c, i) \ |
| draw_list->_VtxWritePtr[d].pos = SNAP_POS(p); \ |
| draw_list->_VtxWritePtr[d].uv = corner_uv_##c[(i)]; \ |
| draw_list->_VtxWritePtr[d].col = col |
| |
| // Write a vertex for one of the edge sections, with d being the vertex index, |
| // p the position, e is the edge (t/l/b/r) and i the index into the UV list |
| #define VTX_WRITE_EDGE(d, p, e, i) \ |
| draw_list->_VtxWritePtr[d].pos = SNAP_POS(p); \ |
| draw_list->_VtxWritePtr[d].uv = edge_uv_##e[(i)]; \ |
| draw_list->_VtxWritePtr[d].col = col |
| |
| // Write a vertex for the center fill, with d being the vertex index and |
| // p the position |
| #define VTX_WRITE_FILL(d, p) \ |
| draw_list->_VtxWritePtr[d].pos = SNAP_POS(p); \ |
| draw_list->_VtxWritePtr[d].uv = round_corner_uv[0]; \ |
| draw_list->_VtxWritePtr[d].col = col |
| |
| int dv = 0; // A count of the number of vertices we've written |
| int di = 0; // The number of indices we have written |
| |
| // Write a triangle using three indices |
| #define IDX_WRITE_TRI(idx0, idx1, idx2) \ |
| draw_list->_IdxWritePtr[di] = (ImDrawIdx)(idx+(idx0)); \ |
| draw_list->_IdxWritePtr[di+1] = (ImDrawIdx)(idx+(idx1)); \ |
| draw_list->_IdxWritePtr[di+2] = (ImDrawIdx)(idx+(idx2)); \ |
| di += 3 |
| |
| // Top-left corner |
| { |
| VTX_WRITE_CORNER(dv + 0, ca, a, 2); |
| VTX_WRITE_CORNER(dv + 1, may, a, 1); |
| VTX_WRITE_CORNER(dv + 2, ia, a, 0); |
| VTX_WRITE_CORNER(dv + 3, max, a, 1); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Top-right corner |
| { |
| VTX_WRITE_CORNER(dv + 0, cb, b, 2); |
| VTX_WRITE_CORNER(dv + 1, mbx, b, 1); |
| VTX_WRITE_CORNER(dv + 2, ib, b, 0); |
| VTX_WRITE_CORNER(dv + 3, mby, b, 1); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Bottom-right corner |
| { |
| VTX_WRITE_CORNER(dv + 0, ic, c, 0); |
| VTX_WRITE_CORNER(dv + 1, mcx, c, 1); |
| VTX_WRITE_CORNER(dv + 2, cc, c, 2); |
| VTX_WRITE_CORNER(dv + 3, mcy, c, 1); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Bottom-left corner |
| { |
| VTX_WRITE_CORNER(dv + 0, cd, d, 2); |
| VTX_WRITE_CORNER(dv + 1, mdx, d, 1); |
| VTX_WRITE_CORNER(dv + 2, id, d, 0); |
| VTX_WRITE_CORNER(dv + 3, mdy, d, 1); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Top edge |
| { |
| VTX_WRITE_EDGE(dv + 0, ttl, t, 1); |
| VTX_WRITE_EDGE(dv + 1, ttr, t, 1); |
| VTX_WRITE_EDGE(dv + 2, tbr, t, 0); |
| VTX_WRITE_EDGE(dv + 3, tbl, t, 0); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Right edge |
| { |
| VTX_WRITE_EDGE(dv + 0, rtl, r, 0); |
| VTX_WRITE_EDGE(dv + 1, rtr, r, 1); |
| VTX_WRITE_EDGE(dv + 2, rbr, r, 1); |
| VTX_WRITE_EDGE(dv + 3, rbl, r, 0); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Bottom edge |
| { |
| VTX_WRITE_EDGE(dv + 0, btl, b, 0); |
| VTX_WRITE_EDGE(dv + 1, btr, b, 0); |
| VTX_WRITE_EDGE(dv + 2, bbr, b, 1); |
| VTX_WRITE_EDGE(dv + 3, bbl, b, 1); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Left edge |
| { |
| VTX_WRITE_EDGE(dv + 0, ltl, l, 1); |
| VTX_WRITE_EDGE(dv + 1, ltr, l, 0); |
| VTX_WRITE_EDGE(dv + 2, lbr, l, 0); |
| VTX_WRITE_EDGE(dv + 3, lbl, l, 1); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| // Fill |
| if (fill) |
| { |
| VTX_WRITE_FILL(dv + 0, ia); |
| VTX_WRITE_FILL(dv + 1, ib); |
| VTX_WRITE_FILL(dv + 2, ic); |
| VTX_WRITE_FILL(dv + 3, id); |
| |
| IDX_WRITE_TRI(dv + 0, dv + 1, dv + 2); |
| IDX_WRITE_TRI(dv + 0, dv + 2, dv + 3); |
| dv += 4; |
| } |
| |
| draw_list->_VtxWritePtr += dv; |
| draw_list->_VtxCurrentIdx += dv; |
| draw_list->_IdxWritePtr += di; |
| |
| IM_ASSERT_PARANOID(di == idcs); |
| IM_ASSERT_PARANOID(dv == vtcs); |
| |
| // Return any unused vertices/indices |
| // (not required ATM as we always generate the right number) |
| //draw_list->PrimUnreserve(idcs - di, vtcs - dv); |
| |
| #undef SNAP_POS |
| #undef IDX_WRITE_TRI |
| #undef VTX_WRITE_CORNER |
| #undef VTX_WRITE_EDGE |
| #undef VTX_WRITE_FILL |
| |
| return true; |
| } |
| |
| // p_min = upper-left, p_max = lower-right |
| // Note we don't render 1 pixels sized rectangles properly. |
| void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| #if 1 |
| flags = FixRectCornerFlags(flags); |
| rounding = ImMin(rounding, ImFabs(p_max.x - p_min.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f); |
| rounding = ImMin(rounding, ImFabs(p_max.y - p_min.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f); |
| #endif |
| |
| // Try to use fast path if we can |
| if (rounding > 0) |
| if (AddRoundCornerRect(this, p_min, p_max, col, rounding, thickness, flags, /* fill */ false)) |
| return; |
| |
| if (Flags & ImDrawListFlags_AntiAliasedLines) |
| PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.50f, 0.50f), rounding, flags); |
| else |
| PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.49f, 0.49f), rounding, flags); // Better looking lower-right corner and rounded non-AA shapes. |
| PathStroke(col, ImDrawFlags_Closed, thickness); |
| } |
| |
| void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| #if 1 |
| flags = FixRectCornerFlags(flags); |
| rounding = ImMin(rounding, ImFabs(p_max.x - p_min.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f); |
| rounding = ImMin(rounding, ImFabs(p_max.y - p_min.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f); |
| #endif |
| |
| if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
| { |
| PrimReserve(6, 4); |
| PrimRect(p_min, p_max, col); |
| } |
| else |
| { |
| // Try fast path first |
| if (AddRoundCornerRect(this, p_min, p_max, col, rounding, 1.0f, flags, /* fill */ true)) |
| return; |
| |
| PathRect(p_min, p_max, rounding, flags); |
| PathFillConvex(col); |
| } |
| } |
| |
| // p_min = upper-left, p_max = lower-right |
| void ImDrawList::AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_max, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left) |
| { |
| if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) & IM_COL32_A_MASK) == 0) |
| return; |
| |
| const ImVec2 uv = _Data->TexUvWhitePixel; |
| PrimReserve(6, 4); |
| PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 2)); |
| PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 3)); |
| PrimWriteVtx(p_min, uv, col_upr_left); |
| PrimWriteVtx(ImVec2(p_max.x, p_min.y), uv, col_upr_right); |
| PrimWriteVtx(p_max, uv, col_bot_right); |
| PrimWriteVtx(ImVec2(p_min.x, p_max.y), uv, col_bot_left); |
| } |
| |
| void ImDrawList::AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| PathLineTo(p1); |
| PathLineTo(p2); |
| PathLineTo(p3); |
| PathLineTo(p4); |
| PathStroke(col, ImDrawFlags_Closed, thickness); |
| } |
| |
| void ImDrawList::AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| PathLineTo(p1); |
| PathLineTo(p2); |
| PathLineTo(p3); |
| PathLineTo(p4); |
| PathFillConvex(col); |
| } |
| |
| void ImDrawList::AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| PathLineTo(p1); |
| PathLineTo(p2); |
| PathLineTo(p3); |
| PathStroke(col, ImDrawFlags_Closed, thickness); |
| } |
| |
| void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| PathLineTo(p1); |
| PathLineTo(p2); |
| PathLineTo(p3); |
| PathFillConvex(col); |
| } |
| |
| // Draw a circle using the rounded corner textures |
| // Returns true if the circle was drawn, or false if for some reason it could not be |
| // (in which case the caller should try the regular circle drawing code) |
| inline bool AddRoundCornerCircle(ImDrawList* draw_list, const ImVec2& center, float radius, float thickness, ImU32 col, bool fill) |
| { |
| if (!(draw_list->Flags & ImDrawListFlags_RoundCornersUseTex)) // Disabled by the draw list flags |
| return false; |
| |
| const ImDrawListSharedData* data = draw_list->_Data; |
| IM_ASSERT(data->Font->ContainerAtlas->TexID == draw_list->_TextureIdStack.back()); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. |
| IM_ASSERT_PARANOID(!(data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedRoundCorners)); // No data in font |
| |
| // Filled rectangles have no stroke width |
| const int stroke_width = fill ? 1 : (int)thickness; |
| |
| if ((stroke_width <= 0) || |
| (stroke_width > ImFontAtlasRoundCornersMaxStrokeWidth)) |
| return false; // We can't handle this |
| |
| // If we have a >1 stroke width, we actually need to increase the radius appropriately as well to match how the geometry renderer does things |
| const int rad = (int)radius + (stroke_width - 1); |
| |
| // We don't support zero radius |
| if ((rad <= 0) || (rad > ImFontAtlasRoundCornersMaxSize)) |
| return false; // We can't handle this |
| |
| const unsigned int index = (stroke_width - 1) + ((rad - 1) * ImFontAtlasRoundCornersMaxStrokeWidth); |
| ImFontRoundedCornerData& round_corner_data = (*data->TexRoundCornerData)[index]; |
| |
| if (round_corner_data.RectId < 0) |
| return false; // No data for this configuration |
| |
| // Calculate UVs for the three points we are interested in from the texture |
| // corner_uv[0] is the innermost point of the circle (solid for filled circles) |
| // corner_uv[1] is either straight down or across from it (depending on if we are using the filled or stroked version) |
| // corner_uv[2] is diagonally across from it |
| // corner_uv[1] is always solid (either inside the circle or on the line), whilst corner_uv[2] is always blank |
| // This represents a 45 degree "wedge" of circle, which then gets mirrored here to produce a 90 degree curve |
| // See ImFontAtlasBuildRenderRoundCornersTexData() for more details of the texture contents |
| // If use_alternative_uvs is true then this means we are drawing a stroked texture that has been packed into the "filled" |
| // corner of the rectangle, so we need to calculate UVs appropriately |
| const ImVec4& uvs = fill ? round_corner_data.TexUvFilled : round_corner_data.TexUvStroked; |
| const bool use_alternative_uvs = fill | round_corner_data.StrokedUsesAlternateUVs; |
| const ImVec2 corner_uv[3] = |
| { |
| ImVec2(uvs.x, uvs.y), |
| use_alternative_uvs ? ImVec2(uvs.x, uvs.w) : ImVec2(uvs.z, uvs.y), |
| ImVec2(uvs.z, uvs.w) |
| }; |
| |
| // Calculate the circle bounds |
| const ImVec2& c = center; |
| ImVec2 tl = ImVec2(c.x - rad, c.y - rad); |
| ImVec2 br = ImVec2(c.x + rad, c.y + rad); |
| |
| // Some useful constants for our calculations |
| const float half_sqrt_two = 0.70710678f; // sqrtf(2.0f) * 0.5f |
| const float width_offset_parametric = round_corner_data.ParametricStrokeWidth; // Stroke width in our parametric coordinate space |
| |
| const int num_verts = fill ? 9 : 16; // Number of vertices we are going to write |
| const int num_indices = fill ? 24 : 48; // Number of indices we are going to write |
| |
| draw_list->PrimReserve(num_indices, num_verts); |
| |
| // Write a vertex |
| // - d is the vertex index to write to |
| // - vert_pos is the vertex position |
| // - uv_coord is the UV coordinate |
| #define VTX_WRITE(d, vert_pos, uv_coord) \ |
| draw_list->_VtxWritePtr[d].pos = vert_pos; \ |
| draw_list->_VtxWritePtr[d].uv = uv_coord; \ |
| draw_list->_VtxWritePtr[d].col = col |
| |
| // Edge vertices working around the circle clockwise from the left |
| VTX_WRITE(0, ImVec2(tl.x, c.y), corner_uv[1]); |
| VTX_WRITE(1, tl, corner_uv[2]); |
| VTX_WRITE(2, ImVec2(c.x, tl.y), corner_uv[1]); |
| VTX_WRITE(3, ImVec2(br.x, tl.y), corner_uv[2]); |
| VTX_WRITE(4, ImVec2(br.x, c.y), corner_uv[1]); |
| VTX_WRITE(5, br, corner_uv[2]); |
| VTX_WRITE(6, ImVec2(c.x, br.y), corner_uv[1]); |
| VTX_WRITE(7, ImVec2(tl.x, br.y), corner_uv[2]); |
| |
| if (fill) |
| { |
| // The center |
| VTX_WRITE(8, c, corner_uv[0]); |
| } |
| else |
| { |
| // Inside vertices on the diagonals of each quadrant |
| const ImVec2 tlbi = ImVec2(ImLerp(c.x, tl.x, half_sqrt_two - width_offset_parametric), ImLerp(c.y, tl.y, half_sqrt_two - width_offset_parametric)); |
| const ImVec2 trbi = ImVec2(ImLerp(c.x, br.x, half_sqrt_two - width_offset_parametric), ImLerp(c.y, tl.y, half_sqrt_two - width_offset_parametric)); |
| const ImVec2 brbi = ImVec2(ImLerp(c.x, br.x, half_sqrt_two - width_offset_parametric), ImLerp(c.y, br.y, half_sqrt_two - width_offset_parametric)); |
| const ImVec2 blbi = ImVec2(ImLerp(c.x, tl.x, half_sqrt_two - width_offset_parametric), ImLerp(c.y, br.y, half_sqrt_two - width_offset_parametric)); |
| |
| // UV for the inside diagonal points |
| ImVec2 uvbi = ImVec2(ImLerp(corner_uv[0].x, corner_uv[2].x, half_sqrt_two - width_offset_parametric), ImLerp(corner_uv[0].y, corner_uv[2].y, half_sqrt_two - width_offset_parametric)); |
| |
| // Left/right/top/bottom interior positions |
| const ImVec2 lbi = ImVec2(ImLerp(tl.x, c.x, width_offset_parametric), c.y); |
| const ImVec2 rbi = ImVec2(ImLerp(br.x, c.x, width_offset_parametric), c.y); |
| const ImVec2 tbi = ImVec2(c.x, ImLerp(tl.y, c.y, width_offset_parametric)); |
| const ImVec2 bbi = ImVec2(c.x, ImLerp(br.y, c.y, width_offset_parametric)); |
| |
| // UV for the interior cardinal points |
| ImVec2 uvi_cardinal = use_alternative_uvs ? |
| ImVec2(corner_uv[0].x, ImLerp(corner_uv[2].y, corner_uv[0].y, width_offset_parametric)) : |
| ImVec2(ImLerp(corner_uv[2].x, corner_uv[0].x, width_offset_parametric), corner_uv[0].y); |
| |
| // Inner vertices, starting from the left |
| VTX_WRITE(8, lbi, uvi_cardinal); |
| VTX_WRITE(9, tlbi, uvbi); |
| VTX_WRITE(10, tbi, uvi_cardinal); |
| VTX_WRITE(11, trbi, uvbi); |
| VTX_WRITE(12, rbi, uvi_cardinal); |
| VTX_WRITE(13, brbi, uvbi); |
| VTX_WRITE(14, bbi, uvi_cardinal); |
| VTX_WRITE(15, blbi, uvbi); |
| } |
| |
| // Write indices for a triangle formed of three indices |
| // d is the array index to write to |
| #define IDX_WRITE_TRI(d, idx0, idx1, idx2) \ |
| draw_list->_IdxWritePtr[d+0] = (ImDrawIdx)(idx+idx0); \ |
| draw_list->_IdxWritePtr[d+1] = (ImDrawIdx)(idx+idx1); \ |
| draw_list->_IdxWritePtr[d+2] = (ImDrawIdx)(idx+idx2) |
| |
| ImDrawIdx idx = (ImDrawIdx)draw_list->_VtxCurrentIdx; |
| |
| if (fill) |
| { |
| // A simple fan of tris from the center |
| IDX_WRITE_TRI(0, 8, 0, 1); |
| IDX_WRITE_TRI(3, 8, 1, 2); |
| IDX_WRITE_TRI(6, 8, 2, 3); |
| IDX_WRITE_TRI(9, 8, 3, 4); |
| IDX_WRITE_TRI(12, 8, 4, 5); |
| IDX_WRITE_TRI(15, 8, 5, 6); |
| IDX_WRITE_TRI(18, 8, 6, 7); |
| IDX_WRITE_TRI(21, 8, 7, 0); |
| } |
| else |
| { |
| // A ring of inner vertices that are tight to the circle, spanning out to the four corners |
| // Top-left quadrant |
| IDX_WRITE_TRI(0, 1, 0, 8); |
| IDX_WRITE_TRI(3, 1, 8, 9); |
| IDX_WRITE_TRI(6, 1, 9, 10); |
| IDX_WRITE_TRI(9, 1, 10, 2); |
| |
| // Top-right quadrant |
| IDX_WRITE_TRI(12, 3, 2, 10); |
| IDX_WRITE_TRI(15, 3, 10, 11); |
| IDX_WRITE_TRI(18, 3, 11, 12); |
| IDX_WRITE_TRI(21, 3, 12, 4); |
| |
| // Bottom-right quadrant |
| IDX_WRITE_TRI(24, 5, 4, 12); |
| IDX_WRITE_TRI(27, 5, 12, 13); |
| IDX_WRITE_TRI(30, 5, 13, 14); |
| IDX_WRITE_TRI(33, 5, 14, 6); |
| |
| // Bottom-left quadrant |
| IDX_WRITE_TRI(36, 7, 6, 14); |
| IDX_WRITE_TRI(39, 7, 14, 15); |
| IDX_WRITE_TRI(42, 7, 15, 8); |
| IDX_WRITE_TRI(45, 7, 8, 0); |
| } |
| |
| draw_list->_VtxWritePtr += num_verts; |
| draw_list->_VtxCurrentIdx += num_verts; |
| draw_list->_IdxWritePtr += num_indices; |
| |
| #undef IDX_WRITE_TRI |
| #undef VTX_WRITE |
| |
| return true; |
| } |
| |
| void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f) |
| return; |
| |
| // First try the fast texture-based renderer, and only if that can't handle this fall back to paths |
| if (AddRoundCornerCircle(this, center, radius, thickness, col, false)) |
| return; |
| |
| // Obtain segment count |
| if (num_segments <= 0) |
| { |
| // Use arc with automatic segment count |
| _PathArcToFastEx(center, radius - 0.5f, 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, 0); |
| _Path.Size--; |
| } |
| else |
| { |
| // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) |
| num_segments = ImClamp(num_segments, 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); |
| |
| // Because we are filling a closed shape we remove 1 from the count of segments/points |
| const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
| PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); |
| } |
| |
| PathStroke(col, ImDrawFlags_Closed, thickness); |
| } |
| |
| void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) |
| { |
| if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f) |
| return; |
| |
| // First try the fast texture-based renderer, and only if that can't handle this fall back to paths |
| if (AddRoundCornerCircle(this, center, radius, 1.0f, col, true)) |
| return; |
| |
| if (num_segments <= 0) |
| { |
| // Use arc with automatic segment count |
| _PathArcToFastEx(center, radius, 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, 0); |
| _Path.Size--; |
| } |
| else |
| { |
| // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) |
| num_segments = ImClamp(num_segments, 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); |
| |
| // Because we are filling a closed shape we remove 1 from the count of segments/points |
| const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
| PathArcTo(center, radius, 0.0f, a_max, num_segments - 1); |
| } |
| |
| PathFillConvex(col); |
| } |
| |
| // Guaranteed to honor 'num_segments' |
| void ImDrawList::AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0 || (num_segments <= 2) || (radius <= 0.0f)) |
| return; |
| |
| // Because we are filling a closed shape we remove 1 from the count of segments/points |
| const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
| PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); |
| PathStroke(col, ImDrawFlags_Closed, thickness); |
| } |
| |
| // Guaranteed to honor 'num_segments' |
| void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) |
| { |
| if ((col & IM_COL32_A_MASK) == 0 || (num_segments <= 2) || (radius <= 0.0f)) |
| return; |
| |
| // Because we are filling a closed shape we remove 1 from the count of segments/points |
| const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
| PathArcTo(center, radius, 0.0f, a_max, num_segments - 1); |
| PathFillConvex(col); |
| } |
| |
| // Ellipse |
| void ImDrawList::AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments, float thickness) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| if (num_segments <= 0) |
| num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. |
| |
| // Because we are filling a closed shape we remove 1 from the count of segments/points |
| const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; |
| PathEllipticalArcTo(center, radius_x, radius_y, rot, 0.0f, a_max, num_segments - 1); |
| PathStroke(col, true, thickness); |
| } |
| |
| void ImDrawList::AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| if (num_segments <= 0) |
| num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. |
| |
| // Because we are filling a closed shape we remove 1 from the count of segments/points |
| const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; |
| PathEllipticalArcTo(center, radius_x, radius_y, rot, 0.0f, a_max, num_segments - 1); |
| PathFillConvex(col); |
| } |
| |
| // Cubic Bezier takes 4 controls points |
| void ImDrawList::AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| PathLineTo(p1); |
| PathBezierCubicCurveTo(p2, p3, p4, num_segments); |
| PathStroke(col, 0, thickness); |
| } |
| |
| // Quadratic Bezier takes 3 controls points |
| void ImDrawList::AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| PathLineTo(p1); |
| PathBezierQuadraticCurveTo(p2, p3, num_segments); |
| PathStroke(col, 0, thickness); |
| } |
| |
| void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| if (text_end == NULL) |
| text_end = text_begin + strlen(text_begin); |
| if (text_begin == text_end) |
| return; |
| |
| // Pull default font/size from the shared ImDrawListSharedData instance |
| if (font == NULL) |
| font = _Data->Font |