Changeset 267


Ignore:
Timestamp:
06/19/14 19:03:25 (11 years ago)
Author:
epyon
Message:
  • gui::element class now pure data
  • gui::element related code in environment
  • gui shaders use general library mechanism
Location:
trunk
Files:
1 deleted
10 edited

Legend:

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

    r108 r267  
    1414#define NV_GUI_COMMON_HH
    1515
    16 #include <nv/object.hh>
     16#include <nv/common.hh>
    1717
    1818namespace nv
  • trunk/nv/gui/gui_element.hh

    r266 r267  
    2828
    2929                        /**
    30                          * Creates a new GUI element with the give root node and postioning data.
     30                         * Creates a new GUI element with the give root node and positioning data.
    3131                         *
    3232                         * @param r The rectangle representing the position and size of this element.
    3333                         */
    34                         explicit element( const rectangle& r );
     34                        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 ) {}
    3536
    3637                public:
    37 
    38                         /**
    39                          * Gets the deepest child element that contains the indicated point.
    40                          *
    41                          * @param p The position to check.
    42                          * @returns The last child element that contains the given point.
    43                          */
    44                         element* get_element( const position& p );
    45 
    46                         /**
    47                          * Event handler for update events.
    48                          *
    49                          * @param elapsed Time since last update.
    50                          */
    51                         virtual void on_update( uint32 /*elapsed*/ ) {}
    52 
    53                         /**
    54                          * Event handler for draw events.
    55                          */
    56                         virtual void on_draw() {}
    57 
    58                         /**
    59                          * Event handler for IO events.  Calls on_event for all affected children.
    60                          *
    61                          * @param event The event data.
    62                          * @returns ???
    63                          */
    64                         virtual bool on_event( const io_event& event );
    6538
    6639                        /**
     
    7043                         * @returns True if the point is within the region of the element, false otherwise.
    7144                         */
    72                         virtual bool contains( const position& p ) const;
    73 
    74                         /**
    75                          * Sets the position and size of this element relative to its parent.
    76                          *
    77                          * @param r The new position and size of the element.
    78                          */
    79                         virtual void set_relative( const rectangle& r );
    80 
    81                         /**
    82                          * Sets the position of this element relative to its parent.
    83                          *
    84                          * @param p The new position of the element.
    85                          */
    86                         virtual void set_relative( const position& p );
     45                        virtual bool contains( const position& p ) const
     46                        {
     47                                return m_absolute.contains( p );
     48                        }
    8749
    8850                        /**
     
    9961                         */
    10062                        virtual const rectangle& get_absolute() const { return m_absolute; }
    101 
    102                         /**
    103                          * Gets the parent element of this element.
    104                          *
    105                          * @returns The parent element.
    106                          */
    107                         virtual element* get_parent() const { return (element*)m_parent; }
    10863
    10964                        /**
     
    177132                        virtual void set_class( const string& class_ ) { m_class = class_; m_dirty = true; }
    178133
    179                         /**
    180                          * Recalcuates the absolute position of the element and the element's children.
    181                          */
    182                         virtual void recalculate_absolute();
    183 
    184                         /**
    185                          * Recalculates the aboslute position of the element's children.
    186                          */
    187                         virtual void recalculate_absolute_children();
    188 
    189                         /**
    190                          * Moves element to back of child list (top of stack)
    191                          *
    192                          * @returns true if object found, false otherwise
    193                          */
    194                         bool move_to_top( element* child );
    195 
    196                         /**
    197                          * Moves element to front of child list (bottom of stack)
    198                          *
    199                          * @returns true if object found, false otherwise
    200                          */
    201                         bool move_to_bottom( element* child );
    202 
    203134                protected:
    204135                        /**
    205136                         * Destroys the element.
    206137                         */
    207                         virtual ~element();
     138                        virtual ~element() { delete m_render_data; }
    208139                protected:
    209140                        friend class environment;
  • trunk/nv/gui/gui_environment.hh

    r264 r267  
    1717#include <nv/gui/gui_style.hh>
    1818#include <nv/lua/lua_state.hh>
     19#include <nv/io_event.hh>
    1920#include <nv/interface/window.hh>
    20 #include <nv/root.hh>
     21#include <nv/array.hh>
    2122
    2223namespace nv
     
    2526        namespace gui
    2627        {
    27                 class screen : public element
    28                 {
    29                 public:
    30                         screen( const rectangle& r ) : element( r ) {}
    31                         virtual bool on_event( const io_event& ) { return false; }
    32                         virtual void recalculate_absolute() { recalculate_absolute_children(); }
    33                 };
    3428
    3529                class environment
     
    4034                        void load_style( const std::string& filename );
    4135                        element* create_element( element* parent, const rectangle& r );
     36                        void update();
     37                        void draw();
     38                        void destroy_element( element* e );
     39                        bool process_io_event( const io_event& ev );
     40                        virtual ~environment();
     41                protected:
     42                        element* get_element( const position& p );
     43                        void add_child( element* child );
     44                        void destroy_children( element* e );
    4245                        void update( element* e, uint32 elapsed );
    4346                        void draw( element* e );
    44                         void update();
    45                         void draw();
    46                         void add_child( element* child );
    47                         void destroy_element( element* e );
    48                         void destroy_children( element* e );
    49                         virtual ~environment();
    50                 protected:
    51 
     47                        bool process_io_event( element* e, const io_event& ev );
     48                        element* get_deepest_child( element* e, const position& p );
     49                        void move_to_top( element* child );
     50                        void move_to_bottom( element* child );
     51                        void set_relative( element* e, const rectangle& r );
     52                        void set_relative( element* e, const position& p );
     53                        void recalculate_absolute( element* e );
     54                       
    5255                        renderer*    m_renderer;
    5356                        window*      m_window;
    54                         screen*      m_screen;
     57                        element*     m_screen;
    5558                        rectangle    m_area;
    5659                };
  • trunk/nv/gui/gui_renderer.hh

    r234 r267  
    2222#include <nv/interface/device.hh>
    2323#include <nv/interface/context.hh>
     24#include <nv/interface/camera.hh>
    2425#include <nv/gfx/sliced_buffer.hh>
    2526#include <nv/gfx/texture_atlas.hh>
     
    6869                        bool          m_reupload;
    6970
     71                        scene_state   m_scene_state;
    7072                        render_state  m_render_state;
    7173                        render_data*  m_render_data;
  • trunk/nv/interface/camera.hh

    r235 r267  
    3333                {
    3434                        m_projection = glm::perspective( fov, aspect, near, far );
     35                }
     36                void set_ortho( f32 left, f32 right, f32 bottom, f32 top, f32 near = -1.0f, f32 far = 1.0f )
     37                {
     38                        m_projection = glm::ortho( left, right, bottom, top, near, far );
    3539                }
    3640                const mat4& get_projection() const
  • trunk/src/gui/gui_environment.cc

    r264 r267  
    2525#include "nv/gfx/texture_atlas.hh"
    2626
    27 #include <vector>
    28 
    29 using namespace nv;
    30 using namespace nv::gui;
    31 
    32 environment::environment( window* w, const std::string& shader_path )
     27nv::gui::environment::environment( window* w, const std::string& shader_path )
    3328        : m_renderer( nullptr ), m_window( w ), m_screen( nullptr )
    3429{
    3530        m_area.dim( dimension( w->get_width(), w->get_height() ) );
    36         m_screen = new screen( m_area );
     31        m_screen = new element( m_area );
    3732        m_renderer = new renderer( w, shader_path );
    3833}
     
    4338}
    4439
    45 element* nv::gui::environment::create_element( element* parent, const rectangle& r )
     40nv::gui::element* nv::gui::environment::create_element( element* parent, const rectangle& r )
    4641{
    4742        element* result = new element( r );
     
    5853}
    5954
    60 void environment::update( element* e, uint32 elapsed )
    61 {
    62         e->on_update( elapsed );
     55void nv::gui::environment::update( element* e, uint32 elapsed )
     56{
     57//      e->on_update( elapsed );
    6358        if ( e->is_visible() )
    6459        {
     
    7570}
    7671
    77 void environment::draw( element* e )
     72void nv::gui::environment::draw( element* e )
    7873{
    7974        if ( e->is_visible() )
    8075        {
    81                 e->on_draw();
     76//              e->on_draw();
    8277                m_renderer->draw( e );
    8378                for ( object* i : *e )
     
    8883}
    8984
    90 void environment::update()
     85void nv::gui::environment::update()
    9186{
    9287        update( m_screen, 0 );
    9388}
    9489
    95 void environment::draw()
     90void nv::gui::environment::draw()
    9691{
    9792        draw( m_screen );
     
    9994}
    10095
    101 void environment::add_child( element* child )
     96void nv::gui::environment::add_child( element* child )
    10297{
    10398        m_screen->add_child( child );
    10499}
    105100
    106 void environment::destroy_children( element* e )
     101void nv::gui::environment::destroy_children( element* e )
    107102{
    108103        while ( !e->m_children.empty() )
     
    113108
    114109
    115 environment::~environment()
     110nv::gui::environment::~environment()
    116111{
    117112        destroy_element( m_screen );
     
    119114}
    120115
     116bool nv::gui::environment::process_io_event( const io_event& )
     117{
     118        return false;
     119}
     120
     121bool nv::gui::environment::process_io_event( element* e, const io_event& ev )
     122{
     123        return e->m_parent ? process_io_event( (element*)e->m_parent, ev ) : false;
     124}
     125
     126nv::gui::element* nv::gui::environment::get_element( const position& p )
     127{
     128        return get_deepest_child( m_screen, p );
     129}
     130
     131nv::gui::element* nv::gui::environment::get_deepest_child( element* e, const position& p )
     132{
     133        if ( !e->is_visible() ) return nullptr;
     134
     135        element* result = nullptr;
     136        element::list::reverse_iterator it = e->m_children.rbegin();
     137
     138        while ( it != e->m_children.rend() )
     139        {
     140                result = get_deepest_child( (element*)(*it), p );
     141                if ( result ) return result;
     142                ++it;
     143        }
     144
     145        if ( e->contains( p ) ) return e;
     146        return nullptr;
     147}
     148
     149void nv::gui::environment::move_to_top( element* child )
     150{
     151        element* parent = (element*)child->m_parent;
     152        if ( parent )
     153        {
     154                auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );
     155                if ( it != parent->m_children.end() )
     156                {
     157                        parent->m_children.erase( it );
     158                        parent->m_children.push_back( child );
     159                        parent->m_dirty = true;
     160                }       
     161        }
     162}
     163
     164void nv::gui::environment::move_to_bottom( element* child )
     165{
     166        element* parent = (element*)child->m_parent;
     167        if ( parent )
     168        {
     169                auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );
     170                if ( it != parent->m_children.end() )
     171                {
     172                        parent->m_children.erase( it );
     173                        parent->m_children.push_front( child );
     174                        parent->m_dirty = true;
     175                }
     176        }
     177}
     178
     179void nv::gui::environment::set_relative( element* e, const rectangle& r )
     180{
     181        e->m_dirty    = true;
     182        e->m_relative = r;
     183        recalculate_absolute( e );
     184}
     185
     186void nv::gui::environment::set_relative( element* e, const position& p )
     187{
     188        set_relative( e, rectangle( p, p + e->m_relative.get_size() ) );
     189}
     190
     191void nv::gui::environment::recalculate_absolute( element* e )
     192{
     193        rectangle pabsolute;
     194
     195        if ( e->m_parent )
     196        {
     197                pabsolute = ((element*)(e->m_parent))->m_absolute;
     198        }
     199
     200        e->m_absolute = e->m_relative + pabsolute.ul;
     201
     202        for ( object* o : e->m_children )
     203        {
     204                recalculate_absolute( ((element*)o) );
     205        }
     206}
     207
  • trunk/src/gui/gui_renderer.cc

    r234 r267  
    112112        sr->varray     = m_window->get_device()->create_vertex_array();
    113113        sr->shader     = m_window->get_device()->create_program( nv::slurp( shader_path + ".vert" ), nv::slurp( shader_path + ".frag" ) );
    114         sr->shader->set_uniform( "tex", 0 );
    115         glm::mat4 projection = glm::ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f, -1.0f, 1.0f );
    116         sr->shader->set_uniform( "nv_projection", projection );
     114        m_scene_state.get_camera().set_ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f );
    117115
    118116        vertex_buffer* vb = (vertex_buffer*)sr->buffer.get_buffer();
     
    274272                sr->varray->update_vertex_buffer( nv::COLOR,    vb, false );
    275273        }
    276         sr->texture->bind( 0 );
    277         sr->shader->set_uniform( "tex", 0 );
    278         m_window->get_context()->draw( TRIANGLES, m_render_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 );
     274        sr->texture->bind( nv::TEX_DIFFUSE );
     275        m_window->get_context()->draw( TRIANGLES, m_render_state, m_scene_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 );
    279276}
    280277
  • trunk/tests/gui_test/gui.frag

    r161 r267  
    22varying vec4 v_color;
    33varying vec2 v_texcoord;
    4 uniform sampler2D tex;
     4uniform sampler2D nv_t_diffuse;
    55 
    66void main(void) {
    7         vec4 texture = texture2D(tex,v_texcoord);
     7        vec4 texture = texture2D(nv_t_diffuse,v_texcoord);
    88        gl_FragColor = v_color * texture;
    99}
  • trunk/tests/gui_test/gui.vert

    r161 r267  
    33attribute vec2 nv_texcoord;
    44attribute vec4 nv_color;
    5 uniform mat4 nv_projection;
     5uniform mat4 nv_m_projection;
    66varying vec4 v_color;
    77varying vec2 v_texcoord;
     
    1010        v_color     = nv_color;
    1111        v_texcoord  = nv_texcoord;
    12         gl_Position = nv_projection * vec4( nv_position.x, nv_position.y, 0.0, 1.0 );
     12        gl_Position = nv_m_projection * vec4( nv_position.x, nv_position.y, 0.0, 1.0 );
    1313}
  • trunk/tests/gui_test/nv_gui_test.cc

    r239 r267  
    1616        void spawn_window();
    1717        void recolor_window();
     18//      void bring_to_front( int i );
    1819        ~application();
    1920protected:
     
    6465                                        switch (event.key.code)
    6566                                        {
     67//                                      case nv::KEY_1      : bring_to_front(0); break;
     68//                                      case nv::KEY_2      : bring_to_front(1); break;
     69//                                      case nv::KEY_3      : bring_to_front(2); break;
     70//                                      case nv::KEY_4      : bring_to_front(3); break;
     71//                                      case nv::KEY_5      : bring_to_front(4); break;
    6672                                        case nv::KEY_N      : spawn_window(); break;
    6773                                        case nv::KEY_K      : kill_window(); break;
     
    8086        glm::ivec2 a( std::rand() % 600, std::rand() % 400 );
    8187        glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 );
    82         nv::gui::element* e = new nv::gui::element( m_guienv, nv::rectangle(a).dim(b) );
     88        nv::gui::element* e = m_guienv->create_element( nullptr, nv::rectangle(a).dim(b) );
    8389        e->set_class( "window" );
    8490        e->set_text("Window "+nv::to_string(m_windows.size()+1) );
    85         m_guienv->add_child( e );
    8691        NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
    8792        m_windows.push_back( e );
     
    9297        if ( m_windows.size() == 0 ) return;
    9398        size_t index = rand() % m_windows.size();
    94         delete m_windows[ index ];
     99        m_guienv->destroy_element( m_windows[index] );
    95100        m_windows.erase( m_windows.begin() + index );
    96101}
     
    102107        delete m_device;
    103108}
     109
     110// void application::bring_to_front( int i )
     111// {
     112//      if ( i >= 0 && i < m_windows.size() )
     113//      {
     114//              m_guienv->bring_to_front( m_windows[i] );
     115//      }
     116// }
    104117
    105118int main(int, char* [])
Note: See TracChangeset for help on using the changeset viewer.