-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
imgui_toggle_renderer.h
97 lines (79 loc) · 3.82 KB
/
imgui_toggle_renderer.h
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
#pragma once
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif // IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#include "imgui_internal.h"
#include "imgui_toggle.h"
#include "imgui_toggle_palette.h"
class ImGuiToggleRenderer
{
public:
ImGuiToggleRenderer();
ImGuiToggleRenderer(const char* label, bool* value, const ImGuiToggleConfig& user_config);
void SetConfig(const char* label, bool* value, const ImGuiToggleConfig& user_config);
bool Render();
private:
// toggle state & context
ImGuiToggleConfig _config;
ImGuiToggleStateConfig _state;
ImGuiTogglePalette _palette;
bool _isMixedValue;
bool _isHovered;
bool _isLastActive;
float _lastActiveTimer;
float _animationPercent;
// imgui specific context
const ImGuiStyle* _style;
ImDrawList* _drawList;
ImGuiID _id;
// raw ui value & label
const char* _label;
bool* _value;
// calculated values
ImRect _boundingBox;
ImVec4 _colorA11yGlyphOff;
ImVec4 _colorA11yGlyphOn;
// inline accessors
inline float GetWidth() const { return _boundingBox.GetWidth(); }
inline float GetHeight() const { return _boundingBox.GetHeight(); }
inline ImVec2 GetPosition() const { return _boundingBox.Min; }
inline ImVec2 GetToggleSize() const { return _boundingBox.GetSize(); }
inline bool IsAnimated() const { return (_config.Flags & ImGuiToggleFlags_Animated) != 0 && _config.AnimationDuration > 0; }
inline bool HasBorderedFrame() const { return (_config.Flags & ImGuiToggleFlags_BorderedFrame) != 0 && _state.FrameBorderThickness > 0; }
inline bool HasShadowedFrame() const { return (_config.Flags & ImGuiToggleFlags_ShadowedKnob) != 0 && _state.FrameShadowThickness > 0; }
inline bool HasBorderedKnob() const { return (_config.Flags & ImGuiToggleFlags_BorderedKnob) != 0 && _state.KnobBorderThickness > 0; }
inline bool HasShadowedKnob() const { return (_config.Flags & ImGuiToggleFlags_ShadowedKnob) != 0 && _state.KnobShadowThickness > 0; }
inline bool HasA11yGlyphs() const { return (_config.Flags & ImGuiToggleFlags_A11y) != 0; }
inline bool HasCircleKnob() const { return _config.KnobRounding >= 1.0f; }
inline bool HasRectangleKnob() const { return _config.KnobRounding < 1.0f; }
// behavior
void ValidateConfig();
bool ToggleBehavior(const ImRect& interaction_bounding_box);
// drawing - general
void DrawToggle();
// drawing - frame
void DrawFrame(ImU32 color_frame);
// drawing a11y
void DrawA11yDot(const ImVec2& pos, ImU32 color);
void DrawA11yGlyph(ImVec2 pos, ImU32 color, bool state, float radius, float thickness);
void DrawA11yLabel(ImVec2 pos, ImU32 color, const char* label);
void DrawA11yFrameOverlay(float knob_radius, bool state);
void DrawA11yFrameOverlays(float knob_radius);
// drawing - knob
void DrawCircleKnob(float radius, ImU32 color_knob);
void DrawRectangleKnob(float radius, ImU32 color_knob);
// drawing - label
void DrawLabel(float x_offset);
// state updating
void UpdateAnimationPercent();
void UpdateStateConfig();
void UpdatePalette();
// helpers
ImVec2 CalculateKnobCenter(float radius, float animation_percent, const ImVec2& offset = ImVec2()) const;
ImRect CalculateKnobBounds(float radius, float animation_percent, const ImVec2& offset = ImVec2()) const;
void DrawRectBorder(ImRect bounds, ImU32 color_border, float rounding, float thickness);
void DrawCircleBorder(const ImVec2& center, float radius, ImU32 color_border, float thickness);
void DrawRectShadow(ImRect bounds, ImU32 color_shadow, float rounding, float thickness);
void DrawCircleShadow(const ImVec2& center, float radius, ImU32 color_shadow, float thickness);
};