[104] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 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 |
|
---|
| 23 | bool style::get( element* e, const std::string& entry, std::string& s )
|
---|
| 24 | {
|
---|
| 25 | lua::stack_guard guard( m_lua );
|
---|
| 26 | if ( !resolve( e, entry, LUA_TSTRING ) ) return false;
|
---|
| 27 | s = lua_tostring( m_lua, -1 );
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | bool style::get( element* e, const std::string& entry, vec4& vec )
|
---|
| 32 | {
|
---|
| 33 | lua::stack_guard guard( m_lua );
|
---|
| 34 | if ( !resolve( e, entry, LUA_TTABLE ) ) return false;
|
---|
| 35 | vec = vec4();
|
---|
| 36 | for (size_t i = 0; i < 4; ++i )
|
---|
| 37 | {
|
---|
| 38 | lua_rawgeti( m_lua, -1, 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 std::string& entry, int& i )
|
---|
| 47 | {
|
---|
| 48 | lua::stack_guard guard( m_lua );
|
---|
| 49 | if ( !resolve( e, entry, LUA_TNUMBER ) ) return false;
|
---|
| 50 | i = lua_tointeger( m_lua, -1 );
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | bool style::get( element* e, const std::string& entry, double& d )
|
---|
| 55 | {
|
---|
| 56 | lua::stack_guard guard( m_lua );
|
---|
| 57 | if ( !resolve( e, entry, 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::resolve( element* e, const std::string& entry, int type )
|
---|
| 67 | {
|
---|
| 68 | const char* centry = entry.c_str();
|
---|
| 69 | const char* cid = e->get_id().c_str();
|
---|
| 70 | const char* cclass = e->get_class().c_str();
|
---|
| 71 | lua_getglobal( m_lua, "default" );
|
---|
| 72 | int global = lua_gettop( m_lua );
|
---|
| 73 |
|
---|
| 74 | // check id
|
---|
| 75 | lua_getfield( m_lua, -1, cid );
|
---|
[114] | 76 | if ( lua_istable( m_lua, -1 ) )
|
---|
[104] | 77 | {
|
---|
| 78 | lua_getfield( m_lua, -1, centry );
|
---|
| 79 | if ( lua_type( m_lua, -1 ) == type ) return true;
|
---|
| 80 | }
|
---|
| 81 | lua_settop( m_lua, global );
|
---|
| 82 |
|
---|
| 83 | // check class
|
---|
| 84 | lua_getfield( m_lua, -1, cclass );
|
---|
[114] | 85 | if ( lua_istable( m_lua, -1 ) )
|
---|
[104] | 86 | {
|
---|
| 87 | lua_getfield( m_lua, -1, centry );
|
---|
| 88 | if ( lua_type( m_lua, -1 ) == type ) return true;
|
---|
| 89 | }
|
---|
| 90 | lua_settop( m_lua, global );
|
---|
| 91 |
|
---|
| 92 | // check entry
|
---|
[114] | 93 | lua_getfield( m_lua, -1, centry );
|
---|
[104] | 94 | if ( lua_type( m_lua, -1 ) == type ) return true;
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|