[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[104] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 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 |
|
---|
[351] | 23 | bool style::get( element* e, const char* centry, const char* cselector, std::string& s )
|
---|
[104] | 24 | {
|
---|
| 25 | lua::stack_guard guard( m_lua );
|
---|
[351] | 26 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TSTRING ) ) return false;
|
---|
[104] | 27 | s = lua_tostring( m_lua, -1 );
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[351] | 31 | bool style::get( element* e, const char* centry, const char* cselector, vec4& vec )
|
---|
[104] | 32 | {
|
---|
| 33 | lua::stack_guard guard( m_lua );
|
---|
[351] | 34 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TTABLE ) ) return false;
|
---|
[104] | 35 | vec = vec4();
|
---|
| 36 | for (size_t i = 0; i < 4; ++i )
|
---|
| 37 | {
|
---|
[121] | 38 | lua_rawgeti( m_lua, -1, static_cast<int>( i+1 ) );
|
---|
[104] | 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 |
|
---|
[351] | 46 | bool style::get( element* e, const char* centry, const char* cselector, int& i )
|
---|
[104] | 47 | {
|
---|
| 48 | lua::stack_guard guard( m_lua );
|
---|
[351] | 49 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TNUMBER ) ) return false;
|
---|
[204] | 50 | i = static_cast< int >( lua_tointeger( m_lua, -1 ) );
|
---|
[104] | 51 | return true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[351] | 54 | bool style::get( element* e, const char* centry, const char* cselector, double& d )
|
---|
[104] | 55 | {
|
---|
| 56 | lua::stack_guard guard( m_lua );
|
---|
[351] | 57 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), cselector, centry, LUA_TNUMBER ) ) return false;
|
---|
[104] | 58 | d = lua_tonumber( m_lua, -1 );
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | style::~style()
|
---|
| 63 | {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[351] | 66 | bool style::find_entry( const char* cselector, const char* centry, int type )
|
---|
[104] | 67 | {
|
---|
[351] | 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 | {
|
---|
[104] | 93 | lua_getglobal( m_lua, "default" );
|
---|
| 94 | int global = lua_gettop( m_lua );
|
---|
| 95 |
|
---|
| 96 | // check id
|
---|
| 97 | lua_getfield( m_lua, -1, cid );
|
---|
[351] | 98 | if ( find_entry( cselector, centry, type ) ) return true;
|
---|
[104] | 99 | lua_settop( m_lua, global );
|
---|
| 100 |
|
---|
| 101 | // check class
|
---|
| 102 | lua_getfield( m_lua, -1, cclass );
|
---|
[351] | 103 | if ( find_entry( cselector, centry, type ) ) return true;
|
---|
[104] | 104 | lua_settop( m_lua, global );
|
---|
| 105 |
|
---|
| 106 | // check entry
|
---|
[351] | 107 | if ( find_entry( cselector, centry, type ) ) return true;
|
---|
[104] | 108 | return false;
|
---|
| 109 | }
|
---|