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