Changeset 257 for trunk/src


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/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • 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.