Fix gizmos showing for non-visible objects
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_lamp.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_lamp.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
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
54 /** \name Spot Lamp Gizmos
55  * \{ */
56
57 static bool WIDGETGROUP_lamp_spot_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
58 {
59         View3D *v3d = CTX_wm_view3d(C);
60         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
61             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
62         {
63                 return false;
64         }
65
66         ViewLayer *view_layer = CTX_data_view_layer(C);
67         Base *base = BASACT(view_layer);
68         if (base && BASE_VISIBLE(v3d, base)) {
69                 Object *ob = base->object;
70                 if (ob->type == OB_LAMP) {
71                         Lamp *la = ob->data;
72                         return (la->type == LA_SPOT);
73                 }
74         }
75         return false;
76 }
77
78 static void WIDGETGROUP_lamp_spot_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
79 {
80         wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
81
82         wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
83         wmGizmo *gz = wwrapper->gizmo;
84         RNA_enum_set(gz->ptr, "transform",  ED_GIZMO_ARROW_XFORM_FLAG_INVERTED);
85
86         gzgroup->customdata = wwrapper;
87
88         ED_gizmo_arrow3d_set_range_fac(gz, 4.0f);
89
90         UI_GetThemeColor3fv(TH_GIZMO_SECONDARY, gz->color);
91 }
92
93 static void WIDGETGROUP_lamp_spot_refresh(const bContext *C, wmGizmoGroup *gzgroup)
94 {
95         wmGizmoWrapper *wwrapper = gzgroup->customdata;
96         wmGizmo *gz = wwrapper->gizmo;
97         ViewLayer *view_layer = CTX_data_view_layer(C);
98         Object *ob = OBACT(view_layer);
99         Lamp *la = ob->data;
100         float dir[3];
101
102         negate_v3_v3(dir, ob->obmat[2]);
103
104         WM_gizmo_set_matrix_rotation_from_z_axis(gz, dir);
105         WM_gizmo_set_matrix_location(gz, ob->obmat[3]);
106
107         /* need to set property here for undo. TODO would prefer to do this in _init */
108         PointerRNA lamp_ptr;
109         const char *propname = "spot_size";
110         RNA_pointer_create(&la->id, &RNA_Light, la, &lamp_ptr);
111         WM_gizmo_target_property_def_rna(gz, "offset", &lamp_ptr, propname, -1);
112 }
113
114 void VIEW3D_GGT_lamp_spot(wmGizmoGroupType *gzgt)
115 {
116         gzgt->name = "Spot Light Widgets";
117         gzgt->idname = "VIEW3D_GGT_lamp_spot";
118
119         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
120                       WM_GIZMOGROUPTYPE_3D |
121                       WM_GIZMOGROUPTYPE_DEPTH_3D);
122
123         gzgt->poll = WIDGETGROUP_lamp_spot_poll;
124         gzgt->setup = WIDGETGROUP_lamp_spot_setup;
125         gzgt->refresh = WIDGETGROUP_lamp_spot_refresh;
126 }
127
128 /** \} */
129
130 /* -------------------------------------------------------------------- */
131
132 /** \name Area Lamp Gizmos
133  * \{ */
134
135 /* scale callbacks */
136 static void gizmo_area_lamp_prop_matrix_get(
137         const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop,
138         void *value_p)
139 {
140         BLI_assert(gz_prop->type->array_length == 16);
141         float (*matrix)[4] = value_p;
142         const Lamp *la = gz_prop->custom_func.user_data;
143
144         matrix[0][0] = la->area_size;
145         matrix[1][1] = ELEM(la->area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE) ? la->area_sizey : la->area_size;
146 }
147
148 static void gizmo_area_lamp_prop_matrix_set(
149         const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop,
150         const void *value_p)
151 {
152         const float (*matrix)[4] = value_p;
153         BLI_assert(gz_prop->type->array_length == 16);
154         Lamp *la = gz_prop->custom_func.user_data;
155
156         if (ELEM(la->area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE)) {
157                 la->area_size = len_v3(matrix[0]);
158                 la->area_sizey = len_v3(matrix[1]);
159         }
160         else {
161                 la->area_size = len_v3(matrix[0]);
162         }
163
164         DEG_id_tag_update(&la->id, ID_RECALC_COPY_ON_WRITE);
165         WM_main_add_notifier(NC_LAMP | ND_LIGHTING_DRAW, la);
166 }
167
168 static bool WIDGETGROUP_lamp_area_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
169 {
170         View3D *v3d = CTX_wm_view3d(C);
171         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
172                 return false;
173         }
174
175         ViewLayer *view_layer = CTX_data_view_layer(C);
176         Base *base = BASACT(view_layer);
177         if (base && BASE_VISIBLE(v3d, base)) {
178                 Object *ob = base->object;
179                 if (ob->type == OB_LAMP) {
180                         Lamp *la = ob->data;
181                         return (la->type == LA_AREA);
182                 }
183         }
184         return false;
185 }
186
187 static void WIDGETGROUP_lamp_area_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
188 {
189         wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
190         wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
191         wmGizmo *gz = wwrapper->gizmo;
192         RNA_enum_set(gz->ptr, "transform",
193                      ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
194
195         gzgroup->customdata = wwrapper;
196
197         WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
198
199         UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
200         UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
201 }
202
203 static void WIDGETGROUP_lamp_area_refresh(const bContext *C, wmGizmoGroup *gzgroup)
204 {
205         wmGizmoWrapper *wwrapper = gzgroup->customdata;
206         ViewLayer *view_layer = CTX_data_view_layer(C);
207         Object *ob = OBACT(view_layer);
208         Lamp *la = ob->data;
209         wmGizmo *gz = wwrapper->gizmo;
210
211         copy_m4_m4(gz->matrix_basis, ob->obmat);
212
213         int flag = ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE;
214         if (ELEM(la->area_shape, LA_AREA_SQUARE, LA_AREA_DISK)) {
215                 flag |= ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM;
216         }
217         RNA_enum_set(gz->ptr, "transform", flag);
218
219         /* need to set property here for undo. TODO would prefer to do this in _init */
220         WM_gizmo_target_property_def_func(
221                 gz, "matrix",
222                 &(const struct wmGizmoPropertyFnParams) {
223                     .value_get_fn = gizmo_area_lamp_prop_matrix_get,
224                     .value_set_fn = gizmo_area_lamp_prop_matrix_set,
225                     .range_get_fn = NULL,
226                     .user_data = la,
227                 });
228 }
229
230 void VIEW3D_GGT_lamp_area(wmGizmoGroupType *gzgt)
231 {
232         gzgt->name = "Area Light Widgets";
233         gzgt->idname = "VIEW3D_GGT_lamp_area";
234
235         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
236                       WM_GIZMOGROUPTYPE_3D |
237                       WM_GIZMOGROUPTYPE_DEPTH_3D);
238
239         gzgt->poll = WIDGETGROUP_lamp_area_poll;
240         gzgt->setup = WIDGETGROUP_lamp_area_setup;
241         gzgt->refresh = WIDGETGROUP_lamp_area_refresh;
242 }
243
244 /** \} */
245
246
247 /* -------------------------------------------------------------------- */
248
249 /** \name Lamp Target Gizmo
250  * \{ */
251
252 static bool WIDGETGROUP_lamp_target_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
253 {
254         View3D *v3d = CTX_wm_view3d(C);
255         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
256                 return false;
257         }
258
259         ViewLayer *view_layer = CTX_data_view_layer(C);
260         Base *base = BASACT(view_layer);
261         if (base && BASE_VISIBLE(v3d, base)) {
262                 Object *ob = base->object;
263                 if (ob->type == OB_LAMP) {
264                         Lamp *la = ob->data;
265                         return (ELEM(la->type, LA_SUN, LA_SPOT, LA_AREA));
266                 }
267 #if 0
268                 else if (ob->type == OB_CAMERA) {
269                         return true;
270                 }
271 #endif
272         }
273         return false;
274 }
275
276 static void WIDGETGROUP_lamp_target_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
277 {
278         wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
279         wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_move_3d", gzgroup, NULL);
280         wmGizmo *gz = wwrapper->gizmo;
281
282         gzgroup->customdata = wwrapper;
283
284         UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
285         UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
286
287         gz->scale_basis = 0.06f;
288
289         wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_transform_axis_target", true);
290
291         RNA_enum_set(gz->ptr, "draw_options",
292                      ED_GIZMO_MOVE_DRAW_FLAG_FILL | ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW);
293
294         WM_gizmo_operator_set(gz, 0, ot, NULL);
295 }
296
297 static void WIDGETGROUP_lamp_target_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
298 {
299         wmGizmoWrapper *wwrapper = gzgroup->customdata;
300         ViewLayer *view_layer = CTX_data_view_layer(C);
301         Object *ob = OBACT(view_layer);
302         wmGizmo *gz = wwrapper->gizmo;
303
304         copy_m4_m4(gz->matrix_basis, ob->obmat);
305         unit_m4(gz->matrix_offset);
306         gz->matrix_offset[3][2] = -2.4f / gz->scale_basis;
307         WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_OFFSET_SCALE, true);
308 }
309
310 void VIEW3D_GGT_lamp_target(wmGizmoGroupType *gzgt)
311 {
312         gzgt->name = "Target Light Widgets";
313         gzgt->idname = "VIEW3D_GGT_lamp_target";
314
315         gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
316                       WM_GIZMOGROUPTYPE_3D);
317
318         gzgt->poll = WIDGETGROUP_lamp_target_poll;
319         gzgt->setup = WIDGETGROUP_lamp_target_setup;
320         gzgt->draw_prepare = WIDGETGROUP_lamp_target_draw_prepare;
321 }
322
323 /** \} */