Files
MonoGame.Extended/Source/MonoGame.Extended.Gui/Controls/GuiCheckBox.cs
T
tspayne87andDylan Wilson 46f3f99b39 [GUI] Adding support for parent propegation, the idea of forms and textbox highlighting (#439)
* 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.
2017-11-21 10:50:24 +10:00

73 lines
2.2 KiB
C#

using System;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Gui.Controls
{
public class GuiCheckBox : GuiControl
{
public GuiCheckBox()
{
}
public GuiCheckBox(GuiSkin skin)
: base(skin)
{
}
public event EventHandler CheckStateChanged;
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
if (_isChecked != value)
{
_isChecked = value;
CheckedStyle?.ApplyIf(this, _isChecked);
CheckStateChanged?.Invoke(this, EventArgs.Empty);
}
}
}
private GuiControlStyle _checkedStyle;
public GuiControlStyle CheckedStyle
{
get { return _checkedStyle; }
set
{
if (_checkedStyle != value)
{
_checkedStyle = value;
CheckedStyle?.ApplyIf(this, _isChecked);
}
}
}
public override bool OnPointerUp(IGuiContext context, GuiPointerEventArgs args)
{
if (IsFocused && BoundingRectangle.Contains(args.Position))
IsChecked = !IsChecked;
return base.OnPointerUp(context, args);
}
protected override void DrawBackground(IGuiContext context, IGuiRenderer renderer, float deltaSeconds)
{
var boundingRectangle = BoundingRectangle;
var checkRectangle = new Rectangle(boundingRectangle.X, boundingRectangle.Y, BackgroundRegion.Width, BackgroundRegion.Height);
renderer.DrawRegion(BackgroundRegion, checkRectangle, Color);
}
protected override void DrawForeground(IGuiContext context, IGuiRenderer renderer, float deltaSeconds, TextInfo textInfo)
{
textInfo.Position = BoundingRectangle.Location.ToVector2() +
new Vector2(BackgroundRegion.Width + 5, BackgroundRegion.Height * 0.5f - textInfo.Font.LineHeight * 0.5f);
base.DrawForeground(context, renderer, deltaSeconds, textInfo);
}
}
}