summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/Button.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/Button.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/Button.cs92
1 files changed, 92 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/Button.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/Button.cs
new file mode 100644
index 0000000..310dc00
--- /dev/null
+++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/Button.cs
@@ -0,0 +1,92 @@
+using System;
+
+namespace MonoGame.Extended.Gui.Controls
+{
+ public class Button : ContentControl
+ {
+ public Button()
+ {
+ }
+
+ public event EventHandler Clicked;
+ public event EventHandler PressedStateChanged;
+
+ private bool _isPressed;
+ public bool IsPressed
+ {
+ get => _isPressed;
+ set
+ {
+ if (_isPressed != value)
+ {
+ _isPressed = value;
+ PressedStyle?.ApplyIf(this, _isPressed);
+ PressedStateChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+ }
+
+ private ControlStyle _pressedStyle;
+ public ControlStyle PressedStyle
+ {
+ get => _pressedStyle;
+ set
+ {
+ if (_pressedStyle != value)
+ {
+ _pressedStyle = value;
+ PressedStyle?.ApplyIf(this, _isPressed);
+ }
+ }
+ }
+
+ private bool _isPointerDown;
+
+ public override bool OnPointerDown(IGuiContext context, PointerEventArgs args)
+ {
+ if (IsEnabled)
+ {
+ _isPointerDown = true;
+ IsPressed = true;
+ }
+
+ return base.OnPointerDown(context, args);
+ }
+
+ public override bool OnPointerUp(IGuiContext context, PointerEventArgs args)
+ {
+ _isPointerDown = false;
+
+ if (IsPressed)
+ {
+ IsPressed = false;
+
+ if (BoundingRectangle.Contains(args.Position) && IsEnabled)
+ Click();
+ }
+
+ return base.OnPointerUp(context, args);
+ }
+
+ public override bool OnPointerEnter(IGuiContext context, PointerEventArgs args)
+ {
+ if (IsEnabled && _isPointerDown)
+ IsPressed = true;
+
+ return base.OnPointerEnter(context, args);
+ }
+
+ public override bool OnPointerLeave(IGuiContext context, PointerEventArgs args)
+ {
+ if (IsEnabled)
+ IsPressed = false;
+
+ return base.OnPointerLeave(context, args);
+ }
+
+ public void Click()
+ {
+ Clicked?.Invoke(this, EventArgs.Empty);
+ }
+ }
+} \ No newline at end of file