Page MenuHome

Graph Editor - Code refactoring for the selection operators
ClosedPublic

Authored by [Inactive] (DR3D) on Oct 13 2020, 4:28 PM.

Details

Summary

This diff is created to separate the functionnal changes of D8687 (curve selection) with the code refactoring it relies on (this diff).

The main idea of this diff is to create functions that initialize all the data required to perform box selections, since both the keyframe-selection operator and the curves-selection operator introduced in D8687 use the same code for initialization.

Diff Detail

Repository
rB Blender

Event Timeline

[Inactive] (DR3D) requested review of this revision.Oct 13 2020, 4:28 PM
[Inactive] (DR3D) created this revision.
Sybren A. Stüvel (sybren) requested changes to this revision.Oct 15 2020, 11:13 AM

Thanks for splitting up the patch. IMO it could have been split up further, with one patch addressing the comment capitalisations (which would be accepted immediately) and the other with the code refactoring. For now it's fine to keep as-is.

source/blender/editors/space_graph/graph_select.c
506–516

Is there really a necessity to have this many parameters? To me it's an indication that this function is trying to do too many things at the same time. If some (input, output) parameter pairs are independent of the others, use a separate function to initialise them, instead of trying to do everything in one big function.

If there is no way around this, I think it would be best to at least order them such that all input parameters are listed first, and the output parameters are listed last, and to have as many parameters as possible declared const.

506–516

Output parameters must be prefixed with r_.

This revision now requires changes to proceed.Oct 15 2020, 11:13 AM

Applied Sybren's advice.

Sybren A. Stüvel (sybren) requested changes to this revision.Oct 19 2020, 1:43 PM

I think it's still a good idea to split up the init function into three parts, for example like in the patch below:

  • This makes it possible to directly return certain structs, rather than rely on return parameters. This in turn makes in possible to declare rectf as const, improving understandability of the rest of the code.
  • It makes it clearer where that pesky void *data is going to; often such a parameter is passed to ANIM_animdata_filter() instead of ac->data, and I expected the same to happen here.
  • It also showed that the r_filter return parameter wasn't even necessary.

I didn't put much thought into the function names, though -- I just used "name of the original init fuction" + "some word that somewhat describes this function". I'm fairly sure better naming is possible.

If you want to clean up the code even more, refactor initialize_box_select_key_editing_data() to use a switch() instead of a sequence of if/else if/else, and replace r_ked->data = data; with r_ked->data = data_circle to make it consistent with the case above it. For me that's just a nice readability improvement, but not a requirement for the acceptance of this patch.

1static rctf initialize_box_select_coords(const bAnimContext *ac, const rctf *rectf_view)
2{
3 const View2D *v2d = &ac->region->v2d;
4 rctf rectf;
5
6 /* Convert mouse coordinates to frame ranges and
7 * channel coordinates corrected for view pan/zoom. */
8 UI_view2d_region_to_view_rctf(v2d, rectf_view, &rectf);
9 return rectf;
10}
11
12static ListBase initialize_box_select_anim_data(bAnimContext *ac)
13{
14 ListBase anim_data;
15
16 const int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_NODUPLIS);
17 ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
18
19 return anim_data;
20}
21
22static void initialize_box_select_key_editing_data(const SpaceGraph *sipo,
23 const bool incl_handles,
24 const short mode,
25 bAnimContext *ac,
26 void *data,
27 rctf *scaled_rectf,
28 KeyframeEditData *r_ked,
29 int *r_mapping_flag)
30{
31 memset(r_ked, 0, sizeof(KeyframeEditData));
32 if (mode == BEZT_OK_REGION_LASSO) {
33 KeyframeEdit_LassoData *data_lasso = data;
34 data_lasso->rectf_scaled = scaled_rectf;
35 r_ked->data = data_lasso;
36 }
37 else if (mode == BEZT_OK_REGION_CIRCLE) {
38 KeyframeEdit_CircleData *data_circle = data;
39 data_circle->rectf_scaled = scaled_rectf;
40 r_ked->data = data;
41 }
42 else {
43 r_ked->data = scaled_rectf;
44 }
45
46 if (sipo->flag & SIPO_SELVHANDLESONLY) {
47 r_ked->iterflags |= KEYFRAME_ITER_HANDLES_DEFAULT_INVISIBLE;
48 }
49
50 /* Treat handles separately? */
51 if (incl_handles) {
52 r_ked->iterflags |= KEYFRAME_ITER_INCL_HANDLES;
53 *r_mapping_flag = 0;
54 }
55 else {
56 *r_mapping_flag = ANIM_UNITCONV_ONLYKEYS;
57 }
58
59 *r_mapping_flag |= ANIM_get_normalization_flags(ac);
60}
61
62
63static void box_select_graphkeys(bAnimContext *ac,
64 const rctf *rectf_view,
65 short mode,
66 short selectmode,
67 bool incl_handles,
68 void *data)
69{
70 const rctf rectf = initialize_box_select_coords(ac, rectf_view);
71 ListBase anim_data = initialize_box_select_anim_data(ac);
72
73 SpaceGraph *sipo = (SpaceGraph *)ac->sl;
74 rctf scaled_rectf;
75 KeyframeEditData ked;
76 int mapping_flag;
77 initialize_box_select_key_editing_data(
78 sipo, incl_handles, mode, ac, data, &scaled_rectf, &ked, &mapping_flag);
79
80 /* ... */
81}

source/blender/editors/space_graph/graph_select.c
516

r_filter isn't used in either of the calls, so it doesn't need to be returned at all.

561

I have no clue what this comment means. Do you? Maybe then you could reword it to make more sense.

This revision now requires changes to proceed.Oct 19 2020, 1:43 PM
[Inactive] (DR3D) marked an inline comment as done.

Split initialization.
Sybren's proposal with minor fix.

[Inactive] (DR3D) marked 3 inline comments as done.Oct 20 2020, 2:18 PM

I made some changes to the box select function (rB02efc0111c1f), could you update the patch for this & rebase onto master?

Took the last changes in master into account.

[Inactive] (DR3D) edited the summary of this revision. (Show Details)Nov 26 2020, 3:42 PM
This revision is now accepted and ready to land.Nov 30 2020, 4:38 PM