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 Rect screenArea(System::getSingleton().getRenderer()->getRect()); 00059 00060 // mouse defaults to middle of the constrained area 00061 d_position.d_x = screenArea.getWidth() / 2; 00062 d_position.d_y = screenArea.getHeight() / 2; 00063 d_position.d_z = 1.0f; 00064 00065 // default constraint is to whole screen 00066 setConstraintArea(&screenArea); 00067 00068 // mouse defaults to visible 00069 d_visible = true; 00070 00071 // no default image though 00072 d_cursorImage = NULL; 00073 00074 // add events 00075 addMouseCursorEvents(); 00076 00077 Logger::getSingleton().logEvent((utf8*)"CEGUI::MouseCursor singleton created."); 00078 } 00079 00080 00081 /************************************************************************* 00082 Destructor 00083 *************************************************************************/ 00084 MouseCursor::~MouseCursor(void) 00085 { 00086 Logger::getSingleton().logEvent((utf8*)"CEGUI::MouseCursor singleton destroyed."); 00087 } 00088 00089 00090 /************************************************************************* 00091 Set the current mouse cursor image 00092 *************************************************************************/ 00093 void MouseCursor::setImage(const Image* image) 00094 { 00095 d_cursorImage = image; 00096 MouseCursorEventArgs args(this); 00097 args.image = image; 00098 onImageChanged(args); 00099 } 00100 00101 00102 /************************************************************************* 00103 Set the current mouse cursor image 00104 *************************************************************************/ 00105 void MouseCursor::setImage(const String& imageset, const String& image_name) 00106 { 00107 setImage(&ImagesetManager::getSingleton().getImageset(imageset)->getImage(image_name)); 00108 } 00109 00110 00111 /************************************************************************* 00112 Draw the mouse cursor 00113 *************************************************************************/ 00114 void MouseCursor::draw(void) const 00115 { 00116 if (d_visible && (d_cursorImage != NULL)) 00117 { 00118 d_cursorImage->draw( d_position, System::getSingleton().getRenderer()->getRect() ); 00119 } 00120 } 00121 00122 00123 /************************************************************************* 00124 Set the current mouse cursor position 00125 *************************************************************************/ 00126 void MouseCursor::setPosition(const Point& position) 00127 { 00128 d_position.d_x = position.d_x; 00129 d_position.d_y = position.d_y; 00130 constrainPosition(); 00131 } 00132 00133 00134 /************************************************************************* 00135 Offset the mouse cursor position by the deltas specified in 'offset'. 00136 *************************************************************************/ 00137 void MouseCursor::offsetPosition(const Point& offset) 00138 { 00139 d_position.d_x += offset.d_x; 00140 d_position.d_y += offset.d_y; 00141 constrainPosition(); 00142 } 00143 00144 00145 /************************************************************************* 00146 Checks the mouse cursor position is within the current 'constrain' 00147 Rect and adjusts as required. 00148 *************************************************************************/ 00149 void MouseCursor::constrainPosition(void) 00150 { 00151 Rect absarea(getConstraintArea()); 00152 00153 if (d_position.d_x >= absarea.d_right) 00154 d_position.d_x = absarea.d_right -1; 00155 00156 if (d_position.d_y >= absarea.d_bottom) 00157 d_position.d_y = absarea.d_bottom -1; 00158 00159 if (d_position.d_y < absarea.d_top) 00160 d_position.d_y = absarea.d_top; 00161 00162 if (d_position.d_x < absarea.d_left) 00163 d_position.d_x = absarea.d_left; 00164 } 00165 00166 00167 /************************************************************************* 00168 Set the area that the mouse cursor is constrained to. 00169 *************************************************************************/ 00170 void MouseCursor::setConstraintArea(const Rect* area) 00171 { 00172 Rect renderer_area = System::getSingleton().getRenderer()->getRect(); 00173 00174 if (area == NULL) 00175 { 00176 d_constraints.d_min.d_x = cegui_reldim(renderer_area.d_left / renderer_area.getWidth()); 00177 d_constraints.d_min.d_y = cegui_reldim(renderer_area.d_top / renderer_area.getHeight()); 00178 d_constraints.d_max.d_x = cegui_reldim(renderer_area.d_right / renderer_area.getWidth()); 00179 d_constraints.d_max.d_y = cegui_reldim(renderer_area.d_bottom / renderer_area.getHeight()); 00180 } 00181 else 00182 { 00183 Rect finalArea(area->getIntersection(renderer_area)); 00184 d_constraints.d_min.d_x = cegui_reldim(finalArea.d_left / renderer_area.getWidth()); 00185 d_constraints.d_min.d_y = cegui_reldim(finalArea.d_top / renderer_area.getHeight()); 00186 d_constraints.d_max.d_x = cegui_reldim(finalArea.d_right / renderer_area.getWidth()); 00187 d_constraints.d_max.d_y = cegui_reldim(finalArea.d_bottom / renderer_area.getHeight()); 00188 } 00189 00190 constrainPosition(); 00191 } 00192 00193 00194 /************************************************************************* 00195 Set the area that the mouse cursor is constrained to. 00196 *************************************************************************/ 00197 void MouseCursor::setUnifiedConstraintArea(const URect* area) 00198 { 00199 Rect renderer_area = System::getSingleton().getRenderer()->getRect(); 00200 00201 if (area) 00202 { 00203 d_constraints = *area; 00204 } 00205 else 00206 { 00207 d_constraints.d_min.d_x = cegui_reldim(renderer_area.d_left / renderer_area.getWidth()); 00208 d_constraints.d_min.d_y = cegui_reldim(renderer_area.d_top / renderer_area.getHeight()); 00209 d_constraints.d_max.d_x = cegui_reldim(renderer_area.d_right / renderer_area.getWidth()); 00210 d_constraints.d_max.d_y = cegui_reldim(renderer_area.d_bottom / renderer_area.getHeight()); 00211 } 00212 00213 constrainPosition(); 00214 } 00215 00216 /************************************************************************* 00217 Set the area that the mouse cursor is constrained to. 00218 *************************************************************************/ 00219 Rect MouseCursor::getConstraintArea(void) const 00220 { 00221 return Rect(d_constraints.asAbsolute(System::getSingleton().getRenderer()->getSize())); 00222 } 00223 00224 /************************************************************************* 00225 Set the area that the mouse cursor is constrained to. 00226 *************************************************************************/ 00227 const URect& MouseCursor::getUnifiedConstraintArea(void) const 00228 { 00229 return d_constraints; 00230 } 00231 00232 /************************************************************************* 00233 Return the current mouse cursor position in display resolution 00234 independant values. 00235 *************************************************************************/ 00236 Point MouseCursor::getDisplayIndependantPosition(void) const 00237 { 00238 Size dsz(System::getSingleton().getRenderer()->getSize()); 00239 00240 return Point(d_position.d_x / (dsz.d_width - 1.0f), d_position.d_y / (dsz.d_height - 1.0f)); 00241 } 00242 00243 00244 /************************************************************************* 00245 Add MouseCursor events 00246 *************************************************************************/ 00247 void MouseCursor::addMouseCursorEvents(void) 00248 { 00249 // mouse cursor events 00250 addEvent(EventImageChanged); 00251 } 00252 00253 00255 /************************************************************************* 00256 00257 Begin event triggers section 00258 00259 *************************************************************************/ 00261 00262 void MouseCursor::onImageChanged(MouseCursorEventArgs& e) 00263 { 00264 fireEvent(EventImageChanged, e, EventNamespace); 00265 } 00266 00267 00268 MouseCursor& MouseCursor::getSingleton(void) 00269 { 00270 return Singleton<MouseCursor>::getSingleton(); 00271 } 00272 00273 00274 MouseCursor* MouseCursor::getSingletonPtr(void) 00275 { 00276 return Singleton<MouseCursor>::getSingletonPtr(); 00277 } 00278 00279 } // End of CEGUI namespace section