1 | // dear imgui, v1.91.6 |
2 | // (drawing and font code) |
3 | |
4 | /* |
5 | |
6 | Index of this file: |
7 | |
8 | // [SECTION] STB libraries implementation |
9 | // [SECTION] Style functions |
10 | // [SECTION] ImDrawList |
11 | // [SECTION] ImTriangulator, ImDrawList concave polygon fill |
12 | // [SECTION] ImDrawListSplitter |
13 | // [SECTION] ImDrawData |
14 | // [SECTION] Helpers ShadeVertsXXX functions |
15 | // [SECTION] ImFontConfig |
16 | // [SECTION] ImFontAtlas |
17 | // [SECTION] ImFontAtlas: glyph ranges helpers |
18 | // [SECTION] ImFontGlyphRangesBuilder |
19 | // [SECTION] ImFont |
20 | // [SECTION] ImGui Internal Render Helpers |
21 | // [SECTION] Decompression code |
22 | // [SECTION] Default font data (ProggyClean.ttf) |
23 | |
24 | */ |
25 | |
26 | #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
27 | #define _CRT_SECURE_NO_WARNINGS |
28 | #endif |
29 | |
30 | #ifndef IMGUI_DEFINE_MATH_OPERATORS |
31 | #define IMGUI_DEFINE_MATH_OPERATORS |
32 | #endif |
33 | |
34 | #include "imgui.h" |
35 | #ifndef IMGUI_DISABLE |
36 | #include "imgui_internal.h" |
37 | #ifdef IMGUI_ENABLE_FREETYPE |
38 | #include "misc/freetype/imgui_freetype.h" |
39 | #endif |
40 | |
41 | #include <stdio.h> // vsnprintf, sscanf, printf |
42 | |
43 | // Visual Studio warnings |
44 | #ifdef _MSC_VER |
45 | #pragma warning (disable: 4127) // condition expression is constant |
46 | #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) |
47 | #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen |
48 | #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). |
49 | #pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer) |
50 | #endif |
51 | |
52 | // Clang/GCC warnings with -Weverything |
53 | #if defined(__clang__) |
54 | #if __has_warning("-Wunknown-warning-option") |
55 | #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! |
56 | #endif |
57 | #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' |
58 | #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. |
59 | #pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok. |
60 | #pragma clang diagnostic ignored "-Wglobal-constructors" // warning: declaration requires a global destructor // similar to above, not sure what the exact difference is. |
61 | #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness |
62 | #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 |
63 | #pragma clang diagnostic ignored "-Wcomma" // warning: possible misuse of comma operator here |
64 | #pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier |
65 | #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. |
66 | #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision |
67 | #pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter |
68 | #pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access |
69 | #pragma clang diagnostic ignored "-Wnontrivial-memaccess" // warning: first argument in call to 'memset' is a pointer to non-trivially copyable type |
70 | #elif defined(__GNUC__) |
71 | #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind |
72 | #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used |
73 | #pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function |
74 | #pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value |
75 | #pragma GCC diagnostic ignored "-Wstack-protector" // warning: stack protector not protecting local variables: variable length buffer |
76 | #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 |
77 | #endif |
78 | |
79 | //------------------------------------------------------------------------- |
80 | // [SECTION] STB libraries implementation (for stb_truetype and stb_rect_pack) |
81 | //------------------------------------------------------------------------- |
82 | |
83 | // Compile time options: |
84 | //#define IMGUI_STB_NAMESPACE ImStb |
85 | //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" |
86 | //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" |
87 | //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION |
88 | //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION |
89 | |
90 | #ifdef IMGUI_STB_NAMESPACE |
91 | namespace IMGUI_STB_NAMESPACE |
92 | { |
93 | #endif |
94 | |
95 | #ifdef _MSC_VER |
96 | #pragma warning (push) |
97 | #pragma warning (disable: 4456) // declaration of 'xx' hides previous local declaration |
98 | #pragma warning (disable: 6011) // (stb_rectpack) Dereferencing NULL pointer 'cur->next'. |
99 | #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. |
100 | #pragma warning (disable: 28182) // (stb_rectpack) Dereferencing NULL pointer. 'cur' contains the same NULL value as 'cur->next' did. |
101 | #endif |
102 | |
103 | #if defined(__clang__) |
104 | #pragma clang diagnostic push |
105 | #pragma clang diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used |
106 | #pragma clang diagnostic ignored "-Wmissing-prototypes" |
107 | #pragma clang diagnostic ignored "-Wimplicit-fallthrough" |
108 | #pragma clang diagnostic ignored "-Wcast-qual" // warning: cast from 'const xxxx *' to 'xxx *' drops const qualifier |
109 | #endif |
110 | |
111 | #if defined(__GNUC__) |
112 | #pragma GCC diagnostic push |
113 | #pragma GCC diagnostic ignored "-Wtype-limits" // warning: comparison is always true due to limited range of data type [-Wtype-limits] |
114 | #pragma GCC diagnostic ignored "-Wcast-qual" // warning: cast from type 'const xxxx *' to type 'xxxx *' casts away qualifiers |
115 | #endif |
116 | |
117 | #ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) |
118 | #ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in another compilation unit |
119 | #define STBRP_STATIC |
120 | #define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0) |
121 | #define STBRP_SORT ImQsort |
122 | #define STB_RECT_PACK_IMPLEMENTATION |
123 | #endif |
124 | #ifdef IMGUI_STB_RECT_PACK_FILENAME |
125 | #include IMGUI_STB_RECT_PACK_FILENAME |
126 | #else |
127 | #include "imstb_rectpack.h" |
128 | #endif |
129 | #endif |
130 | |
131 | #ifdef IMGUI_ENABLE_STB_TRUETYPE |
132 | #ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) |
133 | #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in another compilation unit |
134 | #define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) |
135 | #define STBTT_free(x,u) ((void)(u), IM_FREE(x)) |
136 | #define STBTT_assert(x) do { IM_ASSERT(x); } while(0) |
137 | #define STBTT_fmod(x,y) ImFmod(x,y) |
138 | #define STBTT_sqrt(x) ImSqrt(x) |
139 | #define STBTT_pow(x,y) ImPow(x,y) |
140 | #define STBTT_fabs(x) ImFabs(x) |
141 | #define STBTT_ifloor(x) ((int)ImFloor(x)) |
142 | #define STBTT_iceil(x) ((int)ImCeil(x)) |
143 | #define STBTT_STATIC |
144 | #define STB_TRUETYPE_IMPLEMENTATION |
145 | #else |
146 | #define STBTT_DEF extern |
147 | #endif |
148 | #ifdef IMGUI_STB_TRUETYPE_FILENAME |
149 | #include IMGUI_STB_TRUETYPE_FILENAME |
150 | #else |
151 | #include "imstb_truetype.h" |
152 | #endif |
153 | #endif |
154 | #endif // IMGUI_ENABLE_STB_TRUETYPE |
155 | |
156 | #if defined(__GNUC__) |
157 | #pragma GCC diagnostic pop |
158 | #endif |
159 | |
160 | #if defined(__clang__) |
161 | #pragma clang diagnostic pop |
162 | #endif |
163 | |
164 | #if defined(_MSC_VER) |
165 | #pragma warning (pop) |
166 | #endif |
167 | |
168 | #ifdef IMGUI_STB_NAMESPACE |
169 | } // namespace ImStb |
170 | using namespace IMGUI_STB_NAMESPACE; |
171 | #endif |
172 | |
173 | //----------------------------------------------------------------------------- |
174 | // [SECTION] Style functions |
175 | //----------------------------------------------------------------------------- |
176 | |
177 | void ImGui::StyleColorsDark(ImGuiStyle* dst) |
178 | { |
179 | ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); |
180 | ImVec4* colors = style->Colors; |
181 | |
182 | colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
183 | colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); |
184 | colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); |
185 | colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
186 | colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); |
187 | colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); |
188 | colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
189 | colors[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.29f, 0.48f, 0.54f); |
190 | colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
191 | colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
192 | colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); |
193 | colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f); |
194 | colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); |
195 | colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); |
196 | colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); |
197 | colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); |
198 | colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); |
199 | colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); |
200 | colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
201 | colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f); |
202 | colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
203 | colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
204 | colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
205 | colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); |
206 | colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); |
207 | colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); |
208 | colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
209 | colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; |
210 | colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f); |
211 | colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f); |
212 | colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.20f); |
213 | colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
214 | colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); |
215 | colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; |
216 | colors[ImGuiCol_Tab] = ImLerp(a: colors[ImGuiCol_Header], b: colors[ImGuiCol_TitleBgActive], t: 0.80f); |
217 | colors[ImGuiCol_TabSelected] = ImLerp(a: colors[ImGuiCol_HeaderActive], b: colors[ImGuiCol_TitleBgActive], t: 0.60f); |
218 | colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; |
219 | colors[ImGuiCol_TabDimmed] = ImLerp(a: colors[ImGuiCol_Tab], b: colors[ImGuiCol_TitleBg], t: 0.80f); |
220 | colors[ImGuiCol_TabDimmedSelected] = ImLerp(a: colors[ImGuiCol_TabSelected], b: colors[ImGuiCol_TitleBg], t: 0.40f); |
221 | colors[ImGuiCol_TabDimmedSelectedOverline] = ImVec4(0.50f, 0.50f, 0.50f, 0.00f); |
222 | colors[ImGuiCol_DockingPreview] = colors[ImGuiCol_HeaderActive] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f); |
223 | colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); |
224 | colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); |
225 | colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); |
226 | colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
227 | colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); |
228 | colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); |
229 | colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); // Prefer using Alpha=1.0 here |
230 | colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); // Prefer using Alpha=1.0 here |
231 | colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
232 | colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); |
233 | colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; |
234 | colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); |
235 | colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); |
236 | colors[ImGuiCol_NavCursor] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
237 | colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); |
238 | colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); |
239 | colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); |
240 | } |
241 | |
242 | void ImGui::StyleColorsClassic(ImGuiStyle* dst) |
243 | { |
244 | ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); |
245 | ImVec4* colors = style->Colors; |
246 | |
247 | colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); |
248 | colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); |
249 | colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.85f); |
250 | colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
251 | colors[ImGuiCol_PopupBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f); |
252 | colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f); |
253 | colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
254 | colors[ImGuiCol_FrameBg] = ImVec4(0.43f, 0.43f, 0.43f, 0.39f); |
255 | colors[ImGuiCol_FrameBgHovered] = ImVec4(0.47f, 0.47f, 0.69f, 0.40f); |
256 | colors[ImGuiCol_FrameBgActive] = ImVec4(0.42f, 0.41f, 0.64f, 0.69f); |
257 | colors[ImGuiCol_TitleBg] = ImVec4(0.27f, 0.27f, 0.54f, 0.83f); |
258 | colors[ImGuiCol_TitleBgActive] = ImVec4(0.32f, 0.32f, 0.63f, 0.87f); |
259 | colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f); |
260 | colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.80f); |
261 | colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.60f); |
262 | colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f); |
263 | colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f); |
264 | colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); |
265 | colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f); |
266 | colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); |
267 | colors[ImGuiCol_SliderGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); |
268 | colors[ImGuiCol_Button] = ImVec4(0.35f, 0.40f, 0.61f, 0.62f); |
269 | colors[ImGuiCol_ButtonHovered] = ImVec4(0.40f, 0.48f, 0.71f, 0.79f); |
270 | colors[ImGuiCol_ButtonActive] = ImVec4(0.46f, 0.54f, 0.80f, 1.00f); |
271 | colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f); |
272 | colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f); |
273 | colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.53f, 0.87f, 0.80f); |
274 | colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f); |
275 | colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f); |
276 | colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 1.00f); |
277 | colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f); |
278 | colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f); |
279 | colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f); |
280 | colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; |
281 | colors[ImGuiCol_Tab] = ImLerp(a: colors[ImGuiCol_Header], b: colors[ImGuiCol_TitleBgActive], t: 0.80f); |
282 | colors[ImGuiCol_TabSelected] = ImLerp(a: colors[ImGuiCol_HeaderActive], b: colors[ImGuiCol_TitleBgActive], t: 0.60f); |
283 | colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; |
284 | colors[ImGuiCol_TabDimmed] = ImLerp(a: colors[ImGuiCol_Tab], b: colors[ImGuiCol_TitleBg], t: 0.80f); |
285 | colors[ImGuiCol_TabDimmedSelected] = ImLerp(a: colors[ImGuiCol_TabSelected], b: colors[ImGuiCol_TitleBg], t: 0.40f); |
286 | colors[ImGuiCol_TabDimmedSelectedOverline] = ImVec4(0.53f, 0.53f, 0.87f, 0.00f); |
287 | colors[ImGuiCol_DockingPreview] = colors[ImGuiCol_Header] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f); |
288 | colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); |
289 | colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
290 | colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
291 | colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
292 | colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); |
293 | colors[ImGuiCol_TableHeaderBg] = ImVec4(0.27f, 0.27f, 0.38f, 1.00f); |
294 | colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.45f, 1.00f); // Prefer using Alpha=1.0 here |
295 | colors[ImGuiCol_TableBorderLight] = ImVec4(0.26f, 0.26f, 0.28f, 1.00f); // Prefer using Alpha=1.0 here |
296 | colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
297 | colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f); |
298 | colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; |
299 | colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); |
300 | colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); |
301 | colors[ImGuiCol_NavCursor] = colors[ImGuiCol_HeaderHovered]; |
302 | colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); |
303 | colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); |
304 | colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); |
305 | } |
306 | |
307 | // Those light colors are better suited with a thicker font than the default one + FrameBorder |
308 | void ImGui::StyleColorsLight(ImGuiStyle* dst) |
309 | { |
310 | ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); |
311 | ImVec4* colors = style->Colors; |
312 | |
313 | colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); |
314 | colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); |
315 | colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); |
316 | colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
317 | colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.98f); |
318 | colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.30f); |
319 | colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
320 | colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); |
321 | colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
322 | colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
323 | colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f); |
324 | colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f); |
325 | colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f); |
326 | colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); |
327 | colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f); |
328 | colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f); |
329 | colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f); |
330 | colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f); |
331 | colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
332 | colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); |
333 | colors[ImGuiCol_SliderGrabActive] = ImVec4(0.46f, 0.54f, 0.80f, 0.60f); |
334 | colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); |
335 | colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
336 | colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); |
337 | colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); |
338 | colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); |
339 | colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); |
340 | colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 0.62f); |
341 | colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f); |
342 | colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f); |
343 | colors[ImGuiCol_ResizeGrip] = ImVec4(0.35f, 0.35f, 0.35f, 0.17f); |
344 | colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); |
345 | colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); |
346 | colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; |
347 | colors[ImGuiCol_Tab] = ImLerp(a: colors[ImGuiCol_Header], b: colors[ImGuiCol_TitleBgActive], t: 0.90f); |
348 | colors[ImGuiCol_TabSelected] = ImLerp(a: colors[ImGuiCol_HeaderActive], b: colors[ImGuiCol_TitleBgActive], t: 0.60f); |
349 | colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; |
350 | colors[ImGuiCol_TabDimmed] = ImLerp(a: colors[ImGuiCol_Tab], b: colors[ImGuiCol_TitleBg], t: 0.80f); |
351 | colors[ImGuiCol_TabDimmedSelected] = ImLerp(a: colors[ImGuiCol_TabSelected], b: colors[ImGuiCol_TitleBg], t: 0.40f); |
352 | colors[ImGuiCol_TabDimmedSelectedOverline] = ImVec4(0.26f, 0.59f, 1.00f, 0.00f); |
353 | colors[ImGuiCol_DockingPreview] = colors[ImGuiCol_Header] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f); |
354 | colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); |
355 | colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); |
356 | colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); |
357 | colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); |
358 | colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.45f, 0.00f, 1.00f); |
359 | colors[ImGuiCol_TableHeaderBg] = ImVec4(0.78f, 0.87f, 0.98f, 1.00f); |
360 | colors[ImGuiCol_TableBorderStrong] = ImVec4(0.57f, 0.57f, 0.64f, 1.00f); // Prefer using Alpha=1.0 here |
361 | colors[ImGuiCol_TableBorderLight] = ImVec4(0.68f, 0.68f, 0.74f, 1.00f); // Prefer using Alpha=1.0 here |
362 | colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); |
363 | colors[ImGuiCol_TableRowBgAlt] = ImVec4(0.30f, 0.30f, 0.30f, 0.09f); |
364 | colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; |
365 | colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); |
366 | colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); |
367 | colors[ImGuiCol_NavCursor] = colors[ImGuiCol_HeaderHovered]; |
368 | colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f); |
369 | colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.20f); |
370 | colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); |
371 | } |
372 | |
373 | //----------------------------------------------------------------------------- |
374 | // [SECTION] ImDrawList |
375 | //----------------------------------------------------------------------------- |
376 | |
377 | ImDrawListSharedData::ImDrawListSharedData() |
378 | { |
379 | memset(s: this, c: 0, n: sizeof(*this)); |
380 | for (int i = 0; i < IM_ARRAYSIZE(ArcFastVtx); i++) |
381 | { |
382 | const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(ArcFastVtx); |
383 | ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a)); |
384 | } |
385 | ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); |
386 | } |
387 | |
388 | void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) |
389 | { |
390 | if (CircleSegmentMaxError == max_error) |
391 | return; |
392 | |
393 | IM_ASSERT(max_error > 0.0f); |
394 | CircleSegmentMaxError = max_error; |
395 | for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++) |
396 | { |
397 | const float radius = (float)i; |
398 | CircleSegmentCounts[i] = (ImU8)((i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : IM_DRAWLIST_ARCFAST_SAMPLE_MAX); |
399 | } |
400 | ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); |
401 | } |
402 | |
403 | ImDrawList::ImDrawList(ImDrawListSharedData* shared_data) |
404 | { |
405 | memset(s: this, c: 0, n: sizeof(*this)); |
406 | _Data = shared_data; |
407 | } |
408 | |
409 | ImDrawList::~ImDrawList() |
410 | { |
411 | _ClearFreeMemory(); |
412 | } |
413 | |
414 | // Initialize before use in a new frame. We always have a command ready in the buffer. |
415 | // In the majority of cases, you would want to call PushClipRect() and PushTextureID() after this. |
416 | void ImDrawList::_ResetForNewFrame() |
417 | { |
418 | // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory. |
419 | IM_STATIC_ASSERT(offsetof(ImDrawCmd, ClipRect) == 0); |
420 | IM_STATIC_ASSERT(offsetof(ImDrawCmd, TextureId) == sizeof(ImVec4)); |
421 | IM_STATIC_ASSERT(offsetof(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); |
422 | if (_Splitter._Count > 1) |
423 | _Splitter.Merge(draw_list: this); |
424 | |
425 | CmdBuffer.resize(new_size: 0); |
426 | IdxBuffer.resize(new_size: 0); |
427 | VtxBuffer.resize(new_size: 0); |
428 | Flags = _Data->InitialFlags; |
429 | memset(s: &_CmdHeader, c: 0, n: sizeof(_CmdHeader)); |
430 | _VtxCurrentIdx = 0; |
431 | _VtxWritePtr = NULL; |
432 | _IdxWritePtr = NULL; |
433 | _ClipRectStack.resize(new_size: 0); |
434 | _TextureIdStack.resize(new_size: 0); |
435 | _CallbacksDataBuf.resize(new_size: 0); |
436 | _Path.resize(new_size: 0); |
437 | _Splitter.Clear(); |
438 | CmdBuffer.push_back(v: ImDrawCmd()); |
439 | _FringeScale = 1.0f; |
440 | } |
441 | |
442 | void ImDrawList::_ClearFreeMemory() |
443 | { |
444 | CmdBuffer.clear(); |
445 | IdxBuffer.clear(); |
446 | VtxBuffer.clear(); |
447 | Flags = ImDrawListFlags_None; |
448 | _VtxCurrentIdx = 0; |
449 | _VtxWritePtr = NULL; |
450 | _IdxWritePtr = NULL; |
451 | _ClipRectStack.clear(); |
452 | _TextureIdStack.clear(); |
453 | _CallbacksDataBuf.clear(); |
454 | _Path.clear(); |
455 | _Splitter.ClearFreeMemory(); |
456 | } |
457 | |
458 | ImDrawList* ImDrawList::CloneOutput() const |
459 | { |
460 | ImDrawList* dst = IM_NEW(ImDrawList(_Data)); |
461 | dst->CmdBuffer = CmdBuffer; |
462 | dst->IdxBuffer = IdxBuffer; |
463 | dst->VtxBuffer = VtxBuffer; |
464 | dst->Flags = Flags; |
465 | return dst; |
466 | } |
467 | |
468 | void ImDrawList::AddDrawCmd() |
469 | { |
470 | ImDrawCmd draw_cmd; |
471 | draw_cmd.ClipRect = _CmdHeader.ClipRect; // Same as calling ImDrawCmd_HeaderCopy() |
472 | draw_cmd.TextureId = _CmdHeader.TextureId; |
473 | draw_cmd.VtxOffset = _CmdHeader.VtxOffset; |
474 | draw_cmd.IdxOffset = IdxBuffer.Size; |
475 | |
476 | IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w); |
477 | CmdBuffer.push_back(v: draw_cmd); |
478 | } |
479 | |
480 | // Pop trailing draw command (used before merging or presenting to user) |
481 | // 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 |
482 | void ImDrawList::_PopUnusedDrawCmd() |
483 | { |
484 | while (CmdBuffer.Size > 0) |
485 | { |
486 | ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
487 | if (curr_cmd->ElemCount != 0 || curr_cmd->UserCallback != NULL) |
488 | return;// break; |
489 | CmdBuffer.pop_back(); |
490 | } |
491 | } |
492 | |
493 | void ImDrawList::AddCallback(ImDrawCallback callback, void* userdata, size_t userdata_size) |
494 | { |
495 | IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
496 | ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
497 | IM_ASSERT(curr_cmd->UserCallback == NULL); |
498 | if (curr_cmd->ElemCount != 0) |
499 | { |
500 | AddDrawCmd(); |
501 | curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
502 | } |
503 | |
504 | curr_cmd->UserCallback = callback; |
505 | if (userdata_size == 0) |
506 | { |
507 | // Store user data directly in command (no indirection) |
508 | curr_cmd->UserCallbackData = userdata; |
509 | curr_cmd->UserCallbackDataSize = 0; |
510 | curr_cmd->UserCallbackDataOffset = -1; |
511 | } |
512 | else |
513 | { |
514 | // Copy and store user data in a buffer |
515 | IM_ASSERT(userdata != NULL); |
516 | IM_ASSERT(userdata_size < (1u << 31)); |
517 | curr_cmd->UserCallbackData = NULL; // Will be resolved during Render() |
518 | curr_cmd->UserCallbackDataSize = (int)userdata_size; |
519 | curr_cmd->UserCallbackDataOffset = _CallbacksDataBuf.Size; |
520 | _CallbacksDataBuf.resize(new_size: _CallbacksDataBuf.Size + (int)userdata_size); |
521 | memcpy(dest: _CallbacksDataBuf.Data + (size_t)curr_cmd->UserCallbackDataOffset, src: userdata, n: userdata_size); |
522 | } |
523 | |
524 | AddDrawCmd(); // Force a new command after us (see comment below) |
525 | } |
526 | |
527 | // Compare ClipRect, TextureId and VtxOffset with a single memcmp() |
528 | #define (offsetof(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) |
529 | #define (CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset |
530 | #define (CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset |
531 | #define ImDrawCmd_AreSequentialIdxOffset(CMD_0, CMD_1) (CMD_0->IdxOffset + CMD_0->ElemCount == CMD_1->IdxOffset) |
532 | |
533 | // Try to merge two last draw commands |
534 | void ImDrawList::_TryMergeDrawCmds() |
535 | { |
536 | IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
537 | ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
538 | ImDrawCmd* prev_cmd = curr_cmd - 1; |
539 | if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL) |
540 | { |
541 | prev_cmd->ElemCount += curr_cmd->ElemCount; |
542 | CmdBuffer.pop_back(); |
543 | } |
544 | } |
545 | |
546 | // 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. |
547 | // 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. |
548 | void ImDrawList::_OnChangedClipRect() |
549 | { |
550 | // If current command is used with different settings we need to add a new command |
551 | IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
552 | ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
553 | if (curr_cmd->ElemCount != 0 && memcmp(s1: &curr_cmd->ClipRect, s2: &_CmdHeader.ClipRect, n: sizeof(ImVec4)) != 0) |
554 | { |
555 | AddDrawCmd(); |
556 | return; |
557 | } |
558 | IM_ASSERT(curr_cmd->UserCallback == NULL); |
559 | |
560 | // Try to merge with previous command if it matches, else use current command |
561 | ImDrawCmd* prev_cmd = curr_cmd - 1; |
562 | if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL) |
563 | { |
564 | CmdBuffer.pop_back(); |
565 | return; |
566 | } |
567 | curr_cmd->ClipRect = _CmdHeader.ClipRect; |
568 | } |
569 | |
570 | void ImDrawList::_OnChangedTextureID() |
571 | { |
572 | // If current command is used with different settings we need to add a new command |
573 | IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
574 | ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
575 | if (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != _CmdHeader.TextureId) |
576 | { |
577 | AddDrawCmd(); |
578 | return; |
579 | } |
580 | IM_ASSERT(curr_cmd->UserCallback == NULL); |
581 | |
582 | // Try to merge with previous command if it matches, else use current command |
583 | ImDrawCmd* prev_cmd = curr_cmd - 1; |
584 | if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL) |
585 | { |
586 | CmdBuffer.pop_back(); |
587 | return; |
588 | } |
589 | curr_cmd->TextureId = _CmdHeader.TextureId; |
590 | } |
591 | |
592 | void ImDrawList::_OnChangedVtxOffset() |
593 | { |
594 | // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this. |
595 | _VtxCurrentIdx = 0; |
596 | IM_ASSERT_PARANOID(CmdBuffer.Size > 0); |
597 | ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
598 | //IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); // See #3349 |
599 | if (curr_cmd->ElemCount != 0) |
600 | { |
601 | AddDrawCmd(); |
602 | return; |
603 | } |
604 | IM_ASSERT(curr_cmd->UserCallback == NULL); |
605 | curr_cmd->VtxOffset = _CmdHeader.VtxOffset; |
606 | } |
607 | |
608 | int ImDrawList::_CalcCircleAutoSegmentCount(float radius) const |
609 | { |
610 | // Automatic segment count |
611 | const int radius_idx = (int)(radius + 0.999999f); // ceil to never reduce accuracy |
612 | if (radius_idx >= 0 && radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) |
613 | return _Data->CircleSegmentCounts[radius_idx]; // Use cached value |
614 | else |
615 | return IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); |
616 | } |
617 | |
618 | // 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) |
619 | void ImDrawList::PushClipRect(const ImVec2& cr_min, const ImVec2& cr_max, bool intersect_with_current_clip_rect) |
620 | { |
621 | ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y); |
622 | if (intersect_with_current_clip_rect) |
623 | { |
624 | ImVec4 current = _CmdHeader.ClipRect; |
625 | if (cr.x < current.x) cr.x = current.x; |
626 | if (cr.y < current.y) cr.y = current.y; |
627 | if (cr.z > current.z) cr.z = current.z; |
628 | if (cr.w > current.w) cr.w = current.w; |
629 | } |
630 | cr.z = ImMax(lhs: cr.x, rhs: cr.z); |
631 | cr.w = ImMax(lhs: cr.y, rhs: cr.w); |
632 | |
633 | _ClipRectStack.push_back(v: cr); |
634 | _CmdHeader.ClipRect = cr; |
635 | _OnChangedClipRect(); |
636 | } |
637 | |
638 | void ImDrawList::PushClipRectFullScreen() |
639 | { |
640 | PushClipRect(cr_min: ImVec2(_Data->ClipRectFullscreen.x, _Data->ClipRectFullscreen.y), cr_max: ImVec2(_Data->ClipRectFullscreen.z, _Data->ClipRectFullscreen.w)); |
641 | } |
642 | |
643 | void ImDrawList::PopClipRect() |
644 | { |
645 | _ClipRectStack.pop_back(); |
646 | _CmdHeader.ClipRect = (_ClipRectStack.Size == 0) ? _Data->ClipRectFullscreen : _ClipRectStack.Data[_ClipRectStack.Size - 1]; |
647 | _OnChangedClipRect(); |
648 | } |
649 | |
650 | void ImDrawList::PushTextureID(ImTextureID texture_id) |
651 | { |
652 | _TextureIdStack.push_back(v: texture_id); |
653 | _CmdHeader.TextureId = texture_id; |
654 | _OnChangedTextureID(); |
655 | } |
656 | |
657 | void ImDrawList::PopTextureID() |
658 | { |
659 | _TextureIdStack.pop_back(); |
660 | _CmdHeader.TextureId = (_TextureIdStack.Size == 0) ? (ImTextureID)NULL : _TextureIdStack.Data[_TextureIdStack.Size - 1]; |
661 | _OnChangedTextureID(); |
662 | } |
663 | |
664 | // This is used by ImGui::PushFont()/PopFont(). It works because we never use _TextureIdStack[] elsewhere than in PushTextureID()/PopTextureID(). |
665 | void ImDrawList::_SetTextureID(ImTextureID texture_id) |
666 | { |
667 | if (_CmdHeader.TextureId == texture_id) |
668 | return; |
669 | _CmdHeader.TextureId = texture_id; |
670 | _OnChangedTextureID(); |
671 | } |
672 | |
673 | // Reserve space for a number of vertices and indices. |
674 | // You must finish filling your reserved data before calling PrimReserve() again, as it may reallocate or |
675 | // submit the intermediate results. PrimUnreserve() can be used to release unused allocations. |
676 | void ImDrawList::PrimReserve(int idx_count, int vtx_count) |
677 | { |
678 | // Large mesh support (when enabled) |
679 | IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); |
680 | if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) |
681 | { |
682 | // FIXME: In theory we should be testing that vtx_count <64k here. |
683 | // In practice, RenderText() relies on reserving ahead for a worst case scenario so it is currently useful for us |
684 | // to not make that check until we rework the text functions to handle clipping and large horizontal lines better. |
685 | _CmdHeader.VtxOffset = VtxBuffer.Size; |
686 | _OnChangedVtxOffset(); |
687 | } |
688 | |
689 | ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
690 | draw_cmd->ElemCount += idx_count; |
691 | |
692 | int vtx_buffer_old_size = VtxBuffer.Size; |
693 | VtxBuffer.resize(new_size: vtx_buffer_old_size + vtx_count); |
694 | _VtxWritePtr = VtxBuffer.Data + vtx_buffer_old_size; |
695 | |
696 | int idx_buffer_old_size = IdxBuffer.Size; |
697 | IdxBuffer.resize(new_size: idx_buffer_old_size + idx_count); |
698 | _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size; |
699 | } |
700 | |
701 | // Release the number of reserved vertices/indices from the end of the last reservation made with PrimReserve(). |
702 | void ImDrawList::PrimUnreserve(int idx_count, int vtx_count) |
703 | { |
704 | IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); |
705 | |
706 | ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; |
707 | draw_cmd->ElemCount -= idx_count; |
708 | VtxBuffer.shrink(new_size: VtxBuffer.Size - vtx_count); |
709 | IdxBuffer.shrink(new_size: IdxBuffer.Size - idx_count); |
710 | } |
711 | |
712 | // Fully unrolled with inline call to keep our debug builds decently fast. |
713 | void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col) |
714 | { |
715 | ImVec2 b(c.x, a.y), d(a.x, c.y), uv(_Data->TexUvWhitePixel); |
716 | ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; |
717 | _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); |
718 | _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); |
719 | _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; |
720 | _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; |
721 | _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; |
722 | _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; |
723 | _VtxWritePtr += 4; |
724 | _VtxCurrentIdx += 4; |
725 | _IdxWritePtr += 6; |
726 | } |
727 | |
728 | void ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col) |
729 | { |
730 | 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); |
731 | ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; |
732 | _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); |
733 | _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); |
734 | _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; |
735 | _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; |
736 | _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; |
737 | _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; |
738 | _VtxWritePtr += 4; |
739 | _VtxCurrentIdx += 4; |
740 | _IdxWritePtr += 6; |
741 | } |
742 | |
743 | 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) |
744 | { |
745 | ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; |
746 | _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); |
747 | _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); |
748 | _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; |
749 | _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; |
750 | _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; |
751 | _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; |
752 | _VtxWritePtr += 4; |
753 | _VtxCurrentIdx += 4; |
754 | _IdxWritePtr += 6; |
755 | } |
756 | |
757 | // On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superfluous function calls to optimize debug/non-inlined builds. |
758 | // - Those macros expects l-values and need to be used as their own statement. |
759 | // - Those macros are intentionally not surrounded by the 'do {} while (0)' idiom because even that translates to runtime with debug compilers. |
760 | #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 |
761 | #define IM_FIXNORMAL2F_MAX_INVLEN2 100.0f // 500.0f (see #4053, #3366) |
762 | #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 |
763 | |
764 | // TODO: Thickness anti-aliased lines cap are missing their AA fringe. |
765 | // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. |
766 | void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, ImDrawFlags flags, float thickness) |
767 | { |
768 | if (points_count < 2 || (col & IM_COL32_A_MASK) == 0) |
769 | return; |
770 | |
771 | const bool closed = (flags & ImDrawFlags_Closed) != 0; |
772 | const ImVec2 opaque_uv = _Data->TexUvWhitePixel; |
773 | const int count = closed ? points_count : points_count - 1; // The number of line segments we need to draw |
774 | const bool thick_line = (thickness > _FringeScale); |
775 | |
776 | if (Flags & ImDrawListFlags_AntiAliasedLines) |
777 | { |
778 | // Anti-aliased stroke |
779 | const float AA_SIZE = _FringeScale; |
780 | const ImU32 col_trans = col & ~IM_COL32_A_MASK; |
781 | |
782 | // Thicknesses <1.0 should behave like thickness 1.0 |
783 | thickness = ImMax(lhs: thickness, rhs: 1.0f); |
784 | const int integer_thickness = (int)thickness; |
785 | const float fractional_thickness = thickness - integer_thickness; |
786 | |
787 | // Do we want to draw this line using a texture? |
788 | // - For now, only draw integer-width lines using textures to avoid issues with the way scaling occurs, could be improved. |
789 | // - If AA_SIZE is not 1.0f we cannot use the texture path. |
790 | const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f) && (AA_SIZE == 1.0f); |
791 | |
792 | // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoBakedLines is off |
793 | IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines)); |
794 | |
795 | const int idx_count = use_texture ? (count * 6) : (thick_line ? count * 18 : count * 12); |
796 | const int vtx_count = use_texture ? (points_count * 2) : (thick_line ? points_count * 4 : points_count * 3); |
797 | PrimReserve(idx_count, vtx_count); |
798 | |
799 | // Temporary buffer |
800 | // 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 |
801 | _Data->TempBuffer.reserve_discard(new_capacity: points_count * ((use_texture || !thick_line) ? 3 : 5)); |
802 | ImVec2* temp_normals = _Data->TempBuffer.Data; |
803 | ImVec2* temp_points = temp_normals + points_count; |
804 | |
805 | // Calculate normals (tangents) for each line segment |
806 | for (int i1 = 0; i1 < count; i1++) |
807 | { |
808 | const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; |
809 | float dx = points[i2].x - points[i1].x; |
810 | float dy = points[i2].y - points[i1].y; |
811 | IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
812 | temp_normals[i1].x = dy; |
813 | temp_normals[i1].y = -dx; |
814 | } |
815 | if (!closed) |
816 | temp_normals[points_count - 1] = temp_normals[points_count - 2]; |
817 | |
818 | // 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 |
819 | if (use_texture || !thick_line) |
820 | { |
821 | // [PATH 1] Texture-based lines (thick or non-thick) |
822 | // [PATH 2] Non texture-based lines (non-thick) |
823 | |
824 | // The width of the geometry we need to draw - this is essentially <thickness> pixels for the line itself, plus "one pixel" for AA. |
825 | // - In the texture-based path, we don't use AA_SIZE here because the +1 is tied to the generated texture |
826 | // (see ImFontAtlasBuildRenderLinesTexData() function), and so alternate values won't work without changes to that code. |
827 | // - 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 |
828 | // allow scaling geometry while preserving one-screen-pixel AA fringe). |
829 | const float half_draw_size = use_texture ? ((thickness * 0.5f) + 1) : AA_SIZE; |
830 | |
831 | // If line is not closed, the first and last points need to be generated differently as there are no normals to blend |
832 | if (!closed) |
833 | { |
834 | temp_points[0] = points[0] + temp_normals[0] * half_draw_size; |
835 | temp_points[1] = points[0] - temp_normals[0] * half_draw_size; |
836 | temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * half_draw_size; |
837 | temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * half_draw_size; |
838 | } |
839 | |
840 | // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges |
841 | // 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) |
842 | // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. |
843 | unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment |
844 | for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment |
845 | { |
846 | const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; // i2 is the second point of the line segment |
847 | const unsigned int idx2 = ((i1 + 1) == points_count) ? _VtxCurrentIdx : (idx1 + (use_texture ? 2 : 3)); // Vertex index for end of segment |
848 | |
849 | // Average normals |
850 | float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; |
851 | float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; |
852 | IM_FIXNORMAL2F(dm_x, dm_y); |
853 | dm_x *= half_draw_size; // dm_x, dm_y are offset to the outer edge of the AA area |
854 | dm_y *= half_draw_size; |
855 | |
856 | // Add temporary vertexes for the outer edges |
857 | ImVec2* out_vtx = &temp_points[i2 * 2]; |
858 | out_vtx[0].x = points[i2].x + dm_x; |
859 | out_vtx[0].y = points[i2].y + dm_y; |
860 | out_vtx[1].x = points[i2].x - dm_x; |
861 | out_vtx[1].y = points[i2].y - dm_y; |
862 | |
863 | if (use_texture) |
864 | { |
865 | // Add indices for two triangles |
866 | _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 1); // Right tri |
867 | _IdxWritePtr[3] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[4] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Left tri |
868 | _IdxWritePtr += 6; |
869 | } |
870 | else |
871 | { |
872 | // Add indexes for four triangles |
873 | _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); // Right tri 1 |
874 | _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Right tri 2 |
875 | _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); // Left tri 1 |
876 | _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); // Left tri 2 |
877 | _IdxWritePtr += 12; |
878 | } |
879 | |
880 | idx1 = idx2; |
881 | } |
882 | |
883 | // Add vertexes for each point on the line |
884 | if (use_texture) |
885 | { |
886 | // If we're using textures we only need to emit the left/right edge vertices |
887 | ImVec4 tex_uvs = _Data->TexUvLines[integer_thickness]; |
888 | /*if (fractional_thickness != 0.0f) // Currently always zero when use_texture==false! |
889 | { |
890 | const ImVec4 tex_uvs_1 = _Data->TexUvLines[integer_thickness + 1]; |
891 | tex_uvs.x = tex_uvs.x + (tex_uvs_1.x - tex_uvs.x) * fractional_thickness; // inlined ImLerp() |
892 | tex_uvs.y = tex_uvs.y + (tex_uvs_1.y - tex_uvs.y) * fractional_thickness; |
893 | tex_uvs.z = tex_uvs.z + (tex_uvs_1.z - tex_uvs.z) * fractional_thickness; |
894 | tex_uvs.w = tex_uvs.w + (tex_uvs_1.w - tex_uvs.w) * fractional_thickness; |
895 | }*/ |
896 | ImVec2 tex_uv0(tex_uvs.x, tex_uvs.y); |
897 | ImVec2 tex_uv1(tex_uvs.z, tex_uvs.w); |
898 | for (int i = 0; i < points_count; i++) |
899 | { |
900 | _VtxWritePtr[0].pos = temp_points[i * 2 + 0]; _VtxWritePtr[0].uv = tex_uv0; _VtxWritePtr[0].col = col; // Left-side outer edge |
901 | _VtxWritePtr[1].pos = temp_points[i * 2 + 1]; _VtxWritePtr[1].uv = tex_uv1; _VtxWritePtr[1].col = col; // Right-side outer edge |
902 | _VtxWritePtr += 2; |
903 | } |
904 | } |
905 | else |
906 | { |
907 | // If we're not using a texture, we need the center vertex as well |
908 | for (int i = 0; i < points_count; i++) |
909 | { |
910 | _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; // Center of line |
911 | _VtxWritePtr[1].pos = temp_points[i * 2 + 0]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col_trans; // Left-side outer edge |
912 | _VtxWritePtr[2].pos = temp_points[i * 2 + 1]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col_trans; // Right-side outer edge |
913 | _VtxWritePtr += 3; |
914 | } |
915 | } |
916 | } |
917 | else |
918 | { |
919 | // [PATH 2] Non texture-based lines (thick): we need to draw the solid line core and thus require four vertices per point |
920 | const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f; |
921 | |
922 | // If line is not closed, the first and last points need to be generated differently as there are no normals to blend |
923 | if (!closed) |
924 | { |
925 | const int points_last = points_count - 1; |
926 | temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE); |
927 | temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness); |
928 | temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness); |
929 | temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE); |
930 | temp_points[points_last * 4 + 0] = points[points_last] + temp_normals[points_last] * (half_inner_thickness + AA_SIZE); |
931 | temp_points[points_last * 4 + 1] = points[points_last] + temp_normals[points_last] * (half_inner_thickness); |
932 | temp_points[points_last * 4 + 2] = points[points_last] - temp_normals[points_last] * (half_inner_thickness); |
933 | temp_points[points_last * 4 + 3] = points[points_last] - temp_normals[points_last] * (half_inner_thickness + AA_SIZE); |
934 | } |
935 | |
936 | // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges |
937 | // 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) |
938 | // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. |
939 | unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment |
940 | for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment |
941 | { |
942 | const int i2 = (i1 + 1) == points_count ? 0 : (i1 + 1); // i2 is the second point of the line segment |
943 | const unsigned int idx2 = (i1 + 1) == points_count ? _VtxCurrentIdx : (idx1 + 4); // Vertex index for end of segment |
944 | |
945 | // Average normals |
946 | float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; |
947 | float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; |
948 | IM_FIXNORMAL2F(dm_x, dm_y); |
949 | float dm_out_x = dm_x * (half_inner_thickness + AA_SIZE); |
950 | float dm_out_y = dm_y * (half_inner_thickness + AA_SIZE); |
951 | float dm_in_x = dm_x * half_inner_thickness; |
952 | float dm_in_y = dm_y * half_inner_thickness; |
953 | |
954 | // Add temporary vertices |
955 | ImVec2* out_vtx = &temp_points[i2 * 4]; |
956 | out_vtx[0].x = points[i2].x + dm_out_x; |
957 | out_vtx[0].y = points[i2].y + dm_out_y; |
958 | out_vtx[1].x = points[i2].x + dm_in_x; |
959 | out_vtx[1].y = points[i2].y + dm_in_y; |
960 | out_vtx[2].x = points[i2].x - dm_in_x; |
961 | out_vtx[2].y = points[i2].y - dm_in_y; |
962 | out_vtx[3].x = points[i2].x - dm_out_x; |
963 | out_vtx[3].y = points[i2].y - dm_out_y; |
964 | |
965 | // Add indexes |
966 | _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); |
967 | _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 1); |
968 | _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); |
969 | _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); |
970 | _IdxWritePtr[12] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[13] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[14] = (ImDrawIdx)(idx1 + 3); |
971 | _IdxWritePtr[15] = (ImDrawIdx)(idx1 + 3); _IdxWritePtr[16] = (ImDrawIdx)(idx2 + 3); _IdxWritePtr[17] = (ImDrawIdx)(idx2 + 2); |
972 | _IdxWritePtr += 18; |
973 | |
974 | idx1 = idx2; |
975 | } |
976 | |
977 | // Add vertices |
978 | for (int i = 0; i < points_count; i++) |
979 | { |
980 | _VtxWritePtr[0].pos = temp_points[i * 4 + 0]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col_trans; |
981 | _VtxWritePtr[1].pos = temp_points[i * 4 + 1]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col; |
982 | _VtxWritePtr[2].pos = temp_points[i * 4 + 2]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col; |
983 | _VtxWritePtr[3].pos = temp_points[i * 4 + 3]; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col_trans; |
984 | _VtxWritePtr += 4; |
985 | } |
986 | } |
987 | _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
988 | } |
989 | else |
990 | { |
991 | // [PATH 4] Non texture-based, Non anti-aliased lines |
992 | const int idx_count = count * 6; |
993 | const int vtx_count = count * 4; // FIXME-OPT: Not sharing edges |
994 | PrimReserve(idx_count, vtx_count); |
995 | |
996 | for (int i1 = 0; i1 < count; i1++) |
997 | { |
998 | const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; |
999 | const ImVec2& p1 = points[i1]; |
1000 | const ImVec2& p2 = points[i2]; |
1001 | |
1002 | float dx = p2.x - p1.x; |
1003 | float dy = p2.y - p1.y; |
1004 | IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
1005 | dx *= (thickness * 0.5f); |
1006 | dy *= (thickness * 0.5f); |
1007 | |
1008 | _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; |
1009 | _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col; |
1010 | _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col; |
1011 | _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col; |
1012 | _VtxWritePtr += 4; |
1013 | |
1014 | _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + 2); |
1015 | _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx + 2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx + 3); |
1016 | _IdxWritePtr += 6; |
1017 | _VtxCurrentIdx += 4; |
1018 | } |
1019 | } |
1020 | } |
1021 | |
1022 | // - We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds. |
1023 | // - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. |
1024 | void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col) |
1025 | { |
1026 | if (points_count < 3 || (col & IM_COL32_A_MASK) == 0) |
1027 | return; |
1028 | |
1029 | const ImVec2 uv = _Data->TexUvWhitePixel; |
1030 | |
1031 | if (Flags & ImDrawListFlags_AntiAliasedFill) |
1032 | { |
1033 | // Anti-aliased Fill |
1034 | const float AA_SIZE = _FringeScale; |
1035 | const ImU32 col_trans = col & ~IM_COL32_A_MASK; |
1036 | const int idx_count = (points_count - 2)*3 + points_count * 6; |
1037 | const int vtx_count = (points_count * 2); |
1038 | PrimReserve(idx_count, vtx_count); |
1039 | |
1040 | // Add indexes for fill |
1041 | unsigned int vtx_inner_idx = _VtxCurrentIdx; |
1042 | unsigned int vtx_outer_idx = _VtxCurrentIdx + 1; |
1043 | for (int i = 2; i < points_count; i++) |
1044 | { |
1045 | _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + ((i - 1) << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx + (i << 1)); |
1046 | _IdxWritePtr += 3; |
1047 | } |
1048 | |
1049 | // Compute normals |
1050 | _Data->TempBuffer.reserve_discard(new_capacity: points_count); |
1051 | ImVec2* temp_normals = _Data->TempBuffer.Data; |
1052 | for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) |
1053 | { |
1054 | const ImVec2& p0 = points[i0]; |
1055 | const ImVec2& p1 = points[i1]; |
1056 | float dx = p1.x - p0.x; |
1057 | float dy = p1.y - p0.y; |
1058 | IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
1059 | temp_normals[i0].x = dy; |
1060 | temp_normals[i0].y = -dx; |
1061 | } |
1062 | |
1063 | for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) |
1064 | { |
1065 | // Average normals |
1066 | const ImVec2& n0 = temp_normals[i0]; |
1067 | const ImVec2& n1 = temp_normals[i1]; |
1068 | float dm_x = (n0.x + n1.x) * 0.5f; |
1069 | float dm_y = (n0.y + n1.y) * 0.5f; |
1070 | IM_FIXNORMAL2F(dm_x, dm_y); |
1071 | dm_x *= AA_SIZE * 0.5f; |
1072 | dm_y *= AA_SIZE * 0.5f; |
1073 | |
1074 | // Add vertices |
1075 | _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 |
1076 | _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 |
1077 | _VtxWritePtr += 2; |
1078 | |
1079 | // Add indexes for fringes |
1080 | _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (i0 << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); |
1081 | _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx + (i1 << 1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); |
1082 | _IdxWritePtr += 6; |
1083 | } |
1084 | _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
1085 | } |
1086 | else |
1087 | { |
1088 | // Non Anti-aliased Fill |
1089 | const int idx_count = (points_count - 2)*3; |
1090 | const int vtx_count = points_count; |
1091 | PrimReserve(idx_count, vtx_count); |
1092 | for (int i = 0; i < vtx_count; i++) |
1093 | { |
1094 | _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; |
1095 | _VtxWritePtr++; |
1096 | } |
1097 | for (int i = 2; i < points_count; i++) |
1098 | { |
1099 | _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + i - 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + i); |
1100 | _IdxWritePtr += 3; |
1101 | } |
1102 | _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
1103 | } |
1104 | } |
1105 | |
1106 | void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step) |
1107 | { |
1108 | if (radius < 0.5f) |
1109 | { |
1110 | _Path.push_back(v: center); |
1111 | return; |
1112 | } |
1113 | |
1114 | // Calculate arc auto segment step size |
1115 | if (a_step <= 0) |
1116 | a_step = IM_DRAWLIST_ARCFAST_SAMPLE_MAX / _CalcCircleAutoSegmentCount(radius); |
1117 | |
1118 | // Make sure we never do steps larger than one quarter of the circle |
1119 | a_step = ImClamp(v: a_step, mn: 1, IM_DRAWLIST_ARCFAST_TABLE_SIZE / 4); |
1120 | |
1121 | const int sample_range = ImAbs(x: a_max_sample - a_min_sample); |
1122 | const int a_next_step = a_step; |
1123 | |
1124 | int samples = sample_range + 1; |
1125 | bool = false; |
1126 | if (a_step > 1) |
1127 | { |
1128 | samples = sample_range / a_step + 1; |
1129 | const int overstep = sample_range % a_step; |
1130 | |
1131 | if (overstep > 0) |
1132 | { |
1133 | extra_max_sample = true; |
1134 | samples++; |
1135 | |
1136 | // When we have overstep to avoid awkwardly looking one long line and one tiny one at the end, |
1137 | // distribute first step range evenly between them by reducing first step size. |
1138 | if (sample_range > 0) |
1139 | a_step -= (a_step - overstep) / 2; |
1140 | } |
1141 | } |
1142 | |
1143 | _Path.resize(new_size: _Path.Size + samples); |
1144 | ImVec2* out_ptr = _Path.Data + (_Path.Size - samples); |
1145 | |
1146 | int sample_index = a_min_sample; |
1147 | if (sample_index < 0 || sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) |
1148 | { |
1149 | sample_index = sample_index % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1150 | if (sample_index < 0) |
1151 | sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1152 | } |
1153 | |
1154 | if (a_max_sample >= a_min_sample) |
1155 | { |
1156 | for (int a = a_min_sample; a <= a_max_sample; a += a_step, sample_index += a_step, a_step = a_next_step) |
1157 | { |
1158 | // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more |
1159 | if (sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) |
1160 | sample_index -= IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1161 | |
1162 | const ImVec2 s = _Data->ArcFastVtx[sample_index]; |
1163 | out_ptr->x = center.x + s.x * radius; |
1164 | out_ptr->y = center.y + s.y * radius; |
1165 | out_ptr++; |
1166 | } |
1167 | } |
1168 | else |
1169 | { |
1170 | for (int a = a_min_sample; a >= a_max_sample; a -= a_step, sample_index -= a_step, a_step = a_next_step) |
1171 | { |
1172 | // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more |
1173 | if (sample_index < 0) |
1174 | sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1175 | |
1176 | const ImVec2 s = _Data->ArcFastVtx[sample_index]; |
1177 | out_ptr->x = center.x + s.x * radius; |
1178 | out_ptr->y = center.y + s.y * radius; |
1179 | out_ptr++; |
1180 | } |
1181 | } |
1182 | |
1183 | if (extra_max_sample) |
1184 | { |
1185 | int normalized_max_sample = a_max_sample % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1186 | if (normalized_max_sample < 0) |
1187 | normalized_max_sample += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1188 | |
1189 | const ImVec2 s = _Data->ArcFastVtx[normalized_max_sample]; |
1190 | out_ptr->x = center.x + s.x * radius; |
1191 | out_ptr->y = center.y + s.y * radius; |
1192 | out_ptr++; |
1193 | } |
1194 | |
1195 | IM_ASSERT_PARANOID(_Path.Data + _Path.Size == out_ptr); |
1196 | } |
1197 | |
1198 | void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) |
1199 | { |
1200 | if (radius < 0.5f) |
1201 | { |
1202 | _Path.push_back(v: center); |
1203 | return; |
1204 | } |
1205 | |
1206 | // Note that we are adding a point at both a_min and a_max. |
1207 | // If you are trying to draw a full closed circle you don't want the overlapping points! |
1208 | _Path.reserve(new_capacity: _Path.Size + (num_segments + 1)); |
1209 | for (int i = 0; i <= num_segments; i++) |
1210 | { |
1211 | const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); |
1212 | _Path.push_back(v: ImVec2(center.x + ImCos(a) * radius, center.y + ImSin(a) * radius)); |
1213 | } |
1214 | } |
1215 | |
1216 | // 0: East, 3: South, 6: West, 9: North, 12: East |
1217 | void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) |
1218 | { |
1219 | if (radius < 0.5f) |
1220 | { |
1221 | _Path.push_back(v: center); |
1222 | return; |
1223 | } |
1224 | _PathArcToFastEx(center, radius, a_min_sample: a_min_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_max_sample: a_max_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_step: 0); |
1225 | } |
1226 | |
1227 | void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) |
1228 | { |
1229 | if (radius < 0.5f) |
1230 | { |
1231 | _Path.push_back(v: center); |
1232 | return; |
1233 | } |
1234 | |
1235 | if (num_segments > 0) |
1236 | { |
1237 | _PathArcToN(center, radius, a_min, a_max, num_segments); |
1238 | return; |
1239 | } |
1240 | |
1241 | // Automatic segment count |
1242 | if (radius <= _Data->ArcFastRadiusCutoff) |
1243 | { |
1244 | const bool a_is_reverse = a_max < a_min; |
1245 | |
1246 | // We are going to use precomputed values for mid samples. |
1247 | // Determine first and last sample in lookup table that belong to the arc. |
1248 | const float a_min_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f); |
1249 | const float a_max_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f); |
1250 | |
1251 | const int a_min_sample = a_is_reverse ? (int)ImFloor(f: a_min_sample_f) : (int)ImCeil(a_min_sample_f); |
1252 | const int a_max_sample = a_is_reverse ? (int)ImCeil(a_max_sample_f) : (int)ImFloor(f: a_max_sample_f); |
1253 | const int a_mid_samples = a_is_reverse ? ImMax(lhs: a_min_sample - a_max_sample, rhs: 0) : ImMax(lhs: a_max_sample - a_min_sample, rhs: 0); |
1254 | |
1255 | const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1256 | const float a_max_segment_angle = a_max_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; |
1257 | const bool a_emit_start = ImAbs(x: a_min_segment_angle - a_min) >= 1e-5f; |
1258 | const bool a_emit_end = ImAbs(x: a_max - a_max_segment_angle) >= 1e-5f; |
1259 | |
1260 | _Path.reserve(new_capacity: _Path.Size + (a_mid_samples + 1 + (a_emit_start ? 1 : 0) + (a_emit_end ? 1 : 0))); |
1261 | if (a_emit_start) |
1262 | _Path.push_back(v: ImVec2(center.x + ImCos(a_min) * radius, center.y + ImSin(a_min) * radius)); |
1263 | if (a_mid_samples > 0) |
1264 | _PathArcToFastEx(center, radius, a_min_sample, a_max_sample, a_step: 0); |
1265 | if (a_emit_end) |
1266 | _Path.push_back(v: ImVec2(center.x + ImCos(a_max) * radius, center.y + ImSin(a_max) * radius)); |
1267 | } |
1268 | else |
1269 | { |
1270 | const float arc_length = ImAbs(x: a_max - a_min); |
1271 | const int circle_segment_count = _CalcCircleAutoSegmentCount(radius); |
1272 | const int arc_segment_count = ImMax(lhs: (int)ImCeil(circle_segment_count * arc_length / (IM_PI * 2.0f)), rhs: (int)(2.0f * IM_PI / arc_length)); |
1273 | _PathArcToN(center, radius, a_min, a_max, num_segments: arc_segment_count); |
1274 | } |
1275 | } |
1276 | |
1277 | void ImDrawList::PathEllipticalArcTo(const ImVec2& center, const ImVec2& radius, float rot, float a_min, float a_max, int num_segments) |
1278 | { |
1279 | if (num_segments <= 0) |
1280 | num_segments = _CalcCircleAutoSegmentCount(radius: ImMax(lhs: radius.x, rhs: radius.y)); // A bit pessimistic, maybe there's a better computation to do here. |
1281 | |
1282 | _Path.reserve(new_capacity: _Path.Size + (num_segments + 1)); |
1283 | |
1284 | const float cos_rot = ImCos(rot); |
1285 | const float sin_rot = ImSin(rot); |
1286 | for (int i = 0; i <= num_segments; i++) |
1287 | { |
1288 | const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); |
1289 | ImVec2 point(ImCos(a) * radius.x, ImSin(a) * radius.y); |
1290 | const ImVec2 rel((point.x * cos_rot) - (point.y * sin_rot), (point.x * sin_rot) + (point.y * cos_rot)); |
1291 | point.x = rel.x + center.x; |
1292 | point.y = rel.y + center.y; |
1293 | _Path.push_back(v: point); |
1294 | } |
1295 | } |
1296 | |
1297 | ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) |
1298 | { |
1299 | float u = 1.0f - t; |
1300 | float w1 = u * u * u; |
1301 | float w2 = 3 * u * u * t; |
1302 | float w3 = 3 * u * t * t; |
1303 | float w4 = t * t * t; |
1304 | 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); |
1305 | } |
1306 | |
1307 | ImVec2 ImBezierQuadraticCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, float t) |
1308 | { |
1309 | float u = 1.0f - t; |
1310 | float w1 = u * u; |
1311 | float w2 = 2 * u * t; |
1312 | float w3 = t * t; |
1313 | return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x, w1 * p1.y + w2 * p2.y + w3 * p3.y); |
1314 | } |
1315 | |
1316 | // Closely mimics ImBezierCubicClosestPointCasteljau() in imgui.cpp |
1317 | 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) |
1318 | { |
1319 | float dx = x4 - x1; |
1320 | float dy = y4 - y1; |
1321 | float d2 = (x2 - x4) * dy - (y2 - y4) * dx; |
1322 | float d3 = (x3 - x4) * dy - (y3 - y4) * dx; |
1323 | d2 = (d2 >= 0) ? d2 : -d2; |
1324 | d3 = (d3 >= 0) ? d3 : -d3; |
1325 | if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) |
1326 | { |
1327 | path->push_back(v: ImVec2(x4, y4)); |
1328 | } |
1329 | else if (level < 10) |
1330 | { |
1331 | float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; |
1332 | float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; |
1333 | float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f; |
1334 | float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; |
1335 | float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f; |
1336 | float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f; |
1337 | PathBezierCubicCurveToCasteljau(path, x1, y1, x2: x12, y2: y12, x3: x123, y3: y123, x4: x1234, y4: y1234, tess_tol, level: level + 1); |
1338 | PathBezierCubicCurveToCasteljau(path, x1: x1234, y1: y1234, x2: x234, y2: y234, x3: x34, y3: y34, x4, y4, tess_tol, level: level + 1); |
1339 | } |
1340 | } |
1341 | |
1342 | static void PathBezierQuadraticCurveToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float tess_tol, int level) |
1343 | { |
1344 | float dx = x3 - x1, dy = y3 - y1; |
1345 | float det = (x2 - x3) * dy - (y2 - y3) * dx; |
1346 | if (det * det * 4.0f < tess_tol * (dx * dx + dy * dy)) |
1347 | { |
1348 | path->push_back(v: ImVec2(x3, y3)); |
1349 | } |
1350 | else if (level < 10) |
1351 | { |
1352 | float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; |
1353 | float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; |
1354 | float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; |
1355 | PathBezierQuadraticCurveToCasteljau(path, x1, y1, x2: x12, y2: y12, x3: x123, y3: y123, tess_tol, level: level + 1); |
1356 | PathBezierQuadraticCurveToCasteljau(path, x1: x123, y1: y123, x2: x23, y2: y23, x3, y3, tess_tol, level: level + 1); |
1357 | } |
1358 | } |
1359 | |
1360 | void ImDrawList::PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments) |
1361 | { |
1362 | ImVec2 p1 = _Path.back(); |
1363 | if (num_segments == 0) |
1364 | { |
1365 | IM_ASSERT(_Data->CurveTessellationTol > 0.0f); |
1366 | PathBezierCubicCurveToCasteljau(path: &_Path, x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y, x3: p3.x, y3: p3.y, x4: p4.x, y4: p4.y, tess_tol: _Data->CurveTessellationTol, level: 0); // Auto-tessellated |
1367 | } |
1368 | else |
1369 | { |
1370 | float t_step = 1.0f / (float)num_segments; |
1371 | for (int i_step = 1; i_step <= num_segments; i_step++) |
1372 | _Path.push_back(v: ImBezierCubicCalc(p1, p2, p3, p4, t: t_step * i_step)); |
1373 | } |
1374 | } |
1375 | |
1376 | void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments) |
1377 | { |
1378 | ImVec2 p1 = _Path.back(); |
1379 | if (num_segments == 0) |
1380 | { |
1381 | IM_ASSERT(_Data->CurveTessellationTol > 0.0f); |
1382 | PathBezierQuadraticCurveToCasteljau(path: &_Path, x1: p1.x, y1: p1.y, x2: p2.x, y2: p2.y, x3: p3.x, y3: p3.y, tess_tol: _Data->CurveTessellationTol, level: 0);// Auto-tessellated |
1383 | } |
1384 | else |
1385 | { |
1386 | float t_step = 1.0f / (float)num_segments; |
1387 | for (int i_step = 1; i_step <= num_segments; i_step++) |
1388 | _Path.push_back(v: ImBezierQuadraticCalc(p1, p2, p3, t: t_step * i_step)); |
1389 | } |
1390 | } |
1391 | |
1392 | static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) |
1393 | { |
1394 | /* |
1395 | IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); |
1396 | #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
1397 | // Obsoleted in 1.82 (from February 2021). This code was stripped/simplified and mostly commented in 1.90 (from September 2023) |
1398 | // - Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) |
1399 | if (flags == ~0) { return ImDrawFlags_RoundCornersAll; } |
1400 | // - Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations). Read details in older version of this code. |
1401 | if (flags >= 0x01 && flags <= 0x0F) { return (flags << 4); } |
1402 | // We cannot support hard coded 0x00 with 'float rounding > 0.0f' --> replace with ImDrawFlags_RoundCornersNone or use 'float rounding = 0.0f' |
1403 | #endif |
1404 | */ |
1405 | // If this assert triggers, please update your code replacing hardcoded values with new ImDrawFlags_RoundCorners* values. |
1406 | // Note that ImDrawFlags_Closed (== 0x01) is an invalid flag for AddRect(), AddRectFilled(), PathRect() etc. anyway. |
1407 | // See details in 1.82 Changelog as well as 2021/03/12 and 2023/09/08 entries in "API BREAKING CHANGES" section. |
1408 | IM_ASSERT((flags & 0x0F) == 0 && "Misuse of legacy hardcoded ImDrawCornerFlags values!" ); |
1409 | |
1410 | if ((flags & ImDrawFlags_RoundCornersMask_) == 0) |
1411 | flags |= ImDrawFlags_RoundCornersAll; |
1412 | |
1413 | return flags; |
1414 | } |
1415 | |
1416 | void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawFlags flags) |
1417 | { |
1418 | if (rounding >= 0.5f) |
1419 | { |
1420 | flags = FixRectCornerFlags(flags); |
1421 | rounding = ImMin(lhs: rounding, ImFabs(b.x - a.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f); |
1422 | rounding = ImMin(lhs: rounding, ImFabs(b.y - a.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f); |
1423 | } |
1424 | if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
1425 | { |
1426 | PathLineTo(pos: a); |
1427 | PathLineTo(pos: ImVec2(b.x, a.y)); |
1428 | PathLineTo(pos: b); |
1429 | PathLineTo(pos: ImVec2(a.x, b.y)); |
1430 | } |
1431 | else |
1432 | { |
1433 | const float rounding_tl = (flags & ImDrawFlags_RoundCornersTopLeft) ? rounding : 0.0f; |
1434 | const float rounding_tr = (flags & ImDrawFlags_RoundCornersTopRight) ? rounding : 0.0f; |
1435 | const float rounding_br = (flags & ImDrawFlags_RoundCornersBottomRight) ? rounding : 0.0f; |
1436 | const float rounding_bl = (flags & ImDrawFlags_RoundCornersBottomLeft) ? rounding : 0.0f; |
1437 | PathArcToFast(center: ImVec2(a.x + rounding_tl, a.y + rounding_tl), radius: rounding_tl, a_min_of_12: 6, a_max_of_12: 9); |
1438 | PathArcToFast(center: ImVec2(b.x - rounding_tr, a.y + rounding_tr), radius: rounding_tr, a_min_of_12: 9, a_max_of_12: 12); |
1439 | PathArcToFast(center: ImVec2(b.x - rounding_br, b.y - rounding_br), radius: rounding_br, a_min_of_12: 0, a_max_of_12: 3); |
1440 | PathArcToFast(center: ImVec2(a.x + rounding_bl, b.y - rounding_bl), radius: rounding_bl, a_min_of_12: 3, a_max_of_12: 6); |
1441 | } |
1442 | } |
1443 | |
1444 | void ImDrawList::AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness) |
1445 | { |
1446 | if ((col & IM_COL32_A_MASK) == 0) |
1447 | return; |
1448 | PathLineTo(pos: p1 + ImVec2(0.5f, 0.5f)); |
1449 | PathLineTo(pos: p2 + ImVec2(0.5f, 0.5f)); |
1450 | PathStroke(col, flags: 0, thickness); |
1451 | } |
1452 | |
1453 | // p_min = upper-left, p_max = lower-right |
1454 | // Note we don't render 1 pixels sized rectangles properly. |
1455 | void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags, float thickness) |
1456 | { |
1457 | if ((col & IM_COL32_A_MASK) == 0) |
1458 | return; |
1459 | if (Flags & ImDrawListFlags_AntiAliasedLines) |
1460 | PathRect(a: p_min + ImVec2(0.50f, 0.50f), b: p_max - ImVec2(0.50f, 0.50f), rounding, flags); |
1461 | else |
1462 | PathRect(a: p_min + ImVec2(0.50f, 0.50f), b: p_max - ImVec2(0.49f, 0.49f), rounding, flags); // Better looking lower-right corner and rounded non-AA shapes. |
1463 | PathStroke(col, flags: ImDrawFlags_Closed, thickness); |
1464 | } |
1465 | |
1466 | void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags) |
1467 | { |
1468 | if ((col & IM_COL32_A_MASK) == 0) |
1469 | return; |
1470 | if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
1471 | { |
1472 | PrimReserve(idx_count: 6, vtx_count: 4); |
1473 | PrimRect(a: p_min, c: p_max, col); |
1474 | } |
1475 | else |
1476 | { |
1477 | PathRect(a: p_min, b: p_max, rounding, flags); |
1478 | PathFillConvex(col); |
1479 | } |
1480 | } |
1481 | |
1482 | // p_min = upper-left, p_max = lower-right |
1483 | 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) |
1484 | { |
1485 | if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) & IM_COL32_A_MASK) == 0) |
1486 | return; |
1487 | |
1488 | const ImVec2 uv = _Data->TexUvWhitePixel; |
1489 | PrimReserve(idx_count: 6, vtx_count: 4); |
1490 | PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 1)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 2)); |
1491 | PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 2)); PrimWriteIdx(idx: (ImDrawIdx)(_VtxCurrentIdx + 3)); |
1492 | PrimWriteVtx(pos: p_min, uv, col: col_upr_left); |
1493 | PrimWriteVtx(pos: ImVec2(p_max.x, p_min.y), uv, col: col_upr_right); |
1494 | PrimWriteVtx(pos: p_max, uv, col: col_bot_right); |
1495 | PrimWriteVtx(pos: ImVec2(p_min.x, p_max.y), uv, col: col_bot_left); |
1496 | } |
1497 | |
1498 | void ImDrawList::AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness) |
1499 | { |
1500 | if ((col & IM_COL32_A_MASK) == 0) |
1501 | return; |
1502 | |
1503 | PathLineTo(pos: p1); |
1504 | PathLineTo(pos: p2); |
1505 | PathLineTo(pos: p3); |
1506 | PathLineTo(pos: p4); |
1507 | PathStroke(col, flags: ImDrawFlags_Closed, thickness); |
1508 | } |
1509 | |
1510 | void ImDrawList::AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col) |
1511 | { |
1512 | if ((col & IM_COL32_A_MASK) == 0) |
1513 | return; |
1514 | |
1515 | PathLineTo(pos: p1); |
1516 | PathLineTo(pos: p2); |
1517 | PathLineTo(pos: p3); |
1518 | PathLineTo(pos: p4); |
1519 | PathFillConvex(col); |
1520 | } |
1521 | |
1522 | void ImDrawList::AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness) |
1523 | { |
1524 | if ((col & IM_COL32_A_MASK) == 0) |
1525 | return; |
1526 | |
1527 | PathLineTo(pos: p1); |
1528 | PathLineTo(pos: p2); |
1529 | PathLineTo(pos: p3); |
1530 | PathStroke(col, flags: ImDrawFlags_Closed, thickness); |
1531 | } |
1532 | |
1533 | void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col) |
1534 | { |
1535 | if ((col & IM_COL32_A_MASK) == 0) |
1536 | return; |
1537 | |
1538 | PathLineTo(pos: p1); |
1539 | PathLineTo(pos: p2); |
1540 | PathLineTo(pos: p3); |
1541 | PathFillConvex(col); |
1542 | } |
1543 | |
1544 | void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) |
1545 | { |
1546 | if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f) |
1547 | return; |
1548 | |
1549 | if (num_segments <= 0) |
1550 | { |
1551 | // Use arc with automatic segment count |
1552 | _PathArcToFastEx(center, radius: radius - 0.5f, a_min_sample: 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, a_step: 0); |
1553 | _Path.Size--; |
1554 | } |
1555 | else |
1556 | { |
1557 | // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) |
1558 | num_segments = ImClamp(v: num_segments, mn: 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); |
1559 | |
1560 | // Because we are filling a closed shape we remove 1 from the count of segments/points |
1561 | const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
1562 | PathArcTo(center, radius: radius - 0.5f, a_min: 0.0f, a_max, num_segments: num_segments - 1); |
1563 | } |
1564 | |
1565 | PathStroke(col, flags: ImDrawFlags_Closed, thickness); |
1566 | } |
1567 | |
1568 | void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) |
1569 | { |
1570 | if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f) |
1571 | return; |
1572 | |
1573 | if (num_segments <= 0) |
1574 | { |
1575 | // Use arc with automatic segment count |
1576 | _PathArcToFastEx(center, radius, a_min_sample: 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, a_step: 0); |
1577 | _Path.Size--; |
1578 | } |
1579 | else |
1580 | { |
1581 | // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) |
1582 | num_segments = ImClamp(v: num_segments, mn: 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); |
1583 | |
1584 | // Because we are filling a closed shape we remove 1 from the count of segments/points |
1585 | const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
1586 | PathArcTo(center, radius, a_min: 0.0f, a_max, num_segments: num_segments - 1); |
1587 | } |
1588 | |
1589 | PathFillConvex(col); |
1590 | } |
1591 | |
1592 | // Guaranteed to honor 'num_segments' |
1593 | void ImDrawList::AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) |
1594 | { |
1595 | if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) |
1596 | return; |
1597 | |
1598 | // Because we are filling a closed shape we remove 1 from the count of segments/points |
1599 | const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
1600 | PathArcTo(center, radius: radius - 0.5f, a_min: 0.0f, a_max, num_segments: num_segments - 1); |
1601 | PathStroke(col, flags: ImDrawFlags_Closed, thickness); |
1602 | } |
1603 | |
1604 | // Guaranteed to honor 'num_segments' |
1605 | void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) |
1606 | { |
1607 | if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) |
1608 | return; |
1609 | |
1610 | // Because we are filling a closed shape we remove 1 from the count of segments/points |
1611 | const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; |
1612 | PathArcTo(center, radius, a_min: 0.0f, a_max, num_segments: num_segments - 1); |
1613 | PathFillConvex(col); |
1614 | } |
1615 | |
1616 | // Ellipse |
1617 | void ImDrawList::AddEllipse(const ImVec2& center, const ImVec2& radius, ImU32 col, float rot, int num_segments, float thickness) |
1618 | { |
1619 | if ((col & IM_COL32_A_MASK) == 0) |
1620 | return; |
1621 | |
1622 | if (num_segments <= 0) |
1623 | num_segments = _CalcCircleAutoSegmentCount(radius: ImMax(lhs: radius.x, rhs: radius.y)); // A bit pessimistic, maybe there's a better computation to do here. |
1624 | |
1625 | // Because we are filling a closed shape we remove 1 from the count of segments/points |
1626 | const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; |
1627 | PathEllipticalArcTo(center, radius, rot, a_min: 0.0f, a_max, num_segments: num_segments - 1); |
1628 | PathStroke(col, flags: true, thickness); |
1629 | } |
1630 | |
1631 | void ImDrawList::AddEllipseFilled(const ImVec2& center, const ImVec2& radius, ImU32 col, float rot, int num_segments) |
1632 | { |
1633 | if ((col & IM_COL32_A_MASK) == 0) |
1634 | return; |
1635 | |
1636 | if (num_segments <= 0) |
1637 | num_segments = _CalcCircleAutoSegmentCount(radius: ImMax(lhs: radius.x, rhs: radius.y)); // A bit pessimistic, maybe there's a better computation to do here. |
1638 | |
1639 | // Because we are filling a closed shape we remove 1 from the count of segments/points |
1640 | const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; |
1641 | PathEllipticalArcTo(center, radius, rot, a_min: 0.0f, a_max, num_segments: num_segments - 1); |
1642 | PathFillConvex(col); |
1643 | } |
1644 | |
1645 | // Cubic Bezier takes 4 controls points |
1646 | void ImDrawList::AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments) |
1647 | { |
1648 | if ((col & IM_COL32_A_MASK) == 0) |
1649 | return; |
1650 | |
1651 | PathLineTo(pos: p1); |
1652 | PathBezierCubicCurveTo(p2, p3, p4, num_segments); |
1653 | PathStroke(col, flags: 0, thickness); |
1654 | } |
1655 | |
1656 | // Quadratic Bezier takes 3 controls points |
1657 | void ImDrawList::AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments) |
1658 | { |
1659 | if ((col & IM_COL32_A_MASK) == 0) |
1660 | return; |
1661 | |
1662 | PathLineTo(pos: p1); |
1663 | PathBezierQuadraticCurveTo(p2, p3, num_segments); |
1664 | PathStroke(col, flags: 0, thickness); |
1665 | } |
1666 | |
1667 | void ImDrawList::AddText(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) |
1668 | { |
1669 | if ((col & IM_COL32_A_MASK) == 0) |
1670 | return; |
1671 | |
1672 | // Accept null ranges |
1673 | if (text_begin == text_end || text_begin[0] == 0) |
1674 | return; |
1675 | if (text_end == NULL) |
1676 | text_end = text_begin + strlen(s: text_begin); |
1677 | |
1678 | // Pull default font/size from the shared ImDrawListSharedData instance |
1679 | if (font == NULL) |
1680 | font = _Data->Font; |
1681 | if (font_size == 0.0f) |
1682 | font_size = _Data->FontSize; |
1683 | |
1684 | IM_ASSERT(font->ContainerAtlas->TexID == _CmdHeader.TextureId); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. |
1685 | |
1686 | ImVec4 clip_rect = _CmdHeader.ClipRect; |
1687 | if (cpu_fine_clip_rect) |
1688 | { |
1689 | clip_rect.x = ImMax(lhs: clip_rect.x, rhs: cpu_fine_clip_rect->x); |
1690 | clip_rect.y = ImMax(lhs: clip_rect.y, rhs: cpu_fine_clip_rect->y); |
1691 | clip_rect.z = ImMin(lhs: clip_rect.z, rhs: cpu_fine_clip_rect->z); |
1692 | clip_rect.w = ImMin(lhs: clip_rect.w, rhs: cpu_fine_clip_rect->w); |
1693 | } |
1694 | font->RenderText(draw_list: this, size: font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip: cpu_fine_clip_rect != NULL); |
1695 | } |
1696 | |
1697 | void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end) |
1698 | { |
1699 | AddText(NULL, font_size: 0.0f, pos, col, text_begin, text_end); |
1700 | } |
1701 | |
1702 | 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) |
1703 | { |
1704 | if ((col & IM_COL32_A_MASK) == 0) |
1705 | return; |
1706 | |
1707 | const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; |
1708 | if (push_texture_id) |
1709 | PushTextureID(texture_id: user_texture_id); |
1710 | |
1711 | PrimReserve(idx_count: 6, vtx_count: 4); |
1712 | PrimRectUV(a: p_min, c: p_max, uv_a: uv_min, uv_c: uv_max, col); |
1713 | |
1714 | if (push_texture_id) |
1715 | PopTextureID(); |
1716 | } |
1717 | |
1718 | 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) |
1719 | { |
1720 | if ((col & IM_COL32_A_MASK) == 0) |
1721 | return; |
1722 | |
1723 | const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; |
1724 | if (push_texture_id) |
1725 | PushTextureID(texture_id: user_texture_id); |
1726 | |
1727 | PrimReserve(idx_count: 6, vtx_count: 4); |
1728 | PrimQuadUV(a: p1, b: p2, c: p3, d: p4, uv_a: uv1, uv_b: uv2, uv_c: uv3, uv_d: uv4, col); |
1729 | |
1730 | if (push_texture_id) |
1731 | PopTextureID(); |
1732 | } |
1733 | |
1734 | 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) |
1735 | { |
1736 | if ((col & IM_COL32_A_MASK) == 0) |
1737 | return; |
1738 | |
1739 | flags = FixRectCornerFlags(flags); |
1740 | if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) |
1741 | { |
1742 | AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col); |
1743 | return; |
1744 | } |
1745 | |
1746 | const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; |
1747 | if (push_texture_id) |
1748 | PushTextureID(texture_id: user_texture_id); |
1749 | |
1750 | int vert_start_idx = VtxBuffer.Size; |
1751 | PathRect(a: p_min, b: p_max, rounding, flags); |
1752 | PathFillConvex(col); |
1753 | int vert_end_idx = VtxBuffer.Size; |
1754 | ImGui::ShadeVertsLinearUV(draw_list: this, vert_start_idx, vert_end_idx, a: p_min, b: p_max, uv_a: uv_min, uv_b: uv_max, clamp: true); |
1755 | |
1756 | if (push_texture_id) |
1757 | PopTextureID(); |
1758 | } |
1759 | |
1760 | //----------------------------------------------------------------------------- |
1761 | // [SECTION] ImTriangulator, ImDrawList concave polygon fill |
1762 | //----------------------------------------------------------------------------- |
1763 | // Triangulate concave polygons. Based on "Triangulation by Ear Clipping" paper, O(N^2) complexity. |
1764 | // Reference: https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf |
1765 | // Provided as a convenience for user but not used by main library. |
1766 | //----------------------------------------------------------------------------- |
1767 | // - ImTriangulator [Internal] |
1768 | // - AddConcavePolyFilled() |
1769 | //----------------------------------------------------------------------------- |
1770 | |
1771 | enum ImTriangulatorNodeType |
1772 | { |
1773 | ImTriangulatorNodeType_Convex, |
1774 | ImTriangulatorNodeType_Ear, |
1775 | ImTriangulatorNodeType_Reflex |
1776 | }; |
1777 | |
1778 | struct ImTriangulatorNode |
1779 | { |
1780 | ImTriangulatorNodeType Type; |
1781 | int Index; |
1782 | ImVec2 Pos; |
1783 | ImTriangulatorNode* Next; |
1784 | ImTriangulatorNode* Prev; |
1785 | |
1786 | void Unlink() { Next->Prev = Prev; Prev->Next = Next; } |
1787 | }; |
1788 | |
1789 | struct ImTriangulatorNodeSpan |
1790 | { |
1791 | ImTriangulatorNode** Data = NULL; |
1792 | int Size = 0; |
1793 | |
1794 | void push_back(ImTriangulatorNode* node) { Data[Size++] = node; } |
1795 | void find_erase_unsorted(int idx) { for (int i = Size - 1; i >= 0; i--) if (Data[i]->Index == idx) { Data[i] = Data[Size - 1]; Size--; return; } } |
1796 | }; |
1797 | |
1798 | struct ImTriangulator |
1799 | { |
1800 | static int EstimateTriangleCount(int points_count) { return (points_count < 3) ? 0 : points_count - 2; } |
1801 | static int EstimateScratchBufferSize(int points_count) { return sizeof(ImTriangulatorNode) * points_count + sizeof(ImTriangulatorNode*) * points_count * 2; } |
1802 | |
1803 | void Init(const ImVec2* points, int points_count, void* scratch_buffer); |
1804 | void GetNextTriangle(unsigned int out_triangle[3]); // Return relative indexes for next triangle |
1805 | |
1806 | // Internal functions |
1807 | void BuildNodes(const ImVec2* points, int points_count); |
1808 | void BuildReflexes(); |
1809 | void BuildEars(); |
1810 | void FlipNodeList(); |
1811 | bool IsEar(int i0, int i1, int i2, const ImVec2& v0, const ImVec2& v1, const ImVec2& v2) const; |
1812 | void ReclassifyNode(ImTriangulatorNode* node); |
1813 | |
1814 | // Internal members |
1815 | int _TrianglesLeft = 0; |
1816 | ImTriangulatorNode* _Nodes = NULL; |
1817 | ImTriangulatorNodeSpan _Ears; |
1818 | ImTriangulatorNodeSpan _Reflexes; |
1819 | }; |
1820 | |
1821 | // Distribute storage for nodes, ears and reflexes. |
1822 | // FIXME-OPT: if everything is convex, we could report it to caller and let it switch to an convex renderer |
1823 | // (this would require first building reflexes to bail to convex if empty, without even building nodes) |
1824 | void ImTriangulator::Init(const ImVec2* points, int points_count, void* scratch_buffer) |
1825 | { |
1826 | IM_ASSERT(scratch_buffer != NULL && points_count >= 3); |
1827 | _TrianglesLeft = EstimateTriangleCount(points_count); |
1828 | _Nodes = (ImTriangulatorNode*)scratch_buffer; // points_count x Node |
1829 | _Ears.Data = (ImTriangulatorNode**)(_Nodes + points_count); // points_count x Node* |
1830 | _Reflexes.Data = (ImTriangulatorNode**)(_Nodes + points_count) + points_count; // points_count x Node* |
1831 | BuildNodes(points, points_count); |
1832 | BuildReflexes(); |
1833 | BuildEars(); |
1834 | } |
1835 | |
1836 | void ImTriangulator::BuildNodes(const ImVec2* points, int points_count) |
1837 | { |
1838 | for (int i = 0; i < points_count; i++) |
1839 | { |
1840 | _Nodes[i].Type = ImTriangulatorNodeType_Convex; |
1841 | _Nodes[i].Index = i; |
1842 | _Nodes[i].Pos = points[i]; |
1843 | _Nodes[i].Next = _Nodes + i + 1; |
1844 | _Nodes[i].Prev = _Nodes + i - 1; |
1845 | } |
1846 | _Nodes[0].Prev = _Nodes + points_count - 1; |
1847 | _Nodes[points_count - 1].Next = _Nodes; |
1848 | } |
1849 | |
1850 | void ImTriangulator::BuildReflexes() |
1851 | { |
1852 | ImTriangulatorNode* n1 = _Nodes; |
1853 | for (int i = _TrianglesLeft; i >= 0; i--, n1 = n1->Next) |
1854 | { |
1855 | if (ImTriangleIsClockwise(a: n1->Prev->Pos, b: n1->Pos, c: n1->Next->Pos)) |
1856 | continue; |
1857 | n1->Type = ImTriangulatorNodeType_Reflex; |
1858 | _Reflexes.push_back(node: n1); |
1859 | } |
1860 | } |
1861 | |
1862 | void ImTriangulator::BuildEars() |
1863 | { |
1864 | ImTriangulatorNode* n1 = _Nodes; |
1865 | for (int i = _TrianglesLeft; i >= 0; i--, n1 = n1->Next) |
1866 | { |
1867 | if (n1->Type != ImTriangulatorNodeType_Convex) |
1868 | continue; |
1869 | if (!IsEar(i0: n1->Prev->Index, i1: n1->Index, i2: n1->Next->Index, v0: n1->Prev->Pos, v1: n1->Pos, v2: n1->Next->Pos)) |
1870 | continue; |
1871 | n1->Type = ImTriangulatorNodeType_Ear; |
1872 | _Ears.push_back(node: n1); |
1873 | } |
1874 | } |
1875 | |
1876 | void ImTriangulator::GetNextTriangle(unsigned int out_triangle[3]) |
1877 | { |
1878 | if (_Ears.Size == 0) |
1879 | { |
1880 | FlipNodeList(); |
1881 | |
1882 | ImTriangulatorNode* node = _Nodes; |
1883 | for (int i = _TrianglesLeft; i >= 0; i--, node = node->Next) |
1884 | node->Type = ImTriangulatorNodeType_Convex; |
1885 | _Reflexes.Size = 0; |
1886 | BuildReflexes(); |
1887 | BuildEars(); |
1888 | |
1889 | // If we still don't have ears, it means geometry is degenerated. |
1890 | if (_Ears.Size == 0) |
1891 | { |
1892 | // Return first triangle available, mimicking the behavior of convex fill. |
1893 | IM_ASSERT(_TrianglesLeft > 0); // Geometry is degenerated |
1894 | _Ears.Data[0] = _Nodes; |
1895 | _Ears.Size = 1; |
1896 | } |
1897 | } |
1898 | |
1899 | ImTriangulatorNode* ear = _Ears.Data[--_Ears.Size]; |
1900 | out_triangle[0] = ear->Prev->Index; |
1901 | out_triangle[1] = ear->Index; |
1902 | out_triangle[2] = ear->Next->Index; |
1903 | |
1904 | ear->Unlink(); |
1905 | if (ear == _Nodes) |
1906 | _Nodes = ear->Next; |
1907 | |
1908 | ReclassifyNode(node: ear->Prev); |
1909 | ReclassifyNode(node: ear->Next); |
1910 | _TrianglesLeft--; |
1911 | } |
1912 | |
1913 | void ImTriangulator::FlipNodeList() |
1914 | { |
1915 | ImTriangulatorNode* prev = _Nodes; |
1916 | ImTriangulatorNode* temp = _Nodes; |
1917 | ImTriangulatorNode* current = _Nodes->Next; |
1918 | prev->Next = prev; |
1919 | prev->Prev = prev; |
1920 | while (current != _Nodes) |
1921 | { |
1922 | temp = current->Next; |
1923 | |
1924 | current->Next = prev; |
1925 | prev->Prev = current; |
1926 | _Nodes->Next = current; |
1927 | current->Prev = _Nodes; |
1928 | |
1929 | prev = current; |
1930 | current = temp; |
1931 | } |
1932 | _Nodes = prev; |
1933 | } |
1934 | |
1935 | // A triangle is an ear is no other vertex is inside it. We can test reflexes vertices only (see reference algorithm) |
1936 | bool ImTriangulator::IsEar(int i0, int i1, int i2, const ImVec2& v0, const ImVec2& v1, const ImVec2& v2) const |
1937 | { |
1938 | ImTriangulatorNode** p_end = _Reflexes.Data + _Reflexes.Size; |
1939 | for (ImTriangulatorNode** p = _Reflexes.Data; p < p_end; p++) |
1940 | { |
1941 | ImTriangulatorNode* reflex = *p; |
1942 | if (reflex->Index != i0 && reflex->Index != i1 && reflex->Index != i2) |
1943 | if (ImTriangleContainsPoint(a: v0, b: v1, c: v2, p: reflex->Pos)) |
1944 | return false; |
1945 | } |
1946 | return true; |
1947 | } |
1948 | |
1949 | void ImTriangulator::ReclassifyNode(ImTriangulatorNode* n1) |
1950 | { |
1951 | // Classify node |
1952 | ImTriangulatorNodeType type; |
1953 | const ImTriangulatorNode* n0 = n1->Prev; |
1954 | const ImTriangulatorNode* n2 = n1->Next; |
1955 | if (!ImTriangleIsClockwise(a: n0->Pos, b: n1->Pos, c: n2->Pos)) |
1956 | type = ImTriangulatorNodeType_Reflex; |
1957 | else if (IsEar(i0: n0->Index, i1: n1->Index, i2: n2->Index, v0: n0->Pos, v1: n1->Pos, v2: n2->Pos)) |
1958 | type = ImTriangulatorNodeType_Ear; |
1959 | else |
1960 | type = ImTriangulatorNodeType_Convex; |
1961 | |
1962 | // Update lists when a type changes |
1963 | if (type == n1->Type) |
1964 | return; |
1965 | if (n1->Type == ImTriangulatorNodeType_Reflex) |
1966 | _Reflexes.find_erase_unsorted(idx: n1->Index); |
1967 | else if (n1->Type == ImTriangulatorNodeType_Ear) |
1968 | _Ears.find_erase_unsorted(idx: n1->Index); |
1969 | if (type == ImTriangulatorNodeType_Reflex) |
1970 | _Reflexes.push_back(node: n1); |
1971 | else if (type == ImTriangulatorNodeType_Ear) |
1972 | _Ears.push_back(node: n1); |
1973 | n1->Type = type; |
1974 | } |
1975 | |
1976 | // Use ear-clipping algorithm to triangulate a simple polygon (no self-interaction, no holes). |
1977 | // (Reminder: we don't perform any coarse clipping/culling in ImDrawList layer! |
1978 | // It is up to caller to ensure not making costly calls that will be outside of visible area. |
1979 | // As concave fill is noticeably more expensive than other primitives, be mindful of this... |
1980 | // Caller can build AABB of points, and avoid filling if 'draw_list->_CmdHeader.ClipRect.Overlays(points_bb) == false') |
1981 | void ImDrawList::AddConcavePolyFilled(const ImVec2* points, const int points_count, ImU32 col) |
1982 | { |
1983 | if (points_count < 3 || (col & IM_COL32_A_MASK) == 0) |
1984 | return; |
1985 | |
1986 | const ImVec2 uv = _Data->TexUvWhitePixel; |
1987 | ImTriangulator triangulator; |
1988 | unsigned int triangle[3]; |
1989 | if (Flags & ImDrawListFlags_AntiAliasedFill) |
1990 | { |
1991 | // Anti-aliased Fill |
1992 | const float AA_SIZE = _FringeScale; |
1993 | const ImU32 col_trans = col & ~IM_COL32_A_MASK; |
1994 | const int idx_count = (points_count - 2) * 3 + points_count * 6; |
1995 | const int vtx_count = (points_count * 2); |
1996 | PrimReserve(idx_count, vtx_count); |
1997 | |
1998 | // Add indexes for fill |
1999 | unsigned int vtx_inner_idx = _VtxCurrentIdx; |
2000 | unsigned int vtx_outer_idx = _VtxCurrentIdx + 1; |
2001 | |
2002 | _Data->TempBuffer.reserve_discard(new_capacity: (ImTriangulator::EstimateScratchBufferSize(points_count) + sizeof(ImVec2)) / sizeof(ImVec2)); |
2003 | triangulator.Init(points, points_count, scratch_buffer: _Data->TempBuffer.Data); |
2004 | while (triangulator._TrianglesLeft > 0) |
2005 | { |
2006 | triangulator.GetNextTriangle(out_triangle: triangle); |
2007 | _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (triangle[0] << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (triangle[1] << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx + (triangle[2] << 1)); |
2008 | _IdxWritePtr += 3; |
2009 | } |
2010 | |
2011 | // Compute normals |
2012 | _Data->TempBuffer.reserve_discard(new_capacity: points_count); |
2013 | ImVec2* temp_normals = _Data->TempBuffer.Data; |
2014 | for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) |
2015 | { |
2016 | const ImVec2& p0 = points[i0]; |
2017 | const ImVec2& p1 = points[i1]; |
2018 | float dx = p1.x - p0.x; |
2019 | float dy = p1.y - p0.y; |
2020 | IM_NORMALIZE2F_OVER_ZERO(dx, dy); |
2021 | temp_normals[i0].x = dy; |
2022 | temp_normals[i0].y = -dx; |
2023 | } |
2024 | |
2025 | for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) |
2026 | { |
2027 | // Average normals |
2028 | const ImVec2& n0 = temp_normals[i0]; |
2029 | const ImVec2& n1 = temp_normals[i1]; |
2030 | float dm_x = (n0.x + n1.x) * 0.5f; |
2031 | float dm_y = (n0.y + n1.y) * 0.5f; |
2032 | IM_FIXNORMAL2F(dm_x, dm_y); |
2033 | dm_x *= AA_SIZE * 0.5f; |
2034 | dm_y *= AA_SIZE * 0.5f; |
2035 | |
2036 | // Add vertices |
2037 | _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 |
2038 | _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 |
2039 | _VtxWritePtr += 2; |
2040 | |
2041 | // Add indexes for fringes |
2042 | _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (i0 << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); |
2043 | _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx + (i1 << 1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); |
2044 | _IdxWritePtr += 6; |
2045 | } |
2046 | _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
2047 | } |
2048 | else |
2049 | { |
2050 | // Non Anti-aliased Fill |
2051 | const int idx_count = (points_count - 2) * 3; |
2052 | const int vtx_count = points_count; |
2053 | PrimReserve(idx_count, vtx_count); |
2054 | for (int i = 0; i < vtx_count; i++) |
2055 | { |
2056 | _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; |
2057 | _VtxWritePtr++; |
2058 | } |
2059 | _Data->TempBuffer.reserve_discard(new_capacity: (ImTriangulator::EstimateScratchBufferSize(points_count) + sizeof(ImVec2)) / sizeof(ImVec2)); |
2060 | triangulator.Init(points, points_count, scratch_buffer: _Data->TempBuffer.Data); |
2061 | while (triangulator._TrianglesLeft > 0) |
2062 | { |
2063 | triangulator.GetNextTriangle(out_triangle: triangle); |
2064 | _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx + triangle[0]); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + triangle[1]); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + triangle[2]); |
2065 | _IdxWritePtr += 3; |
2066 | } |
2067 | _VtxCurrentIdx += (ImDrawIdx)vtx_count; |
2068 | } |
2069 | } |
2070 | |
2071 | //----------------------------------------------------------------------------- |
2072 | // [SECTION] ImDrawListSplitter |
2073 | //----------------------------------------------------------------------------- |
2074 | // FIXME: This may be a little confusing, trying to be a little too low-level/optimal instead of just doing vector swap.. |
2075 | //----------------------------------------------------------------------------- |
2076 | |
2077 | void ImDrawListSplitter::ClearFreeMemory() |
2078 | { |
2079 | for (int i = 0; i < _Channels.Size; i++) |
2080 | { |
2081 | if (i == _Current) |
2082 | memset(s: &_Channels[i], c: 0, n: sizeof(_Channels[i])); // Current channel is a copy of CmdBuffer/IdxBuffer, don't destruct again |
2083 | _Channels[i]._CmdBuffer.clear(); |
2084 | _Channels[i]._IdxBuffer.clear(); |
2085 | } |
2086 | _Current = 0; |
2087 | _Count = 1; |
2088 | _Channels.clear(); |
2089 | } |
2090 | |
2091 | void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) |
2092 | { |
2093 | IM_UNUSED(draw_list); |
2094 | IM_ASSERT(_Current == 0 && _Count <= 1 && "Nested channel splitting is not supported. Please use separate instances of ImDrawListSplitter." ); |
2095 | int old_channels_count = _Channels.Size; |
2096 | if (old_channels_count < channels_count) |
2097 | { |
2098 | _Channels.reserve(new_capacity: channels_count); // Avoid over reserving since this is likely to stay stable |
2099 | _Channels.resize(new_size: channels_count); |
2100 | } |
2101 | _Count = channels_count; |
2102 | |
2103 | // Channels[] (24/32 bytes each) hold storage that we'll swap with draw_list->_CmdBuffer/_IdxBuffer |
2104 | // 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. |
2105 | // 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 |
2106 | memset(s: &_Channels[0], c: 0, n: sizeof(ImDrawChannel)); |
2107 | for (int i = 1; i < channels_count; i++) |
2108 | { |
2109 | if (i >= old_channels_count) |
2110 | { |
2111 | IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel(); |
2112 | } |
2113 | else |
2114 | { |
2115 | _Channels[i]._CmdBuffer.resize(new_size: 0); |
2116 | _Channels[i]._IdxBuffer.resize(new_size: 0); |
2117 | } |
2118 | } |
2119 | } |
2120 | |
2121 | void ImDrawListSplitter::Merge(ImDrawList* draw_list) |
2122 | { |
2123 | // 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. |
2124 | if (_Count <= 1) |
2125 | return; |
2126 | |
2127 | SetCurrentChannel(draw_list, channel_idx: 0); |
2128 | draw_list->_PopUnusedDrawCmd(); |
2129 | |
2130 | // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command. |
2131 | int new_cmd_buffer_count = 0; |
2132 | int new_idx_buffer_count = 0; |
2133 | ImDrawCmd* last_cmd = (_Count > 0 && draw_list->CmdBuffer.Size > 0) ? &draw_list->CmdBuffer.back() : NULL; |
2134 | int idx_offset = last_cmd ? last_cmd->IdxOffset + last_cmd->ElemCount : 0; |
2135 | for (int i = 1; i < _Count; i++) |
2136 | { |
2137 | ImDrawChannel& ch = _Channels[i]; |
2138 | if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0 && ch._CmdBuffer.back().UserCallback == NULL) // Equivalent of PopUnusedDrawCmd() |
2139 | ch._CmdBuffer.pop_back(); |
2140 | |
2141 | if (ch._CmdBuffer.Size > 0 && last_cmd != NULL) |
2142 | { |
2143 | // Do not include ImDrawCmd_AreSequentialIdxOffset() in the compare as we rebuild IdxOffset values ourselves. |
2144 | // Manipulating IdxOffset (e.g. by reordering draw commands like done by RenderDimmedBackgroundBehindWindow()) is not supported within a splitter. |
2145 | ImDrawCmd* next_cmd = &ch._CmdBuffer[0]; |
2146 | if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL) |
2147 | { |
2148 | // Merge previous channel last draw command with current channel first draw command if matching. |
2149 | last_cmd->ElemCount += next_cmd->ElemCount; |
2150 | idx_offset += next_cmd->ElemCount; |
2151 | ch._CmdBuffer.erase(it: ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges. |
2152 | } |
2153 | } |
2154 | if (ch._CmdBuffer.Size > 0) |
2155 | last_cmd = &ch._CmdBuffer.back(); |
2156 | new_cmd_buffer_count += ch._CmdBuffer.Size; |
2157 | new_idx_buffer_count += ch._IdxBuffer.Size; |
2158 | for (int cmd_n = 0; cmd_n < ch._CmdBuffer.Size; cmd_n++) |
2159 | { |
2160 | ch._CmdBuffer.Data[cmd_n].IdxOffset = idx_offset; |
2161 | idx_offset += ch._CmdBuffer.Data[cmd_n].ElemCount; |
2162 | } |
2163 | } |
2164 | draw_list->CmdBuffer.resize(new_size: draw_list->CmdBuffer.Size + new_cmd_buffer_count); |
2165 | draw_list->IdxBuffer.resize(new_size: draw_list->IdxBuffer.Size + new_idx_buffer_count); |
2166 | |
2167 | // Write commands and indices in order (they are fairly small structures, we don't copy vertices only indices) |
2168 | ImDrawCmd* cmd_write = draw_list->CmdBuffer.Data + draw_list->CmdBuffer.Size - new_cmd_buffer_count; |
2169 | ImDrawIdx* idx_write = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size - new_idx_buffer_count; |
2170 | for (int i = 1; i < _Count; i++) |
2171 | { |
2172 | ImDrawChannel& ch = _Channels[i]; |
2173 | if (int sz = ch._CmdBuffer.Size) { memcpy(dest: cmd_write, src: ch._CmdBuffer.Data, n: sz * sizeof(ImDrawCmd)); cmd_write += sz; } |
2174 | if (int sz = ch._IdxBuffer.Size) { memcpy(dest: idx_write, src: ch._IdxBuffer.Data, n: sz * sizeof(ImDrawIdx)); idx_write += sz; } |
2175 | } |
2176 | draw_list->_IdxWritePtr = idx_write; |
2177 | |
2178 | // Ensure there's always a non-callback draw command trailing the command-buffer |
2179 | if (draw_list->CmdBuffer.Size == 0 || draw_list->CmdBuffer.back().UserCallback != NULL) |
2180 | draw_list->AddDrawCmd(); |
2181 | |
2182 | // If current command is used with different settings we need to add a new command |
2183 | ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; |
2184 | if (curr_cmd->ElemCount == 0) |
2185 | ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset |
2186 | else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) |
2187 | draw_list->AddDrawCmd(); |
2188 | |
2189 | _Count = 1; |
2190 | } |
2191 | |
2192 | void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) |
2193 | { |
2194 | IM_ASSERT(idx >= 0 && idx < _Count); |
2195 | if (_Current == idx) |
2196 | return; |
2197 | |
2198 | // Overwrite ImVector (12/16 bytes), four times. This is merely a silly optimization instead of doing .swap() |
2199 | memcpy(dest: &_Channels.Data[_Current]._CmdBuffer, src: &draw_list->CmdBuffer, n: sizeof(draw_list->CmdBuffer)); |
2200 | memcpy(dest: &_Channels.Data[_Current]._IdxBuffer, src: &draw_list->IdxBuffer, n: sizeof(draw_list->IdxBuffer)); |
2201 | _Current = idx; |
2202 | memcpy(dest: &draw_list->CmdBuffer, src: &_Channels.Data[idx]._CmdBuffer, n: sizeof(draw_list->CmdBuffer)); |
2203 | memcpy(dest: &draw_list->IdxBuffer, src: &_Channels.Data[idx]._IdxBuffer, n: sizeof(draw_list->IdxBuffer)); |
2204 | draw_list->_IdxWritePtr = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size; |
2205 | |
2206 | // If current command is used with different settings we need to add a new command |
2207 | ImDrawCmd* curr_cmd = (draw_list->CmdBuffer.Size == 0) ? NULL : &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; |
2208 | if (curr_cmd == NULL) |
2209 | draw_list->AddDrawCmd(); |
2210 | else if (curr_cmd->ElemCount == 0) |
2211 | ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset |
2212 | else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) |
2213 | draw_list->AddDrawCmd(); |
2214 | } |
2215 | |
2216 | //----------------------------------------------------------------------------- |
2217 | // [SECTION] ImDrawData |
2218 | //----------------------------------------------------------------------------- |
2219 | |
2220 | void ImDrawData::Clear() |
2221 | { |
2222 | Valid = false; |
2223 | CmdListsCount = TotalIdxCount = TotalVtxCount = 0; |
2224 | CmdLists.resize(new_size: 0); // The ImDrawList are NOT owned by ImDrawData but e.g. by ImGuiContext, so we don't clear them. |
2225 | DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.0f, 0.0f); |
2226 | OwnerViewport = NULL; |
2227 | } |
2228 | |
2229 | // Important: 'out_list' is generally going to be draw_data->CmdLists, but may be another temporary list |
2230 | // as long at it is expected that the result will be later merged into draw_data->CmdLists[]. |
2231 | void ImGui::AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector<ImDrawList*>* out_list, ImDrawList* draw_list) |
2232 | { |
2233 | if (draw_list->CmdBuffer.Size == 0) |
2234 | return; |
2235 | if (draw_list->CmdBuffer.Size == 1 && draw_list->CmdBuffer[0].ElemCount == 0 && draw_list->CmdBuffer[0].UserCallback == NULL) |
2236 | return; |
2237 | |
2238 | // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. |
2239 | // May trigger for you if you are using PrimXXX functions incorrectly. |
2240 | IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); |
2241 | IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size); |
2242 | if (!(draw_list->Flags & ImDrawListFlags_AllowVtxOffset)) |
2243 | IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size); |
2244 | |
2245 | // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window) |
2246 | // If this assert triggers because you are drawing lots of stuff manually: |
2247 | // - First, make sure you are coarse clipping yourself and not trying to draw many things outside visible bounds. |
2248 | // Be mindful that the lower-level ImDrawList API doesn't filter vertices. Use the Metrics/Debugger window to inspect draw list contents. |
2249 | // - If you want large meshes with more than 64K vertices, you can either: |
2250 | // (A) Handle the ImDrawCmd::VtxOffset value in your renderer backend, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. |
2251 | // Most example backends already support this from 1.71. Pre-1.71 backends won't. |
2252 | // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them. |
2253 | // (B) Or handle 32-bit indices in your renderer backend, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. |
2254 | // Most example backends already support this. For example, the OpenGL example code detect index size at compile-time: |
2255 | // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); |
2256 | // Your own engine or render API may use different parameters or function calls to specify index sizes. |
2257 | // 2 and 4 bytes indices are generally supported by most graphics API. |
2258 | // - If for some reason neither of those solutions works for you, a workaround is to call BeginChild()/EndChild() before reaching |
2259 | // the 64K limit to split your draw commands in multiple draw lists. |
2260 | if (sizeof(ImDrawIdx) == 2) |
2261 | IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above" ); |
2262 | |
2263 | // Resolve callback data pointers |
2264 | if (draw_list->_CallbacksDataBuf.Size > 0) |
2265 | for (ImDrawCmd& cmd : draw_list->CmdBuffer) |
2266 | if (cmd.UserCallback != NULL && cmd.UserCallbackDataOffset != -1 && cmd.UserCallbackDataSize > 0) |
2267 | cmd.UserCallbackData = draw_list->_CallbacksDataBuf.Data + cmd.UserCallbackDataOffset; |
2268 | |
2269 | // Add to output list + records state in ImDrawData |
2270 | out_list->push_back(v: draw_list); |
2271 | draw_data->CmdListsCount++; |
2272 | draw_data->TotalVtxCount += draw_list->VtxBuffer.Size; |
2273 | draw_data->TotalIdxCount += draw_list->IdxBuffer.Size; |
2274 | } |
2275 | |
2276 | void ImDrawData::AddDrawList(ImDrawList* draw_list) |
2277 | { |
2278 | IM_ASSERT(CmdLists.Size == CmdListsCount); |
2279 | draw_list->_PopUnusedDrawCmd(); |
2280 | ImGui::AddDrawListToDrawDataEx(draw_data: this, out_list: &CmdLists, draw_list); |
2281 | } |
2282 | |
2283 | // 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! |
2284 | void ImDrawData::DeIndexAllBuffers() |
2285 | { |
2286 | ImVector<ImDrawVert> new_vtx_buffer; |
2287 | TotalVtxCount = TotalIdxCount = 0; |
2288 | for (int i = 0; i < CmdListsCount; i++) |
2289 | { |
2290 | ImDrawList* cmd_list = CmdLists[i]; |
2291 | if (cmd_list->IdxBuffer.empty()) |
2292 | continue; |
2293 | new_vtx_buffer.resize(new_size: cmd_list->IdxBuffer.Size); |
2294 | for (int j = 0; j < cmd_list->IdxBuffer.Size; j++) |
2295 | new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]]; |
2296 | cmd_list->VtxBuffer.swap(rhs&: new_vtx_buffer); |
2297 | cmd_list->IdxBuffer.resize(new_size: 0); |
2298 | TotalVtxCount += cmd_list->VtxBuffer.Size; |
2299 | } |
2300 | } |
2301 | |
2302 | // Helper to scale the ClipRect field of each ImDrawCmd. |
2303 | // Use if your final output buffer is at a different scale than draw_data->DisplaySize, |
2304 | // or if there is a difference between your window resolution and framebuffer resolution. |
2305 | void ImDrawData::ScaleClipRects(const ImVec2& fb_scale) |
2306 | { |
2307 | for (ImDrawList* draw_list : CmdLists) |
2308 | for (ImDrawCmd& cmd : draw_list->CmdBuffer) |
2309 | 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); |
2310 | } |
2311 | |
2312 | //----------------------------------------------------------------------------- |
2313 | // [SECTION] Helpers ShadeVertsXXX functions |
2314 | //----------------------------------------------------------------------------- |
2315 | |
2316 | // Generic linear color gradient, write to RGB fields, leave A untouched. |
2317 | void ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1) |
2318 | { |
2319 | ImVec2 gradient_extent = gradient_p1 - gradient_p0; |
2320 | float gradient_inv_length2 = 1.0f / ImLengthSqr(lhs: gradient_extent); |
2321 | ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; |
2322 | ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; |
2323 | const int col0_r = (int)(col0 >> IM_COL32_R_SHIFT) & 0xFF; |
2324 | const int col0_g = (int)(col0 >> IM_COL32_G_SHIFT) & 0xFF; |
2325 | const int col0_b = (int)(col0 >> IM_COL32_B_SHIFT) & 0xFF; |
2326 | const int col_delta_r = ((int)(col1 >> IM_COL32_R_SHIFT) & 0xFF) - col0_r; |
2327 | const int col_delta_g = ((int)(col1 >> IM_COL32_G_SHIFT) & 0xFF) - col0_g; |
2328 | const int col_delta_b = ((int)(col1 >> IM_COL32_B_SHIFT) & 0xFF) - col0_b; |
2329 | for (ImDrawVert* vert = vert_start; vert < vert_end; vert++) |
2330 | { |
2331 | float d = ImDot(a: vert->pos - gradient_p0, b: gradient_extent); |
2332 | float t = ImClamp(v: d * gradient_inv_length2, mn: 0.0f, mx: 1.0f); |
2333 | int r = (int)(col0_r + col_delta_r * t); |
2334 | int g = (int)(col0_g + col_delta_g * t); |
2335 | int b = (int)(col0_b + col_delta_b * t); |
2336 | vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK); |
2337 | } |
2338 | } |
2339 | |
2340 | // Distribute UV over (a, b) rectangle |
2341 | 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) |
2342 | { |
2343 | const ImVec2 size = b - a; |
2344 | const ImVec2 uv_size = uv_b - uv_a; |
2345 | const ImVec2 scale = ImVec2( |
2346 | size.x != 0.0f ? (uv_size.x / size.x) : 0.0f, |
2347 | size.y != 0.0f ? (uv_size.y / size.y) : 0.0f); |
2348 | |
2349 | ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; |
2350 | ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; |
2351 | if (clamp) |
2352 | { |
2353 | const ImVec2 min = ImMin(lhs: uv_a, rhs: uv_b); |
2354 | const ImVec2 max = ImMax(lhs: uv_a, rhs: uv_b); |
2355 | for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) |
2356 | vertex->uv = ImClamp(v: uv_a + ImMul(lhs: ImVec2(vertex->pos.x, vertex->pos.y) - a, rhs: scale), mn: min, mx: max); |
2357 | } |
2358 | else |
2359 | { |
2360 | for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) |
2361 | vertex->uv = uv_a + ImMul(lhs: ImVec2(vertex->pos.x, vertex->pos.y) - a, rhs: scale); |
2362 | } |
2363 | } |
2364 | |
2365 | void ImGui::ShadeVertsTransformPos(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& pivot_in, float cos_a, float sin_a, const ImVec2& pivot_out) |
2366 | { |
2367 | ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; |
2368 | ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; |
2369 | for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) |
2370 | vertex->pos = ImRotate(v: vertex->pos- pivot_in, cos_a, sin_a) + pivot_out; |
2371 | } |
2372 | |
2373 | //----------------------------------------------------------------------------- |
2374 | // [SECTION] ImFontConfig |
2375 | //----------------------------------------------------------------------------- |
2376 | |
2377 | ImFontConfig::ImFontConfig() |
2378 | { |
2379 | memset(s: this, c: 0, n: sizeof(*this)); |
2380 | FontDataOwnedByAtlas = true; |
2381 | OversampleH = 2; |
2382 | OversampleV = 1; |
2383 | GlyphMaxAdvanceX = FLT_MAX; |
2384 | RasterizerMultiply = 1.0f; |
2385 | RasterizerDensity = 1.0f; |
2386 | EllipsisChar = (ImWchar)-1; |
2387 | } |
2388 | |
2389 | //----------------------------------------------------------------------------- |
2390 | // [SECTION] ImFontAtlas |
2391 | //----------------------------------------------------------------------------- |
2392 | |
2393 | // A work of art lies ahead! (. = white layer, X = black layer, others are blank) |
2394 | // The 2x2 white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. |
2395 | // (This is used when io.MouseDrawCursor = true) |
2396 | const int FONT_ATLAS_DEFAULT_TEX_DATA_W = 122; // Actual texture will be 2 times that + 1 spacing. |
2397 | const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; |
2398 | static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = |
2399 | { |
2400 | "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX - XX XX " |
2401 | "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X -X..X X..X" |
2402 | "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X -X...X X...X" |
2403 | "X - X.X - X.....X - X.....X -X...X - X...X- X..X - X...X X...X " |
2404 | "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X - X...X...X " |
2405 | "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX - X.....X " |
2406 | "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX - X...X " |
2407 | "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX - X.X " |
2408 | "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X - X...X " |
2409 | "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X- X.....X " |
2410 | "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X- X...X...X " |
2411 | "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X- X...X X...X " |
2412 | "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X-X...X X...X" |
2413 | "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X-X..X X..X" |
2414 | "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X- XX XX " |
2415 | "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X--------------" |
2416 | "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X - " |
2417 | "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X - " |
2418 | "X.X X..X - -X.......X- X.......X - XX XX - - X..........X - " |
2419 | "XX X..X - - X.....X - X.....X - X.X X.X - - X........X - " |
2420 | " X..X - - X...X - X...X - X..X X..X - - X........X - " |
2421 | " XX - - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX - " |
2422 | "------------- - X - X -X.....................X- ------------------- " |
2423 | " ----------------------------------- X...XXXXXXXXXXXXX...X - " |
2424 | " - X..X X..X - " |
2425 | " - X.X X.X - " |
2426 | " - XX XX - " |
2427 | }; |
2428 | |
2429 | static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] = |
2430 | { |
2431 | // Pos ........ Size ......... Offset ...... |
2432 | { ImVec2( 0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow |
2433 | { ImVec2(13,0), ImVec2( 7,16), ImVec2( 1, 8) }, // ImGuiMouseCursor_TextInput |
2434 | { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_ResizeAll |
2435 | { ImVec2(21,0), ImVec2( 9,23), ImVec2( 4,11) }, // ImGuiMouseCursor_ResizeNS |
2436 | { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 4) }, // ImGuiMouseCursor_ResizeEW |
2437 | { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW |
2438 | { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE |
2439 | { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand |
2440 | { ImVec2(109,0),ImVec2(13,15), ImVec2( 6, 7) }, // ImGuiMouseCursor_NotAllowed |
2441 | }; |
2442 | |
2443 | ImFontAtlas::ImFontAtlas() |
2444 | { |
2445 | memset(s: this, c: 0, n: sizeof(*this)); |
2446 | TexGlyphPadding = 1; |
2447 | PackIdMouseCursors = PackIdLines = -1; |
2448 | } |
2449 | |
2450 | ImFontAtlas::~ImFontAtlas() |
2451 | { |
2452 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2453 | Clear(); |
2454 | } |
2455 | |
2456 | void ImFontAtlas::ClearInputData() |
2457 | { |
2458 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2459 | for (ImFontConfig& font_cfg : ConfigData) |
2460 | if (font_cfg.FontData && font_cfg.FontDataOwnedByAtlas) |
2461 | { |
2462 | IM_FREE(font_cfg.FontData); |
2463 | font_cfg.FontData = NULL; |
2464 | } |
2465 | |
2466 | // When clearing this we lose access to the font name and other information used to build the font. |
2467 | for (ImFont* font : Fonts) |
2468 | if (font->ConfigData >= ConfigData.Data && font->ConfigData < ConfigData.Data + ConfigData.Size) |
2469 | { |
2470 | font->ConfigData = NULL; |
2471 | font->ConfigDataCount = 0; |
2472 | } |
2473 | ConfigData.clear(); |
2474 | CustomRects.clear(); |
2475 | PackIdMouseCursors = PackIdLines = -1; |
2476 | // Important: we leave TexReady untouched |
2477 | } |
2478 | |
2479 | void ImFontAtlas::ClearTexData() |
2480 | { |
2481 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2482 | if (TexPixelsAlpha8) |
2483 | IM_FREE(TexPixelsAlpha8); |
2484 | if (TexPixelsRGBA32) |
2485 | IM_FREE(TexPixelsRGBA32); |
2486 | TexPixelsAlpha8 = NULL; |
2487 | TexPixelsRGBA32 = NULL; |
2488 | TexPixelsUseColors = false; |
2489 | // Important: we leave TexReady untouched |
2490 | } |
2491 | |
2492 | void ImFontAtlas::ClearFonts() |
2493 | { |
2494 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2495 | Fonts.clear_delete(); |
2496 | TexReady = false; |
2497 | } |
2498 | |
2499 | void ImFontAtlas::Clear() |
2500 | { |
2501 | ClearInputData(); |
2502 | ClearTexData(); |
2503 | ClearFonts(); |
2504 | } |
2505 | |
2506 | void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) |
2507 | { |
2508 | // Build atlas on demand |
2509 | if (TexPixelsAlpha8 == NULL) |
2510 | Build(); |
2511 | |
2512 | *out_pixels = TexPixelsAlpha8; |
2513 | if (out_width) *out_width = TexWidth; |
2514 | if (out_height) *out_height = TexHeight; |
2515 | if (out_bytes_per_pixel) *out_bytes_per_pixel = 1; |
2516 | } |
2517 | |
2518 | void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) |
2519 | { |
2520 | // Convert to RGBA32 format on demand |
2521 | // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp |
2522 | if (!TexPixelsRGBA32) |
2523 | { |
2524 | unsigned char* pixels = NULL; |
2525 | GetTexDataAsAlpha8(out_pixels: &pixels, NULL, NULL); |
2526 | if (pixels) |
2527 | { |
2528 | TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4); |
2529 | const unsigned char* src = pixels; |
2530 | unsigned int* dst = TexPixelsRGBA32; |
2531 | for (int n = TexWidth * TexHeight; n > 0; n--) |
2532 | *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++)); |
2533 | } |
2534 | } |
2535 | |
2536 | *out_pixels = (unsigned char*)TexPixelsRGBA32; |
2537 | if (out_width) *out_width = TexWidth; |
2538 | if (out_height) *out_height = TexHeight; |
2539 | if (out_bytes_per_pixel) *out_bytes_per_pixel = 4; |
2540 | } |
2541 | |
2542 | ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) |
2543 | { |
2544 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2545 | IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0); |
2546 | IM_ASSERT(font_cfg->SizePixels > 0.0f && "Is ImFontConfig struct correctly initialized?" ); |
2547 | IM_ASSERT(font_cfg->OversampleH > 0 && font_cfg->OversampleV > 0 && "Is ImFontConfig struct correctly initialized?" ); |
2548 | IM_ASSERT(font_cfg->RasterizerDensity > 0.0f); |
2549 | |
2550 | // Create new font |
2551 | if (!font_cfg->MergeMode) |
2552 | Fonts.push_back(IM_NEW(ImFont)); |
2553 | else |
2554 | IM_ASSERT(Fonts.Size > 0 && "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. |
2555 | |
2556 | ConfigData.push_back(v: *font_cfg); |
2557 | ImFontConfig& new_font_cfg = ConfigData.back(); |
2558 | if (new_font_cfg.DstFont == NULL) |
2559 | new_font_cfg.DstFont = Fonts.back(); |
2560 | if (!new_font_cfg.FontDataOwnedByAtlas) |
2561 | { |
2562 | new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize); |
2563 | new_font_cfg.FontDataOwnedByAtlas = true; |
2564 | memcpy(dest: new_font_cfg.FontData, src: font_cfg->FontData, n: (size_t)new_font_cfg.FontDataSize); |
2565 | } |
2566 | |
2567 | // Round font size |
2568 | // - We started rounding in 1.90 WIP (18991) as our layout system currently doesn't support non-rounded font size well yet. |
2569 | // - Note that using io.FontGlobalScale or SetWindowFontScale(), with are legacy-ish, partially supported features, can still lead to unrounded sizes. |
2570 | // - We may support it better later and remove this rounding. |
2571 | new_font_cfg.SizePixels = ImTrunc(f: new_font_cfg.SizePixels); |
2572 | |
2573 | if (new_font_cfg.DstFont->EllipsisChar == (ImWchar)-1) |
2574 | new_font_cfg.DstFont->EllipsisChar = font_cfg->EllipsisChar; |
2575 | |
2576 | // Pointers to ConfigData and BuilderData are otherwise dangling |
2577 | ImFontAtlasUpdateConfigDataPointers(atlas: this); |
2578 | |
2579 | // Invalidate texture |
2580 | TexReady = false; |
2581 | ClearTexData(); |
2582 | return new_font_cfg.DstFont; |
2583 | } |
2584 | |
2585 | // Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder) |
2586 | static unsigned int stb_decompress_length(const unsigned char* input); |
2587 | static unsigned int stb_decompress(unsigned char* output, const unsigned char* input, unsigned int length); |
2588 | static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; } |
2589 | static void Decode85(const unsigned char* src, unsigned char* dst) |
2590 | { |
2591 | while (*src) |
2592 | { |
2593 | unsigned int tmp = Decode85Byte(c: src[0]) + 85 * (Decode85Byte(c: src[1]) + 85 * (Decode85Byte(c: src[2]) + 85 * (Decode85Byte(c: src[3]) + 85 * Decode85Byte(c: src[4])))); |
2594 | 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. |
2595 | src += 5; |
2596 | dst += 4; |
2597 | } |
2598 | } |
2599 | #ifndef IMGUI_DISABLE_DEFAULT_FONT |
2600 | static const char* GetDefaultCompressedFontDataTTF(int* out_size); |
2601 | #endif |
2602 | |
2603 | // Load embedded ProggyClean.ttf at size 13, disable oversampling |
2604 | ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template) |
2605 | { |
2606 | #ifndef IMGUI_DISABLE_DEFAULT_FONT |
2607 | ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
2608 | if (!font_cfg_template) |
2609 | { |
2610 | font_cfg.OversampleH = font_cfg.OversampleV = 1; |
2611 | font_cfg.PixelSnapH = true; |
2612 | } |
2613 | if (font_cfg.SizePixels <= 0.0f) |
2614 | font_cfg.SizePixels = 13.0f * 1.0f; |
2615 | if (font_cfg.Name[0] == '\0') |
2616 | ImFormatString(buf: font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), fmt: "ProggyClean.ttf, %dpx" , (int)font_cfg.SizePixels); |
2617 | font_cfg.EllipsisChar = (ImWchar)0x0085; |
2618 | font_cfg.GlyphOffset.y = 1.0f * IM_TRUNC(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units |
2619 | |
2620 | int ttf_compressed_size = 0; |
2621 | const char* ttf_compressed = GetDefaultCompressedFontDataTTF(out_size: &ttf_compressed_size); |
2622 | const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault(); |
2623 | ImFont* font = AddFontFromMemoryCompressedTTF(compressed_font_data: ttf_compressed, compressed_font_data_size: ttf_compressed_size, size_pixels: font_cfg.SizePixels, font_cfg: &font_cfg, glyph_ranges); |
2624 | return font; |
2625 | #else |
2626 | IM_ASSERT(0 && "AddFontDefault() disabled in this build." ); |
2627 | IM_UNUSED(font_cfg_template); |
2628 | return NULL; |
2629 | #endif // #ifndef IMGUI_DISABLE_DEFAULT_FONT |
2630 | } |
2631 | |
2632 | ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) |
2633 | { |
2634 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2635 | size_t data_size = 0; |
2636 | void* data = ImFileLoadToMemory(filename, mode: "rb" , out_file_size: &data_size, padding_bytes: 0); |
2637 | if (!data) |
2638 | { |
2639 | IM_ASSERT_USER_ERROR(0, "Could not load font file!" ); |
2640 | return NULL; |
2641 | } |
2642 | ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
2643 | if (font_cfg.Name[0] == '\0') |
2644 | { |
2645 | // Store a short copy of filename into into the font name for convenience |
2646 | const char* p; |
2647 | for (p = filename + strlen(s: filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {} |
2648 | ImFormatString(buf: font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), fmt: "%s, %.0fpx" , p, size_pixels); |
2649 | } |
2650 | return AddFontFromMemoryTTF(font_data: data, font_data_size: (int)data_size, size_pixels, font_cfg: &font_cfg, glyph_ranges); |
2651 | } |
2652 | |
2653 | // NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build(). |
2654 | ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) |
2655 | { |
2656 | IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!" ); |
2657 | ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
2658 | IM_ASSERT(font_cfg.FontData == NULL); |
2659 | IM_ASSERT(font_data_size > 100 && "Incorrect value for font_data_size!" ); // Heuristic to prevent accidentally passing a wrong value to font_data_size. |
2660 | font_cfg.FontData = font_data; |
2661 | font_cfg.FontDataSize = font_data_size; |
2662 | font_cfg.SizePixels = size_pixels > 0.0f ? size_pixels : font_cfg.SizePixels; |
2663 | if (glyph_ranges) |
2664 | font_cfg.GlyphRanges = glyph_ranges; |
2665 | return AddFont(font_cfg: &font_cfg); |
2666 | } |
2667 | |
2668 | ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) |
2669 | { |
2670 | const unsigned int buf_decompressed_size = stb_decompress_length(input: (const unsigned char*)compressed_ttf_data); |
2671 | unsigned char* buf_decompressed_data = (unsigned char*)IM_ALLOC(buf_decompressed_size); |
2672 | stb_decompress(output: buf_decompressed_data, input: (const unsigned char*)compressed_ttf_data, length: (unsigned int)compressed_ttf_size); |
2673 | |
2674 | ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); |
2675 | IM_ASSERT(font_cfg.FontData == NULL); |
2676 | font_cfg.FontDataOwnedByAtlas = true; |
2677 | return AddFontFromMemoryTTF(font_data: buf_decompressed_data, font_data_size: (int)buf_decompressed_size, size_pixels, font_cfg_template: &font_cfg, glyph_ranges); |
2678 | } |
2679 | |
2680 | ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) |
2681 | { |
2682 | int compressed_ttf_size = (((int)strlen(s: compressed_ttf_data_base85) + 4) / 5) * 4; |
2683 | void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size); |
2684 | Decode85(src: (const unsigned char*)compressed_ttf_data_base85, dst: (unsigned char*)compressed_ttf); |
2685 | ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf_data: compressed_ttf, compressed_ttf_size, size_pixels, font_cfg_template: font_cfg, glyph_ranges); |
2686 | IM_FREE(compressed_ttf); |
2687 | return font; |
2688 | } |
2689 | |
2690 | int ImFontAtlas::AddCustomRectRegular(int width, int height) |
2691 | { |
2692 | |
---|