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/lua/lua_raw.hh>
|
---|
10 |
|
---|
11 | using namespace nv;
|
---|
12 | using namespace nv::gui;
|
---|
13 |
|
---|
14 | style::style()
|
---|
15 | {
|
---|
16 | }
|
---|
17 |
|
---|
18 | void style::load_style( const std::string& filename )
|
---|
19 | {
|
---|
20 | m_lua.do_file( filename );
|
---|
21 | }
|
---|
22 |
|
---|
23 | bool style::get( element* e, const char* centry, const char* cselector, std::string& s )
|
---|
24 | {
|
---|
25 | lua::stack_guard guard( m_lua );
|
---|
26 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TSTRING ) ) return false;
|
---|
27 | s = lua_tostring( m_lua, -1 );
|
---|
28 | return true;
|
---|
29 | }
|
---|
30 |
|
---|
31 | bool style::get( element* e, const char* centry, const char* cselector, vec4& vec )
|
---|
32 | {
|
---|
33 | lua::stack_guard guard( m_lua );
|
---|
34 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TTABLE ) ) return false;
|
---|
35 | vec = vec4();
|
---|
36 | for (size_t i = 0; i < 4; ++i )
|
---|
37 | {
|
---|
38 | lua_rawgeti( m_lua, -1, static_cast<int>( i+1 ) );
|
---|
39 | if ( lua_isnil( m_lua, -1 ) ) return true;
|
---|
40 | vec[i] = (float)lua_tonumber( m_lua, -1 );
|
---|
41 | lua_pop( m_lua, 1 );
|
---|
42 | }
|
---|
43 | return true;
|
---|
44 | }
|
---|
45 |
|
---|
46 | bool style::get( element* e, const char* centry, const char* cselector, int& i )
|
---|
47 | {
|
---|
48 | lua::stack_guard guard( m_lua );
|
---|
49 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TNUMBER ) ) return false;
|
---|
50 | i = static_cast< int >( lua_tointeger( m_lua, -1 ) );
|
---|
51 | return true;
|
---|
52 | }
|
---|
53 |
|
---|
54 | bool style::get( element* e, const char* centry, const char* cselector, double& d )
|
---|
55 | {
|
---|
56 | lua::stack_guard guard( m_lua );
|
---|
57 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TNUMBER ) ) return false;
|
---|
58 | d = lua_tonumber( m_lua, -1 );
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 |
|
---|
62 | style::~style()
|
---|
63 | {
|
---|
64 | }
|
---|
65 |
|
---|
66 | bool 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 |
|
---|
91 | bool style::resolve( const char* cid, const char* cclass, const char* cselector, const char* centry, int type )
|
---|
92 | {
|
---|
93 | lua_getglobal( m_lua, "default" );
|
---|
94 | int global = lua_gettop( m_lua );
|
---|
95 |
|
---|
96 | // check id
|
---|
97 | lua_getfield( m_lua, -1, cid );
|
---|
98 | if ( find_entry( cselector, centry, type ) ) return true;
|
---|
99 | lua_settop( m_lua, global );
|
---|
100 |
|
---|
101 | // check class
|
---|
102 | lua_getfield( m_lua, -1, cclass );
|
---|
103 | if ( find_entry( cselector, centry, type ) ) return true;
|
---|
104 | lua_settop( m_lua, global );
|
---|
105 |
|
---|
106 | // check entry
|
---|
107 | if ( find_entry( cselector, centry, type ) ) return true;
|
---|
108 | return false;
|
---|
109 | }
|
---|