source: trunk/src/gui/gui_style.cc @ 534

Last change on this file since 534 was 534, checked in by epyon, 8 years ago

CONTINUED:

  • getting rid of size_t
  • datatypes now restricted to uint32 size
  • 64-bit compatibility
  • copyright updates where modified
File size: 4.0 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[104]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[104]6
7#include "nv/gui/gui_style.hh"
8
[440]9#include "nv/gui/gui_environment.hh"
[104]10#include <nv/lua/lua_raw.hh>
11
12using namespace nv;
13using namespace nv::gui;
14
[433]15void style::load_style( const string_view& filename )
[104]16{
[440]17        NV_ASSERT_ALWAYS( m_env, "Environment not set in style!" );
[104]18        m_lua.do_file( filename );
19}
20
[440]21bool style::get( element* e, const string_view& centry, const string_view& cselector, string128& s )
[104]22{
23        lua::stack_guard guard( m_lua );
[440]24        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TSTRING ) ) return false;
25        // TODO: create lua_tostringbuffer< size >
26        s.assign( lua_tostring( m_lua, -1 ) );
[104]27        return true;
28}
29
[440]30bool style::get( element* e, const string_view& centry, const string_view& cselector, vec4& vec )
[104]31{
32        lua::stack_guard guard( m_lua );
[440]33        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TTABLE ) ) return false;
[104]34        vec = vec4();
[534]35        for ( uint32 i = 0; i < 4; ++i )
[104]36        {
[487]37                lua_rawgeti( m_lua, -1, int(i+1) );
[104]38                if ( lua_isnil( m_lua, -1 ) ) return true;
[406]39                vec[i] = static_cast< float >( lua_tonumber( m_lua, -1 ) );
[104]40                lua_pop( m_lua, 1 );
41        }
42        return true;
43}
44
[440]45bool style::get( element* e, const string_view& centry, const string_view& cselector, int& i )
[104]46{
47        lua::stack_guard guard( m_lua );
[440]48        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TNUMBER ) ) return false;
[204]49        i = static_cast< int >( lua_tointeger( m_lua, -1 ) );
[104]50        return true;
51}
52
[440]53bool style::get( element* e, const string_view& centry, const string_view& cselector, double& d )
[104]54{
55        lua::stack_guard guard( m_lua );
[440]56        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TNUMBER ) ) return false;
[104]57        d = lua_tonumber( m_lua, -1 );
58        return true;
59}
60
61style::~style()
62{
63}
64
[440]65bool style::find_entry( const string_view& cselector, const string_view& centry, int type )
[104]66{
[351]67        if ( lua_istable( m_lua, -1 ) )
68        {
[440]69                if ( !cselector.empty() )
[351]70                {
[440]71                        lua_getfield( m_lua, -1, cselector.data() );
[351]72                        if ( lua_istable( m_lua, -1 ) )
73                        {
[440]74                                lua_getfield( m_lua, -1, centry.data() );
[351]75                                if ( lua_type( m_lua, -1 ) == type )
76                                {
77                                        return true;
78                                }
79                                lua_pop( m_lua, 1 );
80                        }
81                        lua_pop( m_lua, 1 );
82                }
83
[440]84                lua_getfield( m_lua, -1, centry.data() );
[351]85                if ( lua_type( m_lua, -1 ) == type ) return true;
86        }
87        return false;
88}
89
[440]90bool style::resolve( shash64 cid, shash64 cclass, const string_view& cselector, const string_view& centry, int type )
[351]91{
[104]92        lua_getglobal( m_lua, "default" );
93        int global = lua_gettop( m_lua );
94
95        // check id
[440]96        string_view id( m_env->get_string( cid ) );
97        if ( !id.empty() )
98        {
99                lua_getfield( m_lua, -1, id.data() );
100                if ( find_entry( cselector, centry, type ) ) return true;
101                lua_settop( m_lua, global );
102        }
[104]103        // check class
[440]104        string_view klass( m_env->get_string( cclass ) );
105        if ( !klass.empty() )
106        {
107                lua_getfield( m_lua, -1, klass.data() );
108                if ( find_entry( cselector, centry, type ) ) return true;
109                lua_settop( m_lua, global );
110        }
[104]111
112        // check entry
[351]113        if ( find_entry( cselector, centry, type ) ) return true;
[104]114        return false;
115}
[444]116
117void nv::gui::style::load_flags( element* e )
118{
119        lua::stack_guard guard( m_lua );
120        lua_getglobal( m_lua, "default" );
121
122        // check id
123        string_view id( m_env->get_string( e->m_id ) );
124        if ( !id.empty() )
125        {
126                lua_getfield( m_lua, -1, id.data() );
127                add_flags( e );
128                lua_pop( m_lua, -1 );
129        }
130        // check class
131        string_view klass( m_env->get_string( e->m_class ) );
132        if ( !klass.empty() )
133        {
134                lua_getfield( m_lua, -1, klass.data() );
135                add_flags( e );
136                lua_pop( m_lua, -1 );
137        }
138        add_flags( e );
139}
140
141void nv::gui::style::add_flags( element* e )
142{
143        if ( lua_istable( m_lua, -1 ) )
144        {
145                lua_getfield( m_lua, -1, "hover" );
146                if ( lua_istable( m_lua, -1 ) )
147                {
148                        e->m_flags[DIRTY_HOVER] = true;
149                }
150                lua_pop( m_lua, 1 );
151
152                lua_getfield( m_lua, -1, "selected" );
153                if ( lua_istable( m_lua, -1 ) )
154                {
155                        e->m_flags[DIRTY_SELECT] = true;
156                }
157                lua_pop( m_lua, 1 );
158        }
159}
Note: See TracBrowser for help on using the repository browser.