[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 |
|
---|
[350] | 23 | bool style::get( element* e, const char* centry, std::string& s )
|
---|
[104] | 24 | {
|
---|
| 25 | lua::stack_guard guard( m_lua );
|
---|
[350] | 26 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TSTRING ) ) return false;
|
---|
[104] | 27 | s = lua_tostring( m_lua, -1 );
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[350] | 31 | bool style::get( element* e, const char* centry, vec4& vec )
|
---|
[104] | 32 | {
|
---|
| 33 | lua::stack_guard guard( m_lua );
|
---|
[350] | 34 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), 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 |
|
---|
[350] | 46 | bool style::get( element* e, const char* centry, int& i )
|
---|
[104] | 47 | {
|
---|
| 48 | lua::stack_guard guard( m_lua );
|
---|
[350] | 49 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TNUMBER ) ) return false;
|
---|
[204] | 50 | i = static_cast< int >( lua_tointeger( m_lua, -1 ) );
|
---|
[104] | 51 | return true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[350] | 54 | bool style::get( element* e, const char* centry, double& d )
|
---|
[104] | 55 | {
|
---|
| 56 | lua::stack_guard guard( m_lua );
|
---|
[350] | 57 | if ( !resolve( e->m_id.c_str(), e->m_class.c_str(), centry, LUA_TNUMBER ) ) return false;
|
---|
[104] | 58 | d = lua_tonumber( m_lua, -1 );
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | style::~style()
|
---|
| 63 | {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[350] | 66 | bool style::resolve( const char* cid, const char* cclass, const char* centry, int type )
|
---|
[104] | 67 | {
|
---|
| 68 | lua_getglobal( m_lua, "default" );
|
---|
| 69 | int global = lua_gettop( m_lua );
|
---|
| 70 |
|
---|
| 71 | // check id
|
---|
| 72 | lua_getfield( m_lua, -1, cid );
|
---|
[114] | 73 | if ( lua_istable( m_lua, -1 ) )
|
---|
[104] | 74 | {
|
---|
| 75 | lua_getfield( m_lua, -1, centry );
|
---|
| 76 | if ( lua_type( m_lua, -1 ) == type ) return true;
|
---|
| 77 | }
|
---|
| 78 | lua_settop( m_lua, global );
|
---|
| 79 |
|
---|
| 80 | // check class
|
---|
| 81 | lua_getfield( m_lua, -1, cclass );
|
---|
[114] | 82 | if ( lua_istable( m_lua, -1 ) )
|
---|
[104] | 83 | {
|
---|
| 84 | lua_getfield( m_lua, -1, centry );
|
---|
| 85 | if ( lua_type( m_lua, -1 ) == type ) return true;
|
---|
| 86 | }
|
---|
| 87 | lua_settop( m_lua, global );
|
---|
| 88 |
|
---|
| 89 | // check entry
|
---|
[114] | 90 | lua_getfield( m_lua, -1, centry );
|
---|
[104] | 91 | if ( lua_type( m_lua, -1 ) == type ) return true;
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|