ClangFormat: apply to source, most of intern
[blender.git] / source / blender / blenkernel / BKE_collection.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 #ifndef __BKE_COLLECTION_H__
18 #define __BKE_COLLECTION_H__
19
20 /** \file
21  * \ingroup bke
22  */
23
24 #include "BLI_compiler_compat.h"
25 #include "BLI_ghash.h"
26 #include "BLI_iterator.h"
27 #include "DNA_listBase.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /* Structs */
34
35 struct BLI_Iterator;
36 struct Base;
37 struct Collection;
38 struct Depsgraph;
39 struct ID;
40 struct Main;
41 struct Object;
42 struct Scene;
43 struct ViewLayer;
44
45 typedef struct CollectionParent {
46   struct CollectionParent *next, *prev;
47   struct Collection *collection;
48 } CollectionParent;
49
50 /* Collections */
51
52 struct Collection *BKE_collection_add(struct Main *bmain,
53                                       struct Collection *parent,
54                                       const char *name);
55 void BKE_collection_free(struct Collection *collection);
56 bool BKE_collection_delete(struct Main *bmain, struct Collection *collection, bool hierarchy);
57
58 struct Collection *BKE_collection_copy(struct Main *bmain,
59                                        struct Collection *parent,
60                                        struct Collection *collection);
61 void BKE_collection_copy_data(struct Main *bmain,
62                               struct Collection *collection_dst,
63                               const struct Collection *collection_src,
64                               const int flag);
65 void BKE_collection_make_local(struct Main *bmain,
66                                struct Collection *collection,
67                                const bool lib_local);
68
69 struct Collection *BKE_collection_duplicate(struct Main *bmain,
70                                             struct Collection *parent,
71                                             struct Collection *collection,
72                                             const bool do_hierarchy,
73                                             const bool do_objects,
74                                             const bool do_obdata);
75 struct Collection *BKE_collection_copy_master(struct Main *bmain,
76                                               struct Collection *collection,
77                                               const int flag);
78
79 /* Master Collection for Scene */
80
81 struct Collection *BKE_collection_master(const struct Scene *scene);
82 struct Collection *BKE_collection_master_add(void);
83 struct Scene *BKE_collection_master_scene_search(const struct Main *bmain,
84                                                  const struct Collection *master_collection);
85
86 /* Collection Objects */
87
88 bool BKE_collection_has_object(struct Collection *collection, struct Object *ob);
89 bool BKE_collection_has_object_recursive(struct Collection *collection, struct Object *ob);
90 struct Collection *BKE_collection_object_find(struct Main *bmain,
91                                               struct Scene *scene,
92                                               struct Collection *collection,
93                                               struct Object *ob);
94 bool BKE_collection_is_empty(struct Collection *collection);
95
96 bool BKE_collection_object_add(struct Main *bmain,
97                                struct Collection *collection,
98                                struct Object *ob);
99 void BKE_collection_object_add_from(struct Main *bmain,
100                                     struct Scene *scene,
101                                     struct Object *ob_src,
102                                     struct Object *ob_dst);
103 bool BKE_collection_object_remove(struct Main *bmain,
104                                   struct Collection *collection,
105                                   struct Object *object,
106                                   const bool free_us);
107 void BKE_collection_object_move(struct Main *bmain,
108                                 struct Scene *scene,
109                                 struct Collection *collection_dst,
110                                 struct Collection *collection_src,
111                                 struct Object *ob);
112
113 bool BKE_scene_collections_object_remove(struct Main *bmain,
114                                          struct Scene *scene,
115                                          struct Object *object,
116                                          const bool free_us);
117 void BKE_collections_object_remove_nulls(struct Main *bmain);
118 void BKE_collections_child_remove_nulls(struct Main *bmain, struct Collection *old_collection);
119
120 /* Dependencies. */
121
122 bool BKE_collection_is_in_scene(struct Collection *collection);
123 void BKE_collections_after_lib_link(struct Main *bmain);
124 bool BKE_collection_object_cyclic_check(struct Main *bmain,
125                                         struct Object *object,
126                                         struct Collection *collection);
127
128 /* Object list cache. */
129
130 struct ListBase BKE_collection_object_cache_get(struct Collection *collection);
131 void BKE_collection_object_cache_free(struct Collection *collection);
132
133 struct Base *BKE_collection_or_layer_objects(const struct ViewLayer *view_layer,
134                                              struct Collection *collection);
135
136 /* Editing. */
137
138 struct Collection *BKE_collection_from_index(struct Scene *scene, const int index);
139 void BKE_collection_new_name_get(struct Collection *collection_parent, char *rname);
140 const char *BKE_collection_ui_name_get(struct Collection *collection);
141 bool BKE_collection_objects_select(struct ViewLayer *view_layer,
142                                    struct Collection *collection,
143                                    bool deselect);
144
145 /* Collection children */
146
147 bool BKE_collection_child_add(struct Main *bmain,
148                               struct Collection *parent,
149                               struct Collection *child);
150
151 bool BKE_collection_child_remove(struct Main *bmain,
152                                  struct Collection *parent,
153                                  struct Collection *child);
154
155 bool BKE_collection_move(struct Main *bmain,
156                          struct Collection *to_parent,
157                          struct Collection *from_parent,
158                          struct Collection *relative,
159                          bool relative_after,
160                          struct Collection *collection);
161
162 bool BKE_collection_find_cycle(struct Collection *new_ancestor, struct Collection *collection);
163
164 /* Iteration callbacks. */
165
166 typedef void (*BKE_scene_objects_Cb)(struct Object *ob, void *data);
167 typedef void (*BKE_scene_collections_Cb)(struct Collection *ob, void *data);
168
169 /* Iteratorion over objects in collection. */
170
171 #define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_BEGIN(_collection, _object, _mode) \
172   { \
173     int _base_flag = (_mode == DAG_EVAL_VIEWPORT) ? BASE_ENABLED_VIEWPORT : BASE_ENABLED_RENDER; \
174     int _base_id = 0; \
175     for (Base *_base = (Base *)BKE_collection_object_cache_get(_collection).first; _base; \
176          _base = _base->next, _base_id++) { \
177       if (_base->flag & _base_flag) { \
178         Object *_object = _base->object;
179
180 #define FOREACH_COLLECTION_VISIBLE_OBJECT_RECURSIVE_END \
181   } \
182   } \
183   }
184
185 #define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object) \
186   for (Base *_base = (Base *)BKE_collection_object_cache_get(_collection).first; _base; \
187        _base = _base->next) { \
188     Object *_object = _base->object; \
189     BLI_assert(_object != NULL);
190
191 #define FOREACH_COLLECTION_OBJECT_RECURSIVE_END \
192   } \
193   ((void)0)
194
195 /* Iteration over collections in scene. */
196
197 void BKE_scene_collections_iterator_begin(struct BLI_Iterator *iter, void *data_in);
198 void BKE_scene_collections_iterator_next(struct BLI_Iterator *iter);
199 void BKE_scene_collections_iterator_end(struct BLI_Iterator *iter);
200
201 void BKE_scene_objects_iterator_begin(struct BLI_Iterator *iter, void *data_in);
202 void BKE_scene_objects_iterator_next(struct BLI_Iterator *iter);
203 void BKE_scene_objects_iterator_end(struct BLI_Iterator *iter);
204
205 #define FOREACH_SCENE_COLLECTION_BEGIN(scene, _instance) \
206   ITER_BEGIN (BKE_scene_collections_iterator_begin, \
207               BKE_scene_collections_iterator_next, \
208               BKE_scene_collections_iterator_end, \
209               scene, \
210               Collection *, \
211               _instance)
212
213 #define FOREACH_SCENE_COLLECTION_END ITER_END
214
215 #define FOREACH_COLLECTION_BEGIN(_bmain, _scene, Type, _instance) \
216   { \
217     Type _instance; \
218     Collection *_instance_next; \
219     bool is_scene_collection = (_scene) != NULL; \
220 \
221     if (_scene) { \
222       _instance_next = BKE_collection_master(_scene); \
223     } \
224     else { \
225       _instance_next = (_bmain)->collections.first; \
226     } \
227 \
228     while ((_instance = _instance_next)) { \
229       if (is_scene_collection) { \
230         _instance_next = (_bmain)->collections.first; \
231         is_scene_collection = false; \
232       } \
233       else { \
234         _instance_next = _instance->id.next; \
235       }
236
237 #define FOREACH_COLLECTION_END \
238   } \
239   } \
240   ((void)0)
241
242 #define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance) \
243   ITER_BEGIN (BKE_scene_objects_iterator_begin, \
244               BKE_scene_objects_iterator_next, \
245               BKE_scene_objects_iterator_end, \
246               scene, \
247               Object *, \
248               _instance)
249
250 #define FOREACH_SCENE_OBJECT_END ITER_END
251
252 #ifdef __cplusplus
253 }
254 #endif
255
256 #endif /* __BKE_COLLECTION_H__ */