Changeset 257


Ignore:
Timestamp:
06/14/14 22:40:25 (11 years ago)
Author:
epyon
Message:
  • object can now only created from root
  • all registration is done root side
  • gui::element draw and update logic fully in environment
  • root removed from object - a lot less coupling
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/gui/gui_element.hh

    r132 r257  
    2525                class element : public object
    2626                {
     27                protected:
     28
     29                        /**
     30                         * Creates a new GUI element with the give root node and postioning data.
     31                         *
     32                         * @param r The rectangle representing the position and size of this element.
     33                         */
     34                        explicit element( const rectangle& r );
     35
    2736                public:
    28 
    29                         /**
    30                          * Creates a new GUI element.
    31                          */
    32                         element() : object() {}
    33 
    34                         /**
    35                          * Creates a new GUI element with the give root node and postioning data.
    36                          *
    37                          * @param aroot The root object that will be this object's parent.
    38                          * @param r The rectangle representing the position and size of this element.
    39                          */
    40                         element( root* aroot, const rectangle& r );
    4137
    4238                        /**
     
    4945
    5046                        /**
    51                          * Event handler for update events.  Calls on_update for all affected children.
     47                         * Event handler for update events.
    5248                         *
    5349                         * @param elapsed Time since last update.
    5450                         */
    55                         virtual void on_update( uint32 elapsed );
    56 
    57                         /**
    58                          * Event handler for draw events.  Calls on_draw for all affected children.
    59                          */
    60                         virtual void on_draw();
     51                        virtual void on_update( uint32 /*elapsed*/ ) {}
     52
     53                        /**
     54                         * Event handler for draw events.
     55                         */
     56                        virtual void on_draw() {}
    6157
    6258                        /**
     
    191187                        virtual void recalculate_absolute_children();
    192188
     189                protected:
    193190                        /**
    194191                         * Destroys the element.
  • trunk/nv/gui/gui_environment.hh

    r234 r257  
    2828                {
    2929                public:
    30                         screen( root* aroot, const rectangle& r ) : element( aroot, r ) {}
     30                        screen( const rectangle& r ) : element( r ) {}
    3131                        virtual bool on_event( const io_event& ) { return false; }
    3232                        virtual void recalculate_absolute() { recalculate_absolute_children(); }
     
    3939                        // temporary
    4040                        void load_style( const std::string& filename );
     41                        element* create_element( element* parent, const rectangle& r );
    4142                        void update( element* e, uint32 elapsed );
    4243                        void draw( element* e );
  • trunk/nv/object.hh

    r256 r257  
    3030
    3131                /**
    32                  * Object default constructor, needed for RTTI
    33                  */
    34                 object();
    35 
    36                 /**
    3732                 * Object constructor
    3833                 */
    39                 object( root* aroot, const string& aid );
     34                explicit object( const string& aid );
    4035
    4136                /**
     
    9489
    9590                /**
    96                  * Destroys all children.
    97                  */
    98                 void destroy_children();
    99 
    100                 /**
    10191                 * Searches for child by pointer.
    10292                 *
     
    133123                 */
    134124                virtual object* get_parent() const { return m_parent; }
    135 
    136                 /**
    137                  * Returns the root object of this object.
    138                  *
    139                  * @returns root object of current object
    140                  */
    141                 virtual root* get_root() const { return (root*)m_root; }
    142125
    143126                /**
     
    178161                 * Destroys all children, unregisters the object from the uid_store.
    179162                 */
     163        protected:
    180164                virtual ~object();
    181165
    182166        protected:
    183                 void register_with_lua( const char* lua_name, const char* storage );
    184 
    185         protected:
    186                 root*   m_root;            ///< pointer to root
    187167                string  m_id;              ///< id type of the object
    188168                string  m_name;            ///< name of the object
  • trunk/nv/root.hh

    r256 r257  
    1919        {
    2020        public:
    21                 root() : object( nullptr, "" ), m_lua_state( nullptr ), m_uid_store( nullptr ) { m_root = this; }
    22                 virtual void child_added( object* ) {}
    23                 virtual void child_removed( object* ) {}
    24                 virtual void object_created( object* o );
    25                 virtual void object_destroyed( object* o );
     21                root() : object( "" ), m_lua_state( nullptr ), m_uid_store( nullptr ) {}
    2622                lua::state*    get_lua_state()     const { return m_lua_state; }
    2723                uid_store*     get_uid_store()     const { return m_uid_store; }
    28                 virtual ~root()
    29                 {
    30                         object_destroyed( this );
    31                         destroy_children();
    32                         m_root = nullptr;
     24                template < typename T >
     25                object* create_object( const std::string& id )
     26                {
     27                        object* o = new T(id);
     28                        object_created(o);
     29                        return o;
    3330                }
     31                void register_with_lua( object* o, const char* lua_name, const char* storage );
     32                void destroy_children( object* o );
     33                virtual void destroy_object( object* o );
     34                virtual ~root() { destroy_object( this ); }
    3435        protected:
     36                virtual void object_created( object* o );
     37
    3538                lua::state*    m_lua_state;
    3639                uid_store*     m_uid_store;
  • trunk/src/gui/gui_element.cc

    r126 r257  
    77#include "nv/gui/gui_element.hh"
    88
    9 #include "nv/gui/gui_environment.hh"
    10 
    119using namespace nv;
    1210using namespace nv::gui;
    1311
    14 element::element( root* aroot, const rectangle& r )
    15         : object( aroot, "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr )
     12element::element( const rectangle& r )
     13        : object( "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr )
    1614{
    1715
    18 }
    19 
    20 void element::on_update( uint32 elapsed )
    21 {
    22         if ( is_visible() )
    23         {
    24                 for ( object* i : *this )
    25                 {
    26                         ((element*)i)->on_update( elapsed );
    27                 }
    28         }
    29         ((environment*)m_root)->update( this, elapsed );
    30 }
    31 
    32 void element::on_draw()
    33 {
    34         if ( is_visible() )
    35         {
    36                 ((environment*)m_root)->draw( this );
    37                 for ( object* i : *this )
    38                 {
    39                         ((element*)i)->on_draw();
    40                 }
    41         }
    4216}
    4317
  • trunk/src/gui/gui_environment.cc

    r239 r257  
    3434{
    3535        m_area.dim( dimension( w->get_width(), w->get_height() ) );
    36         m_screen = new screen( this, m_area );
     36        m_screen = new screen( m_area );
    3737        m_renderer = new renderer( w, shader_path );
    3838        root::add_child( m_screen );
     
    4444}
    4545
     46element* nv::gui::environment::create_element( element* parent, const rectangle& r )
     47{
     48        element* result = new element( r );
     49        object_created( result );
     50        if ( parent == nullptr ) parent = m_screen;
     51        parent->add_child( result );
     52        return result;
     53}
     54
    4655void environment::update( element* e, uint32 elapsed )
    4756{
     57        e->on_update( elapsed );
     58        if ( e->is_visible() )
     59        {
     60                for ( object* i : *e )
     61                {
     62                        update( ((element*)i), elapsed );
     63                }
     64        }
    4865        if ( e->is_dirty() || e->m_render_data == nullptr )
    4966        {
     
    5572void environment::draw( element* e )
    5673{
    57         m_renderer->draw( e );
     74        if ( e->is_visible() )
     75        {
     76                e->on_draw();
     77                m_renderer->draw( e );
     78                for ( object* i : *e )
     79                {
     80                        draw((element*)i);
     81                }
     82        }
    5883}
    5984
    6085void environment::update()
    6186{
    62         m_screen->on_update( 0 );
     87        update( m_screen, 0 );
    6388}
    6489
    6590void environment::draw()
    6691{
    67         m_screen->on_draw();
     92        draw( m_screen );
    6893        m_renderer->draw();
    6994}
     
    77102environment::~environment()
    78103{
    79         destroy_children();
     104        destroy_children( this );
    80105        delete m_renderer;
    81106}
  • trunk/src/object.cc

    r256 r257  
    88
    99#include <algorithm>
    10 #include "nv/root.hh"
    1110#include "nv/types.hh"
    1211#include "nv/lua/lua_state.hh"
     
    1514using namespace nv;
    1615
    17 object::object()
    18         : m_root( nullptr )
    19         , m_id()
     16object::object( const string& aid )
     17        : m_id( aid )
    2018        , m_name()
    2119        , m_uid(0)
     
    2624        , m_child_count(0)
    2725{
    28 }
    29 
    30 object::object( root* aroot, const string& aid )
    31         : m_root( aroot )
    32         , m_id( aid )
    33         , m_name()
    34         , m_uid(0)
    35         , m_lua_index(lua::ref_none)
    36         , m_lua_proto_index(lua::ref_none)
    37         , m_parent( nullptr )
    38         , m_children()
    39         , m_child_count(0)
    40 {
    41         if ( m_root )
    42         {
    43                 m_root->object_created( this );
    44         }
    4526}
    4627
     
    5637                m_children.push_back( child );
    5738                m_child_count++;
    58                 m_root->child_added( child );
    5939        }
    6040}
     
    7151                (*it)->m_parent = nullptr;
    7252                m_children.erase(it);
    73                 m_root->child_removed( child );
    7453        }       
    7554}
     
    8968}
    9069
    91 void object::destroy_children()
    92 {
    93         while ( !m_children.empty() )
    94         {
    95                 delete m_children.front();
    96         }
    97 }
    98 
    9970object::~object()
    10071{
    101         if ( m_root )
    102         {
    103                 m_root->object_destroyed( this );
    104         }
    105         detach();
    106         destroy_children();
    10772}
    10873
     
    195160//      db->create_type<object>("object").fields(fields);
    196161// }
    197 
    198 void nv::object::register_with_lua( const char* lua_name, const char* storage )
    199 {
    200         lua::state* state = get_root()->get_lua_state();
    201         if (state)
    202         {
    203                 if ( lua_name != nullptr )
    204                 {
    205                         m_lua_index       = state->register_object( this, lua_name );
    206                 }
    207                 if ( storage != nullptr )
    208                 {
    209                         m_lua_proto_index = state->register_proto( this, storage );
    210                 }
    211         }
    212 }
  • trunk/src/root.cc

    r256 r257  
    1818}
    1919
    20 void nv::root::object_destroyed( object* o )
     20void nv::root::destroy_object( object* o )
    2121{
     22        destroy_children( o );
     23        o->detach();
    2224        if ( m_lua_state && o->m_lua_index != lua::ref_none )
    2325        {
     
    2830                m_uid_store->remove( o->m_uid );
    2931        }
     32        if ( o != this) delete o;
    3033}
     34
     35void nv::root::destroy_children( object* o )
     36{
     37        while ( !o->m_children.empty() )
     38        {
     39                destroy_object( o->m_children.front() );
     40        }
     41}
     42
     43void nv::root::register_with_lua( object* o, const char* lua_name, const char* storage )
     44{
     45        if ( m_lua_state )
     46        {
     47                if ( lua_name != nullptr )
     48                {
     49                        o->m_lua_index       = m_lua_state->register_object( o, lua_name );
     50                }
     51                if ( storage != nullptr )
     52                {
     53                        o->m_lua_proto_index = m_lua_state->register_proto( o, storage );
     54                }
     55        }
     56
     57}
     58
Note: See TracChangeset for help on using the changeset viewer.