Cleanup: Snap Context Refactor

Move runtime parameters out of context creation.

Not being able to choose another region and v3d limits the use of the
snap API.
This commit is contained in:
Germano Cavalcante 2021-10-12 17:05:56 -03:00
parent 988b9bc40c
commit 91c33c8b99
Notes: blender-bot 2023-04-19 22:54:54 +02:00
Referenced by commit b246f81412, Fix T92311: Snap to faces in edit mode with x-ray enabled doesn't work
Referenced by issue #92311, Snap to faces in edit mode with x-ray enabled doesn't work anymore.
17 changed files with 278 additions and 263 deletions

View File

@ -5561,12 +5561,14 @@ static int add_vertex_invoke(bContext *C, wmOperator *op, const wmEvent *event)
if (use_proj) {
const float mval[2] = {UNPACK2(event->mval)};
struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create_view3d(
vc.scene, 0, vc.region, vc.v3d);
struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create(vc.scene,
0);
ED_transform_snap_object_project_view3d(
snap_context,
vc.depsgraph,
vc.region,
vc.v3d,
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = (vc.obedit != NULL) ? SNAP_NOT_ACTIVE : SNAP_ALL,

View File

@ -289,6 +289,8 @@ static int gizmo_move_modal(bContext *C,
if (ED_transform_snap_object_project_view3d(
inter->snap_context_v3d,
CTX_data_ensure_evaluated_depsgraph(C),
region,
CTX_wm_view3d(C),
(SCE_SNAP_MODE_VERTEX | SCE_SNAP_MODE_EDGE | SCE_SNAP_MODE_FACE),
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
@ -381,8 +383,7 @@ static int gizmo_move_invoke(bContext *C, wmGizmo *gz, const wmEvent *event)
if (area) {
switch (area->spacetype) {
case SPACE_VIEW3D: {
inter->snap_context_v3d = ED_transform_snap_object_context_create_view3d(
CTX_data_scene(C), 0, CTX_wm_region(C), CTX_wm_view3d(C));
inter->snap_context_v3d = ED_transform_snap_object_context_create(CTX_data_scene(C), 0);
break;
}
default:

View File

@ -278,15 +278,11 @@ void ED_gizmotypes_snap_3d_draw_util(RegionView3D *rv3d,
immUnbindProgram();
}
SnapObjectContext *ED_gizmotypes_snap_3d_context_ensure(Scene *scene,
const ARegion *region,
const View3D *v3d,
wmGizmo *gz)
SnapObjectContext *ED_gizmotypes_snap_3d_context_ensure(Scene *scene, wmGizmo *gz)
{
SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
if (snap_gizmo->snap_context_v3d == NULL) {
snap_gizmo->snap_context_v3d = ED_transform_snap_object_context_create_view3d(
scene, 0, region, v3d);
snap_gizmo->snap_context_v3d = ED_transform_snap_object_context_create(scene, 0);
}
return snap_gizmo->snap_context_v3d;
}
@ -387,6 +383,8 @@ short ED_gizmotypes_snap_3d_update(wmGizmo *gz,
snap_elem = ED_transform_snap_object_project_view3d_ex(
snap_gizmo->snap_context_v3d,
depsgraph,
region,
v3d,
snap_elements,
&(const struct SnapObjectParams){
.snap_select = snap_select,
@ -576,8 +574,12 @@ static void snap_gizmo_setup(wmGizmo *gz)
#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
snap_gizmo->keymap = WM_modalkeymap_find(gz->parent_gzgroup->type->keyconf,
"Generic Gizmo Tweak Modal Map");
wmKeyConfig *keyconf = gz->parent_gzgroup->type->keyconf;
if (!keyconf) {
/* It can happen when gizmogrouptype is not linked at startup. */
keyconf = ((wmWindowManager *)G.main->wm.first)->defaultconf;
}
snap_gizmo->keymap = WM_modalkeymap_find(keyconf, "Generic Gizmo Tweak Modal Map");
RNA_enum_value_from_id(snap_gizmo->keymap->modal_items, "SNAP_ON", &snap_gizmo->snap_on);
#endif
}

View File

@ -213,7 +213,6 @@ static int gpencil_bake_grease_pencil_animation_exec(bContext *C, wmOperator *op
Main *bmain = CTX_data_main(C);
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
ARegion *region = CTX_wm_region(C);
View3D *v3d = CTX_wm_view3d(C);
ListBase ob_selected_list = {NULL, NULL};
@ -256,7 +255,7 @@ static int gpencil_bake_grease_pencil_animation_exec(bContext *C, wmOperator *op
gsc.ob = ob_gpencil;
/* Init snap context for geometry projection. */
sctx = ED_transform_snap_object_context_create_view3d(scene, 0, region, CTX_wm_view3d(C));
sctx = ED_transform_snap_object_context_create(scene, 0);
}
/* Loop all frame range. */

View File

@ -3758,7 +3758,6 @@ static int gpencil_strokes_reproject_exec(bContext *C, wmOperator *op)
bGPdata *gpd = ED_gpencil_data_get_active(C);
Scene *scene = CTX_data_scene(C);
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ARegion *region = CTX_wm_region(C);
int oldframe = (int)DEG_get_ctime(depsgraph);
const eGP_ReprojectModes mode = RNA_enum_get(op->ptr, "type");
const bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
@ -3767,7 +3766,7 @@ static int gpencil_strokes_reproject_exec(bContext *C, wmOperator *op)
/* Init snap context for geometry projection. */
SnapObjectContext *sctx = NULL;
sctx = ED_transform_snap_object_context_create_view3d(scene, 0, region, CTX_wm_view3d(C));
sctx = ED_transform_snap_object_context_create(scene, 0);
bool changed = false;
/* Init space conversion stuff. */

View File

@ -188,7 +188,6 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op)
Main *bmain = CTX_data_main(C);
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
ARegion *region = CTX_wm_region(C);
View3D *v3d = CTX_wm_view3d(C);
ListBase ob_selected_list = {NULL, NULL};
@ -262,7 +261,7 @@ static int gpencil_bake_mesh_animation_exec(bContext *C, wmOperator *op)
gsc.ob = ob_gpencil;
/* Init snap context for geometry projection. */
sctx = ED_transform_snap_object_context_create_view3d(scene, 0, region, CTX_wm_view3d(C));
sctx = ED_transform_snap_object_context_create(scene, 0);
/* Tag all existing strokes to avoid reprojections. */
LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {

View File

@ -1292,6 +1292,7 @@ void ED_gpencil_stroke_reproject(Depsgraph *depsgraph,
depsgraph, region, v3d, xy, &ray_start[0], &ray_normal[0], true);
if (ED_transform_snap_object_project_ray(sctx,
depsgraph,
v3d,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
},

View File

@ -257,8 +257,6 @@ void ED_gizmotypes_snap_3d_draw_util(struct RegionView3D *rv3d,
const uchar color_point[4],
const short snap_elem_type);
struct SnapObjectContext *ED_gizmotypes_snap_3d_context_ensure(struct Scene *scene,
const struct ARegion *region,
const struct View3D *v3d,
struct wmGizmo *gz);
typedef enum {

View File

@ -83,11 +83,6 @@ struct SnapObjectParams {
typedef struct SnapObjectContext SnapObjectContext;
SnapObjectContext *ED_transform_snap_object_context_create(struct Scene *scene, int flag);
SnapObjectContext *ED_transform_snap_object_context_create_view3d(struct Scene *scene,
int flag,
/* extra args for view3d */
const struct ARegion *region,
const struct View3D *v3d);
void ED_transform_snap_object_context_destroy(SnapObjectContext *sctx);
/* callbacks to filter how snap works */
@ -100,6 +95,7 @@ void ED_transform_snap_object_context_set_editmesh_callbacks(
bool ED_transform_snap_object_project_ray_ex(struct SnapObjectContext *sctx,
struct Depsgraph *depsgraph,
const View3D *v3d,
const struct SnapObjectParams *params,
const float ray_start[3],
const float ray_normal[3],
@ -112,6 +108,7 @@ bool ED_transform_snap_object_project_ray_ex(struct SnapObjectContext *sctx,
float r_obmat[4][4]);
bool ED_transform_snap_object_project_ray(SnapObjectContext *sctx,
struct Depsgraph *depsgraph,
const View3D *v3d,
const struct SnapObjectParams *params,
const float ray_origin[3],
const float ray_direction[3],
@ -121,6 +118,7 @@ bool ED_transform_snap_object_project_ray(SnapObjectContext *sctx,
bool ED_transform_snap_object_project_ray_all(SnapObjectContext *sctx,
struct Depsgraph *depsgraph,
const View3D *v3d,
const struct SnapObjectParams *params,
const float ray_start[3],
const float ray_normal[3],
@ -130,6 +128,8 @@ bool ED_transform_snap_object_project_ray_all(SnapObjectContext *sctx,
short ED_transform_snap_object_project_view3d_ex(struct SnapObjectContext *sctx,
struct Depsgraph *depsgraph,
const ARegion *region,
const View3D *v3d,
const unsigned short snap_to,
const struct SnapObjectParams *params,
const float mval[2],
@ -142,6 +142,8 @@ short ED_transform_snap_object_project_view3d_ex(struct SnapObjectContext *sctx,
float r_obmat[4][4]);
bool ED_transform_snap_object_project_view3d(struct SnapObjectContext *sctx,
struct Depsgraph *depsgraph,
const ARegion *region,
const View3D *v3d,
const unsigned short snap_to,
const struct SnapObjectParams *params,
const float mval[2],
@ -153,6 +155,8 @@ bool ED_transform_snap_object_project_view3d(struct SnapObjectContext *sctx,
bool ED_transform_snap_object_project_all_view3d_ex(SnapObjectContext *sctx,
struct Depsgraph *depsgraph,
const ARegion *region,
const View3D *v3d,
const struct SnapObjectParams *params,
const float mval[2],
float ray_depth,

View File

@ -1701,8 +1701,8 @@ void EDBM_project_snap_verts(
ED_view3d_init_mats_rv3d(obedit, region->regiondata);
struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create_view3d(
CTX_data_scene(C), 0, region, CTX_wm_view3d(C));
struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create(
CTX_data_scene(C), 0);
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
@ -1711,6 +1711,8 @@ void EDBM_project_snap_verts(
V3D_PROJ_RET_OK) {
if (ED_transform_snap_object_project_view3d(snap_context,
depsgraph,
region,
CTX_wm_view3d(C),
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = SNAP_NOT_ACTIVE,

View File

@ -5099,14 +5099,15 @@ void ED_view3d_cursor3d_position_rotation(bContext *C,
float ray_no[3];
float ray_co[3];
struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create_view3d(
scene, 0, region, v3d);
struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create(scene, 0);
float obmat[4][4];
Object *ob_dummy = NULL;
float dist_px = 0;
if (ED_transform_snap_object_project_view3d_ex(snap_context,
CTX_data_ensure_evaluated_depsgraph(C),
region,
v3d,
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,

View File

@ -356,8 +356,7 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
if (do_thickness && inter->co_index != 1) {
Scene *scene = DEG_get_input_scene(depsgraph);
View3D *v3d = ruler_info->area->spacedata.first;
SnapObjectContext *snap_context = ED_gizmotypes_snap_3d_context_ensure(
scene, ruler_info->region, v3d, snap_gizmo);
SnapObjectContext *snap_context = ED_gizmotypes_snap_3d_context_ensure(scene, snap_gizmo);
const float mval_fl[2] = {UNPACK2(mval)};
float ray_normal[3];
float ray_start[3];
@ -367,6 +366,8 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
if (ED_transform_snap_object_project_view3d(snap_context,
depsgraph,
ruler_info->region,
v3d,
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
@ -382,6 +383,7 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
madd_v3_v3v3fl(ray_start, co, ray_normal, eps_bias);
ED_transform_snap_object_project_ray(snap_context,
depsgraph,
v3d,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
.edit_mode_type = SNAP_GEOM_CAGE,

View File

@ -423,6 +423,7 @@ static bool walk_floor_distance_get(RegionView3D *rv3d,
ret = ED_transform_snap_object_project_ray(
walk->snap_context,
walk->depsgraph,
walk->v3d,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
/* Avoid having to convert the edit-mesh to a regular mesh. */
@ -464,6 +465,7 @@ static bool walk_ray_cast(RegionView3D *rv3d,
ret = ED_transform_snap_object_project_ray(walk->snap_context,
walk->depsgraph,
walk->v3d,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
},
@ -602,8 +604,7 @@ static bool initWalkInfo(bContext *C, WalkInfo *walk, wmOperator *op)
walk->rv3d->rflag |= RV3D_NAVIGATING;
walk->snap_context = ED_transform_snap_object_context_create_view3d(
walk->scene, 0, walk->region, walk->v3d);
walk->snap_context = ED_transform_snap_object_context_create(walk->scene, 0);
walk->v3d_camera_control = ED_view3d_cameracontrol_acquire(
walk->depsgraph, walk->scene, walk->v3d, walk->rv3d);

View File

@ -306,6 +306,8 @@ static bool idp_snap_normal_from_gizmo(wmGizmo *gz, float r_normal[3])
*/
static bool idp_poject_surface_normal(SnapObjectContext *snap_context,
struct Depsgraph *depsgraph,
ARegion *region,
View3D *v3d,
const float mval_fl[2],
const float mat_fallback[3][3],
const float normal_fallback[3],
@ -320,6 +322,8 @@ static bool idp_poject_surface_normal(SnapObjectContext *snap_context,
if (ED_transform_snap_object_project_view3d_ex(snap_context,
depsgraph,
region,
v3d,
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
@ -885,11 +889,9 @@ static void view3d_interactive_add_calc_plane(bContext *C,
/* Set the orientation. */
if ((plane_orient == PLACE_ORIENT_SURFACE) || (plane_depth == PLACE_DEPTH_SURFACE)) {
snap_context = (snap_gizmo ?
ED_gizmotypes_snap_3d_context_ensure(scene, region, v3d, snap_gizmo) :
NULL);
snap_context = (snap_gizmo ? ED_gizmotypes_snap_3d_context_ensure(scene, snap_gizmo) : NULL);
if (snap_context == NULL) {
snap_context = ED_transform_snap_object_context_create_view3d(scene, 0, region, v3d);
snap_context = ED_transform_snap_object_context_create(scene, 0);
snap_context_free = true;
}
}
@ -908,6 +910,8 @@ static void view3d_interactive_add_calc_plane(bContext *C,
if ((snap_context != NULL) &&
idp_poject_surface_normal(snap_context,
CTX_data_ensure_evaluated_depsgraph(C),
region,
v3d,
mval_fl,
use_normal_fallback ? r_matrix_orient : NULL,
use_normal_fallback ? normal_fallback : NULL,
@ -938,6 +942,8 @@ static void view3d_interactive_add_calc_plane(bContext *C,
if ((snap_context != NULL) &&
ED_transform_snap_object_project_view3d(snap_context,
CTX_data_ensure_evaluated_depsgraph(C),
region,
v3d,
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,

View File

@ -374,6 +374,8 @@ void applyProject(TransInfo *t)
if (ED_transform_snap_object_project_view3d(
t->tsnap.object_context,
t->depsgraph,
t->region,
t->view,
SCE_SNAP_MODE_FACE,
&(const struct SnapObjectParams){
.snap_select = t->tsnap.modeSelect,
@ -671,8 +673,7 @@ static void initSnappingMode(TransInfo *t)
if (t->spacetype == SPACE_VIEW3D) {
if (t->tsnap.object_context == NULL) {
t->tsnap.use_backface_culling = snap_use_backface_culling(t);
t->tsnap.object_context = ED_transform_snap_object_context_create_view3d(
t->scene, 0, t->region, t->view);
t->tsnap.object_context = ED_transform_snap_object_context_create(t->scene, 0);
if (t->data_type == TC_MESH_VERTS) {
/* Ignore elements being transformed. */
@ -1245,6 +1246,8 @@ short snapObjectsTransform(
return ED_transform_snap_object_project_view3d_ex(
t->tsnap.object_context,
t->depsgraph,
t->region,
t->view,
t->settings->snap_mode,
&(const struct SnapObjectParams){
.snap_select = t->tsnap.modeSelect,
@ -1280,6 +1283,8 @@ bool peelObjectsTransform(TransInfo *t,
ED_transform_snap_object_project_all_view3d_ex(
t->tsnap.object_context,
t->depsgraph,
t->region,
t->view,
&(const struct SnapObjectParams){
.snap_select = t->tsnap.modeSelect,
.edit_mode_type = (t->flag & T_EDIT) != 0 ? SNAP_GEOM_EDIT : SNAP_GEOM_FINAL,

File diff suppressed because it is too large Load Diff

View File

@ -154,6 +154,7 @@ static void rna_Scene_ray_cast(Scene *scene,
bool ret = ED_transform_snap_object_project_ray_ex(sctx,
depsgraph,
NULL,
&(const struct SnapObjectParams){
.snap_select = SNAP_ALL,
},