tangxu
2024-12-24 91105b77c916d06dd30380e20594e29f85eae3da
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Helper;
using DPumpHydr.WinFrmUI.RLT.Manager;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.MaterialDrawHelper;
using static DPumpHydr.WinFrmUI.RLT.Util.MaterialAnimations;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MaterialFloatingActionButton
 
    public class MaterialFloatingActionButton : System.Windows.Forms.Button, MaterialControlI
    {
        [Browsable(false)]
        public int Depth { get; set; }
 
        [Browsable(false)]
        public MaterialSkinManager SkinManager => MaterialSkinManager.Instance;
 
        [Browsable(false)]
        public MaterialMouseState MouseState { get; set; }
 
        private const int FAB_SIZE = 56;
        private const int FAB_MINI_SIZE = 40;
        private const int FAB_ICON_MARGIN = 16;
        private const int FAB_MINI_ICON_MARGIN = 8;
        private const int FAB_ICON_SIZE = 24;
 
        private bool _mouseHover = false;
 
        [DefaultValue(true)]
        [Category("Material"), DisplayName("Draw Shadows")]
        [Description("Draw Shadows around control")]
        public bool DrawShadows { get; set; }
 
        [DefaultValue(false)]
        [Category("Material"), DisplayName("Size Mini")]
        [Description("Set control size to default or mini")]
        public bool Mini
        {
            get => _mini;
            set
            {
                if (Parent != null)
                {
                    Parent.Invalidate();
                }
 
                setSize(value);
            }
        }
 
        private bool _mini;
 
        [DefaultValue(false)]
        [Category("Material"), DisplayName("Animate Show HideButton")]
        public bool AnimateShowHideButton
        {
            get => _animateShowButton;
            set { _animateShowButton = value; Refresh(); }
        }
 
        private bool _animateShowButton;
 
        [DefaultValue(false)]
        [Category("Material")]
        [Description("Define icon to display")]
        public Image Icon
        {
            get => _icon;
            set { _icon = value; Refresh(); }
        }
 
        private Image _icon;
 
        private bool _isHiding = false;
 
        private readonly AnimationManager _animationManager;
 
        private readonly AnimationManager _showAnimationManager;
 
        public MaterialFloatingActionButton()
        {
            AnimateShowHideButton = false;
            Mini = false;
            DrawShadows = true;
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
 
            Size = new Size(FAB_SIZE, FAB_SIZE);
            _animationManager = new AnimationManager(false)
            {
                Increment = 0.03,
                AnimationType = AnimationType.EaseOut
            };
            _animationManager.OnAnimationProgress += sender => Invalidate();
 
            _showAnimationManager = new AnimationManager(true)
            {
                Increment = 0.1,
                AnimationType = AnimationType.EaseOut
            };
            _showAnimationManager.OnAnimationProgress += sender => Invalidate();
            _showAnimationManager.OnAnimationFinished += _showAnimationManager_OnAnimationFinished;
        }
 
        protected override void InitLayout()
        {
            LocationChanged += (sender, e) => { if (DrawShadows) { Parent?.Invalidate(); } };
        }
 
        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);
            if (DrawShadows && Parent != null)
            {
                AddShadowPaintEvent(Parent, drawShadowOnParent);
            }
 
            if (_oldParent != null)
            {
                RemoveShadowPaintEvent(_oldParent, drawShadowOnParent);
            }
 
            _oldParent = Parent;
        }
 
        private Control _oldParent;
 
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (Parent == null)
            {
                return;
            }
 
            if (Visible)
            {
                AddShadowPaintEvent(Parent, drawShadowOnParent);
            }
            else
            {
                RemoveShadowPaintEvent(Parent, drawShadowOnParent);
            }
        }
 
        private bool _shadowDrawEventSubscribed = false;
 
        private void AddShadowPaintEvent(Control control, PaintEventHandler shadowPaintEvent)
        {
            if (_shadowDrawEventSubscribed)
            {
                return;
            }
 
            control.Paint += shadowPaintEvent;
            control.Invalidate();
            _shadowDrawEventSubscribed = true;
        }
 
        private void RemoveShadowPaintEvent(Control control, PaintEventHandler shadowPaintEvent)
        {
            if (!_shadowDrawEventSubscribed)
            {
                return;
            }
 
            control.Paint -= shadowPaintEvent;
            control.Invalidate();
            _shadowDrawEventSubscribed = false;
        }
 
        private void setSize(bool mini)
        {
            _mini = mini;
            Size = _mini ? new Size(FAB_MINI_SIZE, FAB_MINI_SIZE) : new Size(FAB_SIZE, FAB_SIZE);
            fabBounds = _mini ? new Rectangle(0, 0, FAB_MINI_SIZE, FAB_MINI_SIZE) : new Rectangle(0, 0, FAB_SIZE, FAB_SIZE);
            fabBounds.Width -= 1;
            fabBounds.Height -= 1;
        }
 
        private void _showAnimationManager_OnAnimationFinished(object sender)
        {
            if (_isHiding)
            {
                Visible = false;
                _isHiding = false;
            }
        }
 
        private void drawShadowOnParent(object sender, PaintEventArgs e)
        {
            if (Parent == null)
            {
                RemoveShadowPaintEvent((Control)sender, drawShadowOnParent);
                return;
            }
 
            // paint shadow on parent
            Graphics gp = e.Graphics;
            Rectangle rect = new(Location, fabBounds.Size);
            gp.SmoothingMode = SmoothingMode.AntiAlias;
            DrawRoundShadow(gp, rect);
        }
 
        private Rectangle fabBounds;
 
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Graphics g = pevent.Graphics;
 
            g.Clear(Parent.BackColor == Color.Transparent ? ((Parent.Parent == null || (Parent.Parent != null && Parent.Parent.BackColor == Color.Transparent)) ? SkinManager.BackgroundColor : Parent.Parent.BackColor) : Parent.BackColor);
            g.SmoothingMode = SmoothingMode.AntiAlias;
 
            // Paint shadow on element to blend with the parent shadow
            DrawRoundShadow(g, fabBounds);
 
            // draw fab
            g.FillEllipse(Enabled ? _mouseHover ?
                new SolidBrush(SkinManager.ColorScheme.AccentColor.Lighten(0.25f)) :
                SkinManager.ColorScheme.AccentBrush :
                new SolidBrush(BlendColor(SkinManager.ColorScheme.AccentColor, SkinManager.SwitchOffDisabledThumbColor, 197)),
                fabBounds);
 
            if (_animationManager.IsAnimating())
            {
                GraphicsPath regionPath = new();
                regionPath.AddEllipse(new Rectangle(fabBounds.X - 1, fabBounds.Y - 1, fabBounds.Width + 3, fabBounds.Height + 2));
                Region fabRegion = new(regionPath);
 
                GraphicsContainer gcont = g.BeginContainer();
                g.SetClip(fabRegion, CombineMode.Replace);
 
                for (int i = 0; i < _animationManager.GetAnimationCount(); i++)
                {
                    double animationValue = _animationManager.GetProgress(i);
                    Point animationSource = _animationManager.GetSource(i);
                    SolidBrush rippleBrush = new(Color.FromArgb((int)(51 - (animationValue * 50)), Color.White));
                    int rippleSize = (int)(animationValue * Width * 2);
                    g.FillEllipse(rippleBrush, new Rectangle(animationSource.X - (rippleSize / 2), animationSource.Y - (rippleSize / 2), rippleSize, rippleSize));
                }
 
                g.EndContainer(gcont);
            }
 
            if (Icon != null)
            {
                g.DrawImage(Icon, new Rectangle((fabBounds.Width / 2) - 11, (fabBounds.Height / 2) - 11, 24, 24));
            }
 
            if (_showAnimationManager.IsAnimating())
            {
                int target = Convert.ToInt32((_mini ? FAB_MINI_SIZE : FAB_SIZE) * _showAnimationManager.GetProgress());
                fabBounds.Width = target == 0 ? 1 : target;
                fabBounds.Height = target == 0 ? 1 : target;
                fabBounds.X = Convert.ToInt32(((_mini ? FAB_MINI_SIZE : FAB_SIZE) / 2) - ((_mini ? FAB_MINI_SIZE : FAB_SIZE) / 2 * _showAnimationManager.GetProgress()));
                fabBounds.Y = Convert.ToInt32(((_mini ? FAB_MINI_SIZE : FAB_SIZE) / 2) - ((_mini ? FAB_MINI_SIZE : FAB_SIZE) / 2 * _showAnimationManager.GetProgress()));
            }
 
            // Clip to a round shape with a 1px padding
            GraphicsPath clipPath = new();
            clipPath.AddEllipse(new Rectangle(fabBounds.X - 1, fabBounds.Y - 1, fabBounds.Width + 3, fabBounds.Height + 3));
            Region = new Region(clipPath);
        }
 
        protected override void OnMouseClick(MouseEventArgs mevent)
        {
            base.OnMouseClick(mevent);
            _animationManager.StartNewAnimation(AnimationDirection.In, mevent.Location);
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            if (DesignMode)
            {
                return;
            }
 
            _mouseHover = ClientRectangle.Contains(e.Location);
            Invalidate();
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (DesignMode)
            {
                return;
            }
 
            _mouseHover = false;
            Invalidate();
        }
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
 
            if (DrawShadows && Parent != null)
            {
                RemoveShadowPaintEvent(Parent, drawShadowOnParent);
                AddShadowPaintEvent(Parent, drawShadowOnParent);
            }
        }
 
        private Point origin;
 
        public new void Hide()
        {
            if (Visible)
            {
                _isHiding = true;
                _showAnimationManager.StartNewAnimation(AnimationDirection.Out);
            }
        }
 
        public new void Show()
        {
            if (!Visible)
            {
                origin = Location;
                _showAnimationManager.StartNewAnimation(AnimationDirection.In);
                Visible = true;
            }
        }
    }
 
    #endregion
}