Blender V5.3
select_linked_pick.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10#include "BKE_curves.hh"
11#include "BKE_layer.hh"
12
13#include "RNA_access.hh"
14#include "RNA_define.hh"
15
16#include "DEG_depsgraph.hh"
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20
21#include "ED_curves.hh"
22#include "ED_view3d.hh"
23
24namespace blender::ed::curves {
25
30static ClosestCurveDataBlock find_closest_curve(const Depsgraph &depsgraph,
31 const ViewContext &vc,
32 const Span<Base *> bases,
33 const int2 &mval)
34{
36 bases.index_range(),
37 1L,
39 [&](const IndexRange range, const ClosestCurveDataBlock &init) {
40 ClosestCurveDataBlock new_closest = init;
41 for (Base *base : bases.slice(range)) {
42 Object &curves_ob = *base->object;
43 Curves &curves_id = *id_cast<Curves *>(curves_ob.data);
46 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
47 const float4x4 projection = ED_view3d_ob_project_mat_get(vc.rv3d, &curves_ob);
49 curves,
50 deformation,
52 [&](IndexRange range, Span<float3> positions, StringRef /*selection_name*/) {
53 std::optional<FindClosestData> new_closest_elem = closest_elem_find_screen_space(
54 vc,
55 curves.points_by_curve(),
56 positions,
57 curves.cyclic(),
58 projection,
59 range,
60 bke::AttrDomain::Curve,
61 mval,
62 new_closest.elem);
63 if (new_closest_elem) {
64 new_closest.elem = *new_closest_elem;
65 new_closest.curves_id = &curves_id;
66 }
67 });
68 }
69 return new_closest;
70 },
71 [](const ClosestCurveDataBlock &a, const ClosestCurveDataBlock &b) {
72 return (a.elem.distance_sq < b.elem.distance_sq) ? a : b;
73 });
74}
75
76static bool select_linked_pick(bContext &C, const int2 &mval, const SelectPick_Params &params)
77{
78 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(&C);
79 const ViewContext vc = ED_view3d_viewcontext_init(&C, depsgraph);
81 *vc.bmain, vc.scene, vc.view_layer, vc.v3d);
82
83 const ClosestCurveDataBlock closest = find_closest_curve(*depsgraph, vc, bases, mval);
84 if (!closest.curves_id) {
85 return false;
86 }
87
88 bke::CurvesGeometry &closest_curves = closest.curves_id->geometry.wrap();
89 const bke::AttrDomain selection_domain = bke::AttrDomain(closest.curves_id->selection_domain);
90
91 if (selection_domain == bke::AttrDomain::Point) {
92 const OffsetIndices points_by_curve = closest_curves.points_by_curve();
94 closest_curves, bke::AttrDomain::Point, [&](bke::GSpanAttributeWriter &selection) {
95 for (const int point : points_by_curve[closest.elem.index]) {
96 apply_selection_operation_at_index(selection.span, point, params.sel_op);
97 }
98 });
99 }
100 else if (selection_domain == bke::AttrDomain::Curve) {
103 apply_selection_operation_at_index(selection.span, closest.elem.index, params.sel_op);
104 selection.finish();
105 }
106
107 /* Use #ID_RECALC_GEOMETRY instead of #ID_RECALC_SELECT because it is handled as a
108 * generic attribute for now. */
111
112 return true;
113}
114
116 wmOperator *op,
117 const wmEvent *event)
118{
119 SelectPick_Params params{};
120 params.sel_op = RNA_boolean_get(op->ptr, "deselect") ? SEL_OP_SUB : SEL_OP_ADD;
121 params.deselect_all = false;
122 params.select_passthrough = false;
123
124 if (!select_linked_pick(*C, event->mval, params)) {
125 return OPERATOR_CANCELLED;
126 }
127
128 return OPERATOR_FINISHED;
129}
130
132{
133 ot->name = "Select Linked";
134 ot->idname = "CURVES_OT_select_linked_pick";
135 ot->description = "Select all points in the curve under the cursor";
136
139
141
143 "deselect",
144 false,
145 "Deselect",
146 "Deselect linked control points rather than selecting them");
147}
148
149} // namespace blender::ed::curves
Low-level operations for curves.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
bool closest(btVector3 &v)
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
OffsetIndices< int > points_by_curve() const
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
static bool select_linked_pick(bContext &C, const int2 &mval, const SelectPick_Params &params)
void apply_selection_operation_at_index(GMutableSpan selection, const int index, const eSelectOp sel_op)
static ClosestCurveDataBlock find_closest_curve(const Depsgraph &depsgraph, const ViewContext &vc, const Span< Base * > bases, const int2 &mval)
static wmOperatorStatus select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void foreach_selectable_curve_range(const bke::CurvesGeometry &curves, const bke::crazyspace::GeometryDeformation &deformation, eHandleDisplay handle_display, SelectionRangeFn range_consumer)
void foreach_selection_attribute_writer(bke::CurvesGeometry &curves, bke::AttrDomain selection_domain, FunctionRef< void(bke::GSpanAttributeWriter &selection)> fn)
bool editable_curves_poll(bContext *C)
bke::GSpanAttributeWriter ensure_selection_attribute(bke::CurvesGeometry &curves, bke::AttrDomain selection_domain, bke::AttrType create_type, StringRef attribute_name)
void CURVES_OT_select_linked_pick(wmOperatorType *ot)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
MatBase< float, 4, 4 > float4x4
VecBase< int32_t, 2 > int2
void DEG_id_tag_update(ID *id, unsigned int flags)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Vector< Base * > BKE_view_layer_array_from_bases_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
ed::curves::FindClosestData elem
eHandleDisplay handle_display
View3DOverlay overlay
ViewLayer * view_layer
Definition ED_view3d.hh:80
RegionView3D * rv3d
Definition ED_view3d.hh:86
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141