Merge branch 'master' into blender2.8
[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         Object *ob = CTX_data_active_object(C);
66
67         return (ob && ob->pd && ob->pd->forcefield);
68 }
69
70 static void WIDGETGROUP_forcefield_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
71 {
72         /* only wind effector for now */
73         wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
74         gzgroup->customdata = wwrapper;
75
76         wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
77         wmGizmo *gz = wwrapper->gizmo;
78         RNA_enum_set(gz->ptr, "transform",  ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED);
79         ED_gizmo_arrow3d_set_ui_range(gz, -200.0f, 200.0f);
80         ED_gizmo_arrow3d_set_range_fac(gz, 6.0f);
81
82         UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
83         UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
84 }
85
86 static void WIDGETGROUP_forcefield_refresh(const bContext *C, wmGizmoGroup *gzgroup)
87 {
88         wmGizmoWrapper *wwrapper = gzgroup->customdata;
89         wmGizmo *gz = wwrapper->gizmo;
90         Object *ob = CTX_data_active_object(C);
91         PartDeflect *pd = ob->pd;
92
93         if (pd->forcefield == PFIELD_WIND) {
94                 const float size = (ob->type == OB_EMPTY) ? ob->empty_drawsize : 1.0f;
95                 const float ofs[3] = {0.0f, -size, 0.0f};
96                 PointerRNA field_ptr;
97
98                 RNA_pointer_create(&ob->id, &RNA_FieldSettings, pd, &field_ptr);
99                 WM_gizmo_set_matrix_location(gz, ob->obmat[3]);
100                 WM_gizmo_set_matrix_rotation_from_z_axis(gz, ob->obmat[2]);
101                 WM_gizmo_set_matrix_offset_location(gz, ofs);
102                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
103                 WM_gizmo_target_property_def_rna(gz, "offset", &field_ptr, "strength", -1);
104         }
105         else {
106                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, true);
107         }
108 }
109
110 void VIEW3D_GGT_force_field(wmGizmoGroupType *gzgt)
111 {
112         gzgt->name = "Force Field Widgets";
113         gzgt->idname = "VIEW3D_GGT_force_field";
114
115         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
116                       WM_GIZMOGROUPTYPE_3D |
117                       WM_GIZMOGROUPTYPE_SCALE |
118                       WM_GIZMOGROUPTYPE_DEPTH_3D);
119
120         gzgt->poll = WIDGETGROUP_forcefield_poll;
121         gzgt->setup = WIDGETGROUP_forcefield_setup;
122         gzgt->refresh = WIDGETGROUP_forcefield_refresh;
123 }
124
125 /** \} */