// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT using System; using System.Drawing; using OpenTK.Input; namespace ClassicalSharp.Gui { /// Represents an individual 2D gui component. public abstract class Widget : GuiElement { public Widget( Game game ) : base( game ) { HorizontalAnchor = Anchor.LeftOrTop; VerticalAnchor = Anchor.LeftOrTop; } /// Whether this widget is currently being moused over. public virtual bool Active { get; set; } /// Whether this widget is prevented from being interacted with. public virtual bool Disabled { get; set; } /// Invoked when this widget is clicked on. Can be null. public ClickHandler OnClick; /// Horizontal coordinate of top left corner in window space. public int X; /// Vertical coordinate of top left corner in window space. public int Y; /// Horizontal length of widget's bounds in window space. public int Width; /// Vertical length of widget's bounds in window space. public int Height; /// Specifies the horizontal reference point for when the widget is resized. public Anchor HorizontalAnchor; /// Specifies the vertical reference point for when the widget is resized. public Anchor VerticalAnchor; /// Horizontal offset from the reference point in window space. public int XOffset = 0; /// Vertical offset from the reference point in window space. public int YOffset = 0; /// Width and height of widget in window space. public Size Size { get { return new Size( Width, Height ); } } /// Coordinate of top left corner of widget's bounds in window space. public Point TopLeft { get { return new Point( X, Y ); } } /// Coordinate of bottom right corner of widget's bounds in window space. public Point BottomRight { get { return new Point( X + Width, Y + Height ); } } /// Specifies the boundaries of the widget in window space. public Rectangle Bounds { get { return new Rectangle( X, Y, Width, Height ); } } /// Moves the widget to the specified window space coordinates. public virtual void MoveTo( int newX, int newY ) { X = newX; Y = newY; } public override void OnResize( int oldWidth, int oldHeight, int width, int height ) { int x = CalcOffset( width, Width, XOffset, HorizontalAnchor ); int y = CalcOffset( height, Height, YOffset, VerticalAnchor ); MoveTo( x, y ); } } }