Fix gizmos showing for non-visible objects
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_forcefield.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/space_view3d/view3d_gizmo_forcefield.c
22  *  \ingroup spview3d
23  */
24
25
26 #include "BLI_blenlib.h"
27 #include "BLI_math.h"
28 #include "BLI_utildefines.h"
29
30 #include "BKE_context.h"
31 #include "BKE_object.h"
32
33 #include "DNA_object_types.h"
34 #include "DNA_object_force_types.h"
35
36 #include "ED_screen.h"
37 #include "ED_gizmo_library.h"
38
39 #include "UI_resources.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "RNA_access.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "view3d_intern.h"  /* own include */
49
50 /* -------------------------------------------------------------------- */
51
52 /** \name Force Field Gizmos
53  * \{ */
54
55 static bool WIDGETGROUP_forcefield_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
56 {
57         View3D *v3d = CTX_wm_view3d(C);
58
59         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
60             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
61         {
62                 return false;
63         }
64
65         ViewLayer *view_layer = CTX_data_view_layer(C);
66         Base *base = BASACT(view_layer);
67         if (base && BASE_VISIBLE(v3d, base)) {
68                 Object *ob = base->object;
69                 if (ob->pd && ob->pd->forcefield) {
70                         return true;
71                 }
72         }
73         return false;
74 }
75
76 static void WIDGETGROUP_forcefield_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
77 {
78         /* only wind effector for now */
79         wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
80         gzgroup->customdata = wwrapper;
81
82         wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
83         wmGizmo *gz = wwrapper->gizmo;
84         RNA_enum_set(gz->ptr, "transform",  ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED);
85         ED_gizmo_arrow3d_set_ui_range(gz, -200.0f, 200.0f);
86         ED_gizmo_arrow3d_set_range_fac(gz, 6.0f);
87
88         UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
89         UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
90 }
91
92 static void WIDGETGROUP_forcefield_refresh(const bContext *C, wmGizmoGroup *gzgroup)
93 {
94         wmGizmoWrapper *wwrapper = gzgroup->customdata;
95         wmGizmo *gz = wwrapper->gizmo;
96         ViewLayer *view_layer = CTX_data_view_layer(C);
97         Object *ob = OBACT(view_layer);
98         PartDeflect *pd = ob->pd;
99
100         if (pd->forcefield == PFIELD_WIND) {
101                 const float size = (ob->type == OB_EMPTY) ? ob->empty_drawsize : 1.0f;
102                 const float ofs[3] = {0.0f, -size, 0.0f};
103                 PointerRNA field_ptr;
104
105                 RNA_pointer_create(&ob->id, &RNA_FieldSettings, pd, &field_ptr);
106                 WM_gizmo_set_matrix_location(gz, ob->obmat[3]);
107                 WM_gizmo_set_matrix_rotation_from_z_axis(gz, ob->obmat[2]);
108                 WM_gizmo_set_matrix_offset_location(gz, ofs);
109                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
110                 WM_gizmo_target_property_def_rna(gz, "offset", &field_ptr, "strength", -1);
111         }
112         else {
113                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
114         }
115 }
116
117 void VIEW3D_GGT_force_field(wmGizmoGroupType *gzgt)
118 {
119         gzgt->name = "Force Field Widgets";
120         gzgt->idname = "VIEW3D_GGT_force_field";
121
122         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
123                       WM_GIZMOGROUPTYPE_3D |
124                       WM_GIZMOGROUPTYPE_SCALE |
125                       WM_GIZMOGROUPTYPE_DEPTH_3D);
126
127         gzgt->poll = WIDGETGROUP_forcefield_poll;
128         gzgt->setup = WIDGETGROUP_forcefield_setup;
129         gzgt->refresh = WIDGETGROUP_forcefield_refresh;
130 }
131
132 /** \} */