Cleanup: minor refactor to view operator event handling

- Add VIEW_CANCEL event_code.
- De-duplicate operator freeing logic for the roll operator.
- Structure checks so adding cancel is is simplified.
- Split event checks into two blocks, one for model events, another
  for all other events.
This commit is contained in:
Campbell Barton 2022-12-07 16:23:03 +11:00
parent 0e90896cba
commit dcd4eb4c25
7 changed files with 192 additions and 128 deletions

View File

@ -667,35 +667,47 @@ static int image_view_zoom_modal(bContext *C, wmOperator *op, const wmEvent *eve
{
ViewZoomData *vpd = op->customdata;
short event_code = VIEW_PASS;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == TIMER && event->customdata == vpd->timer) {
/* continuous zoom */
/* Execute the events. */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
else if (event->type == TIMER) {
/* Continuous zoom. */
if (event->customdata == vpd->timer) {
event_code = VIEW_APPLY;
}
}
else if (event->type == vpd->launch_event && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
else if (event->type == vpd->launch_event) {
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
}
if (event_code == VIEW_APPLY) {
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
image_zoom_apply(vpd,
op,
event->xy[0],
event->xy[1],
U.viewzoom,
(U.uiflag & USER_ZOOM_INVERT) != 0,
(use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)));
switch (event_code) {
case VIEW_APPLY: {
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
image_zoom_apply(vpd,
op,
event->xy[0],
event->xy[1],
U.viewzoom,
(U.uiflag & USER_ZOOM_INVERT) != 0,
(use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)));
break;
}
case VIEW_CONFIRM: {
ret = OPERATOR_FINISHED;
break;
}
}
else if (event_code == VIEW_CONFIRM) {
if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
image_view_zoom_exit(C, op, false);
return OPERATOR_FINISHED;
}
return OPERATOR_RUNNING_MODAL;
return ret;
}
static void image_view_zoom_cancel(bContext *C, wmOperator *op)

View File

@ -41,6 +41,8 @@ enum {
VIEW_PASS = 0,
VIEW_APPLY,
VIEW_CONFIRM,
/** Only supported by some viewport operators. */
VIEW_CANCEL,
};
/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */

View File

@ -142,11 +142,8 @@ static int viewdolly_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
/* Execute the events. */
if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
@ -161,27 +158,40 @@ static int viewdolly_modal(bContext *C, wmOperator *op, const wmEvent *event)
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewdolly_apply(vod, event->xy, (U.uiflag & USER_ZOOM_INVERT) != 0);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
else {
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == vod->init.event_type) {
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
switch (event_code) {
case VIEW_APPLY: {
viewdolly_apply(vod, event->xy, (U.uiflag & USER_ZOOM_INVERT) != 0);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
break;
}
case VIEW_CONFIRM: {
use_autokey = true;
ret = OPERATOR_FINISHED;
break;
}
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
}
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
}
viewops_data_free(C, vod);
op->customdata = NULL;
}

View File

@ -101,11 +101,8 @@ static int viewmove_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
/* Execute the events. */
if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
@ -120,27 +117,40 @@ static int viewmove_modal(bContext *C, wmOperator *op, const wmEvent *event)
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewmove_apply(vod, event->xy[0], event->xy[1]);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
else {
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == vod->init.event_type) {
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
switch (event_code) {
case VIEW_APPLY: {
viewmove_apply(vod, event->xy[0], event->xy[1]);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
break;
}
case VIEW_CONFIRM: {
use_autokey = true;
ret = OPERATOR_FINISHED;
break;
}
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
}
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
}
viewops_data_free(C, op->customdata);
op->customdata = NULL;
}

View File

@ -87,11 +87,8 @@ static int viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
/* Execute the events. */
if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
@ -106,40 +103,51 @@ static int viewroll_modal(bContext *C, wmOperator *op, const wmEvent *event)
break;
}
}
else if (event->type == vod->init.event_type) {
/* Check `vod->init.event_type` first in case RMB was used to invoke.
* in this case confirming takes precedence over canceling, see: T102937. */
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
else {
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
}
else if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
if (event->val == KM_PRESS) {
/* Note this does not remove auto-keys on locked cameras. */
copy_qt_qt(vod->rv3d->viewquat, vod->init.quat);
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, vod->rv3d);
viewops_data_free(C, op->customdata);
op->customdata = NULL;
return OPERATOR_CANCELLED;
else if (event->type == vod->init.event_type) {
/* Check `vod->init.event_type` first in case RMB was used to invoke.
* in this case confirming takes precedence over canceling, see: T102937. */
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
}
else if (ELEM(event->type, EVT_ESCKEY, RIGHTMOUSE)) {
if (event->val == KM_PRESS) {
event_code = VIEW_CANCEL;
}
}
}
if (event_code == VIEW_APPLY) {
viewroll_apply(vod, event->xy[0], event->xy[1]);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
switch (event_code) {
case VIEW_APPLY: {
viewroll_apply(vod, event->xy[0], event->xy[1]);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
break;
}
case VIEW_CONFIRM: {
use_autokey = true;
ret = OPERATOR_FINISHED;
break;
}
case VIEW_CANCEL: {
/* Note this does not remove auto-keys on locked cameras. */
copy_qt_qt(vod->rv3d->viewquat, vod->init.quat);
ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, vod->rv3d);
ret = OPERATOR_CANCELLED;
break;
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, false);
}
if (ret & OPERATOR_FINISHED) {
if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
viewops_data_free(C, op->customdata);
op->customdata = NULL;
}

View File

@ -327,11 +327,8 @@ static int viewrotate_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
/* Execute the events. */
if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
@ -355,27 +352,40 @@ static int viewrotate_modal(bContext *C, wmOperator *op, const wmEvent *event)
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
viewrotate_apply(vod, event->xy);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
else {
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == vod->init.event_type) {
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
switch (event_code) {
case VIEW_APPLY: {
viewrotate_apply(vod, event->xy);
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
break;
}
case VIEW_CONFIRM: {
use_autokey = true;
ret = OPERATOR_FINISHED;
break;
}
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, true);
}
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
}
viewops_data_free(C, op->customdata);
op->customdata = NULL;
}

View File

@ -377,15 +377,8 @@ static int viewzoom_modal(bContext *C, wmOperator *op, const wmEvent *event)
bool use_autokey = false;
int ret = OPERATOR_RUNNING_MODAL;
/* execute the events */
if (event->type == TIMER && event->customdata == vod->timer) {
/* continuous zoom */
event_code = VIEW_APPLY;
}
else if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == EVT_MODAL_MAP) {
/* Execute the events. */
if (event->type == EVT_MODAL_MAP) {
switch (event->val) {
case VIEW_MODAL_CONFIRM:
event_code = VIEW_CONFIRM;
@ -400,32 +393,51 @@ static int viewzoom_modal(bContext *C, wmOperator *op, const wmEvent *event)
break;
}
}
else if (event->type == vod->init.event_type && event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
if (event_code == VIEW_APPLY) {
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
viewzoom_apply(vod,
event->xy,
(eViewZoom_Style)U.viewzoom,
(U.uiflag & USER_ZOOM_INVERT) != 0,
(use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)));
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
else {
if (event->type == MOUSEMOVE) {
event_code = VIEW_APPLY;
}
else if (event->type == TIMER) {
if (event->customdata == vod->timer) {
/* Continuous zoom. */
event_code = VIEW_APPLY;
}
}
else if (event->type == vod->init.event_type) {
if (event->val == KM_RELEASE) {
event_code = VIEW_CONFIRM;
}
}
}
else if (event_code == VIEW_CONFIRM) {
use_autokey = true;
ret = OPERATOR_FINISHED;
switch (event_code) {
case VIEW_APPLY: {
const bool use_cursor_init = RNA_boolean_get(op->ptr, "use_cursor_init");
viewzoom_apply(vod,
event->xy,
(eViewZoom_Style)U.viewzoom,
(U.uiflag & USER_ZOOM_INVERT) != 0,
(use_cursor_init && (U.uiflag & USER_ZOOM_TO_MOUSEPOS)));
if (ED_screen_animation_playing(CTX_wm_manager(C))) {
use_autokey = true;
}
break;
}
case VIEW_CONFIRM: {
use_autokey = true;
ret = OPERATOR_FINISHED;
break;
}
}
if (use_autokey) {
ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, false, true);
}
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
if ((ret & OPERATOR_RUNNING_MODAL) == 0) {
if (ret & OPERATOR_FINISHED) {
ED_view3d_camera_lock_undo_push(op->type->name, vod->v3d, vod->rv3d, C);
}
viewops_data_free(C, op->customdata);
op->customdata = NULL;
}