[69] | 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 |
|
---|
[126] | 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 |
|
---|
[152] | 24 | #include "nv/gfx/sliced_buffer.hh"
|
---|
[126] | 25 | #include "nv/gfx/texture_atlas.hh"
|
---|
| 26 |
|
---|
| 27 | #include <vector>
|
---|
| 28 |
|
---|
[69] | 29 | using namespace nv;
|
---|
| 30 | using namespace nv::gui;
|
---|
| 31 |
|
---|
[234] | 32 | environment::environment( window* w, const std::string& shader_path )
|
---|
[126] | 33 | : m_renderer( nullptr ), m_window( w ), m_screen( nullptr )
|
---|
| 34 | {
|
---|
| 35 | m_area.dim( dimension( w->get_width(), w->get_height() ) );
|
---|
[257] | 36 | m_screen = new screen( m_area );
|
---|
[234] | 37 | m_renderer = new renderer( w, shader_path );
|
---|
[126] | 38 | }
|
---|
| 39 |
|
---|
| 40 | void nv::gui::environment::load_style( const std::string& filename )
|
---|
| 41 | {
|
---|
| 42 | m_renderer->load_style( filename );
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[257] | 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 |
|
---|
[264] | 53 | void nv::gui::environment::destroy_element( element* e )
|
---|
| 54 | {
|
---|
| 55 | destroy_children( e );
|
---|
| 56 | e->detach();
|
---|
| 57 | delete e;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[126] | 60 | void environment::update( element* e, uint32 elapsed )
|
---|
| 61 | {
|
---|
[257] | 62 | e->on_update( elapsed );
|
---|
| 63 | if ( e->is_visible() )
|
---|
| 64 | {
|
---|
| 65 | for ( object* i : *e )
|
---|
| 66 | {
|
---|
| 67 | update( ((element*)i), elapsed );
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
[126] | 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 |
|
---|
[69] | 77 | void environment::draw( element* e )
|
---|
| 78 | {
|
---|
[257] | 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 | }
|
---|
[69] | 88 | }
|
---|
[126] | 89 |
|
---|
| 90 | void environment::update()
|
---|
| 91 | {
|
---|
[257] | 92 | update( m_screen, 0 );
|
---|
[126] | 93 | }
|
---|
| 94 |
|
---|
| 95 | void environment::draw()
|
---|
| 96 | {
|
---|
[257] | 97 | draw( m_screen );
|
---|
[126] | 98 | m_renderer->draw();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[264] | 101 | void environment::add_child( element* child )
|
---|
[126] | 102 | {
|
---|
| 103 | m_screen->add_child( child );
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[264] | 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 |
|
---|
[126] | 115 | environment::~environment()
|
---|
| 116 | {
|
---|
[264] | 117 | destroy_element( m_screen );
|
---|
[126] | 118 | delete m_renderer;
|
---|
| 119 | }
|
---|
| 120 |
|
---|