Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Layer.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: Layer.cpp,v 1.5 2002/03/12 10:46:07 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Graphics/View.h"
00026 #include "Teddy/PhysicalComponents/EventListener.h"
00027 #include "Teddy/PhysicalComponents/Layer.h"
00028 #include "Teddy/PhysicalComponents/Projection.h"
00029 #include "Teddy/SysSupport/Messages.h"
00030 using namespace Teddy::Graphics;
00031 
00032 
00033 namespace Teddy              {
00034 namespace PhysicalComponents {
00035 
00036 
00038 Layer::Layer( std::string name, View *view ):Area(name){
00039     //GLint *viewp    = view->getViewport();
00040     this->parent    = NULL;
00041     this->view      = view;
00042     this->drawing_ordering = post_self;
00043     IntVector2 vs   = view->getSize();
00044     IntVector2 ziv  = IntVector2(0,0);
00045     this->rect      = Rect( ziv, vs );
00046     this->size      = rect.getSize();
00047     dmsg( M_WM, "Layer constructor done." );
00048 }
00049 
00050 
00052 /*virtual*/ Area *Layer::getHit( const Vector2 &pos ){
00053     EventListener *e    = dynamic_cast<EventListener*>( this );
00054     const char    *name = getName().c_str();
00055     if( e != NULL ){
00056         dmsg( M_WME, "Trying layer %s - Listening for events", name  );
00057     }else{
00058         dmsg( M_WME, "Trying layer %s - not listening for events", name );
00059     }
00060     Area *hit = NULL;
00061 
00062     //  If we are first to draw, we could be hit?
00063     if( event_ordering == pre_self && e != NULL ){
00064         if( rect.hit(pos) ){
00065             dmsg( M_WME, "Hit this layer first" );
00066             return this;
00067         }
00068     }
00069 
00070     //  Test Children for hits
00071     if( !areas.empty() ){
00072         dmsg( M_WME, "Begin try children of layer %s", name );
00073         list<Area*>::iterator a_it = areas.begin();
00074         while( a_it != areas.end() ){
00075             hit = (*a_it)->Area::getHit( pos );
00076             if( hit != NULL ){
00077                 dmsg( M_WME, "Hit child Area" );
00078                 return hit;
00079             }
00080             a_it++;
00081         }
00082         dmsg( M_WME, "End try children of layer %s", name );
00083     }
00084 
00085     //  Test Projection areas for hits
00086     //if( !projs.empty() ){
00087         dmsg( M_WME, "Begin try projections of %s", name );
00088         list<Projection*>::iterator p_it = projs.begin();
00089         while( p_it != projs.end() ){
00090             hit = (*p_it)->getHit( pos );
00091             if( hit != NULL ){
00092                 dmsg( M_WME, "Hit Projection Area" );
00093                 return hit;
00094             }
00095             p_it++;
00096         }
00097         dmsg( M_WME, "End try projections of %s", name );
00098     //}
00099 
00100     //  If we are last to draw, we could be hit?
00101     if( event_ordering == post_self && e != NULL ){
00102         if( rect.hit(pos) ){
00103             dmsg( M_WME, "Hit this last" );
00104             return this;
00105         }
00106     }
00107     return hit;
00108 }
00109 
00110 
00112 void Layer::update( View *view ){ // refresh/update, on resize for example
00113     dmsg( M_WML, "" );
00114     dmsg( M_WML, "Layer::update() ..." );
00115     rect = Rect( IntVector2(0,0), view->getSize() );
00116     size = rect.getSize();
00117     //layout_state.x1 = viewp[2] - viewp[0];
00118     //layout_state.y1 = viewp[3] - viewp[1];
00119     place();
00120     dmsg( M_WML, "Layer::update() done" );
00121 }
00122 
00123 
00125 /*virtual*/ void Layer::drawSelf(){
00126     list<Projection*>::iterator p_it = projs.begin();
00127     while( p_it != projs.end() ){
00128         (*p_it)->drawSelf();
00129         p_it++;
00130     }
00131 }
00132 
00134 void Layer::addProjection( Projection *p ){
00135     if( p!=NULL ){
00136         projs.push_back( p );
00137         p->setParent( this, this->view );
00138         this->insert( p );
00139     }
00140 }
00141 
00142 
00144 /*virtual*/ void Layer::place(){
00145     list<Area*>::iterator a_it = areas.begin();
00146     while( a_it != areas.end() ){
00147         (*a_it)->doSize( rect.getSize() );
00148         a_it++;
00149     }
00150 
00151     a_it = areas.begin();
00152     while( a_it != areas.end() ){
00153         (*a_it)->doPlace( rect );
00154         a_it++;
00155     }
00156 
00157     list<Projection*>::iterator p_it = projs.begin();
00158     while( p_it != projs.end() ){
00159         (*p_it)->doSize ( rect.getSize() );
00160         (*p_it)->doPlace( rect );
00161         p_it++;
00162     }
00163 }
00164 
00165 
00167 /*virtual*/ void Layer::drawLayer(){
00168     if( view == NULL ){
00169         dmsg( M_WM, "drawLayer() NULL view" );
00170         return;
00171     }else{
00172         dmsg( 
00173             M_WM, 
00174             "layer %s drawLayer() size %f x %f", 
00175             getName().c_str(),
00176             size[0],
00177             size[1]
00178         );
00179     }
00180     drawSelf();
00181 
00182     view->begin2d();
00183     list<Area*>::iterator a_it = areas.begin();
00184     while( a_it != areas.end() ){
00185         Area *a = *a_it;
00186         const char *name = a->getName().c_str();
00187         dmsg( M_WM, "drawLayer() calling draw for area %s", name );
00188         a->draw();
00189         a_it++;
00190     }
00191     view->end2d();
00192 }
00193 
00194 
00195 };  //  namespace PhysicalComponents
00196 };  //  namespace Teddy
00197