Changeset 351 for trunk/src/gui


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

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.