Transform: use screen mouse offset position for navigation

In the 3D View, after navigating, the initial cursor position was
updated based on the relative position of the cursor in 3D space.

Now the initial relative position is maintained but moved closer to or
further from the 2d center based on zoom.
This commit is contained in:
Germano Cavalcante
2023-07-25 10:53:08 -03:00
parent d59188e9cf
commit 5ed14e9269
6 changed files with 51 additions and 66 deletions

View File

@@ -62,6 +62,8 @@
* and being able to set it to zero is handy. */
/* #define USE_NUM_NO_ZERO */
using namespace blender;
bool transdata_check_local_islands(TransInfo *t, short around)
{
if (t->options & (CTX_CURSOR | CTX_TEXTURE_SPACE)) {
@@ -2021,12 +2023,12 @@ bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *eve
}
}
int mval[2];
float2 mval;
if (t->flag & T_EVENT_DRAG_START) {
WM_event_drag_start_mval(event, t->region, mval);
WM_event_drag_start_mval_fl(event, t->region, mval);
}
else {
copy_v2_v2_int(mval, event->mval);
mval = float2(event->mval);
}
initMouseInput(t, &t->mouse, t->center2d, mval, use_accurate);
}
@@ -2059,7 +2061,7 @@ bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *eve
* values. */
if (t->flag & T_MODAL) {
/* Setup the mouse input with initial values. */
applyMouseInput(t, &t->mouse, t->mouse.imval, t->values);
applyMouseInput(t, &t->mouse, int2(t->mouse.imval), t->values);
}
if ((prop = RNA_struct_find_property(op->ptr, "preserve_clnor"))) {

View File

@@ -8,6 +8,8 @@
#pragma once
#include "BLI_math_vector_types.hh"
#include "ED_numinput.h"
#include "ED_transform.h"
#include "ED_view3d.h"
@@ -364,9 +366,8 @@ struct MouseInput {
void (*post)(TransInfo *t, float values[3]);
/** Initial mouse position. */
int imval[2];
float imval_unproj[3];
float center[2];
blender::float2 imval;
blender::float2 center;
float factor;
float precision_factor;
bool precision;
@@ -742,16 +743,19 @@ enum MouseInputMode {
INPUT_CUSTOM_RATIO_FLIP,
};
void initMouseInput(
TransInfo *t, MouseInput *mi, const float center[2], const int mval[2], bool precision);
void initMouseInput(TransInfo *t,
MouseInput *mi,
const blender::float2 &center,
const blender::float2 &mval,
bool precision);
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode);
void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float output[3]);
void applyMouseInput(TransInfo *t, MouseInput *mi, const blender::int2 &mval, float output[3]);
void transform_input_update(TransInfo *t, const float fac);
void transform_input_virtual_mval_reset(TransInfo *t);
void transform_input_reset(TransInfo *t, const int mval[2]);
void transform_input_reset(TransInfo *t, const blender::float2 &mval);
void setCustomPoints(TransInfo *t, MouseInput *mi, const int start[2], const int end[2]);
void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const float dir[2]);
void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const blender::float2 &dir);
void setInputPostFct(MouseInput *mi, void (*post)(TransInfo *t, float values[3]));
/** \} */

View File

@@ -55,6 +55,8 @@
#include "transform_orientations.hh"
#include "transform_snap.hh"
using namespace blender;
/* ************************** GENERICS **************************** */
void resetTransModal(TransInfo *t)
@@ -171,20 +173,20 @@ void initTransInfo(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *eve
t->redraw = TREDRAW_HARD; /* redraw first time */
int mval[2];
float2 mval;
if (event) {
if (t->flag & T_EVENT_DRAG_START) {
WM_event_drag_start_mval(event, region, mval);
WM_event_drag_start_mval_fl(event, region, mval);
}
else {
copy_v2_v2_int(mval, event->mval);
mval = float2(event->mval);
}
}
else {
zero_v2_int(mval);
mval = float2(0, 0);
}
copy_v2_v2_int(t->mval, mval);
copy_v2_v2_int(t->mouse.imval, mval);
t->mouse.imval = mval;
t->mode_info = nullptr;

View File

@@ -54,6 +54,8 @@
#include "transform_gizmo.hh"
#include "transform_snap.hh"
using namespace blender;
static wmGizmoGroupType *g_GGT_xform_gizmo = nullptr;
static wmGizmoGroupType *g_GGT_xform_gizmo_context = nullptr;
@@ -509,12 +511,10 @@ static int gizmo_3d_foreach_selected(const bContext *C,
const short orient_index,
const bool use_curve_handles,
const bool use_only_center,
blender::FunctionRef<void(const blender::float3 &)> user_fn,
FunctionRef<void(const float3 &)> user_fn,
const float (**r_mat)[4],
short *r_drawflags)
{
using namespace blender;
const auto run_coord_with_matrix =
[&](const float co[3], const bool use_matrix, const float matrix[4][4]) {
float co_world[3];
@@ -983,9 +983,7 @@ int ED_transform_calc_gizmo_stats(const bContext *C,
copy_m4_m4(tbounds->matrix_space, ob->object_to_world);
}
const auto gizmo_3d_tbounds_calc_fn = [&](const blender::float3 &co) {
calc_tw_center(tbounds, co);
};
const auto gizmo_3d_tbounds_calc_fn = [&](const float3 &co) { calc_tw_center(tbounds, co); };
totsel = gizmo_3d_foreach_selected(C,
orient_index,
@@ -1086,9 +1084,7 @@ static bool gizmo_3d_calc_pos(const bContext *C,
}
float co_sum[3] = {0.0f, 0.0f, 0.0f};
const auto gizmo_3d_calc_center_fn = [&](const blender::float3 &co) {
add_v3_v3(co_sum, co);
};
const auto gizmo_3d_calc_center_fn = [&](const float3 &co) { add_v3_v3(co_sum, co); };
const float(*r_mat)[4] = nullptr;
int totsel;
totsel = gizmo_3d_foreach_selected(C,
@@ -2420,7 +2416,7 @@ void transform_gizmo_3d_model_from_constraint_and_mode_set(TransInfo *t)
wmEvent event = {nullptr};
/* Set the initial mouse value. Used for rotation gizmos. */
copy_v2_v2_int(event.mval, t->mouse.imval);
copy_v2_v2_int(event.mval, int2(t->mouse.imval));
/* We need to update the position of the gizmo before invoking otherwise
* #wmGizmo::scale_final could be calculated wrong. */

View File

@@ -15,7 +15,6 @@
#include "BKE_context.h"
#include "BLI_math.h"
#include "BLI_math_vector_types.hh"
#include "BLI_utildefines.h"
#include "WM_api.h"
@@ -26,6 +25,8 @@
#include "MEM_guardedalloc.h"
using namespace blender;
/* -------------------------------------------------------------------- */
/** \name Callbacks for #MouseInput.apply
* \{ */
@@ -233,17 +234,14 @@ void setCustomPoints(TransInfo * /*t*/,
data[3] = mval_end[1];
}
void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const float dir[2])
void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const float2 &dir)
{
BLI_ASSERT_UNIT_V2(dir);
const int win_axis =
t->region ? ((abs(int(t->region->winx * dir[0])) + abs(int(t->region->winy * dir[1]))) / 2) :
1;
const int mval_start[2] = {
int(mi->imval[0] + dir[0] * win_axis),
int(mi->imval[1] + dir[1] * win_axis),
};
const int mval_end[2] = {mi->imval[0], mi->imval[1]};
const int2 mval_start = int2(mi->imval + dir * win_axis);
const int2 mval_end = int2(mi->imval);
setCustomPoints(t, mi, mval_start, mval_end);
}
@@ -253,18 +251,11 @@ void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const float dir[
/** \name Setup & Handle Mouse Input
* \{ */
void transform_input_reset(TransInfo *t, const int mval[2])
void transform_input_reset(TransInfo *t, const float2 &mval)
{
MouseInput *mi = &t->mouse;
mi->imval[0] = mval[0];
mi->imval[1] = mval[1];
if ((t->spacetype == SPACE_VIEW3D) && (t->region->regiontype == RGN_TYPE_WINDOW)) {
float delta[3] = {mval[0] - mi->center[0], mval[1] - mi->center[1]};
ED_view3d_win_to_delta(t->region, delta, t->zfac, delta);
add_v3_v3v3(mi->imval_unproj, t->center_global, delta);
}
mi->imval = mval;
if (mi->data && ELEM(mi->apply, InputAngle, InputAngleSpring)) {
InputAngle_Data *data = static_cast<InputAngle_Data *>(mi->data);
@@ -275,13 +266,12 @@ void transform_input_reset(TransInfo *t, const int mval[2])
}
void initMouseInput(
TransInfo *t, MouseInput *mi, const float center[2], const int mval[2], const bool precision)
TransInfo *t, MouseInput *mi, const float2 &center, const float2 &mval, const bool precision)
{
mi->factor = 0;
mi->precision = precision;
mi->center[0] = center[0];
mi->center[1] = center[1];
mi->center = center;
mi->post = nullptr;
@@ -422,7 +412,7 @@ void setInputPostFct(MouseInput *mi, void (*post)(TransInfo *t, float values[3])
mi->post = post;
}
void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float output[3])
void applyMouseInput(TransInfo *t, MouseInput *mi, const int2 &mval, float output[3])
{
double mval_db[2];
@@ -464,20 +454,9 @@ void applyMouseInput(TransInfo *t, MouseInput *mi, const int mval[2], float outp
void transform_input_update(TransInfo *t, const float fac)
{
MouseInput *mi = &t->mouse;
t->mouse.factor *= fac;
if ((t->spacetype == SPACE_VIEW3D) && (t->region->regiontype == RGN_TYPE_WINDOW)) {
projectIntView(t, mi->imval_unproj, mi->imval);
}
else {
int offset[2], center_2d_int[2] = {int(mi->center[0]), int(mi->center[1])};
sub_v2_v2v2_int(offset, mi->imval, center_2d_int);
offset[0] *= fac;
offset[1] *= fac;
center_2d_int[0] = t->center2d[0];
center_2d_int[1] = t->center2d[1];
add_v2_v2v2_int(mi->imval, center_2d_int, offset);
}
float2 offset = fac * (mi->imval - mi->center);
mi->imval = t->center2d + offset;
mi->factor *= fac;
float center_old[2];
copy_v2_v2(center_old, mi->center);

View File

@@ -27,6 +27,8 @@
#define RESET_TRANSFORMATION
#define REMOVE_GIZMO
using namespace blender;
/* -------------------------------------------------------------------- */
/** \name Transform Element
* \{ */
@@ -79,7 +81,7 @@ static void snapsource_confirm(TransInfo *t)
SnapSouceCustomData *customdata = static_cast<SnapSouceCustomData *>(t->custom.mode.data);
t->tsnap.mode = customdata->snap_mode_confirm;
int mval[2];
float2 mval;
#ifndef RESET_TRANSFORMATION
if (true) {
if (t->transform_matrix) {
@@ -97,12 +99,12 @@ static void snapsource_confirm(TransInfo *t)
sub_v3_v3(t->tsnap.snap_source, t->vec);
}
projectIntView(t, t->tsnap.snap_source, mval);
projectFloatView(t, t->tsnap.snap_source, mval);
}
else
#endif
{
copy_v2_v2_int(mval, t->mval);
mval = float2(t->mval);
}
snapsource_end(t);
@@ -221,7 +223,7 @@ void transform_mode_snap_source_init(TransInfo *t, wmOperator * /*op*/)
transform_snap_flag_from_modifiers_set(t);
/* Reset initial values to restore gizmo position. */
applyMouseInput(t, &t->mouse, t->mouse.imval, t->values_final);
applyMouseInput(t, &t->mouse, int2(t->mouse.imval), t->values_final);
#endif
#ifdef REMOVE_GIZMO