mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
* Adding in changes to deal with custom control types for loading from a GuiScreen, also adding in block text if a width for the element exists. Adding in Padding to some elements to give the developer some more options in styles. Adding in some changes to the textbox to deal with selecting a section of text. * Adding in a few changes to deal with propagation down the GuiControl tree, this was added to most events. Also, adding in the idea of a form and submit button to deal with keyboard controls when inside of a form. This gives simalar behavier to how forms work in the browser. * Adding in the enter key skip for the textbox.
138 lines
3.8 KiB
C#
138 lines
3.8 KiB
C#
using System;
|
|
using Microsoft.Xna.Framework;
|
|
using MonoGame.Extended.TextureAtlases;
|
|
|
|
namespace MonoGame.Extended.Gui.Controls
|
|
{
|
|
public class GuiButton : GuiControl
|
|
{
|
|
public GuiButton()
|
|
: base(null)
|
|
{
|
|
}
|
|
|
|
public GuiButton(GuiSkin skin)
|
|
: base(skin)
|
|
{
|
|
}
|
|
|
|
private Point _iconPosition;
|
|
|
|
public Color IconColor { get; set; } = Color.White;
|
|
|
|
private TextureRegion2D _iconRegion;
|
|
public TextureRegion2D IconRegion
|
|
{
|
|
get { return _iconRegion; }
|
|
set
|
|
{
|
|
if (_iconRegion != value)
|
|
{
|
|
_iconRegion = value;
|
|
RecalculateIconPosition();
|
|
}
|
|
}
|
|
}
|
|
|
|
public event EventHandler Clicked;
|
|
public event EventHandler PressedStateChanged;
|
|
|
|
private bool _isPressed;
|
|
public bool IsPressed
|
|
{
|
|
get { return _isPressed; }
|
|
set
|
|
{
|
|
if (_isPressed != value)
|
|
{
|
|
_isPressed = value;
|
|
PressedStyle?.ApplyIf(this, _isPressed);
|
|
PressedStateChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
private GuiControlStyle _pressedStyle;
|
|
public GuiControlStyle PressedStyle
|
|
{
|
|
get { return _pressedStyle; }
|
|
set
|
|
{
|
|
if (_pressedStyle != value)
|
|
{
|
|
_pressedStyle = value;
|
|
PressedStyle?.ApplyIf(this, _isPressed);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool _isPointerDown;
|
|
|
|
public override bool OnPointerDown(IGuiContext context, GuiPointerEventArgs args)
|
|
{
|
|
if (IsEnabled)
|
|
{
|
|
_isPointerDown = true;
|
|
IsPressed = true;
|
|
}
|
|
return base.OnPointerDown(context, args);
|
|
}
|
|
|
|
public override bool OnPointerUp(IGuiContext context, GuiPointerEventArgs args)
|
|
{
|
|
_isPointerDown = false;
|
|
|
|
if (IsPressed)
|
|
{
|
|
IsPressed = false;
|
|
|
|
if (BoundingRectangle.Contains(args.Position) && IsEnabled)
|
|
TriggerClicked();
|
|
}
|
|
return base.OnPointerUp(context, args);
|
|
}
|
|
|
|
public override bool OnPointerEnter(IGuiContext context, GuiPointerEventArgs args)
|
|
{
|
|
if (IsEnabled && _isPointerDown)
|
|
IsPressed = true;
|
|
|
|
return base.OnPointerEnter(context, args);
|
|
}
|
|
|
|
public override bool OnPointerLeave(IGuiContext context, GuiPointerEventArgs args)
|
|
{
|
|
if (IsEnabled)
|
|
IsPressed = false;
|
|
return base.OnPointerLeave(context, args);
|
|
}
|
|
|
|
public void TriggerClicked()
|
|
{
|
|
Clicked?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
protected override void OnSizeChanged()
|
|
{
|
|
if (IconRegion != null)
|
|
RecalculateIconPosition();
|
|
}
|
|
|
|
private void RecalculateIconPosition()
|
|
{
|
|
var x = (BoundingRectangle.Width - IconRegion.Width) / 2;
|
|
var y = (BoundingRectangle.Height - IconRegion.Height) / 2;
|
|
_iconPosition = new Point(x, y);
|
|
}
|
|
|
|
protected override void DrawForeground(IGuiContext context, IGuiRenderer renderer, float deltaSeconds, TextInfo textInfo)
|
|
{
|
|
base.DrawForeground(context, renderer, deltaSeconds, textInfo);
|
|
|
|
if (IconRegion != null)
|
|
{
|
|
renderer.DrawRegion(IconRegion, new Rectangle(BoundingRectangle.Location + _iconPosition + Offset.ToPoint(), IconRegion.Bounds.Size), IconColor);
|
|
}
|
|
}
|
|
}
|
|
} |