source: trunk/src/gui/gui_style.cc @ 433

Last change on this file since 433 was 433, checked in by epyon, 10 years ago
  • string.hh split into separate files
  • string.hh - removed std::string conversions (header still stays though)
File size: 2.8 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
[104]2// http://chaosforge.org/
3//
[395]4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[104]6
7#include "nv/gui/gui_style.hh"
8
9#include <nv/lua/lua_raw.hh>
10
11using namespace nv;
12using namespace nv::gui;
13
14style::style()
15{
16}
17
[433]18void style::load_style( const string_view& filename )
[104]19{
20        m_lua.do_file( filename );
21}
22
[351]23bool 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]31bool 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();
[406]36        for ( int i = 0; i < 4; ++i )
[104]37        {
[406]38                lua_rawgeti( m_lua, -1, i+1 );
[104]39                if ( lua_isnil( m_lua, -1 ) ) return true;
[406]40                vec[i] = static_cast< float >( lua_tonumber( m_lua, -1 ) );
[104]41                lua_pop( m_lua, 1 );
42        }
43        return true;
44}
45
[351]46bool 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]54bool 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
62style::~style()
63{
64}
65
[351]66bool 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
91bool 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}
Note: See TracBrowser for help on using the repository browser.