Blender  V2.93
outliner_utils.c
Go to the documentation of this file.
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  * The Original Code is Copyright (C) 2017 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <string.h>
25 
26 #include "BLI_listbase.h"
27 #include "BLI_utildefines.h"
28 
29 #include "DNA_action_types.h"
30 #include "DNA_screen_types.h"
31 #include "DNA_space_types.h"
32 
33 #include "BKE_armature.h"
34 #include "BKE_context.h"
35 #include "BKE_layer.h"
36 #include "BKE_object.h"
37 #include "BKE_outliner_treehash.h"
38 
39 #include "ED_outliner.h"
40 #include "ED_screen.h"
41 
42 #include "UI_interface.h"
43 #include "UI_view2d.h"
44 
45 #include "outliner_intern.h"
46 
47 /* -------------------------------------------------------------------- */
52 {
53  memset(tvc, 0, sizeof(*tvc));
54 
55  /* Scene level. */
56  tvc->scene = CTX_data_scene(C);
58 
59  /* Objects. */
60  tvc->obact = OBACT(tvc->view_layer);
61  if (tvc->obact != NULL) {
62  tvc->ob_edit = OBEDIT_FROM_OBACT(tvc->obact);
63 
64  if ((tvc->obact->type == OB_ARMATURE) ||
65  /* This could be made into its own function. */
66  ((tvc->obact->type == OB_MESH) && tvc->obact->mode & OB_MODE_WEIGHT_PAINT)) {
68  }
69  }
70 }
71 
79  const ListBase *tree,
80  float view_co_y)
81 {
82  LISTBASE_FOREACH (TreeElement *, te_iter, tree) {
83  if (view_co_y < (te_iter->ys + UI_UNIT_Y)) {
84  if (view_co_y >= te_iter->ys) {
85  /* co_y is inside this element */
86  return te_iter;
87  }
88 
89  if (BLI_listbase_is_empty(&te_iter->subtree) ||
90  !TSELEM_OPEN(TREESTORE(te_iter), space_outliner)) {
91  /* No need for recursion. */
92  continue;
93  }
94 
95  /* If the coordinate is lower than the next element, we can continue with that one and skip
96  * recursion too. */
97  const TreeElement *te_next = te_iter->next;
98  if (te_next && (view_co_y < (te_next->ys + UI_UNIT_Y))) {
99  continue;
100  }
101 
102  /* co_y is lower than current element (but not lower than the next one), possibly inside
103  * children */
104  TreeElement *te_sub = outliner_find_item_at_y(space_outliner, &te_iter->subtree, view_co_y);
105  if (te_sub) {
106  return te_sub;
107  }
108  }
109  }
110 
111  return NULL;
112 }
113 
115  float view_co_x,
116  bool *r_is_merged_icon)
117 {
118  TreeElement *child_te = parent_te->subtree.first;
119 
120  bool over_element = false;
121 
122  while (child_te) {
123  over_element = (view_co_x > child_te->xs) && (view_co_x < child_te->xend);
124  if ((child_te->flag & TE_ICONROW) && over_element) {
125  return child_te;
126  }
127  if ((child_te->flag & TE_ICONROW_MERGED) && over_element) {
128  if (r_is_merged_icon) {
129  *r_is_merged_icon = true;
130  }
131  return child_te;
132  }
133 
135  child_te, view_co_x, r_is_merged_icon);
136  if (te != child_te) {
137  return te;
138  }
139 
140  child_te = child_te->next;
141  }
142 
143  /* return parent if no child is hovered */
144  return (TreeElement *)parent_te;
145 }
146 
154  TreeElement *parent_te,
155  float view_co_x,
156  bool *r_is_merged_icon,
157  bool *r_is_over_icon)
158 {
159  /* if parent_te is opened, it doesn't show children in row */
160  TreeElement *te = parent_te;
161  if (!TSELEM_OPEN(TREESTORE(parent_te), space_outliner)) {
162  te = outliner_find_item_at_x_in_row_recursive(parent_te, view_co_x, r_is_merged_icon);
163  }
164 
165  if ((te != parent_te) || outliner_item_is_co_over_icon(parent_te, view_co_x)) {
166  *r_is_over_icon = true;
167  }
168 
169  return te;
170 }
171 
172 /* Find specific item from the trees-tore. */
174 {
175  LISTBASE_FOREACH (TreeElement *, te, lb) {
176  if (te->store_elem == store_elem) {
177  return te;
178  }
179  TreeElement *tes = outliner_find_tree_element(&te->subtree, store_elem);
180  if (tes) {
181  return tes;
182  }
183  }
184  return NULL;
185 }
186 
187 /* Find parent element of te */
189  TreeElement *parent_te,
190  const TreeElement *child_te)
191 {
192  LISTBASE_FOREACH (TreeElement *, te, lb) {
193  if (te == child_te) {
194  return parent_te;
195  }
196 
197  TreeElement *find_te = outliner_find_parent_element(&te->subtree, te, child_te);
198  if (find_te) {
199  return find_te;
200  }
201  }
202  return NULL;
203 }
204 
205 /* tse is not in the treestore, we use its contents to find a match */
207 {
208  TreeStoreElem *tselem;
209 
210  if (tse->id == NULL) {
211  return NULL;
212  }
213 
214  /* check if 'tse' is in treestore */
216  space_outliner->runtime->treehash, tse->type, tse->nr, tse->id);
217  if (tselem) {
218  return outliner_find_tree_element(&space_outliner->tree, tselem);
219  }
220 
221  return NULL;
222 }
223 
224 /* Find treestore that refers to given ID */
225 TreeElement *outliner_find_id(SpaceOutliner *space_outliner, ListBase *lb, const ID *id)
226 {
227  LISTBASE_FOREACH (TreeElement *, te, lb) {
228  TreeStoreElem *tselem = TREESTORE(te);
229  if (tselem->type == TSE_SOME_ID) {
230  if (tselem->id == id) {
231  return te;
232  }
233  }
234 
235  TreeElement *tes = outliner_find_id(space_outliner, &te->subtree, id);
236  if (tes) {
237  return tes;
238  }
239  }
240  return NULL;
241 }
242 
244 {
245  LISTBASE_FOREACH (TreeElement *, te, lb) {
246  if (te->directdata == pchan) {
247  return te;
248  }
249 
250  TreeStoreElem *tselem = TREESTORE(te);
251  if (ELEM(tselem->type, TSE_POSE_BASE, TSE_POSE_CHANNEL)) {
252  TreeElement *tes = outliner_find_posechannel(&te->subtree, pchan);
253  if (tes) {
254  return tes;
255  }
256  }
257  }
258  return NULL;
259 }
260 
262 {
263  LISTBASE_FOREACH (TreeElement *, te, lb) {
264  if (te->directdata == ebone) {
265  return te;
266  }
267 
268  TreeStoreElem *tselem = TREESTORE(te);
269  if (ELEM(tselem->type, TSE_SOME_ID, TSE_EBONE)) {
270  TreeElement *tes = outliner_find_editbone(&te->subtree, ebone);
271  if (tes) {
272  return tes;
273  }
274  }
275  }
276  return NULL;
277 }
278 
280 {
281  TreeStoreElem *tselem;
282  te = te->parent;
283 
284  while (te) {
285  tselem = TREESTORE(te);
286  if ((tselem->type == TSE_SOME_ID) && (te->idcode == idcode)) {
287  return te;
288  }
289  te = te->parent;
290  }
291  return NULL;
292 }
293 
295 {
296  TreeElement *search_te;
297  TreeStoreElem *tselem;
298 
299  search_te = outliner_search_back_te(te, idcode);
300  if (search_te) {
301  tselem = TREESTORE(search_te);
302  return tselem->id;
303  }
304  return NULL;
305 }
306 
315 bool outliner_tree_traverse(const SpaceOutliner *space_outliner,
316  ListBase *tree,
317  int filter_te_flag,
318  int filter_tselem_flag,
319  TreeTraversalFunc func,
320  void *customdata)
321 {
322  for (TreeElement *te = tree->first, *te_next; te; te = te_next) {
324  /* in case te is freed in callback */
325  TreeStoreElem *tselem = TREESTORE(te);
326  ListBase subtree = te->subtree;
327  te_next = te->next;
328 
329  if (filter_te_flag && (te->flag & filter_te_flag) == 0) {
330  /* skip */
331  }
332  else if (filter_tselem_flag && (tselem->flag & filter_tselem_flag) == 0) {
333  /* skip */
334  }
335  else {
336  func_retval = func(te, customdata);
337  }
338  /* Don't access te or tselem from now on! Might've been freed... */
339 
340  if (func_retval == TRAVERSE_BREAK) {
341  return false;
342  }
343 
344  if (func_retval == TRAVERSE_SKIP_CHILDS) {
345  /* skip */
346  }
347  else if (!outliner_tree_traverse(
348  space_outliner, &subtree, filter_te_flag, filter_tselem_flag, func, customdata)) {
349  return false;
350  }
351  }
352 
353  return true;
354 }
355 
356 float outliner_restrict_columns_width(const SpaceOutliner *space_outliner)
357 {
358  int num_columns = 0;
359 
360  switch (space_outliner->outlinevis) {
361  case SO_DATA_API:
362  case SO_SEQUENCE:
363  case SO_LIBRARIES:
365  return 0.0f;
366  case SO_ID_ORPHANS:
367  num_columns = 3;
368  break;
369  case SO_VIEW_LAYER:
370  if (space_outliner->show_restrict_flags & SO_RESTRICT_ENABLE) {
371  num_columns++;
372  }
373  if (space_outliner->show_restrict_flags & SO_RESTRICT_HOLDOUT) {
374  num_columns++;
375  }
376  if (space_outliner->show_restrict_flags & SO_RESTRICT_INDIRECT_ONLY) {
377  num_columns++;
378  }
380  case SO_SCENES:
381  if (space_outliner->show_restrict_flags & SO_RESTRICT_SELECT) {
382  num_columns++;
383  }
384  if (space_outliner->show_restrict_flags & SO_RESTRICT_HIDE) {
385  num_columns++;
386  }
387  if (space_outliner->show_restrict_flags & SO_RESTRICT_VIEWPORT) {
388  num_columns++;
389  }
390  if (space_outliner->show_restrict_flags & SO_RESTRICT_RENDER) {
391  num_columns++;
392  }
393  break;
394  }
395  return (num_columns * UI_UNIT_X + V2D_SCROLL_WIDTH);
396 }
397 
398 /* Find first tree element in tree with matching treestore flag */
400 {
401  LISTBASE_FOREACH (TreeElement *, te, lb) {
402  if ((TREESTORE(te)->flag & flag) == flag) {
403  return te;
404  }
405  TreeElement *active_element = outliner_find_element_with_flag(&te->subtree, flag);
406  if (active_element) {
407  return active_element;
408  }
409  }
410  return NULL;
411 }
412 
413 /* Find if element is visible in the outliner tree */
415 {
416  TreeStoreElem *tselem;
417 
418  while (te->parent) {
419  tselem = TREESTORE(te->parent);
420 
421  if (tselem->flag & TSE_CLOSED) {
422  return false;
423  }
424  te = te->parent;
425  }
426 
427  return true;
428 }
429 
430 /* Find if x coordinate is over an icon or name */
431 bool outliner_item_is_co_over_name_icons(const TreeElement *te, float view_co_x)
432 {
433  /* Special case: count area left of Scene Collection as empty space */
434  bool outside_left = (TREESTORE(te)->type == TSE_VIEW_COLLECTION_BASE) ?
435  (view_co_x > te->xs + UI_UNIT_X) :
436  (view_co_x > te->xs);
437 
438  return outside_left && (view_co_x < te->xend);
439 }
440 
441 bool outliner_item_is_co_over_icon(const TreeElement *te, float view_co_x)
442 {
443  return (view_co_x > (te->xs + UI_UNIT_X)) && (view_co_x < (te->xs + UI_UNIT_X * 2));
444 }
445 
446 /* Find if x coordinate is over element name. */
447 bool outliner_item_is_co_over_name(const TreeElement *te, float view_co_x)
448 {
449  return (view_co_x > (te->xs + UI_UNIT_X * 2)) && (view_co_x < te->xend);
450 }
451 
452 /* Find if x coordinate is over element disclosure toggle */
453 bool outliner_item_is_co_within_close_toggle(const TreeElement *te, float view_co_x)
454 {
455  return (view_co_x > te->xs) && (view_co_x < te->xs + UI_UNIT_X);
456 }
457 
458 /* Scroll view vertically while keeping within total bounds */
459 void outliner_scroll_view(SpaceOutliner *space_outliner, ARegion *region, int delta_y)
460 {
461  int tree_width, tree_height;
462  outliner_tree_dimensions(space_outliner, &tree_width, &tree_height);
463  int y_min = MIN2(region->v2d.cur.ymin, -tree_height);
464 
465  region->v2d.cur.ymax += delta_y;
466  region->v2d.cur.ymin += delta_y;
467 
468  /* Adjust view if delta placed view outside total area */
469  int offset;
470  if (region->v2d.cur.ymax > -UI_UNIT_Y) {
471  offset = region->v2d.cur.ymax;
472  region->v2d.cur.ymax -= offset;
473  region->v2d.cur.ymin -= offset;
474  }
475  else if (region->v2d.cur.ymin < y_min) {
476  offset = y_min - region->v2d.cur.ymin;
477  region->v2d.cur.ymax += offset;
478  region->v2d.cur.ymin += offset;
479  }
480 }
481 
488  ARegion *region)
489 {
490  /* Avoid rebuild if possible. */
491  if (outliner_requires_rebuild_on_open_change(space_outliner)) {
492  ED_region_tag_redraw(region);
493  }
494  else {
496  }
497 }
498 
499 /* Get base of object under cursor. Used for eyedropper tool */
501 {
502  ARegion *region = CTX_wm_region(C);
503  ViewLayer *view_layer = CTX_data_view_layer(C);
504  SpaceOutliner *space_outliner = CTX_wm_space_outliner(C);
505  TreeElement *te;
506  Base *base = NULL;
507  float view_mval[2];
508 
509  UI_view2d_region_to_view(&region->v2d, mval[0], mval[1], &view_mval[0], &view_mval[1]);
510 
511  te = outliner_find_item_at_y(space_outliner, &space_outliner->tree, view_mval[1]);
512  if (te) {
513  TreeStoreElem *tselem = TREESTORE(te);
514  if ((tselem->type == TSE_SOME_ID) && (te->idcode == ID_OB)) {
515  Object *ob = (Object *)tselem->id;
516  base = (te->directdata) ? (Base *)te->directdata : BKE_view_layer_base_find(view_layer, ob);
517  }
518  }
519 
520  return base;
521 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct SpaceOutliner * CTX_wm_space_outliner(const bContext *C)
Definition: context.c:836
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
struct Base * BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob)
Definition: layer.c:394
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_pose_armature_get(struct Object *ob)
Definition: object.c:2487
struct TreeStoreElem * BKE_outliner_treehash_lookup_any(void *treehash, short type, short nr, struct ID *id)
#define ATTR_FALLTHROUGH
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#define ELEM(...)
#define MIN2(a, b)
@ ID_OB
Definition: DNA_ID_enums.h:59
@ OB_MODE_WEIGHT_PAINT
@ OB_ARMATURE
@ OB_MESH
@ TSE_POSE_CHANNEL
@ TSE_VIEW_COLLECTION_BASE
@ TSE_EBONE
@ TSE_SOME_ID
@ TSE_POSE_BASE
@ TSE_CLOSED
#define OBEDIT_FROM_OBACT(ob)
#define OBACT(_view_layer)
@ SO_RESTRICT_HIDE
@ SO_RESTRICT_RENDER
@ SO_RESTRICT_INDIRECT_ONLY
@ SO_RESTRICT_VIEWPORT
@ SO_RESTRICT_ENABLE
@ SO_RESTRICT_HOLDOUT
@ SO_RESTRICT_SELECT
@ SO_OVERRIDES_LIBRARY
@ SO_SEQUENCE
@ SO_DATA_API
@ SO_LIBRARIES
@ SO_VIEW_LAYER
@ SO_SCENES
@ SO_ID_ORPHANS
void ED_region_tag_redraw_no_rebuild(struct ARegion *region)
Definition: area.c:686
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
#define C
Definition: RandGen.cpp:39
#define UI_UNIT_Y
#define UI_UNIT_X
#define V2D_SCROLL_WIDTH
Definition: UI_view2d.h:68
void UI_view2d_region_to_view(const struct View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
void * tree
void outliner_tree_dimensions(SpaceOutliner *space_outliner, int *r_width, int *r_height)
@ TE_ICONROW_MERGED
@ TE_ICONROW
bool outliner_requires_rebuild_on_open_change(const struct SpaceOutliner *space_outliner)
#define TREESTORE(a)
TreeTraversalAction(* TreeTraversalFunc)(struct TreeElement *te, void *customdata)
#define TSELEM_OPEN(telm, sv)
TreeTraversalAction
@ TRAVERSE_SKIP_CHILDS
@ TRAVERSE_BREAK
@ TRAVERSE_CONTINUE
void outliner_scroll_view(SpaceOutliner *space_outliner, ARegion *region, int delta_y)
bool outliner_tree_traverse(const SpaceOutliner *space_outliner, ListBase *tree, int filter_te_flag, int filter_tselem_flag, TreeTraversalFunc func, void *customdata)
TreeElement * outliner_find_id(SpaceOutliner *space_outliner, ListBase *lb, const ID *id)
ID * outliner_search_back(TreeElement *te, short idcode)
TreeElement * outliner_find_element_with_flag(const ListBase *lb, short flag)
bool outliner_item_is_co_within_close_toggle(const TreeElement *te, float view_co_x)
bool outliner_item_is_co_over_name(const TreeElement *te, float view_co_x)
TreeElement * outliner_find_tree_element(ListBase *lb, const TreeStoreElem *store_elem)
TreeElement * outliner_find_editbone(ListBase *lb, const EditBone *ebone)
bool outliner_item_is_co_over_name_icons(const TreeElement *te, float view_co_x)
TreeElement * outliner_search_back_te(TreeElement *te, short idcode)
TreeElement * outliner_find_item_at_y(const SpaceOutliner *space_outliner, const ListBase *tree, float view_co_y)
Base * ED_outliner_give_base_under_cursor(bContext *C, const int mval[2])
bool outliner_is_element_visible(const TreeElement *te)
void outliner_viewcontext_init(const bContext *C, TreeViewContext *tvc)
float outliner_restrict_columns_width(const SpaceOutliner *space_outliner)
bool outliner_item_is_co_over_icon(const TreeElement *te, float view_co_x)
void outliner_tag_redraw_avoid_rebuild_on_open_change(const SpaceOutliner *space_outliner, ARegion *region)
TreeElement * outliner_find_parent_element(ListBase *lb, TreeElement *parent_te, const TreeElement *child_te)
TreeElement * outliner_find_tse(SpaceOutliner *space_outliner, const TreeStoreElem *tse)
TreeElement * outliner_find_posechannel(ListBase *lb, const bPoseChannel *pchan)
static TreeElement * outliner_find_item_at_x_in_row_recursive(const TreeElement *parent_te, float view_co_x, bool *r_is_merged_icon)
TreeElement * outliner_find_item_at_x_in_row(const SpaceOutliner *space_outliner, TreeElement *parent_te, float view_co_x, bool *r_is_merged_icon, bool *r_is_over_icon)
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
struct GHash * treehash
SpaceOutliner_Runtime * runtime
struct TreeElement * parent
ListBase subtree
void * directdata
struct TreeElement * next
struct Scene * scene
struct ViewLayer * view_layer
float ymax
Definition: DNA_vec_types.h:86
float ymin
Definition: DNA_vec_types.h:86