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_element.hh"
|
---|
8 |
|
---|
9 | using namespace nv;
|
---|
10 | using namespace nv::gui;
|
---|
11 |
|
---|
12 | element::element( const rectangle& r )
|
---|
13 | : object( "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr )
|
---|
14 | {
|
---|
15 |
|
---|
16 | }
|
---|
17 |
|
---|
18 | bool element::on_event( const io_event& event )
|
---|
19 | {
|
---|
20 | return m_parent ? ((element*)m_parent)->on_event( event ) : false;
|
---|
21 | }
|
---|
22 |
|
---|
23 | element* element::get_element( const position& p )
|
---|
24 | {
|
---|
25 | if ( !is_visible() ) return nullptr;
|
---|
26 |
|
---|
27 | element* result = nullptr;
|
---|
28 | list::reverse_iterator it = m_children.rbegin();
|
---|
29 |
|
---|
30 | while ( it != m_children.rend() )
|
---|
31 | {
|
---|
32 | result = ((element*)(*it))->get_element( p );
|
---|
33 | if ( result ) return result;
|
---|
34 | ++it;
|
---|
35 | }
|
---|
36 |
|
---|
37 | if ( contains( p ) ) return this;
|
---|
38 | return nullptr;
|
---|
39 | }
|
---|
40 |
|
---|
41 | bool element::contains( const position& p ) const
|
---|
42 | {
|
---|
43 | return m_absolute.contains( p );
|
---|
44 | }
|
---|
45 |
|
---|
46 | void element::set_relative( const rectangle& r )
|
---|
47 | {
|
---|
48 | m_dirty = true;
|
---|
49 | m_relative = r;
|
---|
50 | recalculate_absolute();
|
---|
51 | }
|
---|
52 |
|
---|
53 | void element::set_relative( const position& p )
|
---|
54 | {
|
---|
55 | set_relative( rectangle( p, p + m_relative.get_size() ) );
|
---|
56 | }
|
---|
57 |
|
---|
58 | void element::recalculate_absolute()
|
---|
59 | {
|
---|
60 | rectangle pabsolute;
|
---|
61 |
|
---|
62 | if ( m_parent )
|
---|
63 | {
|
---|
64 | pabsolute = ((element*)(m_parent))->m_absolute;
|
---|
65 | }
|
---|
66 |
|
---|
67 | m_absolute = m_relative + pabsolute.ul;
|
---|
68 |
|
---|
69 | for ( object* o : *this )
|
---|
70 | {
|
---|
71 | ((element*)o)->recalculate_absolute();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | void element::recalculate_absolute_children()
|
---|
76 | {
|
---|
77 | for ( object* o : *this )
|
---|
78 | {
|
---|
79 | ((element*)o)->recalculate_absolute();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | bool element::move_to_top( element* child )
|
---|
85 | {
|
---|
86 | list::iterator it = std::find( m_children.begin(), m_children.end(), (object*)child );
|
---|
87 | if ( it != m_children.end() )
|
---|
88 | {
|
---|
89 | m_children.erase( it );
|
---|
90 | m_children.push_back( child );
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool element::move_to_bottom( element* child )
|
---|
97 | {
|
---|
98 | list::iterator it = std::find( m_children.begin(), m_children.end(), (object*)child );
|
---|
99 | if ( it != m_children.end() )
|
---|
100 | {
|
---|
101 | m_children.erase( it );
|
---|
102 | m_children.push_front( child );
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 |
|
---|
108 | element::~element()
|
---|
109 | {
|
---|
110 | delete m_render_data;
|
---|
111 | }
|
---|