00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <freecloth/gfx/gfxWindow.h>
00020 #include <freecloth/gfx/gfxWindowObserver.h>
00021
00022
00023
00024
00025
00026
00027 class GfxWindow::ObserverIterator
00028 {
00029 public:
00030
00031
00032
00033 typedef GfxWindow::ObserverList::value_type Item;
00034
00035
00036
00037
00038 static ObserverIterator begin( GfxWindow& window )
00039 {
00040 return ObserverIterator(
00041 window,
00042 GfxWindow::_staticObservers.begin(),
00043 window._observers.begin()
00044 );
00045 }
00046
00047
00048 static ObserverIterator end( GfxWindow& window )
00049 {
00050 return ObserverIterator(
00051 window,
00052 GfxWindow::_staticObservers.end(),
00053 window._observers.end()
00054 );
00055 }
00056
00057
00058
00059
00060 inline ObserverIterator()
00061 { }
00062
00063
00064 inline ObserverIterator( const ObserverIterator& other )
00065 : _si( other._si ),
00066 _i( other._i )
00067 { }
00068
00069
00070
00071
00072
00073 inline ObserverIterator& operator ++ ()
00074 {
00075 if( inStatic() ) {
00076 ++_si;
00077 }
00078 else {
00079 ++_i;
00080 }
00081 return *this;
00082 }
00083 inline Item& operator * ()
00084 {
00085 return inStatic() ? *_si : *_i;
00086 }
00087 inline const Item& operator * () const
00088 {
00089 return inStatic() ? *_si : *_i;
00090 }
00091 inline bool operator == ( const ObserverIterator& other ) const
00092 {
00093 return
00094 _si == other._si &&
00095 _i == other._i;
00096 }
00097 inline bool operator != ( const ObserverIterator& other ) const
00098 {
00099 return ! operator==( other );
00100 }
00101
00102
00103 private:
00104
00105
00106
00107
00108 inline ObserverIterator(
00109 GfxWindow& window,
00110 const GfxWindow::ObserverList::iterator& si,
00111 const GfxWindow::ObserverList::iterator& i
00112 ) : _si( si ),
00113 _i( i )
00114 { }
00115
00116 inline bool inStatic() const {
00117 return _si != GfxWindow::_staticObservers.end();
00118 }
00119
00120
00121
00122 ObserverList::iterator _si;
00123 ObserverList::iterator _i;
00124
00125 };
00126
00127
00128
00129
00130 GfxWindow::ObserverList GfxWindow::_staticObservers;
00131
00132
00133
00134 GfxWindow::GfxWindow()
00135 {
00136 }
00137
00138
00139
00140 GfxWindow::~GfxWindow()
00141 {
00142 ObserverList o2( _observers );
00143 ObserverList::iterator i;
00144 for( i = o2.begin(); i != o2.end(); ++i ) {
00145 (*i)->destroyed( *this );
00146 }
00147 }
00148
00149
00150
00151 Float GfxWindow::getAspectRatio() const
00152 {
00153 return static_cast<Float>( getWidth() ) / getHeight();
00154 }
00155
00156
00157
00158 void GfxWindow::addStaticObserver( GfxWindowObserver& observer )
00159 {
00160 _staticObservers.push_back( &observer );
00161 }
00162
00163
00164
00165 void GfxWindow::removeStaticObserver( GfxWindowObserver& observer )
00166 {
00167 _staticObservers.remove( &observer );
00168 }
00169
00170
00171
00172 void GfxWindow::addObserver( GfxWindowObserver& observer )
00173 {
00174 _observers.push_back( &observer );
00175 }
00176
00177
00178
00179 void GfxWindow::removeObserver( GfxWindowObserver& observer )
00180 {
00181 _observers.remove( &observer );
00182 }
00183
00184
00185
00186 GfxWindow::ObserverIterator GfxWindow::allObserversBegin()
00187 {
00188 return ObserverIterator::begin( *this );
00189 }
00190
00191
00192
00193 GfxWindow::ObserverIterator GfxWindow::allObserversEnd()
00194 {
00195 return ObserverIterator::end( *this );
00196 }
00197
00198
00199
00200 void GfxWindow::closeReceived()
00201 {
00202 ObserverList o2( _observers );
00203 ObserverList::iterator i;
00204 for( i = o2.begin(); i != o2.end(); ++i ) {
00205 (*i)->closeReceived( *this );
00206 }
00207 }
00208
00209
00210
00211 void GfxWindow::displayReceived()
00212 {
00213 ObserverIterator i;
00214 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00215 (*i)->displayReceived( *this );
00216 }
00217 }
00218
00219
00220
00221 void GfxWindow::idleReceived()
00222 {
00223 ObserverIterator i;
00224 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00225 (*i)->idleReceived();
00226 }
00227 }
00228
00229
00230
00231 void GfxWindow::keyPressed( KeyID keyId, ModBitfield modbits )
00232 {
00233 DGFX_TRACE_ENTER( "GfxWindow::keyPressed" );
00234 ObserverIterator i;
00235 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00236 (*i)->keyPressed( *this, keyId, modbits );
00237 }
00238 }
00239
00240
00241
00242 void GfxWindow::keyDownReceived( KeyID keyId, ModBitfield modbits )
00243 {
00244 ObserverIterator i;
00245 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00246 (*i)->keyDownReceived( *this, keyId, modbits );
00247 }
00248 }
00249
00250
00251
00252 void GfxWindow::keyUpReceived( KeyID keyId, ModBitfield modbits )
00253 {
00254 ObserverIterator i;
00255 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00256 (*i)->keyUpReceived( *this, keyId, modbits );
00257 }
00258 }
00259
00260
00261
00262 void GfxWindow::mouseDownReceived( UInt32 x, UInt32 y, ButtonID button )
00263 {
00264 ObserverIterator i;
00265 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00266 (*i)->mouseDownReceived( *this, x, y, button );
00267 }
00268 }
00269
00270
00271
00272 void GfxWindow::mouseUpReceived( UInt32 x, UInt32 y, ButtonID button )
00273 {
00274 ObserverIterator i;
00275 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00276 (*i)->mouseUpReceived( *this, x, y, button );
00277 }
00278 }
00279
00280
00281
00282 void GfxWindow::mouseMoveReceived( UInt32 x, UInt32 y )
00283 {
00284 ObserverIterator i;
00285 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00286 (*i)->mouseMoveReceived( *this, x, y );
00287 }
00288 }
00289
00290
00291
00292 void GfxWindow::windowResized( UInt32 x, UInt32 y, UInt32 width, UInt32 height )
00293 {
00294 ObserverIterator i;
00295 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00296 (*i)->windowResized( *this, x, y, width, height );
00297 }
00298 }
00299
00300
00301
00302 void GfxWindow::uiReceived( UInt32 uid )
00303 {
00304 ObserverIterator i;
00305 for( i = allObserversBegin(); i != allObserversEnd(); ++i ) {
00306 (*i)->uiReceived( *this, uid );
00307 }
00308 }