-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
imgui_toggle_renderer.cpp
525 lines (432 loc) · 18.4 KB
/
imgui_toggle_renderer.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
#include "imgui_toggle_renderer.h"
#include "imgui_toggle_palette.h"
#include "imgui_toggle_math.h"
using namespace ImGuiToggleConstants;
using namespace ImGuiToggleMath;
namespace
{
// a small helper to quickly check the mixed value flag.
inline bool IsItemMixedValue()
{
#if IMGUI_VERSION_NUM >= 19135
return (GImGui->LastItemData.ItemFlags & ImGuiItemFlags_MixedValue) != 0;
#else
return (GImGui->LastItemData.InFlags & ImGuiItemFlags_MixedValue) != 0;
#endif
}
} // namespace
ImGuiToggleRenderer::ImGuiToggleRenderer()
{
SetConfig(nullptr, nullptr, ImGuiToggleConfig());
}
ImGuiToggleRenderer::ImGuiToggleRenderer(const char* label, bool* value, const ImGuiToggleConfig& user_config) : _style(nullptr), _label(label), _value(value)
{
SetConfig(label, value, user_config);
}
void ImGuiToggleRenderer::SetConfig(const char* label, bool* value, const ImGuiToggleConfig& user_config)
{
// store mandatory settings
_label = label;
_value = value;
// copy our user's config and ensure it's valid.
_config = user_config;
ValidateConfig();
}
bool ImGuiToggleRenderer::Render()
{
ImGuiWindow* window = ImGui::GetCurrentWindow();
IM_ASSERT(window);
IM_ASSERT(_label != nullptr);
IM_ASSERT(_value != nullptr);
if (window->SkipItems)
{
return false;
}
// update igui context
ImGuiContext& g = *GImGui;
_id = window->GetID(_label);
_drawList = ImGui::GetWindowDrawList();
_style = &ImGui::GetStyle();
// calculate the size of the toggle portion
const float height = _config.Size.y > 0
? _config.Size.y
: ImGui::GetFrameHeight();
const float width = _config.Size.x > 0
? _config.Size.x
: height * _config.WidthRatio;
// get the position of the widget and how large the label should be
ImVec2 widget_position = window->DC.CursorPos;
ImVec2 label_size = ImGui::CalcTextSize(_label, nullptr, true);
// if the knob is offset horizontally outside of the frame in the on state, we want to bump our label over.
const float label_x_offset = ImMax(0.0f, -_config.On.KnobOffset.x / 2.0f);
// calculate bounding boxes for the toggle, and the whole widget including the label for interaction
_boundingBox = ImRect(widget_position, widget_position + ImVec2(width, height));
ImRect total_bounding_box = ImRect(widget_position,
widget_position
+ ImVec2(
width + (label_size.x > 0.0f ? _style->ItemInnerSpacing.x + label_size.x : 0.0f) + label_x_offset,
ImMax(height, label_size.y) + _style->FramePadding.y * 2.0f
));
// handle the toggle input behavior
bool pressed = ToggleBehavior(total_bounding_box);
_isMixedValue = ::IsItemMixedValue();
// draw the toggle itself and the label
DrawToggle();
DrawLabel(label_x_offset);
IMGUI_TEST_ENGINE_ITEM_INFO(_id, _label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*_value ? ImGuiItemStatusFlags_Checked : 0));
return pressed;
}
void ImGuiToggleRenderer::ValidateConfig()
{
IM_ASSERT_USER_ERROR(_config.Size.x >= 0, "Size.x specified was negative.");
IM_ASSERT_USER_ERROR(_config.Size.y >= 0, "Size.y specified was negative.");
// if no flags were specified, use defaults.
if (_config.Flags == ImGuiToggleFlags_None)
{
_config.Flags = ImGuiToggleFlags_Default;
}
// a zero or negative duration would prevent animation.
_config.AnimationDuration = ImMax(_config.AnimationDuration, AnimationDurationMinimum);
// keep our size/scale and rounding numbers sane.
_config.FrameRounding = ImClamp(_config.FrameRounding, FrameRoundingMinimum, FrameRoundingMaximum);
_config.KnobRounding = ImClamp(_config.KnobRounding, KnobRoundingMinimum, KnobRoundingMaximum);
_config.WidthRatio = ImClamp(_config.WidthRatio, WidthRatioMinimum, WidthRatioMaximum);
// Make sure our a11y labels have values.
if (_config.On.Label == nullptr)
{
_config.On.Label = LabelA11yOnDefault;
}
if (_config.Off.Label == nullptr)
{
_config.Off.Label = LabelA11yOffDefault;
}
}
bool ImGuiToggleRenderer::ToggleBehavior(const ImRect& interaction_bounding_box)
{
ImGui::ItemSize(interaction_bounding_box, _style->FramePadding.y);
if (!ImGui::ItemAdd(interaction_bounding_box, _id))
{
ImGuiContext& g = *GImGui;
IMGUI_TEST_ENGINE_ITEM_INFO(_id, _label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*_value ? ImGuiItemStatusFlags_Checked : 0));
return false;
}
// the meat and potatoes: the actual toggle button
const ImGuiButtonFlags button_flags = ImGuiButtonFlags_PressedOnClick;
bool hovered, held;
bool pressed = ImGui::ButtonBehavior(interaction_bounding_box, _id, &hovered, &held, button_flags);
if (pressed)
{
*_value = !(*_value);
ImGui::MarkItemEdited(_id);
}
return pressed;
}
void ImGuiToggleRenderer::DrawToggle()
{
const float height = GetHeight();
const float width = GetWidth();
ImGuiContext& g = *GImGui;
// update imgui state
_isHovered = g.HoveredId == _id;
_isLastActive = g.LastActiveId == _id;
_lastActiveTimer = g.LastActiveIdTimer;
// radius is by default half the diameter
const float knob_radius = height * DiameterToRadiusRatio;
// update the toggle's animation timer, state, and palette.
UpdateAnimationPercent();
UpdateStateConfig();
UpdatePalette();
// get colors modified by hover.
const ImU32 color_frame = ImGui::GetColorU32(_isHovered ? _palette.FrameHover : _palette.Frame);
const ImU32 color_knob = ImGui::GetColorU32(_isHovered ? _palette.KnobHover : _palette.Knob);
// draw the background frame
DrawFrame(color_frame);
// draw accessibility labels, if enabled.
if (HasA11yGlyphs())
{
DrawA11yFrameOverlays(knob_radius);
}
// draw the knob
if (HasCircleKnob())
{
DrawCircleKnob(knob_radius, color_knob);
}
else if (HasRectangleKnob())
{
DrawRectangleKnob(knob_radius, color_knob);
}
else
{
// user didn't specify a knob mode, they get no knob.
IM_ASSERT_USER_ERROR(false, "No toggle knob type to draw.");
}
}
void ImGuiToggleRenderer::DrawFrame(ImU32 color_frame)
{
const float height = GetHeight();
const float frame_rounding = _config.FrameRounding >= 0
? height * _config.FrameRounding
: height * 0.5f;
// draw frame shadow, if enabled
if (HasShadowedFrame())
{
const ImU32 color_frame_shadow = ImGui::GetColorU32(_palette.FrameShadow);
DrawRectShadow(_boundingBox, color_frame_shadow, frame_rounding, _state.FrameShadowThickness);
}
// draw frame background
_drawList->AddRectFilled(_boundingBox.Min, _boundingBox.Max, color_frame, frame_rounding);
// draw frame border, if enabled
if (HasBorderedFrame())
{
const ImU32 color_frame_border = ImGui::GetColorU32(_palette.FrameBorder);
DrawRectBorder(_boundingBox, color_frame_border, frame_rounding, _state.FrameBorderThickness);
}
}
void ImGuiToggleRenderer::DrawA11yDot(const ImVec2& pos, ImU32 color)
{
ImGui::RenderBullet(_drawList, pos, color);
}
void ImGuiToggleRenderer::DrawA11yGlyph(ImVec2 pos, ImU32 color, bool state, float radius, float thickness)
{
if (state)
{
// draw the I bar
const float half_thickness = thickness * 0.5f;
const ImVec2 offset(half_thickness, radius);
_drawList->AddRectFilled(pos - offset, pos + offset, color);
}
else
{
// draw the O ring
const float o_adjustment = 1.0f;
const float o_radius = radius - o_adjustment;
const float o_thickness = thickness + o_adjustment;
pos.x += o_adjustment;
_drawList->AddCircle(pos, o_radius, color, 0, o_thickness);
}
}
void ImGuiToggleRenderer::DrawA11yLabel(ImVec2 pos, ImU32 color, const char* label)
{
// subtract out half the sizes of the text to center them
const ImVec2 text_size = ImGui::CalcTextSize(label);
pos.x -= (text_size.x * 0.5f);
pos.y -= (text_size.y * 0.5f);
// draw the label.
_drawList->AddText(pos, color, label);
}
void ImGuiToggleRenderer::DrawA11yFrameOverlay(float knob_radius, bool state)
{
const float AnimationPercentOff = 0.0f;
const float AnimationPercentOn = 1.0f;
// notice we swap the animation percents as compared to the labels/glyphs, as we want to draw the
// a11y labels where the knob *isn't* when it's in a given state.
ImVec2 pos = CalculateKnobCenter(knob_radius, state ? AnimationPercentOff : AnimationPercentOn);
// next, we want to adjust the position to move to a more pleasing spot in the toggle.
// this is just some tinkering that got to a nice looking area based on the sizes,
// but this is subject to change.
const float diameter = ImMax(1.0f, GetHeight() / 3.0f);
const float radius = diameter * 0.5f;
const float thickness = ImCeil(radius * 0.2f);
const ImVec2 adjustment = ImVec2(radius - thickness, 0.0f)
* (state ? -1.0f : 1.0f); // if state is true, we want to subtract rather than add.
pos += adjustment;
const ImU32 color = state
? ImGui::GetColorU32(_colorA11yGlyphOn)
: ImGui::GetColorU32(_colorA11yGlyphOff);
switch (_config.A11yStyle)
{
case ImGuiToggleA11yStyle_Label:
DrawA11yLabel(pos, color, state ? _config.On.Label : _config.Off.Label);
break;
case ImGuiToggleA11yStyle_Glyph:
DrawA11yGlyph(pos, color, state, radius, thickness);
break;
case ImGuiToggleA11yStyle_Dot:
DrawA11yDot(pos, color);
break;
default:
break;
}
}
void ImGuiToggleRenderer::DrawA11yFrameOverlays(float knob_radius)
{
DrawA11yFrameOverlay(knob_radius, true);
DrawA11yFrameOverlay(knob_radius, false);
}
void ImGuiToggleRenderer::DrawCircleKnob(float radius, ImU32 color_knob)
{
const float inset_size = ImMin(_state.KnobInset.GetAverage(), radius);
IM_ASSERT_USER_ERROR(inset_size <= radius, "Inset size needs to be smaller or equal to the knob's radius for circular knobs.");
const ImVec2 knob_center = CalculateKnobCenter(radius, _animationPercent, _state.KnobOffset);
const float knob_radius = radius - inset_size;
// draw knob shadow, if enabled
if (HasShadowedKnob())
{
const ImU32 color_knob_shadow = ImGui::GetColorU32(_palette.KnobShadow);
DrawCircleShadow(knob_center, knob_radius, color_knob_shadow, _state.KnobShadowThickness);
}
// draw circle knob
_drawList->AddCircleFilled(knob_center, knob_radius, color_knob);
// draw knob border, if enabled
if (HasBorderedKnob())
{
const ImU32 color_knob_border = ImGui::GetColorU32(_palette.KnobBorder);
DrawCircleBorder(knob_center, knob_radius, color_knob_border, _state.KnobBorderThickness);
}
}
void ImGuiToggleRenderer::DrawRectangleKnob(float radius, ImU32 color_knob)
{
const ImRect bounds = CalculateKnobBounds(radius, _animationPercent, _state.KnobOffset);
const float knob_diameter_total = bounds.GetHeight();
const float knob_rounded_radius = (knob_diameter_total * 0.5f) * _config.KnobRounding;
// draw knob shadow, if enabled
if (HasShadowedKnob())
{
const ImU32 color_knob_shadow = ImGui::GetColorU32(_palette.KnobShadow);
DrawRectShadow(bounds, color_knob_shadow, _config.KnobRounding, _state.KnobShadowThickness);
}
// draw rectangle/squircle knob
_drawList->AddRectFilled(bounds.Min, bounds.Max, color_knob, knob_rounded_radius);
// draw knob border, if enabled
if (HasBorderedKnob())
{
const ImU32 color_knob_border = ImGui::GetColorU32(_palette.KnobBorder);
DrawRectBorder(bounds, color_knob_border, knob_rounded_radius, _state.KnobBorderThickness);
}
}
void ImGuiToggleRenderer::DrawLabel(float x_offset)
{
const ImVec2 label_size = ImGui::CalcTextSize(_label, nullptr, true);
const float half_height = GetHeight() * 0.5f;
const float label_x = _boundingBox.Max.x + _style->ItemInnerSpacing.x + x_offset;
const float label_y = _boundingBox.Min.y + half_height - (label_size.y * 0.5f);
const ImVec2 label_pos = ImVec2(label_x, label_y);
ImGuiContext& g = *GImGui;
if (g.LogEnabled)
{
ImGui::LogRenderedText(&label_pos, _isMixedValue ? "[~]" : *_value ? "[x]" : "[ ]");
}
if (label_size.x > 0.0f)
{
ImGui::RenderText(label_pos, _label);
}
}
void ImGuiToggleRenderer::UpdateAnimationPercent()
{
// calculate the lerp percentage for animation,
// but default to 1/0 for if we aren't animating at all,
// or 0.5f if we have a mixed value. Also, trying to keep parity with
// undocumented tristate/mixed/indeterminate checkbox (#2644)
float t = _isMixedValue
? 0.5f
: (*_value ? 1.0f : 0.0f);
if (IsAnimated() && _isLastActive)
{
const float t_anim = ImSaturate(ImInvLerp(0.0f, _config.AnimationDuration, _lastActiveTimer));
t = *_value ? (t_anim) : (1.0f - t_anim);
}
_animationPercent = t;
}
void ImGuiToggleRenderer::UpdateStateConfig()
{
if (!IsAnimated())
{
_state = *_value ? _config.On : _config.Off;
return;
}
_state.FrameBorderThickness = ImLerp(_config.Off.FrameBorderThickness, _config.On.FrameBorderThickness, _animationPercent);
_state.KnobBorderThickness = ImLerp(_config.Off.KnobBorderThickness, _config.On.KnobBorderThickness, _animationPercent);
_state.KnobInset = ImLerp(_config.Off.KnobInset, _config.On.KnobInset, _animationPercent);
_state.KnobOffset = ImLerp(_config.Off.KnobOffset, _config.On.KnobOffset, _animationPercent);
}
void ImGuiToggleRenderer::UpdatePalette()
{
const ImGuiTogglePalette* on_candidate = _config.On.Palette;
const ImGuiTogglePalette* off_candidate = _config.Off.Palette;
if (!IsAnimated())
{
ImGui::UnionPalette(
&_palette,
*_value ? on_candidate : off_candidate,
_style->Colors,
*_value);
// store specific colors that shouldn't blend.
_colorA11yGlyphOff = _palette.A11yGlyph;
_colorA11yGlyphOn = _palette.A11yGlyph;
return;
}
ImGuiTogglePalette off_unioned;
ImGuiTogglePalette on_unioned;
ImGui::UnionPalette(&off_unioned, off_candidate, _style->Colors, false);
ImGui::UnionPalette(&on_unioned, on_candidate, _style->Colors, true);
// otherwise, lets lerp them!
ImGui::BlendPalettes(&_palette, off_unioned, on_unioned, _animationPercent);
// store specific colors that shouldn't blend.
_colorA11yGlyphOff = off_unioned.A11yGlyph;
_colorA11yGlyphOn = on_unioned.A11yGlyph;
}
ImVec2 ImGuiToggleRenderer::CalculateKnobCenter(float radius, float animation_percent, const ImVec2& offset /*= ImVec2()*/) const
{
const ImVec2 pos = GetPosition();
const float double_radius = radius * 2.0f;
const float animation_percent_inverse = 1.0f - animation_percent;
const float knob_x = (pos.x + radius)
+ animation_percent * (GetWidth() - double_radius - offset.x)
+ (animation_percent_inverse * offset.x);
const float knob_y = pos.y + radius + offset.y;
return ImVec2(knob_x, knob_y);
}
ImRect ImGuiToggleRenderer::CalculateKnobBounds(float radius, float animation_percent, const ImVec2& offset /*= ImVec2()*/) const
{
const ImVec2 position = GetPosition();
const float double_radius = radius * 2.0f;
const float animation_percent_inverse = 1.0f - animation_percent;
const float knob_left = (animation_percent * (GetWidth() - double_radius - offset.x))
+ (animation_percent_inverse * offset.x)
+ _state.KnobInset.Left;
const float knob_top = _state.KnobInset.Top + _state.KnobOffset.y;
const float knob_bottom = GetHeight() - _state.KnobInset.Bottom + _state.KnobOffset.y;
const float knob_right = (knob_left - _state.KnobInset.Left) + double_radius - _state.KnobInset.Right;
// if our offsets in the x or y are close to 0,
// we will just skip drawing the whole thing.
if (ImApproximately(knob_left, knob_right) ||
ImApproximately(knob_top, knob_bottom))
{
return ImRect();
}
const ImVec2 knob_min = position + ImVec2(knob_left, knob_top);
const ImVec2 knob_max = position + ImVec2(knob_right, knob_bottom);
return ImRect(knob_min, knob_max);
}
void ImGuiToggleRenderer::DrawRectBorder(ImRect bounds, ImU32 color_border, float rounding, float thickness)
{
// the border should only grow "inside" the bounding box,
// so we need to shrink the bounds used to prevent it from puffing out.
const float half_thickness = thickness * 0.5f;
bounds.Expand(-half_thickness);
_drawList->AddRect(bounds.Min, bounds.Max, color_border, rounding, ImDrawFlags_None, thickness);
}
void ImGuiToggleRenderer::DrawCircleBorder(const ImVec2& center, float radius, ImU32 color_border, float thickness)
{
// the border should only grow "inside" the bounding box,
// so we need to shrink the radius used to prevent it from puffing out.
const float half_thickness = thickness * 0.5f;
radius -= half_thickness;
_drawList->AddCircle(center, radius, color_border, 0, thickness);
}
void ImGuiToggleRenderer::DrawRectShadow(ImRect bounds, ImU32 color_shadow, float rounding, float thickness)
{
// the shadow should only grow "outside" the bounding box,
// so we need to expand the bounds used to puff it out.
const float half_thickness = thickness * 0.5f;
bounds.Expand(half_thickness);
_drawList->AddRect(bounds.Min, bounds.Max, color_shadow, rounding, ImDrawFlags_None, thickness);
}
void ImGuiToggleRenderer::DrawCircleShadow(const ImVec2& center, float radius, ImU32 color_border, float thickness)
{
// the shadow should only grow "outside" the bounding box,
// so we need to expand the radius used to puff it out.
const float half_thickness = thickness * 0.5f;
radius += half_thickness;
_drawList->AddCircle(center, radius, color_border, 0, thickness);
}