Rect.cs :  » Game » RealmForge » CrayzEdsGui » Base » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » CrayzEdsGui » Base » Rect.cs
#region LGPL License

/*************************************************************************
    Crazy Eddie's GUI System (http://crayzedsgui.sourceforge.net)
    Copyright (C)2004 Paul D Turner (crayzed@users.sourceforge.net)
  
  C# Port developed by Chris McGuirk (leedgitar@latenitegames.com)
  Compatible with the Axiom 3D Engine (http://axiomengine.sf.net)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*************************************************************************/

#endregion LGPL License

using System;

namespace CrayzEdsGui.Base{
  /// <summary>
  /// Summary description for Rect.
  /// </summary>
  /// In sync with original version as of:
  /// .h:    1.3
  /// .cpp:  1.3 
  public struct Rect {
    // --------------------------
    #region Fields

    public float left, right, top, bottom;

    #endregion Fields
    // --------------------------

    // --------------------------
    #region Constructor

    /// <summary>
    ///     Constructor.
    /// </summary>
    /// <param name="left">Left.</param>
    /// <param name="top">Top.</param>
    /// <param name="right">Right.</param>
    /// <param name="bottom">Bottom.</param>
    public Rect(float left, float top, float right, float bottom) {
      this.left = left;
      this.top = top;
      this.right = right;
      this.bottom = bottom;
    }

    #endregion Constructor
    // --------------------------

    // --------------------------
    #region Properties

    /// <summary>
    ///    Get/Set the top-left corner of the rectangle.
    /// </summary>
    /// <value>Point that holds the x and y position.</value>
    public Point Position {
      get {
        return new Point(left, top);
      }
      set {
        Size size = this.Size;

        left = value.x;
        top = value.y;

        this.Size = size;
      }
    }

    /// <summary>
    ///     Width of this rectangle, from the left side.
    /// </summary>
    public float Width {
      get {
        return right - left;
      }
      set {
        // HACK: test
        if(float.IsNaN(value)) {
          value = 0;
        }

        right = left + value;
      }
    }

    /// <summary>
    ///     Height of this rectangle, from the top edge.
    /// </summary>
    public float Height {
      get {
        return bottom - top;
      }
      set {
        // HACK: test
        if(float.IsNaN(value)) {
          value = 0;
        }

        bottom = top + value;
      }
    }

    /// <summary>
    ///    Get/Set the size of the rect area.
    /// </summary>
    /// <value>The desired size of the rectangle.</value>
    public Size Size {
      get {
        return new Size(this.Width, this.Height);
      }
      set {
        this.Width = value.width;
        this.Height = value.height;
      }
    }

    #endregion Properties
    // --------------------------

    // --------------------------
    #region Methods

    /// <summary>
    ///    Check the size of this rect if it is bigger than <paramref name="size"/>, resize it so it isn't.
    /// </summary>
    /// <param name="size">Describes the maximum dimensions that this Rect should be limited to.</param>
    public void ConstrainSizeMax(Size size) {
      if(this.Width > size.width) {
        this.Width = size.width;
      }

      if(this.Height > size.height) {
        this.Height = size.height;
      }
    }

    /// <summary>
    ///    Check the size of this rect if it is bigger than <paramref name="size"/>, resize it so it isn't.
    /// </summary>
    /// <param name="size">Describes the minimum dimensions that this Rect should be limited to.</param>
    public void ConstrainSizeMin(Size size) {
      if(this.Width < size.width) {
        this.Width = size.width;
      }

      if(this.Height < size.height) {
        this.Height = size.height;
      }
    }

    /// <summary>
    ///    Check the size of this rect if it is bigger than <paramref name="max"/> or
    ///    smaller than <paramref name="min"/>, resize it so it isn't.
    /// </summary>
    /// <param name="max">Describes the maximum dimensions that this Rect should be limited to.</param>
    /// <param name="min">Describes the minimum dimensions that this Rect should be limited to.</param>
    public void ConstrainSize(Size max, Size min) {
      Size currentSize = this.Size;

      if(this.Width > max.width) {
        this.Width = max.width;
      }
      else if(this.Width < min.width) {
        this.Width = min.width;
      }

      if(this.Height > max.height) {
        this.Height = max.height;
      }
      else if(this.Height < min.height) {
        this.Height = min.height;
      }
    }

    /// <summary>
    ///    return a Rect that is the intersection of 'this' Rect with <paramref name="rect"/>.
    /// </summary>
    /// <remarks>
    ///    It can be assumed that if left == right, or top == bottom, or Width == 0, or Height == 0, then
    ///    'this' rect was totally outside <paramref name="rect"/>.
    /// </remarks>
    /// <param name="rect">Rect to test for intersection.</param>
    /// <returns>Instersection rect.</returns>
    public Rect GetIntersection(Rect rect) {
      // check for total exclusion
      if ((right > rect.left) &&
        (left < rect.right) &&
        (bottom > rect.top) &&
        (top < rect.bottom)) {

        Rect temp;

        // fill in temp with the intersection
        temp.left = (left > rect.left) ? left : rect.left;
        temp.right = (right < rect.right) ? right : rect.right;
        temp.top = (top > rect.top) ? top : rect.top;
        temp.bottom = (bottom < rect.bottom) ? bottom : rect.bottom;

        return temp;
      }

      return new Rect(0.0f, 0.0f, 0.0f, 0.0f);
    }

    /// <summary>
    ///     Return true if the given Point falls within this Rect.
    /// </summary>
    /// <param name="point">Point describing the position to test.</param>
    /// <returns>True if the point is within this rectangle, false otherwise..</returns>
    public bool IsPointInRect(Point point) {
      return ((point.x >= left) && 
          (point.x < right) && 
          (point.y >= top) && 
          (point.y < bottom));
    }

    /// <summary>
    ///     Applies an offset this Rect.
    /// </summary>
    /// <param name="point">Point containing the offsets to be applied to the Rect.</param>
    public void Offset(Point point) {
      left  += point.x;
      right  += point.x;
      top    += point.y;
      bottom  += point.y;
    }

    /// <summary>
    ///    Returns a string representation of this Rect object in the form
    ///    "l:[left] t:[top] r:[right] b:[bottom]
    /// </summary>
    /// <returns>A string representation of this Rect object.</returns>
    public override string ToString()
    {
      return string.Format( "l:{0} t:{1} r:{2} b:{3}", left, top, right, bottom );
    }

    /// <summary>
    ///    Parses the string representation of a Rect and returns the corresponding
    ///    Rect object.
    /// </summary>
    /// <param name="data">String representation of a Rect.</param>
    /// <returns>Rect corresponding to the passed in string.</returns>
    public static Rect Parse( string data )
    {
      string[] parameters = data.Split( new char[] { ' ', ':' } );
      Rect rect = new Rect();

      for( int i = 0; i < parameters.Length; i++ )
      {
        if( 0 == parameters[i].CompareTo( "l" ) )
        {
          rect.left = float.Parse( parameters[++i] );
        }
        else if( 0 == parameters[i].CompareTo( "t" ) )
        {
          rect.top = float.Parse( parameters[++i] );
        }
        else if( 0 == parameters[i].CompareTo( "r" ) )
        {
          rect.right = float.Parse( parameters[++i] );
        }
        else if( 0 == parameters[i].CompareTo( "b" ) )
        {
          rect.bottom = float.Parse( parameters[++i] );
        }
      }
      return rect;
    }

    #endregion Methods
    // --------------------------
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.