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