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/object.hh"
|
---|
8 |
|
---|
9 | #include <algorithm>
|
---|
10 | #include "nv/types.hh"
|
---|
11 | #include "nv/lua/lua_state.hh"
|
---|
12 | #include "nv/uid.hh"
|
---|
13 |
|
---|
14 | using namespace nv;
|
---|
15 |
|
---|
16 | object::object( const string& aid )
|
---|
17 | : m_id( aid )
|
---|
18 | , m_name()
|
---|
19 | , m_uid(0)
|
---|
20 | , m_lua_index(lua::ref_none)
|
---|
21 | , m_lua_proto_index(lua::ref_none)
|
---|
22 | , m_parent( nullptr )
|
---|
23 | , m_children()
|
---|
24 | , m_child_count(0)
|
---|
25 | {
|
---|
26 | }
|
---|
27 |
|
---|
28 | void object::add_child( object* child )
|
---|
29 | {
|
---|
30 | if (child)
|
---|
31 | {
|
---|
32 | if (child->m_parent)
|
---|
33 | {
|
---|
34 | child->detach();
|
---|
35 | }
|
---|
36 | child->m_parent = this;
|
---|
37 | m_children.push_back( child );
|
---|
38 | m_child_count++;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | void object::remove_child( object* child )
|
---|
43 | {
|
---|
44 | if ( child->m_parent != this )
|
---|
45 | {
|
---|
46 | return; // signal error?
|
---|
47 | }
|
---|
48 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
49 | if ( it != m_children.end() )
|
---|
50 | {
|
---|
51 | (*it)->m_parent = nullptr;
|
---|
52 | m_children.erase(it);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | void object::detach()
|
---|
57 | {
|
---|
58 | if (m_parent)
|
---|
59 | {
|
---|
60 | m_parent->remove_child( this );
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | void object::change_parent( object* new_parent )
|
---|
65 | {
|
---|
66 | if (m_parent) detach();
|
---|
67 | if (new_parent) new_parent->add_child( this );
|
---|
68 | }
|
---|
69 |
|
---|
70 | object::~object()
|
---|
71 | {
|
---|
72 | }
|
---|
73 |
|
---|
74 | object* object::find( object* child, bool recursive /*= false */ )
|
---|
75 | {
|
---|
76 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
77 | if ( it != m_children.end() )
|
---|
78 | {
|
---|
79 | return *it;
|
---|
80 | }
|
---|
81 | if ( recursive )
|
---|
82 | {
|
---|
83 | for ( object* i : m_children )
|
---|
84 | {
|
---|
85 | object* r = i->find( child, recursive );
|
---|
86 | if (r) return r;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return nullptr;
|
---|
90 | }
|
---|
91 |
|
---|
92 | object* object::find( uid child, bool recursive /*= false */ )
|
---|
93 | {
|
---|
94 | for ( object* i : m_children )
|
---|
95 | {
|
---|
96 | if (i->m_uid == child) return i;
|
---|
97 | }
|
---|
98 | if ( recursive )
|
---|
99 | {
|
---|
100 | for ( object* i : m_children )
|
---|
101 | {
|
---|
102 | object* r = i->find( child, recursive );
|
---|
103 | if (r) return r;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return nullptr;
|
---|
107 | }
|
---|
108 |
|
---|
109 | object* object::find( string child, bool recursive /*= false */ )
|
---|
110 | {
|
---|
111 | for ( object* i : m_children )
|
---|
112 | {
|
---|
113 | if (i->m_id == child) return i;
|
---|
114 | }
|
---|
115 | if ( recursive )
|
---|
116 | {
|
---|
117 | for ( object* i : m_children )
|
---|
118 | {
|
---|
119 | object* r = i->find( child, recursive );
|
---|
120 | if (r) return r;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return nullptr;
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool object::move_to_top( object* child )
|
---|
127 | {
|
---|
128 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
129 | if ( it != m_children.end() )
|
---|
130 | {
|
---|
131 | m_children.erase( it );
|
---|
132 | m_children.push_back( child );
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool object::move_to_bottom( object* child )
|
---|
139 | {
|
---|
140 | list::iterator it = std::find( m_children.begin(), m_children.end(), child );
|
---|
141 | if ( it != m_children.end() )
|
---|
142 | {
|
---|
143 | m_children.erase( it );
|
---|
144 | m_children.push_front( child );
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // void object::register_type( type_database* db )
|
---|
151 | // {
|
---|
152 | // type_field fields[] = {
|
---|
153 | // type_field("id", &object::m_id),
|
---|
154 | // type_field("uid", &object::m_uid).flag( TF_READONLY ),
|
---|
155 | // type_field("lua_index", &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
156 | // type_field("parent", &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
|
---|
157 | // type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
|
---|
158 | // type_field("children" , &object::m_children).flag( TF_READONLY ),
|
---|
159 | // };
|
---|
160 | // db->create_type<object>("object").fields(fields);
|
---|
161 | // }
|
---|