MouseCursor.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 » MouseCursor.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

#region Using directives

using System;
using System.Text;

#endregion

namespace CrayzEdsGui.Base{

  /// <summary>
  ///    Class that allows access to the GUI system mouse cursor.
  /// </summary>
  /// <remarks>
  ///    The MouseCursor provides functionality to access the position and imagery of the 
  ///    mouse cursor / pointer.
  /// </remarks>
  /// In sync with original version as of:
  /// .h:    1.2
  /// .cpp:  1.2 
  public class MouseCursor {
    #region Fields

    /// <summary>
    ///    Image that is currently set as the mouse cursor.
    /// </summary>
    protected Image cursorImage;
    /// <summary>
    ///    Current location of the cursor.
    /// </summary>
    protected Vector3 position;
    /// <summary>
    ///    True if the cursor will be drawn, else false.
    /// </summary>
    protected bool isVisible;
    /// <summary>
    ///    Specifies the area (in screen pixels) that the mouse can move around in.
    /// </summary>
    protected Rect constraints;

  #endregion Fields

    #region Singleton Implementation

    /// <summary>
    ///    Singlton instance of this class.
    /// </summary>
    private static MouseCursor instance;

    /// <summary>
    ///    Default constructor.
    /// </summary>
    internal MouseCursor() {
      // only create once instance
      if (instance == null) {
        instance = this;

        // default constraint is the whole screen
        constraints = GuiSystem.Instance.Renderer.Rect;

        // mouse default to middle of the constrained area
        position.x = constraints.Width * 0.5f;
        position.y = constraints.Height * 0.5f;
        position.z = 1.0f;

        // mouse defaults to visible
        isVisible = true;

        Logger.Instance.LogEvent("MouseCursor singleton created.");
      }
    }

    /// <summary>
    ///    Gets the singleton class instance.
    /// </summary>
    /// <value></value>
    public static MouseCursor Instance {
      get {
        return instance;
      }
    }

  #endregion Singleton Implementation

    #region Public Properties

    /// <summary>
    ///    Gets/Sets the current constraint area of the mouse cursor.
    /// </summary>
    /// <value><see cref="Rect"/> object describing the active area that the mouse cursor is constrained to.</value>
    public Rect ConstraintArea {
      get {
        return constraints;
      }
      set {
        constraints = value;
      }
    }

    /// <summary>
    ///    Returns whether the mouse cursor is visible.
    /// </summary>
    /// <value>True if the cursor is visible, false otherwise.</value>
    public bool IsVisible {
      get {
        return isVisible;
      }
    }

    /// <summary>
    ///    Gets/Sets the current mouse cursor position.
    /// </summary>
    /// <value><see cref="Point"/> object describing the mouse cursor position.</value>
    public Point Position {
      get {
        return new Point(position.x, position.y);
      }
      set {
        position.x = value.x;
        position.y = value.y;

        ConstrainPosition();
      }
    }

  #endregion Public Properties

    #region Public Methods

    /// <summary>
    ///    Makes the cursor draw itself.
    /// </summary>
    public void Draw() {
      if(isVisible && (cursorImage != null)) {
        // TODO: Add overload with default color rect
        cursorImage.Draw(
          position, 
          GuiSystem.Instance.Renderer.Rect, 
          new ColorRect());
      }
    }

    /// <summary>
    ///    Hides the mouse cursor.
    /// </summary>
    public void Hide() {
      isVisible = false;
    }

    /// <summary>
    ///    Offset the mouse cursor position by the deltas specified in <paramref name="offset"/>.
    /// </summary>
    /// <param name="offset">Point which describes the amount to move the cursor in each axis.</param>
    public void OffsetPosition(Point offset) {
      position.x += offset.x;
      position.y += offset.y;

      ConstrainPosition();
    }

    /// <summary>
    ///    Set the current mouse cursor image.
    /// </summary>
    /// <param name="imagesetName">
    ///    The name of the <see cref="Imageset"/> that contains the desired <see cref="Image"/>.
    /// </param>
    /// <param name="imageName">Name of a desired image within the specified image set.</param>
    /// <exception cref="UnknownObjectException">
    ///    Thrown if <paramref name="imagesetName"/> is not known, or if <paramref name="imagesetName"/> 
    ///    contains no <see cref="Image"/> named <paramref name="imageName"/>.
    ///  </exception>
    public void SetImage(string imagesetName, string imageName) {
      cursorImage = ImagesetManager.Instance.GetImageset(imagesetName).GetImage(imageName);
    }

    /// <summary>
    ///    Set the current mouse cursor image.
    /// </summary>
    /// <param name="image">Reference to an existing <see cref="Image"/> object to use.</param>
    public void SetImage(Image image) {
      cursorImage = image;
    }

    /// <summary>
    ///    Shows the mouse cursor.
    /// </summary>
    public void Show() {
      isVisible = true;
    }

  #endregion Public Methods

    #region Private Methods

    /// <summary>
    ///    Checks the mouse cursor position is within the current 'constrain' Rect and adjusts as required.
    /// </summary>
    private void ConstrainPosition() {
      if (position.x >= constraints.right) {
        position.x = constraints.right - 1;
      }

      if (position.y >= constraints.bottom) {
        position.y = constraints.bottom - 1;
      }

      if (position.y < constraints.top) {
        position.y = constraints.top;
      }

      if (position.x < constraints.left) {
        position.x = constraints.left;
      }
    }

  #endregion Private 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.