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         RegionView3D *rv3d = CTX_wm_region_view3d(C);
114
115         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
116             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
117         {
118                 return false;
119         }
120
121         Object *ob = CTX_data_active_object(C);
122
123         if (ob && ob->type == OB_EMPTY) {
124                 if (ob->empty_drawtype == OB_EMPTY_IMAGE) {
125                         return BKE_image_empty_visible_in_view3d(ob, rv3d);
126                 }
127         }
128         return false;
129 }
130
131 static void WIDGETGROUP_empty_image_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
132 {
133         struct EmptyImageWidgetGroup *igzgroup = MEM_mallocN(sizeof(struct EmptyImageWidgetGroup), __func__);
134         igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
135         wmGizmo *gz = igzgroup->gizmo;
136         RNA_enum_set(gz->ptr, "transform",
137                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
138
139         gzgroup->customdata = igzgroup;
140
141         WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
142
143         UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
144         UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
145 }
146
147 static void WIDGETGROUP_empty_image_refresh(const bContext *C, wmGizmoGroup *gzgroup)
148 {
149         struct EmptyImageWidgetGroup *igzgroup = gzgroup->customdata;
150         Object *ob = CTX_data_active_object(C);
151         wmGizmo *gz = igzgroup->gizmo;
152
153         copy_m4_m4(gz->matrix_basis, ob->obmat);
154
155         RNA_enum_set(gz->ptr, "transform",
156                      ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
157                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
158                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM);
159
160         igzgroup->state.ob = ob;
161
162         /* Use dimensions for aspect. */
163         if (ob->data != NULL) {
164                 const Image *image = ob->data;
165                 ImageUser iuser = *ob->iuser;
166                 float size[2];
167                 BKE_image_get_size_fl(ob->data, &iuser, size);
168
169                 /* Get the image aspect even if the buffer is invalid */
170                 if (image->aspx > image->aspy) {
171                         size[1] *= image->aspy / image->aspx;
172                 }
173                 else if (image->aspx < image->aspy) {
174                         size[0] *= image->aspx / image->aspy;
175                 }
176
177                 const float dims_max = max_ff(size[0], size[1]);
178                 igzgroup->state.dims[0] = size[0] / dims_max;
179                 igzgroup->state.dims[1] = size[1] / dims_max;
180         }
181         else {
182                 copy_v2_fl(igzgroup->state.dims, 1.0f);
183         }
184         RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
185
186         WM_gizmo_target_property_def_func(
187                 gz, "matrix",
188                 &(const struct wmGizmoPropertyFnParams) {
189                     .value_get_fn = gizmo_empty_image_prop_matrix_get,
190                     .value_set_fn = gizmo_empty_image_prop_matrix_set,
191                     .range_get_fn = NULL,
192                     .user_data = igzgroup,
193                 });
194 }
195
196 void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
197 {
198         gzgt->name = "Area Light Widgets";
199         gzgt->idname = "VIEW3D_GGT_empty_image";
200
201         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
202                       WM_GIZMOGROUPTYPE_3D |
203                       WM_GIZMOGROUPTYPE_DEPTH_3D);
204
205         gzgt->poll = WIDGETGROUP_empty_image_poll;
206         gzgt->setup = WIDGETGROUP_empty_image_setup;
207         gzgt->refresh = WIDGETGROUP_empty_image_refresh;
208 }
209
210 /** \} */