DRW: show image empty frame when the 'side' is hidden
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_empty.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup spview3d
19  */
20
21
22 #include "BLI_math.h"
23 #include "BLI_utildefines.h"
24
25 #include "BKE_context.h"
26 #include "BKE_object.h"
27 #include "BKE_image.h"
28
29 #include "DEG_depsgraph.h"
30
31 #include "DNA_object_types.h"
32 #include "DNA_light_types.h"
33
34 #include "ED_screen.h"
35 #include "ED_gizmo_library.h"
36
37 #include "UI_resources.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "RNA_access.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "view3d_intern.h"  /* own include */
47
48 /* -------------------------------------------------------------------- */
49 /** \name Empty Image Gizmos
50  * \{ */
51
52 struct EmptyImageWidgetGroup {
53         wmGizmo *gizmo;
54         struct {
55                 Object *ob;
56                 float dims[2];
57         } state;
58 };
59
60 /* translate callbacks */
61 static void gizmo_empty_image_prop_matrix_get(
62         const wmGizmo *gz, wmGizmoProperty *gz_prop,
63         void *value_p)
64 {
65         float (*matrix)[4] = value_p;
66         BLI_assert(gz_prop->type->array_length == 16);
67         struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
68         const Object *ob = igzgroup->state.ob;
69
70         unit_m4(matrix);
71         matrix[0][0] = ob->empty_drawsize;
72         matrix[1][1] = ob->empty_drawsize;
73
74         float dims[2] = {0.0f, 0.0f};
75         RNA_float_get_array(gz->ptr, "dimensions", dims);
76         dims[0] *= ob->empty_drawsize;
77         dims[1] *= ob->empty_drawsize;
78
79         matrix[3][0] = (ob->ima_ofs[0] * dims[0]) + (0.5f * dims[0]);
80         matrix[3][1] = (ob->ima_ofs[1] * dims[1]) + (0.5f * dims[1]);
81 }
82
83 static void gizmo_empty_image_prop_matrix_set(
84         const wmGizmo *gz, wmGizmoProperty *gz_prop,
85         const void *value_p)
86 {
87         const float (*matrix)[4] = value_p;
88         BLI_assert(gz_prop->type->array_length == 16);
89         struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
90         Object *ob = igzgroup->state.ob;
91
92         ob->empty_drawsize = matrix[0][0];
93         DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
94
95         float dims[2];
96         RNA_float_get_array(gz->ptr, "dimensions", dims);
97         dims[0] *= ob->empty_drawsize;
98         dims[1] *= ob->empty_drawsize;
99
100         ob->ima_ofs[0] = (matrix[3][0] - (0.5f * dims[0])) / dims[0];
101         ob->ima_ofs[1] = (matrix[3][1] - (0.5f * dims[1])) / dims[1];
102 }
103
104 static bool WIDGETGROUP_empty_image_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
105 {
106         View3D *v3d = CTX_wm_view3d(C);
107         RegionView3D *rv3d = CTX_wm_region_view3d(C);
108
109         if ((v3d->flag2 & V3D_HIDE_OVERLAYS) ||
110             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
111         {
112                 return false;
113         }
114
115         ViewLayer *view_layer = CTX_data_view_layer(C);
116         Base *base = BASACT(view_layer);
117         if (base && BASE_SELECTABLE(v3d, base)) {
118                 Object *ob = base->object;
119                 if (ob->type == OB_EMPTY) {
120                         if (ob->empty_drawtype == OB_EMPTY_IMAGE) {
121                                 return BKE_object_empty_image_frame_is_visible_in_view3d(ob, rv3d);
122                         }
123                 }
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         wmGizmo *gz = igzgroup->gizmo;
148         ViewLayer *view_layer = CTX_data_view_layer(C);
149         Object *ob = OBACT(view_layer);
150
151         copy_m4_m4(gz->matrix_basis, ob->obmat);
152
153         RNA_enum_set(gz->ptr, "transform",
154                      ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
155                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
156                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM);
157
158         igzgroup->state.ob = ob;
159
160         /* Use dimensions for aspect. */
161         if (ob->data != NULL) {
162                 const Image *image = ob->data;
163                 ImageUser iuser = *ob->iuser;
164                 float size[2];
165                 BKE_image_get_size_fl(ob->data, &iuser, size);
166
167                 /* Get the image aspect even if the buffer is invalid */
168                 if (image->aspx > image->aspy) {
169                         size[1] *= image->aspy / image->aspx;
170                 }
171                 else if (image->aspx < image->aspy) {
172                         size[0] *= image->aspx / image->aspy;
173                 }
174
175                 const float dims_max = max_ff(size[0], size[1]);
176                 igzgroup->state.dims[0] = size[0] / dims_max;
177                 igzgroup->state.dims[1] = size[1] / dims_max;
178         }
179         else {
180                 copy_v2_fl(igzgroup->state.dims, 1.0f);
181         }
182         RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
183
184         WM_gizmo_target_property_def_func(
185                 gz, "matrix",
186                 &(const struct wmGizmoPropertyFnParams) {
187                     .value_get_fn = gizmo_empty_image_prop_matrix_get,
188                     .value_set_fn = gizmo_empty_image_prop_matrix_set,
189                     .range_get_fn = NULL,
190                     .user_data = igzgroup,
191                 });
192 }
193
194 void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
195 {
196         gzgt->name = "Area Light Widgets";
197         gzgt->idname = "VIEW3D_GGT_empty_image";
198
199         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
200                       WM_GIZMOGROUPTYPE_3D |
201                       WM_GIZMOGROUPTYPE_DEPTH_3D);
202
203         gzgt->poll = WIDGETGROUP_empty_image_poll;
204         gzgt->setup = WIDGETGROUP_empty_image_setup;
205         gzgt->refresh = WIDGETGROUP_empty_image_refresh;
206 }
207
208 /** \} */