Blender  V3.3
outliner_utils.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2017 Blender Foundation. All rights reserved. */
3 
8 #include <cstring>
9 
10 #include "BLI_listbase.h"
11 #include "BLI_utildefines.h"
12 
13 #include "DNA_action_types.h"
14 #include "DNA_screen_types.h"
15 #include "DNA_space_types.h"
16 
17 #include "BKE_armature.h"
18 #include "BKE_context.h"
19 #include "BKE_layer.h"
20 #include "BKE_object.h"
21 #include "BKE_outliner_treehash.h"
22 
23 #include "ED_outliner.h"
24 #include "ED_screen.h"
25 
26 #include "UI_interface.h"
27 #include "UI_view2d.h"
28 
29 #include "outliner_intern.hh"
30 #include "tree/tree_display.hh"
31 #include "tree/tree_iterator.hh"
32 
33 using namespace blender::ed::outliner;
34 
35 /* -------------------------------------------------------------------- */
40 {
41  memset(tvc, 0, sizeof(*tvc));
42 
43  /* Scene level. */
44  tvc->scene = CTX_data_scene(C);
46 
47  /* Objects. */
48  tvc->obact = OBACT(tvc->view_layer);
49  if (tvc->obact != nullptr) {
50  tvc->ob_edit = OBEDIT_FROM_OBACT(tvc->obact);
51 
52  if ((tvc->obact->type == OB_ARMATURE) ||
53  /* This could be made into its own function. */
54  ((tvc->obact->type == OB_MESH) && tvc->obact->mode & OB_MODE_WEIGHT_PAINT)) {
56  }
57  }
58 }
59 
63  const ListBase *tree,
64  float view_co_y)
65 {
66  LISTBASE_FOREACH (TreeElement *, te_iter, tree) {
67  if (view_co_y < (te_iter->ys + UI_UNIT_Y)) {
68  if (view_co_y >= te_iter->ys) {
69  /* co_y is inside this element */
70  return te_iter;
71  }
72 
73  if (BLI_listbase_is_empty(&te_iter->subtree) ||
74  !TSELEM_OPEN(TREESTORE(te_iter), space_outliner)) {
75  /* No need for recursion. */
76  continue;
77  }
78 
79  /* If the coordinate is lower than the next element, we can continue with that one and skip
80  * recursion too. */
81  const TreeElement *te_next = te_iter->next;
82  if (te_next && (view_co_y < (te_next->ys + UI_UNIT_Y))) {
83  continue;
84  }
85 
86  /* co_y is lower than current element (but not lower than the next one), possibly inside
87  * children */
88  TreeElement *te_sub = outliner_find_item_at_y(space_outliner, &te_iter->subtree, view_co_y);
89  if (te_sub) {
90  return te_sub;
91  }
92  }
93  }
94 
95  return nullptr;
96 }
97 
99  float view_co_x,
100  bool *r_is_merged_icon)
101 {
102  TreeElement *child_te = reinterpret_cast<TreeElement *>(parent_te->subtree.first);
103 
104  while (child_te) {
105  const bool over_element = (view_co_x > child_te->xs) && (view_co_x < child_te->xend);
106  if ((child_te->flag & TE_ICONROW) && over_element) {
107  return child_te;
108  }
109  if ((child_te->flag & TE_ICONROW_MERGED) && over_element) {
110  if (r_is_merged_icon) {
111  *r_is_merged_icon = true;
112  }
113  return child_te;
114  }
115 
117  child_te, view_co_x, r_is_merged_icon);
118  if (te != child_te) {
119  return te;
120  }
121 
122  child_te = child_te->next;
123  }
124 
125  /* return parent if no child is hovered */
126  return (TreeElement *)parent_te;
127 }
128 
130  TreeElement *parent_te,
131  float view_co_x,
132  bool *r_is_merged_icon,
133  bool *r_is_over_icon)
134 {
135  TreeStoreElem *parent_tselem = TREESTORE(parent_te);
136  TreeElement *te = parent_te;
137 
138  /* If parent_te is opened, or it is a ViewLayer, it doesn't show children in row. */
139  if (!TSELEM_OPEN(parent_tselem, space_outliner) && parent_tselem->type != TSE_R_LAYER) {
140  te = outliner_find_item_at_x_in_row_recursive(parent_te, view_co_x, r_is_merged_icon);
141  }
142 
143  if ((te != parent_te) || outliner_item_is_co_over_icon(parent_te, view_co_x)) {
144  *r_is_over_icon = true;
145  }
146 
147  return te;
148 }
149 
151 {
152  LISTBASE_FOREACH (TreeElement *, te, lb) {
153  if (te->store_elem == store_elem) {
154  return te;
155  }
156  TreeElement *tes = outliner_find_tree_element(&te->subtree, store_elem);
157  if (tes) {
158  return tes;
159  }
160  }
161  return nullptr;
162 }
163 
165  TreeElement *parent_te,
166  const TreeElement *child_te)
167 {
168  LISTBASE_FOREACH (TreeElement *, te, lb) {
169  if (te == child_te) {
170  return parent_te;
171  }
172 
173  TreeElement *find_te = outliner_find_parent_element(&te->subtree, te, child_te);
174  if (find_te) {
175  return find_te;
176  }
177  }
178  return nullptr;
179 }
180 
182 {
183  TreeStoreElem *tselem;
184 
185  if (tse->id == nullptr) {
186  return nullptr;
187  }
188 
189  /* Check if 'tse' is in tree-store. */
191  space_outliner->runtime->treehash, tse->type, tse->nr, tse->id);
192  if (tselem) {
193  return outliner_find_tree_element(&space_outliner->tree, tselem);
194  }
195 
196  return nullptr;
197 }
198 
199 TreeElement *outliner_find_id(SpaceOutliner *space_outliner, ListBase *lb, const ID *id)
200 {
201  LISTBASE_FOREACH (TreeElement *, te, lb) {
202  TreeStoreElem *tselem = TREESTORE(te);
203  if (tselem->type == TSE_SOME_ID) {
204  if (tselem->id == id) {
205  return te;
206  }
207  }
208 
209  TreeElement *tes = outliner_find_id(space_outliner, &te->subtree, id);
210  if (tes) {
211  return tes;
212  }
213  }
214  return nullptr;
215 }
216 
218 {
219  LISTBASE_FOREACH (TreeElement *, te, lb) {
220  if (te->directdata == pchan) {
221  return te;
222  }
223 
224  TreeStoreElem *tselem = TREESTORE(te);
225  if (ELEM(tselem->type, TSE_POSE_BASE, TSE_POSE_CHANNEL)) {
226  TreeElement *tes = outliner_find_posechannel(&te->subtree, pchan);
227  if (tes) {
228  return tes;
229  }
230  }
231  }
232  return nullptr;
233 }
234 
236 {
237  LISTBASE_FOREACH (TreeElement *, te, lb) {
238  if (te->directdata == ebone) {
239  return te;
240  }
241 
242  TreeStoreElem *tselem = TREESTORE(te);
243  if (ELEM(tselem->type, TSE_SOME_ID, TSE_EBONE)) {
244  TreeElement *tes = outliner_find_editbone(&te->subtree, ebone);
245  if (tes) {
246  return tes;
247  }
248  }
249  }
250  return nullptr;
251 }
252 
254 {
255  TreeStoreElem *tselem;
256  te = te->parent;
257 
258  while (te) {
259  tselem = TREESTORE(te);
260  if ((tselem->type == TSE_SOME_ID) && (te->idcode == idcode)) {
261  return te;
262  }
263  te = te->parent;
264  }
265  return nullptr;
266 }
267 
269 {
270  TreeElement *search_te;
271  TreeStoreElem *tselem;
272 
273  search_te = outliner_search_back_te(te, idcode);
274  if (search_te) {
275  tselem = TREESTORE(search_te);
276  return tselem->id;
277  }
278  return nullptr;
279 }
280 
281 bool outliner_tree_traverse(const SpaceOutliner *space_outliner,
282  ListBase *tree,
283  int filter_te_flag,
284  int filter_tselem_flag,
285  TreeTraversalFunc func,
286  void *customdata)
287 {
288  for (TreeElement *te = reinterpret_cast<TreeElement *>(tree->first), *te_next; te;
289  te = te_next) {
291  /* in case te is freed in callback */
292  TreeStoreElem *tselem = TREESTORE(te);
293  ListBase subtree = te->subtree;
294  te_next = te->next;
295 
296  if (filter_te_flag && (te->flag & filter_te_flag) == 0) {
297  /* skip */
298  }
299  else if (filter_tselem_flag && (tselem->flag & filter_tselem_flag) == 0) {
300  /* skip */
301  }
302  else {
303  func_retval = func(te, customdata);
304  }
305  /* Don't access te or tselem from now on! Might've been freed... */
306 
307  if (func_retval == TRAVERSE_BREAK) {
308  return false;
309  }
310 
311  if (func_retval == TRAVERSE_SKIP_CHILDS) {
312  /* skip */
313  }
314  else if (!outliner_tree_traverse(
315  space_outliner, &subtree, filter_te_flag, filter_tselem_flag, func, customdata)) {
316  return false;
317  }
318  }
319 
320  return true;
321 }
322 
323 float outliner_right_columns_width(const SpaceOutliner *space_outliner)
324 {
325  int num_columns = 0;
326 
327  switch (space_outliner->outlinevis) {
328  case SO_DATA_API:
329  case SO_SEQUENCE:
330  case SO_LIBRARIES:
331  return 0.0f;
335  num_columns = OL_RNA_COL_SIZEX / UI_UNIT_X;
336  break;
338  num_columns = 1;
339  break;
340  }
341  break;
342  case SO_ID_ORPHANS:
343  num_columns = 3;
344  break;
345  case SO_VIEW_LAYER:
346  if (space_outliner->show_restrict_flags & SO_RESTRICT_ENABLE) {
347  num_columns++;
348  }
349  if (space_outliner->show_restrict_flags & SO_RESTRICT_HOLDOUT) {
350  num_columns++;
351  }
352  if (space_outliner->show_restrict_flags & SO_RESTRICT_INDIRECT_ONLY) {
353  num_columns++;
354  }
356  case SO_SCENES:
357  if (space_outliner->show_restrict_flags & SO_RESTRICT_SELECT) {
358  num_columns++;
359  }
360  if (space_outliner->show_restrict_flags & SO_RESTRICT_HIDE) {
361  num_columns++;
362  }
363  if (space_outliner->show_restrict_flags & SO_RESTRICT_VIEWPORT) {
364  num_columns++;
365  }
366  if (space_outliner->show_restrict_flags & SO_RESTRICT_RENDER) {
367  num_columns++;
368  }
369  break;
370  }
371  return (num_columns * UI_UNIT_X + V2D_SCROLL_WIDTH);
372 }
373 
375 {
376  LISTBASE_FOREACH (TreeElement *, te, lb) {
377  if ((TREESTORE(te)->flag & flag) == flag) {
378  return te;
379  }
380  TreeElement *active_element = outliner_find_element_with_flag(&te->subtree, flag);
381  if (active_element) {
382  return active_element;
383  }
384  }
385  return nullptr;
386 }
387 
389 {
390  TreeStoreElem *tselem;
391 
392  while (te->parent) {
393  tselem = TREESTORE(te->parent);
394 
395  if (tselem->flag & TSE_CLOSED) {
396  return false;
397  }
398  te = te->parent;
399  }
400 
401  return true;
402 }
403 
404 bool outliner_is_element_in_view(const TreeElement *te, const View2D *v2d)
405 {
406  return ((te->ys + UI_UNIT_Y) >= v2d->cur.ymin) && (te->ys <= v2d->cur.ymax);
407 }
408 
409 bool outliner_item_is_co_over_name_icons(const TreeElement *te, float view_co_x)
410 {
411  /* Special case: count area left of Scene Collection as empty space */
412  bool outside_left = (TREESTORE(te)->type == TSE_VIEW_COLLECTION_BASE) ?
413  (view_co_x > te->xs + UI_UNIT_X) :
414  (view_co_x > te->xs);
415 
416  return outside_left && (view_co_x < te->xend);
417 }
418 
419 bool outliner_item_is_co_over_icon(const TreeElement *te, float view_co_x)
420 {
421  return (view_co_x > (te->xs + UI_UNIT_X)) && (view_co_x < (te->xs + UI_UNIT_X * 2));
422 }
423 
424 bool outliner_item_is_co_over_name(const TreeElement *te, float view_co_x)
425 {
426  return (view_co_x > (te->xs + UI_UNIT_X * 2)) && (view_co_x < te->xend);
427 }
428 
429 bool outliner_item_is_co_within_close_toggle(const TreeElement *te, float view_co_x)
430 {
431  return (view_co_x > te->xs) && (view_co_x < te->xs + UI_UNIT_X);
432 }
433 
434 void outliner_scroll_view(SpaceOutliner *space_outliner, ARegion *region, int delta_y)
435 {
436  int tree_width, tree_height;
437  outliner_tree_dimensions(space_outliner, &tree_width, &tree_height);
438  int y_min = MIN2(region->v2d.cur.ymin, -tree_height);
439 
440  region->v2d.cur.ymax += delta_y;
441  region->v2d.cur.ymin += delta_y;
442 
443  /* Adjust view if delta placed view outside total area */
444  int offset;
445  if (region->v2d.cur.ymax > -UI_UNIT_Y) {
446  offset = region->v2d.cur.ymax;
447  region->v2d.cur.ymax -= offset;
448  region->v2d.cur.ymin -= offset;
449  }
450  else if (region->v2d.cur.ymin < y_min) {
451  offset = y_min - region->v2d.cur.ymin;
452  region->v2d.cur.ymax += offset;
453  region->v2d.cur.ymin += offset;
454  }
455 }
456 
458  ARegion *region)
459 {
460  /* Avoid rebuild if possible. */
461  if (space_outliner->runtime->tree_display->is_lazy_built()) {
462  ED_region_tag_redraw(region);
463  }
464  else {
466  }
467 }
468 
470 {
471  ARegion *region = CTX_wm_region(C);
472  ViewLayer *view_layer = CTX_data_view_layer(C);
473  SpaceOutliner *space_outliner = CTX_wm_space_outliner(C);
474  TreeElement *te;
475  Base *base = nullptr;
476  float view_mval[2];
477 
478  UI_view2d_region_to_view(&region->v2d, mval[0], mval[1], &view_mval[0], &view_mval[1]);
479 
480  te = outliner_find_item_at_y(space_outliner, &space_outliner->tree, view_mval[1]);
481  if (te) {
482  TreeStoreElem *tselem = TREESTORE(te);
483  if ((tselem->type == TSE_SOME_ID) && (te->idcode == ID_OB)) {
484  Object *ob = (Object *)tselem->id;
485  base = (te->directdata) ? (Base *)te->directdata : BKE_view_layer_base_find(view_layer, ob);
486  }
487  }
488 
489  return base;
490 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct SpaceOutliner * CTX_wm_space_outliner(const bContext *C)
Definition: context.c:860
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
struct Base * BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob)
Definition: layer.c:379
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_pose_armature_get(struct Object *ob)
Definition: object.cc:2511
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:269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
#define ELEM(...)
#define MIN2(a, b)
@ ID_OB
Definition: DNA_ID_enums.h:47
@ OB_MODE_WEIGHT_PAINT
@ OB_ARMATURE
@ OB_MESH
@ TSE_POSE_CHANNEL
@ TSE_VIEW_COLLECTION_BASE
@ TSE_EBONE
@ TSE_SOME_ID
@ TSE_R_LAYER
@ TSE_POSE_BASE
@ TSE_CLOSED
#define OBEDIT_FROM_OBACT(ob)
#define OBACT(_view_layer)
eSpaceOutliner_LibOverrideViewMode
@ SO_LIB_OVERRIDE_VIEW_HIERARCHIES
@ SO_LIB_OVERRIDE_VIEW_PROPERTIES
@ 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:674
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:655
#define C
Definition: RandGen.cpp:25
#define UI_UNIT_Y
#define UI_UNIT_X
#define V2D_SCROLL_WIDTH
Definition: UI_view2d.h:55
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
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
void outliner_tree_dimensions(SpaceOutliner *space_outliner, int *r_width, int *r_height)
@ TE_ICONROW_MERGED
@ TE_ICONROW
#define TREESTORE(a)
#define OL_RNA_COL_SIZEX
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)
bool outliner_is_element_in_view(const TreeElement *te, const View2D *v2d)
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)
float outliner_right_columns_width(const SpaceOutliner *space_outliner)
void outliner_viewcontext_init(const bContext *C, TreeViewContext *tvc)
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:368
void * first
Definition: DNA_listBase.h:31
std::unique_ptr< outliner::AbstractTreeDisplay > tree_display
struct GHash * treehash
short lib_override_view_mode
SpaceOutliner_Runtime * runtime
struct TreeElement * parent
ListBase subtree
struct TreeElement * next
struct Scene * scene
struct ViewLayer * view_layer
float ymax
Definition: DNA_vec_types.h:70
float ymin
Definition: DNA_vec_types.h:70
Establish and manage Outliner trees for different display modes.