Blender V5.3
interface_template_bone_collection_tree.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10
11#include "BLT_translation.hh"
12
13#include "ANIM_armature_iter.hh"
15
16#include "UI_interface.hh"
17#include "UI_tree_view.hh"
18
19#include "RNA_access.hh"
20#include "RNA_prototypes.hh"
21
22#include "ED_armature.hh"
23#include "ED_undo.hh"
24
25#include "WM_api.hh"
26
27#include <fmt/format.h>
28
29namespace blender::ui {
30namespace bonecollections {
31
32using namespace blender::animrig;
33
35 protected:
39
40 public:
45 BoneCollectionTreeView(bArmature &armature, Object *armature_object);
46 void build_tree() override;
47
48 bool listen(const wmNotifier &notifier) const override;
49
50 private:
51 void build_tree_node_recursive(TreeViewItemContainer &parent, const int bcoll_index);
52
55 void build_bcolls_with_selected_bones();
56};
57
80
82 private:
83 ArmatureBoneCollection drag_arm_bcoll_;
84
85 public:
87 bArmature &armature,
88 const int bcoll_index);
89
90 std::optional<eWM_DragDataType> get_drag_type() const override;
91 void *create_drag_data() const override;
92 void on_drag_start(bContext &C, AbstractViewItem &item) override;
93};
94
96 private:
97 ArmatureBoneCollection drop_bonecoll_;
98
99 public:
101 DropBehavior behavior,
102 const ArmatureBoneCollection &drop_bonecoll)
103 : TreeViewItemDropTarget(item, behavior), drop_bonecoll_(drop_bonecoll)
104 {
105 }
106
107 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override
108 {
109 if (drag.type != WM_DRAG_BONE_COLLECTION) {
110 return false;
111 }
112
113 const ArmatureBoneCollection *drag_arm_bcoll = static_cast<const ArmatureBoneCollection *>(
114 drag.poin);
115
116 /* Do not allow dropping onto another armature. */
117 if (drag_arm_bcoll->armature != drop_bonecoll_.armature) {
118 *r_disabled_hint = "Cannot drag & drop bone collections between Armatures.";
119 return false;
120 }
121
122 /* Dragging onto itself doesn't do anything. */
123 if (drag_arm_bcoll->bcoll_index == drop_bonecoll_.bcoll_index) {
124 return false;
125 }
126
127 /* Do not allow dropping onto its own descendants. */
129 drag_arm_bcoll->armature, drag_arm_bcoll->bcoll_index, drop_bonecoll_.bcoll_index))
130 {
131 *r_disabled_hint = "Cannot drag a collection onto a descendent";
132 return false;
133 }
134
135 return true;
136 }
137
138 std::string drop_tooltip(const DragInfo &drag_info) const override
139 {
140 const ArmatureBoneCollection *drag_bone_collection =
141 static_cast<const ArmatureBoneCollection *>(drag_info.drag_data.poin);
142 const BoneCollection &drag_bcoll = drag_bone_collection->bcoll();
143 const BoneCollection &drop_bcoll = drop_bonecoll_.bcoll();
144
145 const StringRef drag_name = drag_bcoll.name;
146 const StringRef drop_name = drop_bcoll.name;
147
148 switch (drag_info.drop_location) {
150 return fmt::format(fmt::runtime(TIP_("Move {} into {}")), drag_name, drop_name);
152 return fmt::format(fmt::runtime(TIP_("Move {} above {}")), drag_name, drop_name);
154 return fmt::format(fmt::runtime(TIP_("Move {} below {}")), drag_name, drop_name);
155 }
156
157 return "";
158 }
159
160 bool on_drop(bContext *C, const DragInfo &drag_info) const override
161 {
162 const ArmatureBoneCollection *drag_arm_bcoll = static_cast<const ArmatureBoneCollection *>(
163 drag_info.drag_data.poin);
164 bArmature *arm = drop_bonecoll_.armature;
165
166 const int from_bcoll_index = drag_arm_bcoll->bcoll_index;
167 const int to_bcoll_index = drop_bonecoll_.bcoll_index;
168
169 int new_bcoll_index = -1;
170 switch (drag_info.drop_location) {
173 arm, from_bcoll_index, to_bcoll_index, MoveLocation::Before);
174 break;
175
176 case DropLocation::Into: {
177 if (!ANIM_armature_bonecoll_is_editable(arm, &drop_bonecoll_.bcoll())) {
178 return false;
179 }
180
181 const int from_parent_index = armature_bonecoll_find_parent_index(arm, from_bcoll_index);
182 /* The bone collection becomes the last child of the new parent, as
183 * that's consistent with the drag & drop of scene collections in the
184 * outliner. */
185 new_bcoll_index = armature_bonecoll_move_to_parent(
186 arm, from_bcoll_index, -1, from_parent_index, to_bcoll_index);
187 break;
188 }
191 arm, from_bcoll_index, to_bcoll_index, MoveLocation::After);
192 break;
193 }
194
195 if (new_bcoll_index < 0) {
196 return false;
197 }
198
199 ANIM_armature_bonecoll_active_index_set(arm, new_bcoll_index);
201
202 ED_undo_push(C, "Reorder Armature Bone Collections");
203 return true;
204 }
205};
206
208 private:
209 bArmature &armature_;
210 int bcoll_index_;
211 BoneCollection &bone_collection_;
212 bool has_any_selected_bones_;
213
214 public:
215 BoneCollectionItem(bArmature &armature, const int bcoll_index, const bool has_any_selected_bones)
216 : armature_(armature),
217 bcoll_index_(bcoll_index),
218 bone_collection_(*armature.collection_array[bcoll_index]),
219 has_any_selected_bones_(has_any_selected_bones)
220 {
221 this->label_ = bone_collection_.name;
222 }
223
224 void build_row(Layout &row) override
225 {
226 Layout &sub = row.row(true);
227
228 Button *name_label = uiItemL_ex(&sub, bone_collection_.name, ICON_NONE, false, false);
229 if (!ANIM_armature_bonecoll_is_editable(&armature_, &bone_collection_)) {
230 button_flag_enable(name_label, BUT_INACTIVE);
231 }
232
233 /* Contains Active Bone icon. */
234 /* Performance note: this check potentially loops over all bone collections the active bone is
235 * assigned to. And this happens for each redraw of each bone collection in the armature. */
236 {
237 int icon;
238 if (ANIM_armature_bonecoll_contains_active_bone(&armature_, &bone_collection_)) {
239 icon = ICON_LAYER_ACTIVE;
240 }
241 else if (has_any_selected_bones_) {
242 icon = ICON_LAYER_USED;
243 }
244 else {
245 icon = ICON_BLANK1;
246 }
247 sub.label("", icon);
248 }
249
250 /* Visibility eye icon. */
251 {
252 const bool is_solo_active = armature_.flag & ARM_BCOLL_SOLO_ACTIVE;
253 Layout &visibility_sub = sub.row(true);
254 visibility_sub.active_set(!is_solo_active && bone_collection_.is_visible_ancestors());
255
256 const int icon = bone_collection_.is_visible() ? ICON_HIDE_OFF : ICON_HIDE_ON;
257 PointerRNA bcoll_ptr = rna_pointer();
258 visibility_sub.prop(&bcoll_ptr, "is_visible", ITEM_R_ICON_ONLY, "", icon);
259 }
260
261 /* Solo icon. */
262 {
263 const int icon = bone_collection_.is_solo() ? ICON_SOLO_ON : ICON_SOLO_OFF;
264 PointerRNA bcoll_ptr = rna_pointer();
265 sub.prop(&bcoll_ptr, "is_solo", ITEM_R_ICON_ONLY, "", icon);
266 }
267 }
268
269 void build_context_menu(bContext &C, Layout &column) const override
270 {
271 MenuType *mt = WM_menutype_find("ARMATURE_MT_collection_tree_context_menu", true);
272 if (!mt) {
273 return;
274 }
275 menutype_draw(&C, mt, &column);
276 }
277
278 std::optional<bool> should_be_active() const override
279 {
280 return armature_.runtime->active_collection_index == bcoll_index_;
281 }
282
283 void on_activate(bContext &C) override
284 {
285 /* Let RNA handle the property change. This makes sure all the notifiers and DEG
286 * update calls are properly called. */
288 &armature_.id, RNA_BoneCollections, &armature_);
289 PropertyRNA *prop = RNA_struct_find_property(&bcolls_ptr, "active_index");
290
291 RNA_property_int_set(&bcolls_ptr, prop, bcoll_index_);
292 RNA_property_update(&C, &bcolls_ptr, prop);
293
294 ED_undo_push(&C, "Change Armature's Active Bone Collection");
295 }
296
297 std::optional<bool> should_be_collapsed() const override
298 {
299 const bool is_collapsed = !bone_collection_.is_expanded();
300 return is_collapsed;
301 }
302
303 bool set_collapsed(const bool collapsed) override
304 {
305 if (!AbstractTreeViewItem::set_collapsed(collapsed)) {
306 return false;
307 }
308
309 /* Ensure that the flag in DNA is set. */
310 ANIM_armature_bonecoll_is_expanded_set(&bone_collection_, !collapsed);
311 return true;
312 }
313
314 void on_collapse_change(bContext &C, const bool is_collapsed) override
315 {
316 const bool is_expanded = !is_collapsed;
317
318 /* Let RNA handle the property change. This makes sure all the notifiers and DEG
319 * update calls are properly called. */
321 &armature_.id, RNA_BoneCollection, &bone_collection_);
322 PropertyRNA *prop = RNA_struct_find_property(&bcoll_ptr, "is_expanded");
323
324 RNA_property_boolean_set(&bcoll_ptr, prop, is_expanded);
325 RNA_property_update(&C, &bcoll_ptr, prop);
326 }
327
328 bool supports_renaming() const override
329 {
330 return ANIM_armature_bonecoll_is_editable(&armature_, &bone_collection_);
331 }
332
333 bool rename(const bContext &C, StringRefNull new_name) override
334 {
335 /* Let RNA handle the renaming. This makes sure all the notifiers and DEG
336 * update calls are properly called. */
337 PointerRNA bcoll_ptr = rna_pointer();
338 PropertyRNA *prop = RNA_struct_find_property(&bcoll_ptr, "name");
339
340 RNA_property_string_set(&bcoll_ptr, prop, new_name.c_str());
341 RNA_property_update(&const_cast<bContext &>(C), &bcoll_ptr, prop);
342
343 ED_undo_push(&const_cast<bContext &>(C), "Rename Armature Bone Collection");
344 return true;
345 }
346
348 {
349 return bone_collection_.name;
350 }
351
352 void delete_item(bContext *C) override
353 {
354 ANIM_armature_bonecoll_remove(&armature_, &bone_collection_);
355 ED_undo_push(C, "Delete Bone Collection");
356 }
357 std::unique_ptr<AbstractViewItemDragController> create_drag_controller() const override
358 {
359 /* Reject dragging linked (or otherwise uneditable) bone collections. */
360 if (!ANIM_armature_bonecoll_is_editable(&armature_, &bone_collection_)) {
361 return {};
362 }
363
364 BoneCollectionTreeView &tree_view = static_cast<BoneCollectionTreeView &>(get_tree_view());
365 return std::make_unique<BoneCollectionDragController>(tree_view, armature_, bcoll_index_);
366 }
367
368 std::unique_ptr<TreeViewItemDropTarget> create_drop_target() override
369 {
370 ArmatureBoneCollection drop_bonecoll(&armature_, bcoll_index_);
371 /* For now, only support DropBehavior::Insert until there's code for actually reordering
372 * siblings. Currently only 'move to another parent' is implemented. */
373 return std::make_unique<BoneCollectionDropTarget>(
374 *this, DropBehavior::ReorderAndInsert, drop_bonecoll);
375 }
376
377 protected:
380 {
381 return RNA_pointer_create_discrete(&armature_.id, RNA_BoneCollection, &bone_collection_);
382 }
383};
384
386 : armature_(armature), armature_object_(armature_object)
387{
388}
389
391{
392 build_bcolls_with_selected_bones();
393
394 for (int bcoll_index = 0; bcoll_index < armature_.collection_root_count; bcoll_index++) {
395 build_tree_node_recursive(*this, bcoll_index);
396 }
397}
398
399void BoneCollectionTreeView::build_tree_node_recursive(TreeViewItemContainer &parent,
400 const int bcoll_index)
401{
402 BoneCollection *bcoll = armature_.collection_array[bcoll_index];
403 const bool has_any_selected_bones = bcolls_with_selected_bones_.contains(bcoll);
404 BoneCollectionItem &bcoll_tree_item = parent.add_tree_item<BoneCollectionItem>(
405 armature_, bcoll_index, has_any_selected_bones);
406 for (int child_index = bcoll->child_index; child_index < bcoll->child_index + bcoll->child_count;
407 child_index++)
408 {
409 build_tree_node_recursive(bcoll_tree_item, child_index);
410 }
411}
412
414{
415 return notifier.data == ND_BONE_COLLECTION;
416}
417
418void BoneCollectionTreeView::build_bcolls_with_selected_bones()
419{
421
422 /* Armature Edit mode. */
423 if (armature_.edbo) {
424 for (EditBone &ebone : *armature_.edbo) {
425 if ((ebone.flag & BONE_SELECTED) == 0) {
426 continue;
427 }
428
429 for (BoneCollectionReference &ref : ebone.bone_collections) {
431 }
432 }
433 return;
434 }
435 if (!armature_object_) {
436 /* Beyond this point we need the object that the armature is used by. */
437 return;
438 }
439 /* Any other mode. */
440 for (bPoseChannel &pchan : armature_object_->pose->chanbase) {
441 /* Not using `animrig::bone_is_selected` because that would query for collection visibility
442 * which we don't care about here. */
443 if (!(pchan.flag & POSE_SELECTED_ALL)) {
444 continue;
445 }
446 Bone *bone = pchan.bone_get(armature_);
447 if (!bone) {
448 continue;
449 }
450 for (const BoneCollectionReference &ref : bone->runtime.collections) {
452 }
453 }
454}
455
457 bArmature &armature,
458 const int bcoll_index)
459 : AbstractViewItemDragController(tree_view), drag_arm_bcoll_(&armature, bcoll_index)
460{
461}
462
463std::optional<eWM_DragDataType> BoneCollectionDragController::get_drag_type() const
464{
466}
467
469{
471 *drag_data = drag_arm_bcoll_;
472 return drag_data;
473}
474
476{
477 ANIM_armature_bonecoll_active_index_set(drag_arm_bcoll_.armature, drag_arm_bcoll_.bcoll_index);
478}
479
480} // namespace bonecollections
481
483{
484 bArmature *armature = ED_armature_context(C);
485 if (armature == nullptr) {
486 return;
487 }
488 BLI_assert(GS(armature->id.name) == ID_AR);
489 Object *armature_object = CTX_data_active_object(C);
490 if (armature_object && armature_object->data != &armature->id) {
491 /* With pinning, the armature may not be from the active object. */
492 armature_object = nullptr;
493 }
494
495 Block *block = layout->block();
496
497 AbstractTreeView *tree_view = block_add_view(
498 *block,
499 "Bone Collection Tree View",
500 std::make_unique<bonecollections::BoneCollectionTreeView>(*armature, armature_object));
501 tree_view->set_context_menu_title("Bone Collection");
502 tree_view->set_default_rows(5);
503
504 TreeViewBuilder::build_tree_view(*C, *tree_view, *layout);
505}
506
507} // namespace blender::ui
Iterators for armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
#define TIP_(msgid)
#define C
Definition RandGen.cpp:29
#define ND_BONE_COLLECTION
Definition WM_types.hh:515
#define NC_OBJECT
Definition WM_types.hh:392
constexpr const char * c_str() const
Abstract base class for defining a customizable tree-view item.
AbstractTreeView & get_tree_view() const
Definition tree_view.cc:775
virtual bool set_collapsed(bool collapsed)
Definition tree_view.cc:847
void set_default_rows(int default_rows)
Definition tree_view.cc:234
void set_context_menu_title(const std::string &title)
static void build_tree_view(const bContext &C, AbstractTreeView &tree_view, Layout &layout, bool add_box=true)
ItemT & add_tree_item(Args &&...args)
TreeViewItemDropTarget(AbstractTreeViewItem &view_item, DropBehavior behavior=DropBehavior::Insert)
Definition tree_view.cc:557
BoneCollectionDragController(BoneCollectionTreeView &tree_view, bArmature &armature, const int bcoll_index)
bool on_drop(bContext *C, const DragInfo &drag_info) const override
bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override
std::string drop_tooltip(const DragInfo &drag_info) const override
BoneCollectionDropTarget(AbstractTreeViewItem &item, DropBehavior behavior, const ArmatureBoneCollection &drop_bonecoll)
std::unique_ptr< TreeViewItemDropTarget > create_drop_target() override
void build_context_menu(bContext &C, Layout &column) const override
void on_collapse_change(bContext &C, const bool is_collapsed) override
BoneCollectionItem(bArmature &armature, const int bcoll_index, const bool has_any_selected_bones)
bool rename(const bContext &C, StringRefNull new_name) override
std::unique_ptr< AbstractViewItemDragController > create_drag_controller() const override
bool armature_bonecoll_is_descendant_of(const bArmature *armature, int potential_parent_index, int potential_descendant_index)
int armature_bonecoll_find_parent_index(const bArmature *armature, int bcoll_index)
int armature_bonecoll_move_to_parent(bArmature *armature, int from_bcoll_index, int to_child_num, int from_parent_index, int to_parent_index)
AbstractGridView * block_add_view(Block &block, StringRef idname, std::unique_ptr< AbstractGridView > grid_view)
void template_bone_collection_tree(Layout *layout, bContext *C)
void button_flag_enable(Button *but, int64_t flag)
Button * uiItemL_ex(Layout *layout, StringRef name, int icon, bool highlight, bool redalert)
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ANIM_armature_bonecoll_is_expanded_set(BoneCollection *bcoll, bool is_expanded)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void ANIM_armature_bonecoll_active_index_set(bArmature *armature, int bone_collection_index)
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
void ANIM_armature_bonecoll_remove(bArmature *armature, BoneCollection *bcoll)
@ WM_DRAG_BONE_COLLECTION
Definition WM_types.hh:1318
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
bArmature * ED_armature_context(const bContext *C)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
bool ANIM_armature_bonecoll_contains_active_bone(const bArmature *armature, const BoneCollection *bcoll)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
int ANIM_armature_bonecoll_move_before_after_index(bArmature *armature, int from_index, int to_index, MoveLocation before_after)
Object * CTX_data_active_object(const bContext *C)
bool ANIM_armature_bonecoll_is_editable(const bArmature *armature, const BoneCollection *bcoll)
ListBaseT< BoneCollectionReference > collections
Bone_Runtime runtime
ListBaseT< BoneCollectionReference > bone_collections
char name[258]
Definition DNA_ID.h:433
struct bPose * pose
ListBaseT< EditBone > * edbo
struct BoneCollection ** collection_array
ListBaseT< bPoseChannel > chanbase
const wmDrag & drag_data
const DropLocation drop_location
void label(StringRef name, int icon)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & row(bool align)
eWM_DragDataType type
Definition WM_types.hh:1424
unsigned int data
Definition WM_types.hh:371