using System; using Orvid.Graphics; using System.Collections.Generic; namespace OForms.Controls { /// /// An abstract class that represents a Control and it's Children. /// public abstract class Control : IDisposable { #region Events /// /// The event that occurs when the control is clicked. /// public event MouseEvent Click; /// /// The event that occurs when a mouse /// enters the bounds of the control. /// public event MouseEvent MouseEnter; /// /// The event that occurs when a mouse /// leaves the bounds of the control. /// public event MouseEvent MouseLeave; /// /// The event that occurs when a mouse /// button is pressed down over the control. /// public event MouseEvent MouseDown; /// /// The event that occurs when a mouse /// button is released, but only if /// the MouseDown event was handled /// by this control. /// public event MouseEvent MouseUp; /// /// The event that occurs just before this /// control is disposed of. /// public event DisposingEvent BeforeDispose; /// /// The event that occurs directly after /// this control is disposed of. /// public event DisposingEvent AfterDispose; /// /// This event occurs directly before the /// window containing this control is closed. /// public event DisposingEvent Closing; /// /// The dummy MouseEvent method. /// /// The location of the mouse. /// The current state of the mouse buttons. private void DummyMouseEvent(Vec2 loc, MouseButtons button) { } /// /// The dummy DisposingEvent method. /// private void DummyDisposingEvent() { } /// /// Invokes the Click event. /// /// Location of the mouse. /// The current state of the mouse buttons. internal void DoClick(Vec2 v, MouseButtons b) { Click.Invoke(v, b); } /// /// Invokes the MouseEnter event. /// /// Location of the mouse. /// The current state of the mouse buttons. internal void DoMouseEnter(Vec2 v, MouseButtons b) { MouseEnter.Invoke(v, b); } /// /// Invokes the MouseLeave event. /// /// Location of the mouse. /// The current state of the mouse buttons. internal void DoMouseLeave(Vec2 v, MouseButtons b) { MouseLeave.Invoke(v, b); } /// /// Invokes the MouseDown event. /// /// Location of the mouse. /// The current state of the mouse buttons. internal void DoMouseDown(Vec2 v, MouseButtons b) { MouseDown.Invoke(v, b); } /// /// Invokes the MouseUp event. /// /// Location of the mouse. /// The current state of the mouse buttons. internal void DoMouseUp(Vec2 v, MouseButtons b) { MouseUp.Invoke(v, b); } /// /// Invokes the BeforeDispose event. /// internal void DoBeforeDispose() { BeforeDispose.Invoke(); } /// /// Invokes the AfterDispose event. /// internal void DoAfterDispose() { AfterDispose.Invoke(); } /// /// Invokes the Closing event. /// internal void DoClosing() { Closing.Invoke(); } #endregion public object Parent; private Vec2 iSize; /// /// The size of the control. /// public Vec2 Size { get { return iSize; } set { iSize = value; } } private int iX; /// /// The X location of this control in it's parent container. /// public int X { get { return iX; } set { iX = value; } } private int iY; /// /// The Y location of this control in it's parent container. /// public int Y { get { return iY; } set { iY = value; } } /// /// The graphics buffer for this control. /// protected Image Buffer; private BoundingBox iBounds; /// /// The Bounds for this control. /// public BoundingBox Bounds { get { return iBounds; } set { iBounds = value; } } private bool iIsIn = false; /// /// True if the mouse is currently in the control. /// internal bool IsIn { get { return iIsIn; } set { iIsIn = value; } } private bool iIsMouseDown = false; /// /// True if the mouse pressed down on this control. /// internal bool IsMouseDown { get { return iIsMouseDown; } set { iIsMouseDown = value; } } /// /// The child controls of this control. /// public List Children = new List(); /// /// The default constructor. /// /// The location of the control in it's parent container. /// The size of the control. public Control(Vec2 loc, Vec2 size) { this.X = loc.X; this.Y = loc.Y; this.Size = size; this.Closing = new DisposingEvent(DummyDisposingEvent); this.BeforeDispose = new DisposingEvent(DummyDisposingEvent); this.AfterDispose = new DisposingEvent(DummyDisposingEvent); this.Click = new MouseEvent(DummyMouseEvent); this.MouseEnter = new MouseEvent(DummyMouseEvent); this.MouseLeave = new MouseEvent(DummyMouseEvent); this.MouseDown = new MouseEvent(DummyMouseEvent); this.MouseUp = new MouseEvent(DummyMouseEvent); this.Bounds = new BoundingBox(this.X, this.X + Size.X, this.Y + Size.Y, this.Y); } /// /// Draws the control on the specified image. /// /// The image to draw this control on. public abstract void Draw(Image i); /// /// Disposes of the resources of this control, and all child controls. /// public virtual void Dispose() { this.Bounds = null; this.Buffer = null; foreach (Control c in Children) { c.DoBeforeDispose(); c.Dispose(); c.DoAfterDispose(); } this.Children = null; } } }