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_environment.hh"
|
---|
8 |
|
---|
9 | #include "nv/gui/gui_renderer.hh"
|
---|
10 |
|
---|
11 |
|
---|
12 | /*
|
---|
13 |
|
---|
14 | TODO: parse a lua stylesheet as per Trac wiki
|
---|
15 |
|
---|
16 | IDEA: Store everything in std::unordered_maps, with lua_value's?
|
---|
17 |
|
---|
18 | A lua_value is a variant stores strings as const char* that deletes them on destructor?
|
---|
19 | Question is that there might not be much gained on speed anyway, due to Lua's speed.
|
---|
20 | Special function field allows delayed per parse execution?
|
---|
21 |
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "nv/gfx/sliced_buffer.hh"
|
---|
25 | #include "nv/gfx/texture_atlas.hh"
|
---|
26 |
|
---|
27 | #include <vector>
|
---|
28 |
|
---|
29 | using namespace nv;
|
---|
30 | using namespace nv::gui;
|
---|
31 |
|
---|
32 | environment::environment( window* w, const std::string& shader_path )
|
---|
33 | : m_renderer( nullptr ), m_window( w ), m_screen( nullptr )
|
---|
34 | {
|
---|
35 | m_area.dim( dimension( w->get_width(), w->get_height() ) );
|
---|
36 | m_screen = new screen( m_area );
|
---|
37 | m_renderer = new renderer( w, shader_path );
|
---|
38 | }
|
---|
39 |
|
---|
40 | void nv::gui::environment::load_style( const std::string& filename )
|
---|
41 | {
|
---|
42 | m_renderer->load_style( filename );
|
---|
43 | }
|
---|
44 |
|
---|
45 | element* nv::gui::environment::create_element( element* parent, const rectangle& r )
|
---|
46 | {
|
---|
47 | element* result = new element( r );
|
---|
48 | if ( parent == nullptr ) parent = m_screen;
|
---|
49 | parent->add_child( result );
|
---|
50 | return result;
|
---|
51 | }
|
---|
52 |
|
---|
53 | void nv::gui::environment::destroy_element( element* e )
|
---|
54 | {
|
---|
55 | destroy_children( e );
|
---|
56 | e->detach();
|
---|
57 | delete e;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void environment::update( element* e, uint32 elapsed )
|
---|
61 | {
|
---|
62 | e->on_update( elapsed );
|
---|
63 | if ( e->is_visible() )
|
---|
64 | {
|
---|
65 | for ( object* i : *e )
|
---|
66 | {
|
---|
67 | update( ((element*)i), elapsed );
|
---|
68 | }
|
---|
69 | }
|
---|
70 | if ( e->is_dirty() || e->m_render_data == nullptr )
|
---|
71 | {
|
---|
72 | m_renderer->redraw( e, elapsed );
|
---|
73 | e->set_dirty( false );
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | void environment::draw( element* e )
|
---|
78 | {
|
---|
79 | if ( e->is_visible() )
|
---|
80 | {
|
---|
81 | e->on_draw();
|
---|
82 | m_renderer->draw( e );
|
---|
83 | for ( object* i : *e )
|
---|
84 | {
|
---|
85 | draw((element*)i);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void environment::update()
|
---|
91 | {
|
---|
92 | update( m_screen, 0 );
|
---|
93 | }
|
---|
94 |
|
---|
95 | void environment::draw()
|
---|
96 | {
|
---|
97 | draw( m_screen );
|
---|
98 | m_renderer->draw();
|
---|
99 | }
|
---|
100 |
|
---|
101 | void environment::add_child( element* child )
|
---|
102 | {
|
---|
103 | m_screen->add_child( child );
|
---|
104 | }
|
---|
105 |
|
---|
106 | void environment::destroy_children( element* e )
|
---|
107 | {
|
---|
108 | while ( !e->m_children.empty() )
|
---|
109 | {
|
---|
110 | destroy_element( (element*)e->m_children.front() );
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | environment::~environment()
|
---|
116 | {
|
---|
117 | destroy_element( m_screen );
|
---|
118 | delete m_renderer;
|
---|
119 | }
|
---|
120 |
|
---|