summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/ToggleButton.cs
blob: 5858894be3ffd62be00cd69ee4e3086741fa6ced (plain)
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
using System;

namespace MonoGame.Extended.Gui.Controls
{
    public class ToggleButton : Button
    {
        public ToggleButton()
        {
        }

        public event EventHandler CheckedStateChanged;

        private bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                if (_isChecked != value)
                {
                    _isChecked = value;
                    CheckedStyle?.ApplyIf(this, _isChecked);
                    CheckedStateChanged?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        private ControlStyle _checkedStyle;
        public ControlStyle CheckedStyle
        {
            get { return _checkedStyle; }
            set
            {
                if (_checkedStyle != value)
                {
                    _checkedStyle = value;
                    CheckedStyle?.ApplyIf(this, _isChecked);
                }
            }
        }

        private ControlStyle _checkedHoverStyle;
        public ControlStyle CheckedHoverStyle
        {
            get { return _checkedHoverStyle; }
            set
            {
                if (_checkedHoverStyle != value)
                {
                    _checkedHoverStyle = value;
                    CheckedHoverStyle?.ApplyIf(this, _isChecked && IsHovered);
                }
            }
        }

        public override bool OnPointerUp(IGuiContext context, PointerEventArgs args)
        {
            base.OnPointerUp(context, args);

            if (BoundingRectangle.Contains(args.Position))
            {
                HoverStyle?.Revert(this);
                CheckedHoverStyle?.Revert(this);

                IsChecked = !IsChecked;

                if (IsChecked)
                    CheckedHoverStyle?.Apply(this);
                else
                    HoverStyle?.Apply(this);
            }

            return true;
        }

        public override bool OnPointerEnter(IGuiContext context, PointerEventArgs args)
        {
            if (IsChecked)
            {
                CheckedHoverStyle?.Apply(this);
                return true;
            }

            return base.OnPointerEnter(context, args);
        }

        public override bool OnPointerLeave(IGuiContext context, PointerEventArgs args)
        {
            if (IsChecked)
            {
                CheckedHoverStyle?.Revert(this);
                return true;
            }

            return base.OnPointerLeave(context, args);
        }
    }
}