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

Last change on this file since 535 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
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/gui/gui_style.hh"
8
9#include "nv/gui/gui_environment.hh"
10#include <nv/lua/lua_raw.hh>
11
12using namespace nv;
13using namespace nv::gui;
14
15void style::load_style( const string_view& filename )
16{
17        NV_ASSERT_ALWAYS( m_env, "Environment not set in style!" );
18        m_lua.do_file( filename );
19}
20
21bool style::get( element* e, const string_view& centry, const string_view& cselector, string128& s )
22{
23        lua::stack_guard guard( m_lua );
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 ) );
27        return true;
28}
29
30bool style::get( element* e, const string_view& centry, const string_view& cselector, vec4& vec )
31{
32        lua::stack_guard guard( m_lua );
33        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TTABLE ) ) return false;
34        vec = vec4();
35        for ( uint32 i = 0; i < 4; ++i )
36        {
37                lua_rawgeti( m_lua, -1, int(i+1) );
38                if ( lua_isnil( m_lua, -1 ) ) return true;
39                vec[i] = static_cast< float >( lua_tonumber( m_lua, -1 ) );
40                lua_pop( m_lua, 1 );
41        }
42        return true;
43}
44
45bool style::get( element* e, const string_view& centry, const string_view& cselector, int& i )
46{
47        lua::stack_guard guard( m_lua );
48        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TNUMBER ) ) return false;
49        i = static_cast< int >( lua_tointeger( m_lua, -1 ) );
50        return true;
51}
52
53bool style::get( element* e, const string_view& centry, const string_view& cselector, double& d )
54{
55        lua::stack_guard guard( m_lua );
56        if ( !resolve( e->m_id, e->m_class, cselector, centry, LUA_TNUMBER ) ) return false;
57        d = lua_tonumber( m_lua, -1 );
58        return true;
59}
60
61style::~style()
62{
63}
64
65bool style::find_entry( const string_view& cselector, const string_view& centry, int type )
66{
67        if ( lua_istable( m_lua, -1 ) )
68        {
69                if ( !cselector.empty() )
70                {
71                        lua_getfield( m_lua, -1, cselector.data() );
72                        if ( lua_istable( m_lua, -1 ) )
73                        {
74                                lua_getfield( m_lua, -1, centry.data() );
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
84                lua_getfield( m_lua, -1, centry.data() );
85                if ( lua_type( m_lua, -1 ) == type ) return true;
86        }
87        return false;
88}
89
90bool style::resolve( shash64 cid, shash64 cclass, const string_view& cselector, const string_view& centry, int type )
91{
92        lua_getglobal( m_lua, "default" );
93        int global = lua_gettop( m_lua );
94
95        // check id
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        }
103        // check class
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        }
111
112        // check entry
113        if ( find_entry( cselector, centry, type ) ) return true;
114        return false;
115}
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.