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