Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_empty.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_empty.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 #include "BKE_image.h"
33
34 #include "DEG_depsgraph.h"
35
36 #include "DNA_object_types.h"
37 #include "DNA_lamp_types.h"
38
39 #include "ED_screen.h"
40 #include "ED_gizmo_library.h"
41
42 #include "UI_resources.h"
43
44 #include "MEM_guardedalloc.h"
45
46 #include "RNA_access.h"
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "view3d_intern.h"  /* own include */
52
53 /* -------------------------------------------------------------------- */
54
55 /** \name Empty Image Gizmos
56  * \{ */
57
58 struct EmptyImageWidgetGroup {
59         wmGizmo *gizmo;
60         struct {
61                 Object *ob;
62                 float dims[2];
63         } state;
64 };
65
66 /* translate callbacks */
67 static void gizmo_empty_image_prop_matrix_get(
68         const wmGizmo *gz, wmGizmoProperty *gz_prop,
69         void *value_p)
70 {
71         float (*matrix)[4] = value_p;
72         BLI_assert(gz_prop->type->array_length == 16);
73         struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
74         const Object *ob = igzgroup->state.ob;
75
76         unit_m4(matrix);
77         matrix[0][0] = ob->empty_drawsize;
78         matrix[1][1] = ob->empty_drawsize;
79
80         float dims[2] = {0.0f, 0.0f};
81         RNA_float_get_array(gz->ptr, "dimensions", dims);
82         dims[0] *= ob->empty_drawsize;
83         dims[1] *= ob->empty_drawsize;
84
85         matrix[3][0] = (ob->ima_ofs[0] * dims[0]) + (0.5f * dims[0]);
86         matrix[3][1] = (ob->ima_ofs[1] * dims[1]) + (0.5f * dims[1]);
87 }
88
89 static void gizmo_empty_image_prop_matrix_set(
90         const wmGizmo *gz, wmGizmoProperty *gz_prop,
91         const void *value_p)
92 {
93         const float (*matrix)[4] = value_p;
94         BLI_assert(gz_prop->type->array_length == 16);
95         struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
96         Object *ob = igzgroup->state.ob;
97
98         ob->empty_drawsize = matrix[0][0];
99         DEG_id_tag_update(&ob->id, DEG_TAG_TRANSFORM);
100
101         float dims[2];
102         RNA_float_get_array(gz->ptr, "dimensions", dims);
103         dims[0] *= ob->empty_drawsize;
104         dims[1] *= ob->empty_drawsize;
105
106         ob->ima_ofs[0] = (matrix[3][0] - (0.5f * dims[0])) / dims[0];
107         ob->ima_ofs[1] = (matrix[3][1] - (0.5f * dims[1])) / dims[1];
108 }
109
110 static bool WIDGETGROUP_empty_image_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
111 {
112         View3D *v3d = CTX_wm_view3d(C);
113
114         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
115             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
116         {
117                 return false;
118         }
119
120         Object *ob = CTX_data_active_object(C);
121
122         if (ob && ob->type == OB_EMPTY) {
123                 return (ob->empty_drawtype == OB_EMPTY_IMAGE);
124         }
125         return false;
126 }
127
128 static void WIDGETGROUP_empty_image_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
129 {
130         struct EmptyImageWidgetGroup *igzgroup = MEM_mallocN(sizeof(struct EmptyImageWidgetGroup), __func__);
131         igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
132         wmGizmo *gz = igzgroup->gizmo;
133         RNA_enum_set(gz->ptr, "transform",
134                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
135
136         gzgroup->customdata = igzgroup;
137
138         WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
139
140         UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
141         UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
142 }
143
144 static void WIDGETGROUP_empty_image_refresh(const bContext *C, wmGizmoGroup *gzgroup)
145 {
146         struct EmptyImageWidgetGroup *igzgroup = gzgroup->customdata;
147         Object *ob = CTX_data_active_object(C);
148         wmGizmo *gz = igzgroup->gizmo;
149
150         copy_m4_m4(gz->matrix_basis, ob->obmat);
151
152         RNA_enum_set(gz->ptr, "transform",
153                      ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
154                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
155                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM);
156
157         igzgroup->state.ob = ob;
158
159         /* Use dimensions for aspect. */
160         if (ob->data != NULL) {
161                 const Image *image = ob->data;
162                 ImageUser iuser = *ob->iuser;
163                 float size[2];
164                 BKE_image_get_size_fl(ob->data, &iuser, size);
165
166                 /* Get the image aspect even if the buffer is invalid */
167                 if (image->aspx > image->aspy) {
168                         size[1] *= image->aspy / image->aspx;
169                 }
170                 else if (image->aspx < image->aspy) {
171                         size[0] *= image->aspx / image->aspy;
172                 }
173
174                 const float dims_max = max_ff(size[0], size[1]);
175                 igzgroup->state.dims[0] = size[0] / dims_max;
176                 igzgroup->state.dims[1] = size[1] / dims_max;
177         }
178         else {
179                 copy_v2_fl(igzgroup->state.dims, 1.0f);
180         }
181         RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
182
183         WM_gizmo_target_property_def_func(
184                 gz, "matrix",
185                 &(const struct wmGizmoPropertyFnParams) {
186                     .value_get_fn = gizmo_empty_image_prop_matrix_get,
187                     .value_set_fn = gizmo_empty_image_prop_matrix_set,
188                     .range_get_fn = NULL,
189                     .user_data = igzgroup,
190                 });
191 }
192
193 void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
194 {
195         gzgt->name = "Area Light Widgets";
196         gzgt->idname = "VIEW3D_GGT_empty_image";
197
198         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
199                       WM_GIZMOGROUPTYPE_3D |
200                       WM_GIZMOGROUPTYPE_DEPTH_3D);
201
202         gzgt->poll = WIDGETGROUP_empty_image_poll;
203         gzgt->setup = WIDGETGROUP_empty_image_setup;
204         gzgt->refresh = WIDGETGROUP_empty_image_refresh;
205 }
206
207 /** \} */