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

Last change on this file since 303 was 268, checked in by epyon, 11 years ago
  • gui::element completely independent of object
  • gui::element is pure data
File size: 2.3 KB
Line 
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
11using namespace nv;
12using namespace nv::gui;
13
14style::style()
15{
16}
17
18void style::load_style( const std::string& filename )
19{
20        m_lua.do_file( filename );
21}
22
23bool 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
31bool 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, 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
46bool 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 = static_cast< int >( lua_tointeger( m_lua, -1 ) );
51        return true;
52}
53
54bool 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
62style::~style()
63{
64}
65
66bool style::resolve( element* e, const std::string& entry, int type )
67{
68        const char* centry = entry.c_str();
69        const char* cid    = e->m_id.c_str();
70        const char* cclass = e->m_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 );
76        if ( lua_istable( m_lua, -1 ) )
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 );
85        if ( lua_istable( m_lua, -1 ) )
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
93        lua_getfield( m_lua, -1, centry );
94        if ( lua_type( m_lua, -1 ) == type ) return true;
95        return false;
96}
Note: See TracBrowser for help on using the repository browser.