Changeset 268


Ignore:
Timestamp:
06/19/14 20:22:54 (11 years ago)
Author:
epyon
Message:
  • gui::element completely independent of object
  • gui::element is pure data
Location:
trunk
Files:
6 edited

Legend:

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

    r267 r268  
    1414#define NV_GUI_ELEMENT_HH
    1515
    16 #include <nv/object.hh>
     16#include <nv/common.hh>
    1717#include <nv/position.hh>
    1818#include <nv/io_event.hh>
     
    2323        namespace gui
    2424        {
    25                 class element : public object
     25                class element
    2626                {
    2727                protected:
     28                        /// List type
     29                        typedef std::list<element*> list;
    2830
    29                         /**
    30                          * Creates a new GUI element with the give root node and positioning data.
    31                          *
    32                          * @param r The rectangle representing the position and size of this element.
    33                          */
     31
    3432                        explicit element( const rectangle& r )
    35                                         : object( "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr ) {}
    36 
    37                 public:
    38 
    39                         /**
    40                          * Checks if this element contains the given point.
    41                          *
    42                          * @param p The point to check.
    43                          * @returns True if the point is within the region of the element, false otherwise.
    44                          */
    45                         virtual bool contains( const position& p ) const
    46                         {
    47                                 return m_absolute.contains( p );
    48                         }
    49 
    50                         /**
    51                          * Gets the position and size of the element relative to its parent.
    52                          *
    53                          * @returns The element's position and size relative to its parent.
    54                          */
    55                         virtual const rectangle& get_relative() const { return m_relative; }
    56 
    57                         /**
    58                          * Gets the position and size of the element relative to the root (window).
    59                          *
    60                          * @returns The element's position and size relative to the root (window).
    61                          */
    62                         virtual const rectangle& get_absolute() const { return m_absolute; }
    63 
    64                         /**
    65                          * Gets whether this element is currently accepting events.
    66                          *
    67                          * @returns True if the element is receiving events, false otherwise.
    68                          */
    69                         virtual bool is_enabled() const { return m_enabled; }
    70 
    71                         /**
    72                          * Gets whether this element is currently visible.
    73                          *
    74                          * @returns True if the element is visible, false otherwise.
    75                          */
    76                         virtual bool is_visible() const { return m_visible; }
    77 
    78                         /**
    79                          * Gets whether this element needs to be redrawn.
    80                          *
    81                          * @returns True if the element needs to be redrawn, false if not.
    82                          */
    83                         virtual bool is_dirty()   const { return m_dirty; }
    84 
    85                         /**
    86                          * Sets whether this element is currently accepting events.
    87                          *
    88                          * @param value True to allow the element and its children to receive events, false to disable.
    89                          */
    90                         virtual void set_enabled( bool value ) { m_enabled = value; }
    91 
    92                         /**
    93                          * Sets whether this element is visible on the screen.
    94                          *
    95                          * @param value True to display the element and its children, false to hide it and its children.
    96                          */
    97                         virtual void set_visible( bool value ) { m_visible = value; }
    98 
    99                         /**
    100                          * Sets whether this element needs to be redrawn.
    101                          *
    102                          * @param value True to request that the element and its children be redrawn, false if not.
    103                          */
    104                         virtual void set_dirty( bool value )   { m_dirty   = value; }
    105 
    106                         /**
    107                          * Gets the text associated with this element.
    108                          *
    109                          * @returns A string containing the associated text.
    110                          */
    111                         virtual const string& get_text() const { return m_text; }
    112 
    113                         /**
    114                          * Sets the text associated with this element.
    115                          *
    116                          * @param text The new text to associate with this element.
    117                          */
    118                         virtual void set_text( const string& text ) { m_text = text; m_dirty = true; }
    119 
    120                         /**
    121                          * Gets the class name associated with this element.
    122                          *
    123                          * @returns A string containing the class name of this element.
    124                          */
    125                         virtual const string& get_class() const { return m_class; }
    126 
    127                         /**
    128                          * sets the class name associated with this element.
    129                          *
    130                          * @param class_ The new class name.
    131                          */
    132                         virtual void set_class( const string& class_ ) { m_class = class_; m_dirty = true; }
     33                                : m_id( "" )
     34                                , m_parent( nullptr )
     35                                , m_children()
     36                                , m_child_count(0)
     37                                , m_class("")
     38                                , m_relative( r )
     39                                , m_absolute( r )
     40                                , m_enabled( true )
     41                                , m_visible( true )
     42                                , m_dirty( true )
     43                                , m_render_data( nullptr ) {}
    13344
    13445                protected:
    135                         /**
    136                          * Destroys the element.
    137                          */
    13846                        virtual ~element() { delete m_render_data; }
    13947                protected:
    14048                        friend class environment;
    14149                        friend class renderer;
     50                        friend class style;
    14251
     52                        string    m_id;              ///< id type of the object
     53                        element*  m_parent;          ///< pointer to parent
     54                        list      m_children;        ///< children objects
     55                        size_t    m_child_count;     ///< number of children
    14356                        string    m_class; ///< Class name.
    14457                        string    m_text; ///< Displayed label or text.
  • trunk/nv/gui/gui_environment.hh

    r267 r268  
    2626        namespace gui
    2727        {
     28                class handle
     29                {
     30
     31                };
    2832
    2933                class environment
     
    3438                        void load_style( const std::string& filename );
    3539                        element* create_element( element* parent, const rectangle& r );
     40                        void set_text( element* e, const string& text );
     41                        void set_class( element* e, const string& text );
    3642                        void update();
    3743                        void draw();
     
    4248                        element* get_element( const position& p );
    4349                        void add_child( element* child );
     50                        void add_child( element* parent, element* child );
     51                        void remove_child( element* parent, element* child );
    4452                        void destroy_children( element* e );
    4553                        void update( element* e, uint32 elapsed );
  • trunk/src/gui/gui_environment.cc

    r267 r268  
    4242        element* result = new element( r );
    4343        if ( parent == nullptr ) parent = m_screen;
    44         parent->add_child( result );
     44        add_child( parent, result );
    4545        return result;
    4646}
     
    4949{
    5050        destroy_children( e );
    51         e->detach();
     51        if ( e->m_parent )
     52        {
     53                remove_child( e->m_parent, e );
     54        }
    5255        delete e;
    5356}
     
    5659{
    5760//      e->on_update( elapsed );
    58         if ( e->is_visible() )
    59         {
    60                 for ( object* i : *e )
    61                 {
    62                         update( ((element*)i), elapsed );
    63                 }
    64         }
    65         if ( e->is_dirty() || e->m_render_data == nullptr )
     61        if ( e->m_visible )
     62        {
     63                for ( element* i : e->m_children )
     64                {
     65                        update( i, elapsed );
     66                }
     67        }
     68        if ( e->m_dirty || e->m_render_data == nullptr )
    6669        {
    6770                m_renderer->redraw( e, elapsed );
    68                 e->set_dirty( false );
     71                e->m_dirty = false;
    6972        }
    7073}
     
    7275void nv::gui::environment::draw( element* e )
    7376{
    74         if ( e->is_visible() )
     77        if ( e->m_visible )
    7578        {
    7679//              e->on_draw();
    7780                m_renderer->draw( e );
    78                 for ( object* i : *e )
    79                 {
    80                         draw((element*)i);
     81                for ( element* i : e->m_children )
     82                {
     83                        draw(i);
    8184                }
    8285        }
     
    9699void nv::gui::environment::add_child( element* child )
    97100{
    98         m_screen->add_child( child );
     101        add_child( m_screen, child );
     102}
     103
     104void nv::gui::environment::add_child( element* parent, element* child )
     105{
     106        if ( child )
     107        {
     108                if ( child->m_parent )
     109                {
     110                        remove_child( child->m_parent, child );
     111                }
     112                child->m_parent = parent;
     113                parent->m_children.push_back( child );
     114                parent->m_child_count++;
     115        }
    99116}
    100117
     
    103120        while ( !e->m_children.empty() )
    104121        {
    105                 destroy_element( (element*)e->m_children.front() );
     122                destroy_element( e->m_children.front() );
    106123        }
    107124}
     
    121138bool nv::gui::environment::process_io_event( element* e, const io_event& ev )
    122139{
    123         return e->m_parent ? process_io_event( (element*)e->m_parent, ev ) : false;
     140        return e->m_parent ? process_io_event( e->m_parent, ev ) : false;
    124141}
    125142
     
    131148nv::gui::element* nv::gui::environment::get_deepest_child( element* e, const position& p )
    132149{
    133         if ( !e->is_visible() ) return nullptr;
     150        if ( !e->m_visible ) return nullptr;
    134151
    135152        element* result = nullptr;
     
    138155        while ( it != e->m_children.rend() )
    139156        {
    140                 result = get_deepest_child( (element*)(*it), p );
     157                result = get_deepest_child( *it, p );
    141158                if ( result ) return result;
    142159                ++it;
    143160        }
    144161
    145         if ( e->contains( p ) ) return e;
     162        if ( e->m_absolute.contains(p) ) return e;
    146163        return nullptr;
    147164}
     
    149166void nv::gui::environment::move_to_top( element* child )
    150167{
    151         element* parent = (element*)child->m_parent;
     168        element* parent = child->m_parent;
    152169        if ( parent )
    153170        {
    154                 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );
     171                auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child );
    155172                if ( it != parent->m_children.end() )
    156173                {
     
    164181void nv::gui::environment::move_to_bottom( element* child )
    165182{
    166         element* parent = (element*)child->m_parent;
     183        element* parent = child->m_parent;
    167184        if ( parent )
    168185        {
    169                 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );
     186                auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child );
    170187                if ( it != parent->m_children.end() )
    171188                {
     
    195212        if ( e->m_parent )
    196213        {
    197                 pabsolute = ((element*)(e->m_parent))->m_absolute;
     214                pabsolute = e->m_parent->m_absolute;
    198215        }
    199216
    200217        e->m_absolute = e->m_relative + pabsolute.ul;
    201218
    202         for ( object* o : e->m_children )
    203         {
    204                 recalculate_absolute( ((element*)o) );
    205         }
    206 }
    207 
     219        for ( element* o : e->m_children )
     220        {
     221                recalculate_absolute( o );
     222        }
     223}
     224
     225void nv::gui::environment::set_class( element* e, const string& text )
     226{
     227        e->m_class = text;
     228        e->m_dirty = true;
     229}
     230
     231void nv::gui::environment::set_text( element* e, const string& text )
     232{
     233        e->m_text = text;
     234        e->m_dirty = true;
     235}
     236
     237void nv::gui::environment::remove_child( element* parent, element* child )
     238{
     239        if ( child->m_parent != parent )
     240        {
     241                return; // signal error?
     242        }
     243        auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child );
     244        if ( it != parent->m_children.end() )
     245        {
     246                (*it)->m_parent = nullptr;
     247                parent->m_children.erase(it);
     248        }       
     249
     250}
     251
  • trunk/src/gui/gui_renderer.cc

    r267 r268  
    198198
    199199        qvec.clear();
    200         rectangle abs = e->get_absolute();
    201         if ( e->get_absolute() != m_area )
     200        rectangle abs = e->m_absolute;
     201        if ( e->m_absolute != m_area )
    202202        {
    203203                int border;
     
    220220                }
    221221
    222                 text = e->get_text();
     222                text = e->m_text;
    223223                if ( !text.empty() )
    224224                {
  • trunk/src/gui/gui_style.cc

    r204 r268  
    6767{
    6868        const char* centry = entry.c_str();
    69         const char* cid    = e->get_id().c_str();
    70         const char* cclass = e->get_class().c_str();
     69        const char* cid    = e->m_id.c_str();
     70        const char* cclass = e->m_class.c_str();
    7171        lua_getglobal( m_lua, "default" );
    7272        int global = lua_gettop( m_lua );
  • trunk/tests/gui_test/nv_gui_test.cc

    r267 r268  
    8787        glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 );
    8888        nv::gui::element* e = m_guienv->create_element( nullptr, nv::rectangle(a).dim(b) );
    89         e->set_class( "window" );
    90         e->set_text("Window "+nv::to_string(m_windows.size()+1) );
     89        m_guienv->set_class( e, "window" );
     90        m_guienv->set_text( e, "Window "+nv::to_string(m_windows.size()+1) );
    9191        NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
    9292        m_windows.push_back( e );
Note: See TracChangeset for help on using the changeset viewer.