Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIMouseCursor.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIMouseCursor.cpp
00003         created:        21/2/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements MouseCursor class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIMouseCursor.h"
00027 #include "CEGUIExceptions.h"
00028 #include "CEGUILogger.h"
00029 #include "CEGUISystem.h"
00030 #include "CEGUIRenderer.h"
00031 #include "CEGUIImagesetManager.h"
00032 #include "CEGUIImageset.h"
00033 #include "CEGUIImage.h"
00034 
00035 // Start of CEGUI namespace section
00036 namespace CEGUI
00037 {
00038 const String MouseCursor::EventNamespace("MouseCursor");
00039 
00040 /*************************************************************************
00041         Static Data Definitions
00042 *************************************************************************/
00043 // singleton instance pointer
00044 template<> MouseCursor* Singleton<MouseCursor>::ms_Singleton    = NULL;
00045 
00046 
00047 /*************************************************************************
00048         Event name constants
00049 *************************************************************************/
00050 const String MouseCursor::EventImageChanged( (utf8*)"ImageChanged" );
00051 
00052 
00053 /*************************************************************************
00054         constructor
00055 *************************************************************************/
00056 MouseCursor::MouseCursor(void)
00057 {
00058         // default constraint is to whole screen
00059         d_constraints = System::getSingleton().getRenderer()->getRect();
00060 
00061         // mouse defaults to middle of the constrained area
00062         d_position.d_x = d_constraints.getWidth() / 2;
00063         d_position.d_y = d_constraints.getHeight() / 2;
00064         d_position.d_z = 1.0f;
00065 
00066         // mouse defaults to visible
00067         d_visible = true;
00068 
00069         // no default image though
00070         d_cursorImage = NULL;
00071 
00072         // add events
00073         addMouseCursorEvents();
00074 
00075         Logger::getSingleton().logEvent((utf8*)"CEGUI::MouseCursor singleton created.");
00076 }
00077 
00078 
00079 /*************************************************************************
00080         Destructor
00081 *************************************************************************/
00082 MouseCursor::~MouseCursor(void)
00083 {
00084         Logger::getSingleton().logEvent((utf8*)"CEGUI::MouseCursor singleton destroyed.");
00085 }
00086 
00087 
00088 /*************************************************************************
00089         Set the current mouse cursor image
00090 *************************************************************************/
00091 void MouseCursor::setImage(const Image* image)
00092 {
00093         d_cursorImage = image;
00094         MouseCursorEventArgs args(this);
00095         args.image = image;
00096         onImageChanged(args);
00097 }
00098 
00099 
00100 /*************************************************************************
00101         Set the current mouse cursor image
00102 *************************************************************************/
00103 void MouseCursor::setImage(const String& imageset, const String& image_name)
00104 {
00105         setImage(&ImagesetManager::getSingleton().getImageset(imageset)->getImage(image_name));
00106 }
00107 
00108 
00109 /*************************************************************************
00110         Draw the mouse cursor
00111 *************************************************************************/
00112 void MouseCursor::draw(void) const
00113 {
00114         if (d_visible && (d_cursorImage != NULL))
00115         {
00116                 d_cursorImage->draw( d_position, System::getSingleton().getRenderer()->getRect() );
00117         }
00118 }
00119 
00120 
00121 /*************************************************************************
00122         Set the current mouse cursor position
00123 *************************************************************************/
00124 void MouseCursor::setPosition(const Point& position)
00125 {
00126         d_position.d_x = position.d_x;
00127         d_position.d_y = position.d_y;
00128         constrainPosition();
00129 }
00130 
00131 
00132 /*************************************************************************
00133         Offset the mouse cursor position by the deltas specified in 'offset'.
00134 *************************************************************************/
00135 void MouseCursor::offsetPosition(const Point& offset)
00136 {
00137         d_position.d_x += offset.d_x;
00138         d_position.d_y += offset.d_y;
00139         constrainPosition();
00140 }
00141 
00142 
00143 /*************************************************************************
00144         Checks the mouse cursor position is within the current 'constrain'
00145         Rect and adjusts as required.
00146 *************************************************************************/
00147 void MouseCursor::constrainPosition(void)
00148 {
00149         if (d_position.d_x >= d_constraints.d_right)
00150                 d_position.d_x = d_constraints.d_right -1;
00151 
00152         if (d_position.d_y >= d_constraints.d_bottom)
00153                 d_position.d_y = d_constraints.d_bottom -1;
00154 
00155         if (d_position.d_y < d_constraints.d_top)
00156                 d_position.d_y = d_constraints.d_top;
00157 
00158         if (d_position.d_x < d_constraints.d_left)
00159                 d_position.d_x = d_constraints.d_left;
00160 }
00161 
00162 
00163 /*************************************************************************
00164         Set the area that the mouse cursor is constrained to.
00165 *************************************************************************/
00166 void MouseCursor::setConstraintArea(const Rect* area)
00167 {
00168         Rect renderer_area = System::getSingleton().getRenderer()->getRect();
00169 
00170         if (area == NULL)
00171         {
00172                 d_constraints = renderer_area;
00173         }
00174         else
00175         {
00176                 d_constraints = area->getIntersection(renderer_area);
00177         }
00178 
00179         constrainPosition();
00180 }
00181 
00182 
00183 /*************************************************************************
00184         Return the current mouse cursor position in display resolution
00185         independant values.     
00186 *************************************************************************/
00187 Point MouseCursor::getDisplayIndependantPosition(void) const
00188 {
00189         Size dsz(System::getSingleton().getRenderer()->getSize());
00190 
00191         return Point(d_position.d_x / (dsz.d_width - 1.0f), d_position.d_y / (dsz.d_height - 1.0f));
00192 }
00193 
00194 
00195 /*************************************************************************
00196         Add MouseCursor events
00197 *************************************************************************/
00198 void MouseCursor::addMouseCursorEvents(void)
00199 {
00200         // mouse cursor events
00201         addEvent(EventImageChanged);
00202 }
00203 
00204 
00206 /*************************************************************************
00207 
00208         Begin event triggers section
00209 
00210 *************************************************************************/
00212 
00213 void MouseCursor::onImageChanged(MouseCursorEventArgs& e)
00214 {
00215         fireEvent(EventImageChanged, e, EventNamespace);
00216 }
00217 
00218 
00219 MouseCursor& MouseCursor::getSingleton(void)
00220 {
00221         return Singleton<MouseCursor>::getSingleton();
00222 }
00223 
00224 
00225 MouseCursor* MouseCursor::getSingletonPtr(void)
00226 {
00227         return Singleton<MouseCursor>::getSingletonPtr();
00228 }
00229 
00230 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:06 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1