Gizmo: de-duplicate poll logic

Checking the active tool or operator was a common way to check
if the gizmo was still in use.
This commit is contained in:
2018-09-18 16:10:07 +10:00
parent 0a3b66cfb5
commit d421adb83e
10 changed files with 130 additions and 98 deletions

View File

@@ -0,0 +1,42 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file ED_gizmo_utils.h
* \ingroup editors
*
* \name Generic Gizmo Utilities.
*/
#ifndef __ED_GIZMO_UTILS_H__
#define __ED_GIZMO_UTILS_H__
struct bContext;
struct wmGizmoGroupType;
/** Wrapper function (operator name can't be guessed). */
bool ED_gizmo_poll_or_unlink_delayed_from_operator(
const struct bContext *C, struct wmGizmoGroupType *gzgt,
const char *idname);
/** Use this as poll function directly for: #wmGizmoGroupType.poll */
bool ED_gizmo_poll_or_unlink_delayed_from_tool(
const bContext *C, struct wmGizmoGroupType *gzgt);
#endif /* __ED_GIZMO_UTILS_H__ */

View File

@@ -35,6 +35,7 @@
#include "BKE_editmesh.h" #include "BKE_editmesh.h"
#include "ED_gizmo_library.h" #include "ED_gizmo_library.h"
#include "ED_gizmo_utils.h"
#include "ED_mesh.h" #include "ED_mesh.h"
#include "ED_object.h" #include "ED_object.h"
#include "ED_screen.h" #include "ED_screen.h"
@@ -199,12 +200,7 @@ static void gizmo_placement_prop_matrix_set(
static bool gizmo_mesh_placement_poll(const bContext *C, wmGizmoGroupType *gzgt) static bool gizmo_mesh_placement_poll(const bContext *C, wmGizmoGroupType *gzgt)
{ {
wmOperator *op = WM_operator_last_redo(C); return ED_gizmo_poll_or_unlink_delayed_from_operator(C, gzgt, "MESH_OT_primitive_cube_add_gizmo");
if (op == NULL || !STREQ(op->type->idname, "MESH_OT_primitive_cube_add_gizmo")) {
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
} }
static void gizmo_mesh_placement_modal_from_setup( static void gizmo_mesh_placement_modal_from_setup(

View File

@@ -50,6 +50,7 @@
#include "ED_mesh.h" #include "ED_mesh.h"
#include "ED_screen.h" #include "ED_screen.h"
#include "ED_view3d.h" #include "ED_view3d.h"
#include "ED_gizmo_utils.h"
#include "UI_resources.h" #include "UI_resources.h"
@@ -642,12 +643,7 @@ static void gizmo_bisect_prop_angle_set(
static bool gizmo_mesh_bisect_poll(const bContext *C, wmGizmoGroupType *gzgt) static bool gizmo_mesh_bisect_poll(const bContext *C, wmGizmoGroupType *gzgt)
{ {
wmOperator *op = WM_operator_last_redo(C); return ED_gizmo_poll_or_unlink_delayed_from_operator(C, gzgt, "MESH_OT_bisect");
if (op == NULL || !STREQ(op->type->idname, "MESH_OT_bisect")) {
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
} }
static void gizmo_mesh_bisect_setup(const bContext *C, wmGizmoGroup *gzgroup) static void gizmo_mesh_bisect_setup(const bContext *C, wmGizmoGroup *gzgroup)

View File

@@ -54,6 +54,7 @@
#include "ED_transform.h" #include "ED_transform.h"
#include "ED_view3d.h" #include "ED_view3d.h"
#include "ED_gizmo_library.h" #include "ED_gizmo_library.h"
#include "ED_gizmo_utils.h"
#include "UI_resources.h" #include "UI_resources.h"
@@ -417,20 +418,6 @@ static void gizmo_mesh_extrude_orientation_matrix_set(
} }
} }
static bool gizmo_mesh_extrude_poll(const bContext *C, wmGizmoGroupType *gzgt)
{
ScrArea *sa = CTX_wm_area(C);
bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group) ||
!ED_operator_editmesh_view3d((bContext *)C))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}
static void gizmo_mesh_extrude_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup) static void gizmo_mesh_extrude_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
{ {
struct GizmoExtrudeGroup *ggd = MEM_callocN(sizeof(GizmoExtrudeGroup), __func__); struct GizmoExtrudeGroup *ggd = MEM_callocN(sizeof(GizmoExtrudeGroup), __func__);
@@ -687,7 +674,7 @@ static void MESH_GGT_extrude(struct wmGizmoGroupType *gzgt)
gzgt->gzmap_params.spaceid = SPACE_VIEW3D; gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW; gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
gzgt->poll = gizmo_mesh_extrude_poll; gzgt->poll = ED_gizmo_poll_or_unlink_delayed_from_tool;
gzgt->setup = gizmo_mesh_extrude_setup; gzgt->setup = gizmo_mesh_extrude_setup;
gzgt->refresh = gizmo_mesh_extrude_refresh; gzgt->refresh = gizmo_mesh_extrude_refresh;
gzgt->draw_prepare = gizmo_mesh_extrude_draw_prepare; gzgt->draw_prepare = gizmo_mesh_extrude_draw_prepare;

View File

@@ -32,6 +32,7 @@
#include "WM_types.h" #include "WM_types.h"
#include "WM_message.h" #include "WM_message.h"
#include "ED_gizmo_utils.h"
#include "ED_screen.h" #include "ED_screen.h"
#include "ED_view3d.h" #include "ED_view3d.h"
@@ -67,20 +68,6 @@ typedef struct GizmoGroupData_SpinInit {
} data; } data;
} GizmoGroupData_SpinInit; } GizmoGroupData_SpinInit;
static bool gizmo_mesh_spin_init_poll(const bContext *C, wmGizmoGroupType *gzgt)
{
ScrArea *sa = CTX_wm_area(C);
bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group) ||
!ED_operator_editmesh_view3d((bContext *)C))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}
static void gizmo_mesh_spin_init_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup) static void gizmo_mesh_spin_init_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
{ {
/* alpha values for normal/highlighted states */ /* alpha values for normal/highlighted states */
@@ -241,7 +228,7 @@ void MESH_GGT_spin(struct wmGizmoGroupType *gzgt)
gzgt->gzmap_params.spaceid = SPACE_VIEW3D; gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW; gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
gzgt->poll = gizmo_mesh_spin_init_poll; gzgt->poll = ED_gizmo_poll_or_unlink_delayed_from_tool;
gzgt->setup = gizmo_mesh_spin_init_setup; gzgt->setup = gizmo_mesh_spin_init_setup;
gzgt->refresh = gizmo_mesh_spin_init_refresh; gzgt->refresh = gizmo_mesh_spin_init_refresh;
gzgt->message_subscribe = gizmo_mesh_spin_init_message_subscribe; gzgt->message_subscribe = gizmo_mesh_spin_init_message_subscribe;
@@ -506,12 +493,7 @@ static void gizmo_spin_prop_angle_set(
static bool gizmo_mesh_spin_redo_poll(const bContext *C, wmGizmoGroupType *gzgt) static bool gizmo_mesh_spin_redo_poll(const bContext *C, wmGizmoGroupType *gzgt)
{ {
wmOperator *op = WM_operator_last_redo(C); return ED_gizmo_poll_or_unlink_delayed_from_operator(C, gzgt, "MESH_OT_spin");
if (op == NULL || !STREQ(op->type->idname, "MESH_OT_spin")) {
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
} }

View File

@@ -30,6 +30,7 @@
#include "BKE_context.h" #include "BKE_context.h"
#include "ED_gizmo_utils.h"
#include "ED_screen.h" #include "ED_screen.h"
#include "UI_resources.h" #include "UI_resources.h"
@@ -49,18 +50,6 @@ struct GizmoGroupPreSelElem {
wmGizmo *gizmo; wmGizmo *gizmo;
}; };
static bool WIDGETGROUP_mesh_preselect_elem_poll(const bContext *C, wmGizmoGroupType *gzgt)
{
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C);
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}
static void WIDGETGROUP_mesh_preselect_elem_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup) static void WIDGETGROUP_mesh_preselect_elem_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
{ {
const wmGizmoType *gzt_presel = WM_gizmotype_find("GIZMO_GT_mesh_preselect_elem_3d", true); const wmGizmoType *gzt_presel = WM_gizmotype_find("GIZMO_GT_mesh_preselect_elem_3d", true);
@@ -82,7 +71,7 @@ void VIEW3D_GGT_mesh_preselect_elem(wmGizmoGroupType *gzgt)
gzgt->gzmap_params.spaceid = SPACE_VIEW3D; gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW; gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
gzgt->poll = WIDGETGROUP_mesh_preselect_elem_poll; gzgt->poll = ED_gizmo_poll_or_unlink_delayed_from_tool;
gzgt->setup = WIDGETGROUP_mesh_preselect_elem_setup; gzgt->setup = WIDGETGROUP_mesh_preselect_elem_setup;
} }
@@ -97,18 +86,6 @@ struct GizmoGroupPreSelEdgeRing {
wmGizmo *gizmo; wmGizmo *gizmo;
}; };
static bool WIDGETGROUP_mesh_preselect_edgering_poll(const bContext *C, wmGizmoGroupType *gzgt)
{
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C);
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}
static void WIDGETGROUP_mesh_preselect_edgering_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup) static void WIDGETGROUP_mesh_preselect_edgering_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
{ {
const wmGizmoType *gzt_presel = WM_gizmotype_find("GIZMO_GT_mesh_preselect_edgering_3d", true); const wmGizmoType *gzt_presel = WM_gizmotype_find("GIZMO_GT_mesh_preselect_edgering_3d", true);
@@ -130,7 +107,7 @@ void VIEW3D_GGT_mesh_preselect_edgering(wmGizmoGroupType *gzgt)
gzgt->gzmap_params.spaceid = SPACE_VIEW3D; gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW; gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
gzgt->poll = WIDGETGROUP_mesh_preselect_edgering_poll; gzgt->poll = ED_gizmo_poll_or_unlink_delayed_from_tool;
gzgt->setup = WIDGETGROUP_mesh_preselect_edgering_setup; gzgt->setup = WIDGETGROUP_mesh_preselect_edgering_setup;
} }

View File

@@ -45,6 +45,7 @@
#include "BIF_gl.h" #include "BIF_gl.h"
#include "ED_gizmo_utils.h"
#include "ED_gpencil.h" #include "ED_gpencil.h"
#include "ED_screen.h" #include "ED_screen.h"
#include "ED_transform_snap_object_context.h" #include "ED_transform_snap_object_context.h"
@@ -975,18 +976,6 @@ void VIEW3D_GT_ruler_item(wmGizmoType *gzt)
/** \name Ruler Gizmo Group /** \name Ruler Gizmo Group
* \{ */ * \{ */
static bool WIDGETGROUP_ruler_poll(const bContext *C, wmGizmoGroupType *gzgt)
{
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C);
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}
static void WIDGETGROUP_ruler_setup(const bContext *C, wmGizmoGroup *gzgroup) static void WIDGETGROUP_ruler_setup(const bContext *C, wmGizmoGroup *gzgroup)
{ {
RulerInfo *ruler_info = MEM_callocN(sizeof(RulerInfo), __func__); RulerInfo *ruler_info = MEM_callocN(sizeof(RulerInfo), __func__);
@@ -1015,7 +1004,7 @@ void VIEW3D_GGT_ruler(wmGizmoGroupType *gzgt)
gzgt->gzmap_params.spaceid = SPACE_VIEW3D; gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW; gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
gzgt->poll = WIDGETGROUP_ruler_poll; gzgt->poll = ED_gizmo_poll_or_unlink_delayed_from_tool;
gzgt->setup = WIDGETGROUP_ruler_setup; gzgt->setup = WIDGETGROUP_ruler_setup;
} }

View File

@@ -76,6 +76,7 @@
#include "ED_gpencil.h" #include "ED_gpencil.h"
#include "ED_screen.h" #include "ED_screen.h"
#include "ED_gizmo_library.h" #include "ED_gizmo_library.h"
#include "ED_gizmo_utils.h"
#include "UI_resources.h" #include "UI_resources.h"
@@ -1669,15 +1670,9 @@ static void WIDGETGROUP_gizmo_draw_prepare(const bContext *C, wmGizmoGroup *gzgr
static bool WIDGETGROUP_gizmo_poll(const struct bContext *C, struct wmGizmoGroupType *gzgt) static bool WIDGETGROUP_gizmo_poll(const struct bContext *C, struct wmGizmoGroupType *gzgt)
{ {
/* it's a given we only use this in 3D view */ if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C);
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false; return false;
} }
View3D *v3d = CTX_wm_view3d(C); View3D *v3d = CTX_wm_view3d(C);
if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) { if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
return false; return false;
@@ -1719,9 +1714,11 @@ struct XFormCageWidgetGroup {
static bool WIDGETGROUP_xform_cage_poll(const bContext *C, wmGizmoGroupType *gzgt) static bool WIDGETGROUP_xform_cage_poll(const bContext *C, wmGizmoGroupType *gzgt)
{ {
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C); if (!ED_gizmo_poll_or_unlink_delayed_from_tool(C, gzgt)) {
if (!STREQ(gzgt->idname, tref_rt->gizmo_group)) { return false;
WM_gizmo_group_type_unlink_delayed_ptr(gzgt); }
View3D *v3d = CTX_wm_view3d(C);
if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_TOOL)) {
return false; return false;
} }
return true; return true;

View File

@@ -40,9 +40,10 @@ set(INC_SYS
) )
set(SRC set(SRC
keymap_templates.c
ed_transverts.c ed_transverts.c
ed_util.c ed_util.c
gizmo_utils.c
keymap_templates.c
numinput.c numinput.c
select_utils.c select_utils.c
@@ -57,6 +58,7 @@ set(SRC
../include/ED_datafiles.h ../include/ED_datafiles.h
../include/ED_fileselect.h ../include/ED_fileselect.h
../include/ED_gizmo_library.h ../include/ED_gizmo_library.h
../include/ED_gizmo_utils.h
../include/ED_gpencil.h ../include/ED_gpencil.h
../include/ED_image.h ../include/ED_image.h
../include/ED_info.h ../include/ED_info.h

View File

@@ -0,0 +1,64 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file gizmo_utils.c
* \ingroup edutil
*
* \name Generic Gizmo Utilities.
*/
#include <string.h>
#include "BLI_utildefines.h"
#include "BKE_context.h"
#include "DNA_workspace_types.h"
#include "WM_types.h"
#include "WM_api.h"
#include "WM_toolsystem.h"
#include "ED_gizmo_utils.h"
bool ED_gizmo_poll_or_unlink_delayed_from_operator(
const bContext *C, wmGizmoGroupType *gzgt,
const char *idname)
{
wmOperator *op = WM_operator_last_redo(C);
if (op == NULL || !STREQ(op->type->idname, idname)) {
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}
/** Can use this as poll function directly. */
bool ED_gizmo_poll_or_unlink_delayed_from_tool(const bContext *C, wmGizmoGroupType *gzgt)
{
bToolRef_Runtime *tref_rt = WM_toolsystem_runtime_from_context((bContext *)C);
if ((tref_rt == NULL) ||
!STREQ(gzgt->idname, tref_rt->gizmo_group))
{
WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
return false;
}
return true;
}