Outliner: Add object(s) to new collection
[blender.git] / source / blender / editors / space_view3d / view3d_manipulator_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_manipulator_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.h"
35
36 #include "ED_screen.h"
37 #include "ED_manipulator_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 Manipulators
53  * \{ */
54
55 static bool WIDGETGROUP_forcefield_poll(const bContext *C, wmManipulatorGroupType *UNUSED(wgt))
56 {
57         Object *ob = CTX_data_active_object(C);
58
59         return (ob && ob->pd && ob->pd->forcefield);
60 }
61
62 static void WIDGETGROUP_forcefield_setup(const bContext *UNUSED(C), wmManipulatorGroup *mgroup)
63 {
64         /* only wind effector for now */
65         wmManipulatorWrapper *wwrapper = MEM_mallocN(sizeof(wmManipulatorWrapper), __func__);
66         mgroup->customdata = wwrapper;
67
68         wwrapper->manipulator = WM_manipulator_new("MANIPULATOR_WT_arrow_3d", mgroup, NULL);
69         wmManipulator *mpr = wwrapper->manipulator;
70         RNA_enum_set(mpr->ptr, "draw_options",  ED_MANIPULATOR_ARROW_STYLE_CONSTRAINED);
71         ED_manipulator_arrow3d_set_ui_range(mpr, -200.0f, 200.0f);
72         ED_manipulator_arrow3d_set_range_fac(mpr, 6.0f);
73
74         UI_GetThemeColor3fv(TH_MANIPULATOR_PRIMARY, mpr->color);
75         UI_GetThemeColor3fv(TH_MANIPULATOR_HI, mpr->color_hi);
76 }
77
78 static void WIDGETGROUP_forcefield_refresh(const bContext *C, wmManipulatorGroup *mgroup)
79 {
80         wmManipulatorWrapper *wwrapper = mgroup->customdata;
81         wmManipulator *mpr = wwrapper->manipulator;
82         Object *ob = CTX_data_active_object(C);
83         PartDeflect *pd = ob->pd;
84
85         if (pd->forcefield == PFIELD_WIND) {
86                 const float size = (ob->type == OB_EMPTY) ? ob->empty_drawsize : 1.0f;
87                 const float ofs[3] = {0.0f, -size, 0.0f};
88                 PointerRNA field_ptr;
89
90                 RNA_pointer_create(&ob->id, &RNA_FieldSettings, pd, &field_ptr);
91                 WM_manipulator_set_matrix_location(mpr, ob->obmat[3]);
92                 WM_manipulator_set_matrix_rotation_from_z_axis(mpr, ob->obmat[2]);
93                 WM_manipulator_set_matrix_offset_location(mpr, ofs);
94                 WM_manipulator_set_flag(mpr, WM_MANIPULATOR_HIDDEN, false);
95                 WM_manipulator_target_property_def_rna(mpr, "offset", &field_ptr, "strength", -1);
96         }
97         else {
98                 WM_manipulator_set_flag(mpr, WM_MANIPULATOR_HIDDEN, true);
99         }
100 }
101
102 void VIEW3D_WGT_force_field(wmManipulatorGroupType *wgt)
103 {
104         wgt->name = "Force Field Widgets";
105         wgt->idname = "VIEW3D_WGT_force_field";
106
107         wgt->flag |= (WM_MANIPULATORGROUPTYPE_PERSISTENT |
108                       WM_MANIPULATORGROUPTYPE_3D |
109                       WM_MANIPULATORGROUPTYPE_SCALE |
110                       WM_MANIPULATORGROUPTYPE_DEPTH_3D);
111
112         wgt->poll = WIDGETGROUP_forcefield_poll;
113         wgt->setup = WIDGETGROUP_forcefield_setup;
114         wgt->refresh = WIDGETGROUP_forcefield_refresh;
115 }
116
117 /** \} */
118