CheckboxWidget delegate methods.

This commit is contained in:
Paul Chote
2011-01-06 11:40:06 +13:00
parent 7443b3ce89
commit 0cef2e4f53
6 changed files with 78 additions and 140 deletions

View File

@@ -11,6 +11,7 @@
using System;
using System.Drawing;
using OpenRA.Graphics;
using System.Reflection;
namespace OpenRA.Widgets
{
@@ -19,7 +20,12 @@ namespace OpenRA.Widgets
public string Text = "";
public int baseLine = 1;
public bool Bold = false;
public Func<bool> Checked = () => false;
public Func<bool> IsChecked = () => false;
public event Action<bool> OnChange = _ => {};
object boundObject;
bool boundReadOnly;
FieldInfo boundField;
public override void DrawInner( WorldRenderer wr )
{
@@ -35,19 +41,37 @@ namespace OpenRA.Widgets
new float2(rect.Left + rect.Height * 1.5f,
pos.Y - baseLine + (Bounds.Height - textSize.Y)/2), Color.White);
if (Checked())
if ((boundObject != null && (bool)boundField.GetValue(boundObject)) || IsChecked())
WidgetUtils.DrawRGBA(
ChromeProvider.GetImage("checkbox", "checked"),
new float2(rect.Left + 2, rect.Top + 2));
}
public void Bind(object obj, string field) { Bind(obj, field, false); }
public void BindReadOnly(object obj, string field) { Bind(obj, field, true); }
void Bind(object obj, string field, bool readOnly)
{
boundObject = obj;
boundReadOnly = readOnly;
boundField = obj.GetType().GetField(field);
}
// TODO: SliderWidget doesn't support delegate methods for mouse input
public override bool HandleMouseInput(MouseInput mi)
{
// Checkboxes require lmb
if (mi.Button != MouseButton.Left)
if (mi.Button != MouseButton.Left || mi.Event != MouseInputEvent.Down)
return false;
return base.HandleMouseInput(mi);
bool newVal = !IsChecked();
if (boundObject != null && !boundReadOnly)
{
newVal = !(bool)boundField.GetValue(boundObject);
boundField.SetValue(boundObject, newVal);
}
OnChange(newVal);
return true;
}
public CheckboxWidget() : base() { }
@@ -56,7 +80,6 @@ namespace OpenRA.Widgets
: base(other)
{
Text = other.Text;
Checked = other.Checked;
}
public override Widget Clone() { return new CheckboxWidget(this); }