-
Notifications
You must be signed in to change notification settings - Fork 2
/
imgui_user.cpp
590 lines (492 loc) · 23.6 KB
/
imgui_user.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#ifndef IMGUI_DISABLE
#ifndef DPRINTF
#ifdef WIN32
#include "debugapi.h"
#define DPRINTF( format, ...) do { char buffer[1024]; sprintf(buffer, format, __VA_ARGS__); OutputDebugStringA( buffer ); } while(0)
#else
#define DPRINTF( format,...)
#endif
#endif
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include "imgui_src/imgui.h"
#include "imgui_src/imgui_internal.h"
//=============== CODE BORROWED FROM IMGUI_WIDGETS.CPP
static const ImGuiDataTypeInfo GDataTypeInfo[] =
{
{ sizeof(char), "S8", "%d", "%d" }, // ImGuiDataType_S8
{ sizeof(unsigned char), "U8", "%u", "%u" },
{ sizeof(short), "S16", "%d", "%d" }, // ImGuiDataType_S16
{ sizeof(unsigned short), "U16", "%u", "%u" },
{ sizeof(int), "S32", "%d", "%d" }, // ImGuiDataType_S32
{ sizeof(unsigned int), "U32", "%u", "%u" },
#ifdef _MSC_VER
{ sizeof(ImS64), "S64", "%I64d","%I64d" }, // ImGuiDataType_S64
{ sizeof(ImU64), "U64", "%I64u","%I64u" },
#else
{ sizeof(ImS64), "S64", "%lld", "%lld" }, // ImGuiDataType_S64
{ sizeof(ImU64), "U64", "%llu", "%llu" },
#endif
{ sizeof(float), "float", "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg)
{ sizeof(double), "double","%f", "%lf" }, // ImGuiDataType_Double
};
// FIXME-LEGACY: Prior to 1.61 our DragInt() function internally used floats and because of this the compile-time default value for format was "%.0f".
// Even though we changed the compile-time default, we expect users to have carried %f around, which would break the display of DragInt() calls.
// To honor backward compatibility we are rewriting the format string, unless IMGUI_DISABLE_OBSOLETE_FUNCTIONS is enabled. What could possibly go wrong?!
static const char* PatchFormatStringFloatToInt(const char* fmt)
{
if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '0' && fmt[3] == 'f' && fmt[4] == 0) // Fast legacy path for "%.0f" which is expected to be the most common case.
return "%d";
const char* fmt_start = ImParseFormatFindStart(fmt); // Find % (if any, and ignore %%)
const char* fmt_end = ImParseFormatFindEnd(fmt_start); // Find end of format specifier, which itself is an exercise of confidence/recklessness (because snprintf is dependent on libc or user).
if (fmt_end > fmt_start && fmt_end[-1] == 'f')
{
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (fmt_start == fmt && fmt_end[0] == 0)
return "%d";
ImGuiContext& g = *GImGui;
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%.*s%%d%s", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision.
return g.TempBuffer;
#else
IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d"
#endif
}
return fmt;
}
//END OF BORROWED CODE
namespace ImGui
{
void FitImage(ImVec2& min, ImVec2& max, const ImVec2& rect_size,
const ImVec2& texture_size, const ImVec2& anchor,
ImGuiImageScaleMode fit_mode, bool keep_size)
{
ImVec2 scaled_texture_size;
switch (fit_mode) {
case ImGuiImageScaleMode_FitWidth:
scaled_texture_size = texture_size * rect_size.x / texture_size.x;
break;
case ImGuiImageScaleMode_FitHeight:
scaled_texture_size = texture_size * rect_size.y / texture_size.y;
break;
case ImGuiImageScaleMode_Stretch:
scaled_texture_size = rect_size;
break;
default:
scaled_texture_size = texture_size * ImMin(rect_size.x / texture_size.x, rect_size.y / texture_size.y);
break;
}
if (keep_size)
{
min += anchor * (rect_size - texture_size);
max = min + texture_size;
}
else
{
min += anchor * (rect_size - scaled_texture_size);
max = min + scaled_texture_size;
}
}
void ScaledImage(const char* str_id, const ImVec2& texture_size, ImTextureID texture_id, const ImVec2& size,
ImGuiImageScaleMode fit_mode, bool keep_size, const ImVec2& anchor,
const ImVec4& tint_col, const ImVec4& border_col, const ImVec4& bg_col,
const ImVec2& uv0, const ImVec2& uv1)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return;
ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);
ItemSize(bb);
if (!ItemAdd(bb, window->GetID(str_id)))
return;
if (bg_col.w > 0.0f)
window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(bg_col));
// Backup area to draw border after image
ImRect backup_bb = bb;
window->DrawList->PushClipRect(bb.Min, bb.Max, true);
FitImage(bb.Min, bb.Max, size, texture_size, anchor, fit_mode, keep_size);
window->DrawList->AddImage(texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col));
window->DrawList->PopClipRect();
if (border_col.w > 0.0f)
window->DrawList->AddRect(backup_bb.Min, backup_bb.Max, GetColorU32(border_col));
}
bool ScaledImageButtonEx(ImGuiID id, const ImVec2& texture_size, ImTextureID texture_id, const ImVec2& size_arg,
ImGuiImageScaleMode fit_mode, bool keep_size, ImGuiButtonFlags flags, const ImVec2& anchor,
const ImVec2& clip_offset,
const ImVec4& tint_col, const ImVec4& border_col, const ImVec4& bg_col,
const ImVec2& uv0, const ImVec2& uv1)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiStyle style = g.Style;
const ImVec2 padding = style.FramePadding;
ImVec2 size = CalcItemSize(size_arg, texture_size.x + padding.x * 2.0f, texture_size.y + padding.y * 2.0f);
ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2);
ItemSize(bb);
if (!ItemAdd(bb, id))
return false;
bool hovered, held;
bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags);
// Render bg frame
const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
RenderNavHighlight(bb, id);
RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
// Calculate image area
bb.Min = ImVec2(ImMax(bb.Min.x, bb.Min.x + padding.x), ImMax(bb.Min.y, bb.Min.y + padding.y));
bb.Max = ImVec2(ImMin(bb.Max.x, bb.Max.x - padding.x), ImMin(bb.Max.y, bb.Max.y - padding.y));
//
if (bg_col.w > 0.0f)
window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(bg_col));
// Backup area to draw border after image
ImRect backup_bb = bb;
window->DrawList->PushClipRect(bb.Min - clip_offset, bb.Max + clip_offset, true);
FitImage(bb.Min, bb.Max, bb.GetSize(), texture_size, anchor, fit_mode, keep_size);
window->DrawList->AddImage(texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col));
window->DrawList->PopClipRect();
if (border_col.w > 0.0f)
window->DrawList->AddRect(backup_bb.Min, backup_bb.Max, GetColorU32(border_col));
return pressed;
}
bool ScaledImageButton(const char* str_id, const ImVec2& texture_size, ImTextureID texture_id, const ImVec2& size,
ImGuiImageScaleMode fit_mode, bool keep_size, ImGuiButtonFlags flags, const ImVec2& anchor,
const ImVec2& clip_offset,
const ImVec4& tint_col, const ImVec4& border_col, const ImVec4& bg_col,
const ImVec2& uv0, const ImVec2& uv1)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
if (window->SkipItems)
return false;
return ScaledImageButtonEx(window->GetID(str_id), texture_size, texture_id, size, fit_mode, keep_size, flags, anchor, clip_offset, tint_col, border_col, bg_col, uv0, uv1);
}
bool ScaledImageButtonWithText(const ImVec2& texture_size, ImTextureID texture_id, const char* label, const ImVec2& image_size_arg,
const ImVec2& button_size, ImGuiButtonFlags flags,
ImGuiImageScaleMode fit_mode, bool keep_size, const ImVec2& anchor, ImGuiDir image_side,
const ImVec2& clip_offset,
const ImVec4& tint_col, const ImVec4& border_col, const ImVec4& bg_col,
const ImVec2& uv0, const ImVec2& uv1)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const ImVec2 label_size = CalcTextSize(label, NULL, true);
const ImVec2 padding = style.FramePadding;
const ImVec2 image_size = CalcItemSize(image_size_arg, texture_size.x + padding.x * 2.0f, texture_size.y + padding.y * 2.0f);
ImVec2 pos = window->DC.CursorPos;
if ((flags & ImGuiButtonFlags_AlignTextBaseLine) && padding.y < window->DC.CurrLineTextBaseOffset) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag)
pos.y += window->DC.CurrLineTextBaseOffset - padding.y;
ImVec2 size = CalcItemSize(button_size, label_size.x + padding.x * 2.0f, label_size.y + padding.y * 2.0f);
ImRect bb(pos, pos + size);
ItemSize(size, padding.y);
if (!ItemAdd(bb, id))
return false;
if (g.CurrentItemFlags & ImGuiItemFlags_ButtonRepeat)
flags |= ImGuiButtonFlags_Repeat;
bool hovered, held;
bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags);
// Render
const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
RenderNavHighlight(bb, id);
RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);
// Calculate image area
bb.Min = ImVec2(ImMax(bb.Min.x, bb.Min.x + padding.x), ImMax(bb.Min.y, bb.Min.y + padding.y));
bb.Max = ImVec2(ImMin(bb.Max.x, bb.Max.x - padding.x), ImMin(bb.Max.y, bb.Max.y - padding.y));
if (g.LogEnabled)
LogSetNextTextDecoration("[", "]");
ImRect ibb;
switch (image_side) {
case ImGuiDir_Right:
{
ImVec2 backup_max = bb.Max;
bb.Max.x = ImClamp(bb.Max.x - image_size.x, bb.Min.x, bb.Max.x);
ibb = ImRect(ImVec2(bb.Max.x, bb.Min.y), backup_max);
}
break;
case ImGuiDir_Up:
{
ImVec2 backup_min = bb.Min;
bb.Min.y = ImClamp(bb.Min.y + image_size.y, bb.Min.y, bb.Max.y);
ibb = ImRect(backup_min, ImVec2(bb.Max.x, bb.Min.y));
}
break;
case ImGuiDir_Down:
{
ImVec2 backup_max = bb.Max;
bb.Max.y = ImClamp(bb.Max.y - image_size.y, bb.Min.y, bb.Max.y);
ibb = ImRect(ImVec2(bb.Min.x, bb.Max.y), backup_max);
}
break;
// Left align by default
default:
{
ImVec2 backup_min = bb.Min;
bb.Min.x = ImClamp(bb.Min.x + image_size.x, bb.Min.x, bb.Max.x);
ibb = ImRect(backup_min, ImVec2(bb.Min.x , bb.Max.y));
}
break;
}
if (bg_col.w > 0.0f)
window->DrawList->AddRectFilled(ibb.Min, ibb.Max, GetColorU32(bg_col));
ImRect backup_ibb = ibb;
window->DrawList->PushClipRect(ibb.Min - clip_offset, ibb.Max + clip_offset, true);
FitImage(ibb.Min, ibb.Max, ibb.GetSize(), texture_size, anchor, fit_mode, keep_size);
window->DrawList->AddImage(texture_id, ibb.Min, ibb.Max, uv0, uv1, GetColorU32(tint_col));
window->DrawList->PopClipRect();
if (border_col.w > 0.0f)
window->DrawList->AddRect(backup_ibb.Min, backup_ibb.Max, GetColorU32(border_col));
RenderTextClipped(bb.Min, bb.Max, label, NULL, &label_size, style.ButtonTextAlign, &bb);
return pressed;
}
bool FilledSliderScalar(const char* label, bool mirror, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const float w = CalcItemWidth();
const ImVec2 label_size = CalcTextSize(label, NULL, true);
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f));
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0;
ItemSize(total_bb, style.FramePadding.y);
if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemFlags_Inputable : 0))
return false;
// Default format string when passing NULL
if (format == NULL)
format = DataTypeGetInfo(data_type)->PrintFmt;
const bool hovered = ItemHoverable(frame_bb, id);
bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id);
if (!temp_input_is_active)
{
// Tabbing or CTRL-clicking on Slider turns it into an input box
const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0;
const bool clicked = hovered && IsMouseClicked(0, id);
const bool make_active = (input_requested_by_tabbing || clicked || g.NavActivateId == id);
if (make_active && clicked)
SetKeyOwner(ImGuiKey_MouseLeft, id);
if (make_active && temp_input_allowed)
if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput)))
temp_input_is_active = true;
if (make_active && !temp_input_is_active)
{
SetActiveID(id, window);
SetFocusID(id, window);
FocusWindow(window);
g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right);
}
}
if (temp_input_is_active)
{
// Only clamp CTRL+Click input when ImGuiSliderFlags_AlwaysClamp is set
const bool is_clamp_input = (flags & ImGuiSliderFlags_AlwaysClamp) != 0;
return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL);
}
// Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
RenderNavHighlight(frame_bb, id);
RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);
// Slider behavior
ImRect grab_bb;
const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, flags, &grab_bb);
if (value_changed)
MarkItemEdited(id);
// Render grab
if (grab_bb.Max.x > grab_bb.Min.x)
{
if (mirror)
{
window->DrawList->AddRectFilled(grab_bb.Min, ImVec2(frame_bb.Max.x - 2.0f, grab_bb.Max.y), GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
}
else
{
window->DrawList->AddRectFilled(ImVec2(frame_bb.Min.x + 2.0f, grab_bb.Min.y), grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
}
}
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
char value_buf[64];
const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format);
if (g.LogEnabled)
LogSetNextTextDecoration("{", "}");
RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f));
if (label_size.x > 0.0f)
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | (temp_input_allowed ? ImGuiItemStatusFlags_Inputable : 0));
return value_changed;
}
bool FilledSliderScalarN(const char* label, bool mirror, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, ImGuiSliderFlags flags)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext& g = *GImGui;
bool value_changed = false;
BeginGroup();
PushID(label);
PushMultiItemsWidths(components, CalcItemWidth());
size_t type_size = GDataTypeInfo[data_type].Size;
for (int i = 0; i < components; i++)
{
PushID(i);
if (i > 0)
SameLine(0, g.Style.ItemInnerSpacing.x);
value_changed |= FilledSliderScalar("", mirror, data_type, v, v_min, v_max, format, flags);
PopID();
PopItemWidth();
v = (void*)((char*)v + type_size);
}
PopID();
const char* label_end = FindRenderedTextEnd(label);
if (label != label_end)
{
SameLine(0, g.Style.ItemInnerSpacing.x);
TextEx(label, label_end);
}
EndGroup();
return value_changed;
}
bool FilledSliderFloat(const char* label, bool mirror, float* v, float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalar(label, mirror, ImGuiDataType_Float, v, &v_min, &v_max, format, flags);
}
bool FilledSliderFloat2(const char* label, bool mirror, float v[2], float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalarN(label, mirror, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, flags);
}
bool FilledSliderFloat3(const char* label, bool mirror, float v[3], float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalarN(label, mirror, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, flags);
}
bool FilledSliderFloat4(const char* label, bool mirror, float v[4], float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalarN(label, mirror, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, flags);
}
bool FilledSliderAngle(const char* label, bool mirror, float* v_rad, float v_degrees_min, float v_degrees_max, const char* format, ImGuiSliderFlags flags)
{
if (format == NULL)
format = "%.0f deg";
float v_deg = (*v_rad) * 360.0f / (2 * IM_PI);
bool value_changed = FilledSliderFloat(label, mirror, &v_deg, v_degrees_min, v_degrees_max, format, flags);
*v_rad = v_deg * (2 * IM_PI) / 360.0f;
return value_changed;
}
bool FilledSliderInt(const char* label, bool mirror, int* v, int v_min, int v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalar(label, mirror, ImGuiDataType_S32, v, &v_min, &v_max, format, flags);
}
bool FilledSliderInt2(const char* label, bool mirror, int v[2], int v_min, int v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalarN(label, mirror, ImGuiDataType_S32, v, 2, &v_min, &v_max, format, flags);
}
bool FilledSliderInt3(const char* label, bool mirror, int v[3], int v_min, int v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalarN(label, mirror, ImGuiDataType_S32, v, 3, &v_min, &v_max, format, flags);
}
bool FilledSliderInt4(const char* label, bool mirror, int v[4], int v_min, int v_max, const char* format, ImGuiSliderFlags flags)
{
return FilledSliderScalarN(label, mirror, ImGuiDataType_S32, v, 4, &v_min, &v_max, format, flags);
}
bool VFilledSliderScalar(const char* label, bool mirror, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const ImVec2 label_size = CalcTextSize(label, NULL, true);
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size);
const ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
ItemSize(bb, style.FramePadding.y);
if (!ItemAdd(frame_bb, id))
return false;
// Default format string when passing NULL
if (format == NULL)
format = DataTypeGetInfo(data_type)->PrintFmt;
const bool hovered = ItemHoverable(frame_bb, id);
const bool clicked = hovered && IsMouseClicked(0, id);
if (clicked || g.NavActivateId == id)
{
if (clicked)
SetKeyOwner(ImGuiKey_MouseLeft, id);
SetActiveID(id, window);
SetFocusID(id, window);
FocusWindow(window);
g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down);
}
// Draw frame
const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
RenderNavHighlight(frame_bb, id);
RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);
// Slider behavior
ImRect grab_bb;
const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, flags | ImGuiSliderFlags_Vertical, &grab_bb);
if (value_changed)
MarkItemEdited(id);
// Render grab
if (grab_bb.Max.y > grab_bb.Min.y)
{
if (mirror)
{
window->DrawList->AddRectFilled(grab_bb.Min, ImVec2(frame_bb.Max.x - 2.0f, frame_bb.Max.y), GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
}
else
{
window->DrawList->AddRectFilled(ImVec2(frame_bb.Min.x + 2.0f, frame_bb.Min.y), grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);
}
}
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
// For the vertical slider we allow centered text to overlap the frame padding
char value_buf[64];
const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format);
RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.0f));
if (label_size.x > 0.0f)
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
return value_changed;
}
bool VFilledSliderFloat(const char* label, bool mirror, const ImVec2& size, float* v, float v_min, float v_max, const char* format, ImGuiSliderFlags flags)
{
return VFilledSliderScalar(label, mirror, size, ImGuiDataType_Float, v, &v_min, &v_max, format, flags);
}
bool VFilledSliderInt(const char* label, bool mirror, const ImVec2& size, int* v, int v_min, int v_max, const char* format, ImGuiSliderFlags flags)
{
return VFilledSliderScalar(label, mirror, size, ImGuiDataType_S32, v, &v_min, &v_max, format, flags);
}
void ScrollWhenDragging(float dx, float dy, ImGuiMouseButton mouse_button)
{
ImGuiContext& g = *ImGui::GetCurrentContext();
if(g.MovingWindow != nullptr)
return;
ImGuiWindow* window = g.CurrentWindow;
if(!window->ScrollbarX && !window->ScrollbarY) // Nothing to scroll
return;
ImGuiIO& io = ImGui::GetIO();
bool hovered = false;
bool held = false;
const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow;
bool window_highlight = (window_to_highlight && (window->RootWindowForTitleBarHighlight == window_to_highlight->RootWindowForTitleBarHighlight));
ImGuiButtonFlags button_flags = (mouse_button == ImGuiMouseButton_Left) ? ImGuiButtonFlags_MouseButtonLeft : (mouse_button == ImGuiMouseButton_Right) ? ImGuiButtonFlags_MouseButtonRight : ImGuiButtonFlags_MouseButtonMiddle;
if(io.MouseDown[mouse_button] && window_highlight)
{
ImGui::ButtonBehavior(window->InnerClipRect, window->GetID("##scrolldraggingoverlay"), &hovered, &held, button_flags);
if((window->InnerClipRect.Contains(io.MousePos)))
held = true;
else if(window->InnerClipRect.Contains(io.MouseClickedPos[mouse_button]) ) // If mouse has moved outside window, check if click was inside
held = true;
else
held = false;
}
if (held)
{
ImGui::SetScrollX(window, window->Scroll.x + dx * io.MouseDelta.x);
ImGui::SetScrollY(window, window->Scroll.y + dy * io.MouseDelta.y);
}
}
}
#endif