00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00036 namespace CEGUI
00037 {
00038 const String MouseCursor::EventNamespace("MouseCursor");
00039
00040
00041
00042
00043
00044 template<> MouseCursor* Singleton<MouseCursor>::ms_Singleton = NULL;
00045
00046
00047
00048
00049
00050 const String MouseCursor::EventImageChanged( (utf8*)"ImageChanged" );
00051
00052
00053
00054
00055
00056 MouseCursor::MouseCursor(void)
00057 {
00058
00059 d_constraints = System::getSingleton().getRenderer()->getRect();
00060
00061
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
00067 d_visible = true;
00068
00069
00070 d_cursorImage = NULL;
00071
00072
00073 addMouseCursorEvents();
00074
00075 Logger::getSingleton().logEvent((utf8*)"CEGUI::MouseCursor singleton created.");
00076 }
00077
00078
00079
00080
00081
00082 MouseCursor::~MouseCursor(void)
00083 {
00084 Logger::getSingleton().logEvent((utf8*)"CEGUI::MouseCursor singleton destroyed.");
00085 }
00086
00087
00088
00089
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
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
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
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
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
00145
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
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
00185
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
00197
00198 void MouseCursor::addMouseCursorEvents(void)
00199 {
00200
00201 addEvent(EventImageChanged);
00202 }
00203
00204
00206
00207
00208
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 }