[319] | 1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
[69] | 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 |
|
---|
[318] | 27 | nv::gui::environment::environment( window* w )
|
---|
[270] | 28 | : m_renderer( nullptr ), m_window( w )
|
---|
[126] | 29 | {
|
---|
| 30 | m_area.dim( dimension( w->get_width(), w->get_height() ) );
|
---|
[269] | 31 |
|
---|
[271] | 32 | m_screen = m_elements.create();
|
---|
| 33 | element* screen = m_elements.get( m_screen );
|
---|
[269] | 34 | screen->m_absolute = m_area;
|
---|
| 35 | screen->m_relative = m_area;
|
---|
| 36 |
|
---|
[318] | 37 | m_renderer = new renderer( w );
|
---|
[126] | 38 | }
|
---|
| 39 |
|
---|
| 40 | void nv::gui::environment::load_style( const std::string& filename )
|
---|
| 41 | {
|
---|
| 42 | m_renderer->load_style( filename );
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[269] | 45 | nv::gui::handle nv::gui::environment::create_element( const rectangle& r )
|
---|
[257] | 46 | {
|
---|
[269] | 47 | return create_element( m_screen, r );
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | nv::gui::handle nv::gui::environment::create_element( handle parent, const rectangle& r )
|
---|
| 51 | {
|
---|
| 52 | if ( parent.is_nil() ) parent = m_screen;
|
---|
[271] | 53 | handle result = m_elements.create();
|
---|
| 54 | element* e = m_elements.get( result );
|
---|
[344] | 55 | rectangle ar = r;
|
---|
| 56 |
|
---|
| 57 | if ( ar.ul.x < 0 ) { ar.ul.x += m_area.lr.x; ar.lr.x += m_area.lr.x; }
|
---|
| 58 | if ( ar.ul.y < 0 ) { ar.ul.y += m_area.lr.y; ar.lr.y += m_area.lr.y; }
|
---|
| 59 |
|
---|
| 60 | e->m_absolute = ar;
|
---|
| 61 | e->m_relative = ar;
|
---|
[268] | 62 | add_child( parent, result );
|
---|
[257] | 63 | return result;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[269] | 66 | void nv::gui::environment::destroy_element( handle e )
|
---|
| 67 | {
|
---|
[271] | 68 | element* dead_element = m_elements.get( e );
|
---|
[269] | 69 | if ( dead_element == nullptr ) return;
|
---|
[264] | 70 | destroy_children( e );
|
---|
[269] | 71 | remove_child( dead_element->m_parent, e );
|
---|
| 72 |
|
---|
| 73 | delete dead_element->m_render_data;
|
---|
| 74 | dead_element->m_render_data = nullptr;
|
---|
| 75 | dead_element->m_parent = handle();
|
---|
| 76 |
|
---|
[271] | 77 | m_elements.destroy( e );
|
---|
[264] | 78 | }
|
---|
| 79 |
|
---|
[269] | 80 | void nv::gui::environment::update( handle e, uint32 elapsed )
|
---|
[126] | 81 | {
|
---|
[271] | 82 | element* el = m_elements.get( e );
|
---|
[269] | 83 | if ( !el ) return;
|
---|
| 84 | // el->on_update( elapsed );
|
---|
| 85 | if ( el->m_visible )
|
---|
[257] | 86 | {
|
---|
[269] | 87 | for ( handle i : el->m_children )
|
---|
[257] | 88 | {
|
---|
[268] | 89 | update( i, elapsed );
|
---|
[257] | 90 | }
|
---|
| 91 | }
|
---|
[269] | 92 | if ( el->m_dirty || el->m_render_data == nullptr )
|
---|
[126] | 93 | {
|
---|
[269] | 94 | m_renderer->redraw( el, elapsed );
|
---|
| 95 | el->m_dirty = false;
|
---|
[126] | 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[269] | 99 | void nv::gui::environment::draw( handle e )
|
---|
[69] | 100 | {
|
---|
[271] | 101 | element* el = m_elements.get( e );
|
---|
[269] | 102 | if ( !el ) return;
|
---|
| 103 | if ( el->m_visible )
|
---|
[257] | 104 | {
|
---|
[269] | 105 | // el->on_draw();
|
---|
| 106 | m_renderer->draw( el );
|
---|
| 107 | for ( handle i : el->m_children )
|
---|
[257] | 108 | {
|
---|
[268] | 109 | draw(i);
|
---|
[257] | 110 | }
|
---|
| 111 | }
|
---|
[69] | 112 | }
|
---|
[126] | 113 |
|
---|
[267] | 114 | void nv::gui::environment::update()
|
---|
[126] | 115 | {
|
---|
[257] | 116 | update( m_screen, 0 );
|
---|
[126] | 117 | }
|
---|
| 118 |
|
---|
[267] | 119 | void nv::gui::environment::draw()
|
---|
[126] | 120 | {
|
---|
[257] | 121 | draw( m_screen );
|
---|
[126] | 122 | m_renderer->draw();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[269] | 125 | void nv::gui::environment::add_child( handle child )
|
---|
[126] | 126 | {
|
---|
[268] | 127 | add_child( m_screen, child );
|
---|
[126] | 128 | }
|
---|
| 129 |
|
---|
[269] | 130 | void nv::gui::environment::add_child( handle parent, handle child )
|
---|
[268] | 131 | {
|
---|
[271] | 132 | element* e = m_elements.get( child );
|
---|
| 133 | element* p = m_elements.get( parent );
|
---|
[269] | 134 | if ( e && p )
|
---|
[268] | 135 | {
|
---|
[269] | 136 | remove_child( e->m_parent, child );
|
---|
| 137 | e->m_parent = parent;
|
---|
| 138 | p->m_children.push_back( child );
|
---|
| 139 | p->m_child_count++;
|
---|
[268] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[269] | 143 | void nv::gui::environment::destroy_children( handle e )
|
---|
[264] | 144 | {
|
---|
[271] | 145 | element* parent = m_elements.get(e);
|
---|
[269] | 146 | if ( parent )
|
---|
[264] | 147 | {
|
---|
[269] | 148 | while ( !parent->m_children.empty() )
|
---|
| 149 | {
|
---|
| 150 | destroy_element( parent->m_children.front() );
|
---|
| 151 | }
|
---|
[264] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
[267] | 156 | nv::gui::environment::~environment()
|
---|
[126] | 157 | {
|
---|
[269] | 158 | destroy_children( handle() );
|
---|
[126] | 159 | delete m_renderer;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[267] | 162 | bool nv::gui::environment::process_io_event( const io_event& )
|
---|
| 163 | {
|
---|
| 164 | return false;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[269] | 167 | bool nv::gui::environment::process_io_event( handle e, const io_event& ev )
|
---|
[267] | 168 | {
|
---|
[271] | 169 | element* el = m_elements.get( e );
|
---|
[270] | 170 | return el && el->m_parent.is_valid() ? process_io_event( el->m_parent, ev ) : false;
|
---|
[267] | 171 | }
|
---|
| 172 |
|
---|
[269] | 173 | nv::gui::handle nv::gui::environment::get_element( const position& p )
|
---|
[267] | 174 | {
|
---|
| 175 | return get_deepest_child( m_screen, p );
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[269] | 178 | nv::gui::handle nv::gui::environment::get_deepest_child( handle e, const position& p )
|
---|
| 179 | {
|
---|
[271] | 180 | element* el = m_elements.get(e);
|
---|
[269] | 181 | if ( !el && !el->m_visible ) return handle();
|
---|
[267] | 182 |
|
---|
[269] | 183 | handle result;
|
---|
| 184 | element::list::reverse_iterator it = el->m_children.rbegin();
|
---|
| 185 |
|
---|
| 186 | while ( it != el->m_children.rend() )
|
---|
[267] | 187 | {
|
---|
[268] | 188 | result = get_deepest_child( *it, p );
|
---|
[270] | 189 | if ( result.is_valid() ) return result;
|
---|
[267] | 190 | ++it;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[269] | 193 | if ( el->m_absolute.contains(p) ) return e;
|
---|
| 194 | return handle();
|
---|
[267] | 195 | }
|
---|
| 196 |
|
---|
[269] | 197 | void nv::gui::environment::move_to_top( handle child )
|
---|
[267] | 198 | {
|
---|
[271] | 199 | element* e = m_elements.get( child );
|
---|
| 200 | element* parent = m_elements.get( e->m_parent );
|
---|
[269] | 201 | if ( e && parent )
|
---|
[267] | 202 | {
|
---|
[268] | 203 | auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child );
|
---|
[267] | 204 | if ( it != parent->m_children.end() )
|
---|
| 205 | {
|
---|
| 206 | parent->m_children.erase( it );
|
---|
| 207 | parent->m_children.push_back( child );
|
---|
| 208 | parent->m_dirty = true;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[269] | 213 | void nv::gui::environment::move_to_bottom( handle child )
|
---|
[267] | 214 | {
|
---|
[271] | 215 | element* e = m_elements.get( child );
|
---|
| 216 | element* parent = m_elements.get( e->m_parent );
|
---|
[269] | 217 | if ( e && parent )
|
---|
[267] | 218 | {
|
---|
[268] | 219 | auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child );
|
---|
[267] | 220 | if ( it != parent->m_children.end() )
|
---|
| 221 | {
|
---|
| 222 | parent->m_children.erase( it );
|
---|
| 223 | parent->m_children.push_front( child );
|
---|
| 224 | parent->m_dirty = true;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[269] | 229 | void nv::gui::environment::set_relative( handle e, const rectangle& r )
|
---|
[267] | 230 | {
|
---|
[271] | 231 | element* el = m_elements.get(e);
|
---|
[269] | 232 | if ( el )
|
---|
| 233 | {
|
---|
| 234 | el->m_dirty = true;
|
---|
| 235 | el->m_relative = r;
|
---|
| 236 | recalculate_absolute( e );
|
---|
| 237 | }
|
---|
[267] | 238 | }
|
---|
| 239 |
|
---|
[269] | 240 | void nv::gui::environment::set_relative( handle e, const position& p )
|
---|
[267] | 241 | {
|
---|
[271] | 242 | element* el = m_elements.get(e);
|
---|
[269] | 243 | if ( el )
|
---|
| 244 | {
|
---|
| 245 | set_relative( e, rectangle( p, p + el->m_relative.get_size() ) );
|
---|
| 246 | }
|
---|
[267] | 247 | }
|
---|
| 248 |
|
---|
[269] | 249 | void nv::gui::environment::recalculate_absolute( handle e )
|
---|
[267] | 250 | {
|
---|
[271] | 251 | element* el = m_elements.get(e);
|
---|
| 252 | rectangle pabsolute = m_elements.get( el->m_parent )->m_absolute;
|
---|
[269] | 253 | el->m_absolute = el->m_relative + pabsolute.ul;
|
---|
[267] | 254 |
|
---|
[269] | 255 | for ( handle o : el->m_children )
|
---|
[267] | 256 | {
|
---|
[268] | 257 | recalculate_absolute( o );
|
---|
[267] | 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[269] | 261 | void nv::gui::environment::set_class( handle e, const string& text )
|
---|
[268] | 262 | {
|
---|
[271] | 263 | element* ep = m_elements.get(e);
|
---|
[269] | 264 | if ( ep != nullptr )
|
---|
| 265 | {
|
---|
| 266 | ep->m_class = text;
|
---|
| 267 | ep->m_dirty = true;
|
---|
| 268 | }
|
---|
[268] | 269 | }
|
---|
| 270 |
|
---|
[269] | 271 | void nv::gui::environment::set_text( handle e, const string& text )
|
---|
[268] | 272 | {
|
---|
[271] | 273 | element* ep = m_elements.get(e);
|
---|
[269] | 274 | if ( ep != nullptr )
|
---|
| 275 | {
|
---|
| 276 | ep->m_text = text;
|
---|
| 277 | ep->m_dirty = true;
|
---|
| 278 | }
|
---|
[268] | 279 | }
|
---|
| 280 |
|
---|
[269] | 281 | void nv::gui::environment::remove_child( handle parent, handle child )
|
---|
[268] | 282 | {
|
---|
[271] | 283 | element* p = m_elements.get( parent );
|
---|
[269] | 284 | if ( p )
|
---|
[268] | 285 | {
|
---|
[269] | 286 | auto it = std::find( p->m_children.begin(), p->m_children.end(), child );
|
---|
| 287 | if ( it != p->m_children.end() )
|
---|
| 288 | {
|
---|
[271] | 289 | element* e = m_elements.get( *it );
|
---|
[269] | 290 | e->m_parent = handle();
|
---|
| 291 | p->m_children.erase(it);
|
---|
| 292 | }
|
---|
[268] | 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|