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 |
|
---|
12 | using namespace nv;
|
---|
13 | using namespace nv::gui;
|
---|
14 |
|
---|
15 | void 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 |
|
---|
21 | bool 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 |
|
---|
30 | bool 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 ( int i = 0; i < 4; ++i )
|
---|
36 | {
|
---|
37 | lua_rawgeti( m_lua, -1, 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 |
|
---|
45 | bool 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 |
|
---|
53 | bool 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 |
|
---|
61 | style::~style()
|
---|
62 | {
|
---|
63 | }
|
---|
64 |
|
---|
65 | bool 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 |
|
---|
90 | bool 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 | }
|
---|