| // dear imgui, v1.89.6 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 |
| #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)ImFloorSigned(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 < 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)ImFloorSigned(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)ImFloorSigned(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); |
| } |
| } |
| |
| 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)); |
| } |
| } |
| |
| IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); |
| static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) |
| { |
| #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| // Obsoleted in 1.82 (from February 2021) |
| // Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) |
| // ~0 --> ImDrawFlags_RoundCornersAll or 0 |
| if (flags == ~0) |
| return ImDrawFlags_RoundCornersAll; |
| |
| // Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations) |
| // 0x01 --> ImDrawFlags_RoundCornersTopLeft (VALUE 0x01 OVERLAPS ImDrawFlags_Closed but ImDrawFlags_Closed is never valid in this path!) |
| // 0x02 --> ImDrawFlags_RoundCornersTopRight |
| // 0x03 --> ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersTopRight |
| // 0x04 --> ImDrawFlags_RoundCornersBotLeft |
| // 0x05 --> ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBotLeft |
| // ... |
| // 0x0F --> ImDrawFlags_RoundCornersAll or 0 |
| // (See all values in ImDrawCornerFlags_) |
| 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 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... |
| 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) |
| { |
| 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); |
| } |
| |
| // 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 (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 (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
| { |
| PrimReserve(6, 4); |
| PrimRect(p_min, p_max, col); |
| } |
| else |
| { |
| 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); |
| } |
| |
| 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; |
| |
| 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; |
| |
| 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) |
| 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) |
| 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); |
| } |
| |
| // 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; |
| if (font_size == 0.0f) |
| font_size = _Data->FontSize; |
| |
| IM_ASSERT(font->ContainerAtlas->TexID == _CmdHeader.TextureId); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. |
| |
| ImVec4 clip_rect = _CmdHeader.ClipRect; |
| if (cpu_fine_clip_rect) |
| { |
| clip_rect.x = ImMax(clip_rect.x, cpu_fine_clip_rect->x); |
| clip_rect.y = ImMax(clip_rect.y, cpu_fine_clip_rect->y); |
| clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z); |
| clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w); |
| } |
| font->RenderText(this, font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL); |
| } |
| |
| void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end) |
| { |
| AddText(NULL, 0.0f, pos, col, text_begin, text_end); |
| } |
| |
| void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; |
| if (push_texture_id) |
| PushTextureID(user_texture_id); |
| |
| PrimReserve(6, 4); |
| PrimRectUV(p_min, p_max, uv_min, uv_max, col); |
| |
| if (push_texture_id) |
| PopTextureID(); |
| } |
| |
| void ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1, const ImVec2& uv2, const ImVec2& uv3, const ImVec2& uv4, ImU32 col) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; |
| if (push_texture_id) |
| PushTextureID(user_texture_id); |
| |
| PrimReserve(6, 4); |
| PrimQuadUV(p1, p2, p3, p4, uv1, uv2, uv3, uv4, col); |
| |
| if (push_texture_id) |
| PopTextureID(); |
| } |
| |
| void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags) |
| { |
| if ((col & IM_COL32_A_MASK) == 0) |
| return; |
| |
| flags = FixRectCornerFlags(flags); |
| if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
| { |
| AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col); |
| return; |
| } |
| |
| const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; |
| if (push_texture_id) |
| PushTextureID(user_texture_id); |
| |
| int vert_start_idx = VtxBuffer.Size; |
| PathRect(p_min, p_max, rounding, flags); |
| PathFillConvex(col); |
| int vert_end_idx = VtxBuffer.Size; |
| ImGui::ShadeVertsLinearUV(this, vert_start_idx, vert_end_idx, p_min, p_max, uv_min, uv_max, true); |
| |
| if (push_texture_id) |
| PopTextureID(); |
| } |
| |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] ImDrawListSplitter |
| //----------------------------------------------------------------------------- |
| // FIXME: This may be a little confusing, trying to be a little too low-level/optimal instead of just doing vector swap.. |
| //----------------------------------------------------------------------------- |
| |
| void ImDrawListSplitter::ClearFreeMemory() |
| { |
| for (int i = 0; i < _Channels.Size; i++) |
| { |
| if (i == _Current) |
| memset(&_Channels[i], 0, sizeof(_Channels[i])); // Current channel is a copy of CmdBuffer/IdxBuffer, don't destruct again |
| _Channels[i]._CmdBuffer.clear(); |
| _Channels[i]._IdxBuffer.clear(); |
| } |
| _Current = 0; |
| _Count = 1; |
| _Channels.clear(); |
| } |
| |
| void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) |
| { |
| IM_UNUSED(draw_list); |
| IM_ASSERT(_Current == 0 && _Count <= 1 && "Nested channel splitting is not supported. Please use separate instances of ImDrawListSplitter."); |
| int old_channels_count = _Channels.Size; |
| if (old_channels_count < channels_count) |
| { |
| _Channels.reserve(channels_count); // Avoid over reserving since this is likely to stay stable |
| _Channels.resize(channels_count); |
| } |
| _Count = channels_count; |
| |
| // Channels[] (24/32 bytes each) hold storage that we'll swap with draw_list->_CmdBuffer/_IdxBuffer |
| // The content of Channels[0] at this point doesn't matter. We clear it to make state tidy in a debugger but we don't strictly need to. |
| // When we switch to the next channel, we'll copy draw_list->_CmdBuffer/_IdxBuffer into Channels[0] and then Channels[1] into draw_list->CmdBuffer/_IdxBuffer |
| memset(&_Channels[0], 0, sizeof(ImDrawChannel)); |
| for (int i = 1; i < channels_count; i++) |
| { |
| if (i >= old_channels_count) |
| { |
| IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel(); |
| } |
| else |
| { |
| _Channels[i]._CmdBuffer.resize(0); |
| _Channels[i]._IdxBuffer.resize(0); |
| } |
| } |
| } |
| |
| void ImDrawListSplitter::Merge(ImDrawList* draw_list) |
| { |
| // Note that we never use or rely on _Channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use. |
| if (_Count <= 1) |
| return; |
| |
| SetCurrentChannel(draw_list, 0); |
| draw_list->_PopUnusedDrawCmd(); |
| |
| // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command. |
| int new_cmd_buffer_count = 0; |
| int new_idx_buffer_count = 0; |
| ImDrawCmd* last_cmd = (_Count > 0 && draw_list->CmdBuffer.Size > 0) ? &draw_list->CmdBuffer.back() : NULL; |
| int idx_offset = last_cmd ? last_cmd->IdxOffset + last_cmd->ElemCount : 0; |
| for (int i = 1; i < _Count; i++) |
| { |
| ImDrawChannel& ch = _Channels[i]; |
| if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0 && ch._CmdBuffer.back().UserCallback == NULL) // Equivalent of PopUnusedDrawCmd() |
| ch._CmdBuffer.pop_back(); |
| |
| if (ch._CmdBuffer.Size > 0 && last_cmd != NULL) |
| { |
| // Do not include ImDrawCmd_AreSequentialIdxOffset() in the compare as we rebuild IdxOffset values ourselves. |
| // Manipulating IdxOffset (e.g. by reordering draw commands like done by RenderDimmedBackgroundBehindWindow()) is not supported within a splitter. |
| ImDrawCmd* next_cmd = &ch._CmdBuffer[0]; |
| if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL) |
| { |
| // Merge previous channel last draw command with current channel first draw command if matching. |
| last_cmd->ElemCount += next_cmd->ElemCount; |
| idx_offset += next_cmd->ElemCount; |
| ch._CmdBuffer.erase(ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges. |
| } |
| } |
| if (ch._CmdBuffer.Size > 0) |
| last_cmd = &ch._CmdBuffer.back(); |
| new_cmd_buffer_count += ch._CmdBuffer.Size; |
| new_idx_buffer_count += ch._IdxBuffer.Size; |
| for (int cmd_n = 0; cmd_n < ch._CmdBuffer.Size; cmd_n++) |
| { |
| ch._CmdBuffer.Data[cmd_n].IdxOffset = idx_offset; |
| idx_offset += ch._CmdBuffer.Data[cmd_n].ElemCount; |
| } |
| } |
| draw_list->CmdBuffer.resize(draw_list->CmdBuffer.Size + new_cmd_buffer_count); |
| draw_list->IdxBuffer.resize(draw_list->IdxBuffer.Size + new_idx_buffer_count); |
| |
| // Write commands and indices in order (they are fairly small structures, we don't copy vertices only indices) |
| ImDrawCmd* cmd_write = draw_list->CmdBuffer.Data + draw_list->CmdBuffer.Size - new_cmd_buffer_count; |
| ImDrawIdx* idx_write = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size - new_idx_buffer_count; |
| for (int i = 1; i < _Count; i++) |
| { |
| ImDrawChannel& ch = _Channels[i]; |
| if (int sz = ch._CmdBuffer.Size) { memcpy(cmd_write, ch._CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; } |
| if (int sz = ch._IdxBuffer.Size) { memcpy(idx_write, ch._IdxBuffer.Data, sz * sizeof(ImDrawIdx)); idx_write += sz; } |
| } |
| draw_list->_IdxWritePtr = idx_write; |
| |
| // Ensure there's always a non-callback draw command trailing the command-buffer |
| if (draw_list->CmdBuffer.Size == 0 || draw_list->CmdBuffer.back().UserCallback != NULL) |
| draw_list->AddDrawCmd(); |
| |
| // If current command is used with different settings we need to add a new command |
| ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; |
| if (curr_cmd->ElemCount == 0) |
| ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset |
| else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) |
| draw_list->AddDrawCmd(); |
| |
| _Count = 1; |
| } |
| |
| void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) |
| { |
| IM_ASSERT(idx >= 0 && idx < _Count); |
| if (_Current == idx) |
| return; |
| |
| // Overwrite ImVector (12/16 bytes), four times. This is merely a silly optimization instead of doing .swap() |
| memcpy(&_Channels.Data[_Current]._CmdBuffer, &draw_list->CmdBuffer, sizeof(draw_list->CmdBuffer)); |
| memcpy(&_Channels.Data[_Current]._IdxBuffer, &draw_list->IdxBuffer, sizeof(draw_list->IdxBuffer)); |
| _Current = idx; |
| memcpy(&draw_list->CmdBuffer, &_Channels.Data[idx]._CmdBuffer, sizeof(draw_list->CmdBuffer)); |
| memcpy(&draw_list->IdxBuffer, &_Channels.Data[idx]._IdxBuffer, sizeof(draw_list->IdxBuffer)); |
| draw_list->_IdxWritePtr = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size; |
| |
| // If current command is used with different settings we need to add a new command |
| ImDrawCmd* curr_cmd = (draw_list->CmdBuffer.Size == 0) ? NULL : &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; |
| if (curr_cmd == NULL) |
| draw_list->AddDrawCmd(); |
| else if (curr_cmd->ElemCount == 0) |
| ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset |
| else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) |
| draw_list->AddDrawCmd(); |
| } |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] ImDrawData |
| //----------------------------------------------------------------------------- |
| |
| // For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! |
| void ImDrawData::DeIndexAllBuffers() |
| { |
| ImVector<ImDrawVert> new_vtx_buffer; |
| TotalVtxCount = TotalIdxCount = 0; |
| for (int i = 0; i < CmdListsCount; i++) |
| { |
| ImDrawList* cmd_list = CmdLists[i]; |
| if (cmd_list->IdxBuffer.empty()) |
| continue; |
| new_vtx_buffer.resize(cmd_list->IdxBuffer.Size); |
| for (int j = 0; j < cmd_list->IdxBuffer.Size; j++) |
| new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]]; |
| cmd_list->VtxBuffer.swap(new_vtx_buffer); |
| cmd_list->IdxBuffer.resize(0); |
| TotalVtxCount += cmd_list->VtxBuffer.Size; |
| } |
| } |
| |
| // Helper to scale the ClipRect field of each ImDrawCmd. |
| // Use if your final output buffer is at a different scale than draw_data->DisplaySize, |
| // or if there is a difference between your window resolution and framebuffer resolution. |
| void ImDrawData::ScaleClipRects(const ImVec2& fb_scale) |
| { |
| for (int i = 0; i < CmdListsCount; i++) |
| { |
| ImDrawList* cmd_list = CmdLists[i]; |
| for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) |
| { |
| ImDrawCmd* cmd = &cmd_list->CmdBuffer[cmd_i]; |
| cmd->ClipRect = ImVec4(cmd->ClipRect.x * fb_scale.x, cmd->ClipRect.y * fb_scale.y, cmd->ClipRect.z * fb_scale.x, cmd->ClipRect.w * fb_scale.y); |
| } |
| } |
| } |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] Helpers ShadeVertsXXX functions |
| //----------------------------------------------------------------------------- |
| |
| // Generic linear color gradient, write to RGB fields, leave A untouched. |
| void ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1) |
| { |
| ImVec2 gradient_extent = gradient_p1 - gradient_p0; |
| float gradient_inv_length2 = 1.0f / ImLengthSqr(gradient_extent); |
| ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; |
| ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; |
| const int col0_r = (int)(col0 >> IM_COL32_R_SHIFT) & 0xFF; |
| const int col0_g = (int)(col0 >> IM_COL32_G_SHIFT) & 0xFF; |
| const int col0_b = (int)(col0 >> IM_COL32_B_SHIFT) & 0xFF; |
| const int col_delta_r = ((int)(col1 >> IM_COL32_R_SHIFT) & 0xFF) - col0_r; |
| const int col_delta_g = ((int)(col1 >> IM_COL32_G_SHIFT) & 0xFF) - col0_g; |
| const int col_delta_b = ((int)(col1 >> IM_COL32_B_SHIFT) & 0xFF) - col0_b; |
| for (ImDrawVert* vert = vert_start; vert < vert_end; vert++) |
| { |
| float d = ImDot(vert->pos - gradient_p0, gradient_extent); |
| float t = ImClamp(d * gradient_inv_length2, 0.0f, 1.0f); |
| int r = (int)(col0_r + col_delta_r * t); |
| int g = (int)(col0_g + col_delta_g * t); |
| int b = (int)(col0_b + col_delta_b * t); |
| vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK); |
| } |
| } |
| |
| // Distribute UV over (a, b) rectangle |
| void ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp) |
| { |
| const ImVec2 size = b - a; |
| const ImVec2 uv_size = uv_b - uv_a; |
| const ImVec2 scale = ImVec2( |
| size.x != 0.0f ? (uv_size.x / size.x) : 0.0f, |
| size.y != 0.0f ? (uv_size.y / size.y) : 0.0f); |
| |
| ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; |
| ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; |
| if (clamp) |
| { |
| const ImVec2 min = ImMin(uv_a, uv_b); |
| const ImVec2 max = ImMax(uv_a, uv_b); |
| for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) |
| vertex->uv = ImClamp(uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale), min, max); |
| } |
| else |
| { |
| for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) |
| vertex->uv = uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale); |
| } |
| } |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] ImFontConfig |
| //----------------------------------------------------------------------------- |
| |
| ImFontConfig::ImFontConfig() |
| { |
| memset(this, 0, sizeof(*this)); |
| FontDataOwnedByAtlas = true; |
| OversampleH = 3; // FIXME: 2 may be a better default? |
| OversampleV = 1; |
| GlyphMaxAdvanceX = FLT_MAX; |
| RasterizerMultiply = 1.0f; |
| EllipsisChar = (ImWchar)-1; |
| } |
| |
| //----------------------------------------------------------------------------- |
| // [SECTION] ImFontAtlas |
| //----------------------------------------------------------------------------- |
| |
| // A work of art lies ahead! (. = white layer, X = black layer, others are blank) |
| // The 2x2 white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. |
| // (This is used when io.MouseDrawCursor = true) |
| const int FONT_ATLAS_DEFAULT_TEX_DATA_W = 122; // Actual texture will be 2 times that + 1 spacing. |
| const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; |
| static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = |
| { |
| "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX - XX XX " |
| "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X -X..X X..X" |
| "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X -X...X X...X" |
| "X - X.X - X.....X - X.....X -X...X - X...X- X..X - X...X X...X " |
| "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X - X...X...X " |
| "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX - X.....X " |
| "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX - X...X " |
| "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX - X.X " |
| "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X - X...X " |
| "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X- X.....X " |
| "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X- X...X...X " |
| "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X- X...X X...X " |
| "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X-X...X X...X" |
| "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X-X..X X..X" |
| "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X- XX XX " |
| "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X--------------" |
| "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X - " |
| "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X - " |
| "X.X X..X - -X.......X- X.......X - XX XX - - X..........X - " |
| "XX X..X - - X.....X - X.....X - X.X X.X - - X........X - " |
| " X..X - - X...X - X...X - X..X X..X - - X........X - " |
| " XX - - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX - " |
| "------------- - X - X -X.....................X- ------------------- " |
| " ----------------------------------- X...XXXXXXXXXXXXX...X - " |
| " - X..X X..X - " |
| " - X.X X.X - " |
| " - XX XX - " |
| }; |
| |
| static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] = |
| { |
| // Pos ........ Size ......... Offset ...... |
| { ImVec2( 0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow |
| { ImVec2(13,0), ImVec2( 7,16), ImVec2( 1, 8) }, // ImGuiMouseCursor_TextInput |
| { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_ResizeAll |
| { ImVec2(21,0), ImVec2( 9,23), ImVec2( 4,11) }, // ImGuiMouseCursor_ResizeNS |
| { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 4) }, // ImGuiMouseCursor_ResizeEW |
| { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW |
| { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE |
| { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand |
| { ImVec2(109,0),ImVec2(13,15), ImVec2( 6, 7) }, // ImGuiMouseCursor_NotAllowed |
| }; |
| |
| ImFontAtlas::ImFontAtlas() |
| { |
| memset(this, 0, sizeof(*this)); |
| TexGlyphPadding = 1; |
| PackIdMouseCursors = PackIdLines = -1; |
| } |
| |
| ImFontAtlas::~ImFontAtlas() |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| Clear(); |
| } |
| |
| void ImFontAtlas::ClearInputData() |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| for (int i = 0; i < ConfigData.Size; i++) |
| if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) |
| { |
| IM_FREE(ConfigData[i].FontData); |
| ConfigData[i].FontData = NULL; |
| } |
| |
| // When clearing this we lose access to the font name and other information used to build the font. |
| for (int i = 0; i < Fonts.Size; i++) |
| if (Fonts[i]->ConfigData >= ConfigData.Data && Fonts[i]->ConfigData < ConfigData.Data + ConfigData.Size) |
| { |
| Fonts[i]->ConfigData = NULL; |
| Fonts[i]->ConfigDataCount = 0; |
| } |
| ConfigData.clear(); |
| CustomRects.clear(); |
| PackIdMouseCursors = PackIdLines = -1; |
| // Important: we leave TexReady untouched |
| } |
| |
| void ImFontAtlas::ClearTexData() |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| if (TexPixelsAlpha8) |
| IM_FREE(TexPixelsAlpha8); |
| if (TexPixelsRGBA32) |
| IM_FREE(TexPixelsRGBA32); |
| TexPixelsAlpha8 = NULL; |
| TexPixelsRGBA32 = NULL; |
| TexPixelsUseColors = false; |
| // Important: we leave TexReady untouched |
| } |
| |
| void ImFontAtlas::ClearFonts() |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| Fonts.clear_delete(); |
| TexReady = false; |
| } |
| |
| void ImFontAtlas::Clear() |
| { |
| ClearInputData(); |
| ClearTexData(); |
| ClearFonts(); |
| } |
| |
| void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) |
| { |
| // Build atlas on demand |
| if (TexPixelsAlpha8 == NULL) |
| Build(); |
| |
| *out_pixels = TexPixelsAlpha8; |
| if (out_width) *out_width = TexWidth; |
| if (out_height) *out_height = TexHeight; |
| if (out_bytes_per_pixel) *out_bytes_per_pixel = 1; |
| } |
| |
| void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) |
| { |
| // Convert to RGBA32 format on demand |
| // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp |
| if (!TexPixelsRGBA32) |
| { |
| unsigned char* pixels = NULL; |
| GetTexDataAsAlpha8(&pixels, NULL, NULL); |
| if (pixels) |
| { |
| TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4); |
| const unsigned char* src = pixels; |
| unsigned int* dst = TexPixelsRGBA32; |
| for (int n = TexWidth * TexHeight; n > 0; n--) |
| *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++)); |
| } |
| } |
| |
| *out_pixels = (unsigned char*)TexPixelsRGBA32; |
| if (out_width) *out_width = TexWidth; |
| if (out_height) *out_height = TexHeight; |
| if (out_bytes_per_pixel) *out_bytes_per_pixel = 4; |
| } |
| |
| ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0); |
| IM_ASSERT(font_cfg->SizePixels > 0.0f); |
| |
| // Create new font |
| if (!font_cfg->MergeMode) |
| Fonts.push_back(IM_NEW(ImFont)); |
| else |
| IM_ASSERT(!Fonts.empty() && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font. |
| |
| ConfigData.push_back(*font_cfg); |
| ImFontConfig& new_font_cfg = ConfigData.back(); |
| if (new_font_cfg.DstFont == NULL) |
| new_font_cfg.DstFont = Fonts.back(); |
| if (!new_font_cfg.FontDataOwnedByAtlas) |
| { |
| new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize); |
| new_font_cfg.FontDataOwnedByAtlas = true; |
| memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize); |
| } |
| |
| if (new_font_cfg.DstFont->EllipsisChar == (ImWchar)-1) |
| new_font_cfg.DstFont->EllipsisChar = font_cfg->EllipsisChar; |
| |
| // Invalidate texture |
| TexReady = false; |
| ClearTexData(); |
| return new_font_cfg.DstFont; |
| } |
| |
| // Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder) |
| static unsigned int stb_decompress_length(const unsigned char* input); |
| static unsigned int stb_decompress(unsigned char* output, const unsigned char* input, unsigned int length); |
| static const char* GetDefaultCompressedFontDataTTFBase85(); |
| static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; } |
| static void Decode85(const unsigned char* src, unsigned char* dst) |
| { |
| while (*src) |
| { |
| unsigned int tmp = Decode85Byte(src[0]) + 85 * (Decode85Byte(src[1]) + 85 * (Decode85Byte(src[2]) + 85 * (Decode85Byte(src[3]) + 85 * Decode85Byte(src[4])))); |
| dst[0] = ((tmp >> 0) & 0xFF); dst[1] = ((tmp >> 8) & 0xFF); dst[2] = ((tmp >> 16) & 0xFF); dst[3] = ((tmp >> 24) & 0xFF); // We can't assume little-endianness. |
| src += 5; |
| dst += 4; |
| } |
| } |
| |
| // Load embedded ProggyClean.ttf at size 13, disable oversampling |
| ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template) |
| { |
| ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
| if (!font_cfg_template) |
| { |
| font_cfg.OversampleH = font_cfg.OversampleV = 1; |
| font_cfg.PixelSnapH = true; |
| } |
| if (font_cfg.SizePixels <= 0.0f) |
| font_cfg.SizePixels = 13.0f * 1.0f; |
| if (font_cfg.Name[0] == '\0') |
| ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels); |
| font_cfg.EllipsisChar = (ImWchar)0x0085; |
| font_cfg.GlyphOffset.y = 1.0f * IM_FLOOR(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units |
| |
| const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85(); |
| const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault(); |
| ImFont* font = AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_cfg.SizePixels, &font_cfg, glyph_ranges); |
| return font; |
| } |
| |
| ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| size_t data_size = 0; |
| void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0); |
| if (!data) |
| { |
| IM_ASSERT_USER_ERROR(0, "Could not load font file!"); |
| return NULL; |
| } |
| ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
| if (font_cfg.Name[0] == '\0') |
| { |
| // Store a short copy of filename into into the font name for convenience |
| const char* p; |
| for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {} |
| ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", p, size_pixels); |
| } |
| return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges); |
| } |
| |
| // NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build(). |
| ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) |
| { |
| IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); |
| ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
| IM_ASSERT(font_cfg.FontData == NULL); |
| font_cfg.FontData = ttf_data; |
| font_cfg.FontDataSize = ttf_size; |
| font_cfg.SizePixels = size_pixels > 0.0f ? size_pixels : font_cfg.SizePixels; |
| if (glyph_ranges) |
| font_cfg.GlyphRanges = glyph_ranges; |
| return AddFont(&font_cfg); |
| } |
| |
| ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) |
| { |
| const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data); |
| unsigned char* buf_decompressed_data = (unsigned char*)IM_ALLOC(buf_decompressed_size); |
| stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size); |
| |
| ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
| IM_ASSERT(font_cfg.FontData == NULL); |
| font_cfg.FontDataOwnedByAtlas = true; |
| return AddFontFromMemoryTTF(buf_decompressed_data, (int)buf_decompressed_size, size_pixels, &font_cfg, glyph_ranges); |
| } |
| |
| ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) |
| { |
| int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4; |
| void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size); |
| Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf); |
| ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges); |
| IM_FREE(compressed_ttf); |
| return font; |
| } |
| |
| int ImFontAtlas::AddCustomRectRegular(int width, int height) |
| { |
| IM_ASSERT(width > 0 && width <= 0xFFFF); |
| IM_ASSERT(height > 0 && height <= 0xFFFF); |
| ImFontAtlasCustomRect r; |
| r.Width = (unsigned short)width; |
| r |