Changeset 351


Ignore:
Timestamp:
02/15/15 21:33:48 (10 years ago)
Author:
epyon
Message:
  • gui hover and selected support
  • minor changes
Location:
trunk
Files:
14 edited

Legend:

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

    r319 r351  
    1616#include <nv/core/common.hh>
    1717#include <nv/core/handle.hh>
     18#include <nv/core/flags.hh>
    1819
    1920namespace nv
     
    2122        namespace gui
    2223        {
     24                enum flag
     25                {
     26                        ENABLED,  //!< GUI element accepts events
     27                        VISIBLE,  //!< GUI element visible
     28                        DIRTY,    //!< GUI element needs redraw
     29                        SELECTED, //!< GUI element selected
     30                        HOVER,    //!< GUI element in hover zone
     31                };
     32
    2333                enum alignment
    2434                {
     
    4353                typedef nv::handle<> handle;
    4454
     55                typedef nv::flags< 32, flag > flags;
     56
    4557        } // namespace gui
    4658
  • trunk/nv/gui/gui_element.hh

    r319 r351  
    3232                        typedef std::list<handle> list;
    3333
    34 
    35                         element()
    36                                 : m_id( "" )
    37                                 , m_child_count(0)
    38                                 , m_class("")
    39                                 , m_enabled( true )
    40                                 , m_visible( true )
    41                                 , m_dirty( true )
    42                                 , m_render_data( nullptr ) {}
    43 
    4434                        string    m_id;              ///< id type of the object
    4535                        handle    m_parent;          ///< pointer to parent
     36                        flags     m_flags;
    4637                        list      m_children;        ///< children objects
    4738                        size_t    m_child_count;     ///< number of children
     
    5041                        rectangle m_relative; ///< Position relative to parent.
    5142                        rectangle m_absolute; ///< Position relative to window/screen.
    52                         bool m_enabled; ///< Whether the element accepts events.
    53                         bool m_visible; ///< Whether the element is drawn.
    54                         bool m_dirty; ///< Whether the element needs updating.
    5543                        render_data* m_render_data; ///<   -?-
    5644                };
  • trunk/nv/gui/gui_environment.hh

    r347 r351  
    4343                        virtual ~environment();
    4444                protected:
     45                        bool set_selected( handle e );
    4546                        handle get_element( const position& p );
    4647                        void add_child( handle child );
     
    5758                        void set_relative( handle e, const position& p );
    5859                        void recalculate_absolute( handle e );
    59                        
     60
    6061                        handle_store< element, handle > m_elements;
    6162                        renderer*     m_renderer;
     
    6364                        handle        m_screen;
    6465                        rectangle     m_area;
     66                        position      m_mouse_position;
     67                        handle        m_selected;
    6568                };
    6669
  • trunk/nv/gui/gui_renderer.hh

    r328 r351  
    3030        namespace gui
    3131        {
    32                 struct vertex
    33                 {
    34                         ivec2 position;
    35                         vec2  texcoord;
    36                         vec4  color;
    37                         vertex() {}
    38                         vertex( const nv::ivec2& cr, const nv::vec2& tc, const nv::vec4& c )
    39                                 : position( cr ), texcoord( tc ), color( c ) {}
    40                 };
    41 
    4232                struct image_info
    4333                {
     
    6353                        void draw( element* e );
    6454                        void draw();
     55                        void on_hover_change( element* e, bool hover );
     56                        void on_select_change( element* e, bool select );
    6557                        virtual ~renderer();
    6658                private:
  • trunk/nv/gui/gui_style.hh

    r350 r351  
    2828                        style();
    2929                        void load_style( const std::string& filename );
    30                         bool get( element* e, const char* centry, std::string& s );
    31                         bool get( element* e, const char* centry, vec4& vec );
    32                         bool get( element* e, const char* centry, int& i );
    33                         bool get( element* e, const char* centry, double& d );
     30                        bool get( element* e, const char* centry, const char* cselector, std::string& s );
     31                        bool get( element* e, const char* centry, const char* cselector, vec4& vec );
     32                        bool get( element* e, const char* centry, const char* cselector, int& i );
     33                        bool get( element* e, const char* centry, const char* cselector, double& d );
    3434                        ~style();
    3535                protected:
    36                         bool resolve( const char* cid, const char* cclass, const char* centry, int type );
     36                        bool find_entry( const char* cselector, const char* centry, int type );
     37                        bool resolve( const char* cid, const char* cclass, const char* cselector, const char* centry, int type );
    3738                protected:
    3839                        lua::state m_lua; //!< separate lua state for style calculation
  • trunk/nv/interface/image_data.hh

    r340 r351  
    2828                RGB16F,
    2929                RGBA16F,
     30                BGR,
     31                BGRA,
    3032        };
    3133       
     
    4850                const ivec2 get_size() const { return m_size; }
    4951                // TODO : better depth check (template?)
    50                 size_t get_depth()    const { return m_format.format == RGB ? 3 : 4; }
     52                size_t get_depth()    const { return m_format.format == RGB || m_format.format == BGR ? 3 : 4; }
    5153                image_format get_format() const { return m_format; }
    5254                ~image_data() { if (m_data) delete[] m_data; }
  • trunk/nv/lib/detail/gl_types.inc

    r331 r351  
    243243#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
    244244#define GL_BGR 0x80E0
     245#define GL_BGRA 0x80E1
    245246#define GL_MAX_ELEMENTS_VERTICES 0x80E8
    246247#define GL_MAX_ELEMENTS_INDICES 0x80E9
  • trunk/src/gl/gl_enum.cc

    r350 r351  
    180180        case RGB16F  : return GL_RGB;
    181181        case RGBA16F : return GL_RGBA;
     182        case BGR     : return GL_BGR;
     183        case BGRA    : return GL_BGRA;
    182184        NV_RETURN_COVERED_DEFAULT( 0 );
    183185        }
     
    194196        case RGB16F  : return GL_RGBA16F;
    195197        case RGBA16F : return GL_RGBA16F;
     198        case BGR     : return GL_RGB8;
     199        case BGRA    : return GL_RGBA8;
    196200        NV_RETURN_COVERED_DEFAULT( 0 );
    197201        }
  • trunk/src/gui/gui_environment.cc

    r344 r351  
    2929{
    3030        m_area.dim( dimension( w->get_width(), w->get_height() ) );
    31        
    32         m_screen           = m_elements.create();
    33         element* screen    = m_elements.get( m_screen );
    34         screen->m_absolute = m_area;
    35         screen->m_relative = m_area;
    36 
    3731        m_renderer = new renderer( w );
     32        m_screen   = create_element( handle(), m_area );
    3833}
    3934
     
    5146{
    5247        if ( parent.is_nil() ) parent = m_screen;
     48
    5349        handle result = m_elements.create();
    5450        element* e    = m_elements.get( result );
     
    5854        if ( ar.ul.y < 0 ) { ar.ul.y += m_area.lr.y; ar.lr.y += m_area.lr.y; }
    5955
     56        e->m_child_count = 0;
     57        e->m_flags[ENABLED] = true;
     58        e->m_flags[VISIBLE] = true;
     59        e->m_flags[DIRTY]   = true;
     60        e->m_render_data    = nullptr;
    6061        e->m_absolute = ar;
    6162        e->m_relative = ar;
    62         add_child( parent, result );
     63
     64        if ( !parent.is_nil() ) // screen creation
     65                add_child( parent, result );
    6366        return result;
    6467}
     
    8386        if ( !el ) return;
    8487        //      el->on_update( elapsed );
    85         if ( el->m_visible )
     88        if ( el->m_flags[ENABLED] )
     89        {
     90                bool hover     = el->m_flags[HOVER];
     91                bool new_hover = el->m_absolute.contains( m_mouse_position );
     92                if ( hover != new_hover )
     93                {
     94                        el->m_flags[HOVER] = new_hover;
     95                        // gain loose hover event
     96                        m_renderer->on_hover_change( el, hover );
     97                }
     98        }
     99
     100        if ( el->m_flags[VISIBLE] )
    86101        {
    87102                for ( handle i : el->m_children )
     
    90105                }
    91106        }
    92         if ( el->m_dirty || el->m_render_data == nullptr )
     107        if ( el->m_flags[DIRTY] || el->m_render_data == nullptr )
    93108        {
    94109                m_renderer->redraw( el, elapsed );
    95                 el->m_dirty = false;
     110                el->m_flags[DIRTY] = false;
    96111        }
    97112}
     
    101116        element* el = m_elements.get( e );
    102117        if ( !el ) return;
    103         if ( el->m_visible )
     118        if ( el->m_flags[VISIBLE] )
    104119        {
    105120//              el->on_draw();
     
    160175}
    161176
    162 bool nv::gui::environment::process_io_event( const io_event& )
    163 {
     177bool nv::gui::environment::process_io_event( const io_event& ev )
     178{
     179        switch ( ev.type )
     180        {
     181//      case EV_KEY          :
     182        case EV_MOUSE_BUTTON :
     183                if ( ev.mbutton.pressed && ev.mbutton.button == MOUSE_LEFT )
     184                {
     185                        handle h = get_element( position( ev.mbutton.x, ev.mbutton.y ) );
     186                        set_selected( h );
     187                        return true;
     188                }
     189                return false;
     190        case EV_MOUSE_MOVE:
     191                m_mouse_position = position( ev.mmove.x, ev.mmove.y );
     192                return false;
     193//      case EV_MOUSE_WHEEL  :
     194        default:
     195                break;
     196        }
     197        return false;
     198}
     199
     200bool nv::gui::environment::set_selected( handle e )
     201{
     202        if ( e != m_selected )
     203        {
     204                element* eold = m_elements.get( m_selected );
     205                element* el   = m_elements.get( e );
     206                if ( eold )
     207                {
     208                        eold->m_flags[SELECTED] = false;
     209                        m_renderer->on_select_change( eold, false );
     210                }
     211                if ( el )
     212                {
     213                        el->m_flags[SELECTED] = true;
     214                        m_renderer->on_select_change( el, true );
     215                }
     216                m_selected = e;
     217                return true;
     218        }
    164219        return false;
    165220}
     
    179234{
    180235        element* el = m_elements.get(e);
    181         if ( !el && !el->m_visible ) return handle();
     236        if ( !el && !el->m_flags[VISIBLE] ) return handle();
    182237
    183238        handle result;
     
    206261                        parent->m_children.erase( it );
    207262                        parent->m_children.push_back( child );
    208                         parent->m_dirty = true;
     263                        parent->m_flags[DIRTY] = true;
    209264                }       
    210265        }
     
    222277                        parent->m_children.erase( it );
    223278                        parent->m_children.push_front( child );
    224                         parent->m_dirty = true;
     279                        parent->m_flags[DIRTY] = true;
    225280                }
    226281        }
     
    232287        if ( el )
    233288        {
    234                 el->m_dirty    = true;
    235                 el->m_relative = r;
     289                el->m_flags[DIRTY] = true;
     290                el->m_relative     = r;
    236291                recalculate_absolute( e );
    237292        }
     
    264319        if ( ep != nullptr )
    265320        {
    266                 ep->m_class = text;
    267                 ep->m_dirty = true;
     321                ep->m_class        = text;
     322                ep->m_flags[DIRTY] = true;
    268323        }
    269324}
     
    274329        if ( ep != nullptr )
    275330        {
    276                 ep->m_text = text;
    277                 ep->m_dirty = true;
     331                ep->m_text         = text;
     332                ep->m_flags[DIRTY] = true;
    278333        }
    279334}
  • trunk/src/gui/gui_renderer.cc

    r346 r351  
    4444using namespace nv;
    4545using namespace nv::gui;
     46
     47struct vertex
     48{
     49        ivec2 position;
     50        vec2  texcoord;
     51        vec4  color;
     52        vertex() {}
     53        vertex( const nv::ivec2& cr, const nv::vec2& tc, const nv::vec4& c )
     54                : position( cr ), texcoord( tc ), color( c )
     55        {
     56        }
     57};
    4658
    4759const ivec2 atlas_size = ivec2( 1024, 1024 );
     
    239251                std::string path;
    240252                std::string text;
    241 
    242                 if ( m_style.get( e, "skin", path ) )
     253                const char* stext[] = { nullptr, "selected", "hover" };
     254                const char* selector = stext[border];
     255                if ( e->m_flags[HOVER] )    selector = stext[2];
     256                if ( e->m_flags[SELECTED] ) selector = stext[1];
     257
     258                if ( m_style.get( e, "skin", selector, path ) )
    243259                {
    244260                        size_t image_id = load_image( path );
     
    286302                {
    287303
    288                         if ( m_style.get( e, "border", border ) && m_style.get( e, "border_color", color ) )
     304                        if ( m_style.get( e, "border", selector, border ) && m_style.get( e, "border_color", selector, color ) )
    289305                        {
    290306                                rectangle inner = abs.shrinked( border );
     
    296312                        }
    297313
    298                         if ( m_style.get( e, "background_color", color ) )
     314                        if ( m_style.get( e, "background_color", selector, color ) )
    299315                        {
    300316                                qvec.emplace_back( abs.ul, abs.lr, color );
     
    305321                if ( !text.empty() )
    306322                {
    307                         if ( m_style.get( e, "text_color", color ) && m_style.get( e, "text_font", path ) && m_style.get( e, "text_size", border ) )
     323                        if ( m_style.get( e, "text_color", selector, color ) && m_style.get( e, "text_font", selector, path ) && m_style.get( e, "text_size", selector, border ) )
    308324                        {
    309325                                size_t font_id = load_font( path, (uint16)border );
     
    331347}
    332348
     349void renderer::on_hover_change( element* e, bool hover )
     350{
     351        // TODO: FIX
     352        NV_LOG( nv::LOG_DEBUG, "on_hover_change" );
     353        e->m_flags[DIRTY] = true;
     354}
     355
     356void renderer::on_select_change( element* e, bool select )
     357{
     358        // TODO: FIX
     359        NV_LOG( nv::LOG_DEBUG, "on_select_change" );
     360        e->m_flags[DIRTY] = true;
     361}
     362
     363
    333364void renderer::draw( element* e )
    334365{
  • trunk/src/gui/gui_style.cc

    r350 r351  
    2121}
    2222
    23 bool style::get( element* e, const char* centry, std::string& s )
     23bool style::get( element* e, const char* centry, const char* cselector, std::string& s )
    2424{
    2525        lua::stack_guard guard( m_lua );
    26         if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TSTRING ) ) return false;
     26        if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TSTRING ) ) return false;
    2727        s = lua_tostring( m_lua, -1 );
    2828        return true;
    2929}
    3030
    31 bool style::get( element* e, const char* centry, vec4& vec )
     31bool style::get( element* e, const char* centry, const char* cselector, vec4& vec )
    3232{
    3333        lua::stack_guard guard( m_lua );
    34         if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TTABLE ) ) return false;
     34        if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TTABLE ) ) return false;
    3535        vec = vec4();
    3636        for (size_t i = 0; i < 4; ++i )
     
    4444}
    4545
    46 bool style::get( element* e, const char* centry, int& i )
     46bool style::get( element* e, const char* centry, const char* cselector, int& i )
    4747{
    4848        lua::stack_guard guard( m_lua );
    49         if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TNUMBER ) ) return false;
     49        if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TNUMBER ) ) return false;
    5050        i = static_cast< int >( lua_tointeger( m_lua, -1 ) );
    5151        return true;
    5252}
    5353
    54 bool style::get( element* e, const char* centry, double& d )
     54bool style::get( element* e, const char* centry, const char* cselector, double& d )
    5555{
    5656        lua::stack_guard guard( m_lua );
    57         if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TNUMBER ) ) return false;
     57        if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TNUMBER ) ) return false;
    5858        d = lua_tonumber( m_lua, -1 );
    5959        return true;
     
    6464}
    6565
    66 bool style::resolve( const char* cid, const char* cclass, const char* centry, int type )
     66bool style::find_entry( const char* cselector, const char* centry, int type )
     67{
     68        if ( lua_istable( m_lua, -1 ) )
     69        {
     70                if ( cselector )
     71                {
     72                        lua_getfield( m_lua, -1, cselector );
     73                        if ( lua_istable( m_lua, -1 ) )
     74                        {
     75                                lua_getfield( m_lua, -1, centry );
     76                                if ( lua_type( m_lua, -1 ) == type )
     77                                {
     78                                        return true;
     79                                }
     80                                lua_pop( m_lua, 1 );
     81                        }
     82                        lua_pop( m_lua, 1 );
     83                }
     84
     85                lua_getfield( m_lua, -1, centry );
     86                if ( lua_type( m_lua, -1 ) == type ) return true;
     87        }
     88        return false;
     89}
     90
     91bool style::resolve( const char* cid, const char* cclass, const char* cselector, const char* centry, int type )
    6792{
    6893        lua_getglobal( m_lua, "default" );
     
    7196        // check id
    7297        lua_getfield( m_lua, -1, cid );
    73         if ( lua_istable( m_lua, -1 ) )
    74         {
    75                 lua_getfield( m_lua, -1, centry );
    76                 if ( lua_type( m_lua, -1 ) == type ) return true;
    77         }
     98        if ( find_entry( cselector, centry, type ) ) return true;
    7899        lua_settop( m_lua, global );
    79100
    80101        // check class
    81102        lua_getfield( m_lua, -1, cclass );
    82         if ( lua_istable( m_lua, -1 ) )
    83         {
    84                 lua_getfield( m_lua, -1, centry );
    85                 if ( lua_type( m_lua, -1 ) == type ) return true;
    86         }
     103        if ( find_entry( cselector, centry, type ) ) return true;
    87104        lua_settop( m_lua, global );
    88105
    89106        // check entry
    90         lua_getfield( m_lua, -1, centry );
    91         if ( lua_type( m_lua, -1 ) == type ) return true;
     107        if ( find_entry( cselector, centry, type ) ) return true;
    92108        return false;
    93109}
  • trunk/tests/gui_test/nv_gui_test.cc

    r328 r351  
    8080                                break;
    8181                        }
     82                        m_guienv->process_io_event( event );
    8283                }
    8384        }
  • trunk/tests/gui_test/premake4.lua

    r321 r351  
    1515                objdir ("../../".._ACTION.."/release")
    1616
     17        dofile("../../nv.lua")
    1718        dofile("gui_test.lua")
    18         dofile("../../nv.lua")
  • trunk/tests/gui_test/test.style.lua

    r329 r351  
    1010                skin = "button.png",
    1111
    12                 [":hover"] = {
     12                hover = {
    1313                        text_color = { 1.0, 1.0, 1.0, 1.0 },
     14                },
     15
     16                selected = {
     17                        text_color = { 1.0, 1.0, 0.0, 1.0 },
    1418                }
    1519
Note: See TracChangeset for help on using the changeset viewer.