Set up an event or delegate on your ButtonState
class, that your buttons thus can call.
Change your automatic State
property in Button
to one with a backing store that hooks and unhooks the this event or delegate whenever the state is changed.
public class Button
{
public Button()
{
State = new ButtonStateNormal(this);
}
private ButtonState _state;
public event EventHandler<MouseEventArgs> Click;
public ButtonState State
{
protected get
{
return _state;
}
set
{
if (_state == value)
return;
if (_state != null)
_state.Click -= OnClick;
if (value != null)
value.Click += OnClick;
_state = value;
}
}
protected virtual void OnClick(MouseEventArgs e)
{
if (Click != null)
Click(this, e);
}
public void CheckForInput(){
State.CheckForInput();
}
}
public abstract class ButtonState
{
public abstract void CheckForInput();
public ClickDelegate Click;
public delegate void ClickDelegate(MouseEventArgs a);
}
And then you can do this in your concrete State classes
public class ButtonStateDown : ButtonState
{
Button button;
public ButtonStateDown(Button button)
{
this.button = button;
}
public void CheckForInput()
{
//...
//Check for user input, fire the event of the button and change the state
//...
if(Click != null)
Click(new MouseEventArgs(mouse.X, mouse.Y))
button.State = new ButtonStateNormal(button);
}
}