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