Blender  V3.3
view3d_gizmo_light.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_math.h"
8 #include "BLI_utildefines.h"
9 
10 #include "BKE_context.h"
11 #include "BKE_layer.h"
12 #include "BKE_object.h"
13 
14 #include "DEG_depsgraph.h"
15 
16 #include "DNA_light_types.h"
17 #include "DNA_object_types.h"
18 
19 #include "ED_gizmo_library.h"
20 #include "ED_screen.h"
21 
22 #include "UI_resources.h"
23 
24 #include "MEM_guardedalloc.h"
25 
26 #include "RNA_access.h"
27 #include "RNA_prototypes.h"
28 
29 #include "WM_api.h"
30 #include "WM_types.h"
31 
32 #include "view3d_intern.h" /* own include */
33 
34 /* -------------------------------------------------------------------- */
39 {
40  View3D *v3d = CTX_wm_view3d(C);
42  return false;
43  }
44  if ((v3d->gizmo_show_light & V3D_GIZMO_SHOW_LIGHT_SIZE) == 0) {
45  return false;
46  }
47 
48  ViewLayer *view_layer = CTX_data_view_layer(C);
49  Base *base = BASACT(view_layer);
50  if (base && BASE_SELECTABLE(v3d, base)) {
51  Object *ob = base->object;
52  if (ob->type == OB_LAMP) {
53  Light *la = ob->data;
54  return (la->type == LA_SPOT);
55  }
56  }
57  return false;
58 }
59 
61 {
62  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
63 
64  wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
65  wmGizmo *gz = wwrapper->gizmo;
67 
68  gzgroup->customdata = wwrapper;
69 
71 
73 }
74 
76 {
77  wmGizmoWrapper *wwrapper = gzgroup->customdata;
78  wmGizmo *gz = wwrapper->gizmo;
79  ViewLayer *view_layer = CTX_data_view_layer(C);
80  Object *ob = OBACT(view_layer);
81  Light *la = ob->data;
82  float dir[3];
83 
84  negate_v3_v3(dir, ob->obmat[2]);
85 
88 
89  /* need to set property here for undo. TODO: would prefer to do this in _init. */
90  PointerRNA lamp_ptr;
91  const char *propname = "spot_size";
92  RNA_pointer_create(&la->id, &RNA_Light, la, &lamp_ptr);
93  WM_gizmo_target_property_def_rna(gz, "offset", &lamp_ptr, propname, -1);
94 }
95 
97 {
98  gzgt->name = "Spot Light Widgets";
99  gzgt->idname = "VIEW3D_GGT_light_spot";
100 
102 
107 }
108 
111 /* -------------------------------------------------------------------- */
115 /* scale callbacks */
117  wmGizmoProperty *gz_prop,
118  void *value_p)
119 {
120  BLI_assert(gz_prop->type->array_length == 16);
121  float(*matrix)[4] = value_p;
122  const Light *la = gz_prop->custom_func.user_data;
123 
124  matrix[0][0] = la->area_size;
125  matrix[1][1] = ELEM(la->area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE) ? la->area_sizey :
126  la->area_size;
127 }
128 
130  wmGizmoProperty *gz_prop,
131  const void *value_p)
132 {
133  const float(*matrix)[4] = value_p;
134  BLI_assert(gz_prop->type->array_length == 16);
135  Light *la = gz_prop->custom_func.user_data;
136 
138  la->area_size = len_v3(matrix[0]);
139  la->area_sizey = len_v3(matrix[1]);
140  }
141  else {
142  la->area_size = len_v3(matrix[0]);
143  }
144 
147 }
148 
150 {
151  View3D *v3d = CTX_wm_view3d(C);
153  return false;
154  }
155  if ((v3d->gizmo_show_light & V3D_GIZMO_SHOW_LIGHT_SIZE) == 0) {
156  return false;
157  }
158 
159  ViewLayer *view_layer = CTX_data_view_layer(C);
160  Base *base = BASACT(view_layer);
161  if (base && BASE_SELECTABLE(v3d, base)) {
162  Object *ob = base->object;
163  if (ob->type == OB_LAMP) {
164  Light *la = ob->data;
165  return (la->type == LA_AREA);
166  }
167  }
168  return false;
169 }
170 
172 {
173  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
174  wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
175  wmGizmo *gz = wwrapper->gizmo;
177 
178  gzgroup->customdata = wwrapper;
179 
181 
184 }
185 
187 {
188  wmGizmoWrapper *wwrapper = gzgroup->customdata;
189  ViewLayer *view_layer = CTX_data_view_layer(C);
190  Object *ob = OBACT(view_layer);
191  Light *la = ob->data;
192  wmGizmo *gz = wwrapper->gizmo;
193 
194  copy_m4_m4(gz->matrix_basis, ob->obmat);
195 
199  }
200  RNA_enum_set(gz->ptr, "transform", flag);
201 
202  /* need to set property here for undo. TODO: would prefer to do this in _init. */
204  "matrix",
205  &(const struct wmGizmoPropertyFnParams){
206  .value_get_fn = gizmo_area_light_prop_matrix_get,
207  .value_set_fn = gizmo_area_light_prop_matrix_set,
208  .range_get_fn = NULL,
209  .user_data = la,
210  });
211 }
212 
214 {
215  gzgt->name = "Area Light Widgets";
216  gzgt->idname = "VIEW3D_GGT_light_area";
217 
219 
224 }
225 
228 /* -------------------------------------------------------------------- */
233 {
234  View3D *v3d = CTX_wm_view3d(C);
236  return false;
237  }
239  return false;
240  }
241 
242  ViewLayer *view_layer = CTX_data_view_layer(C);
243  Base *base = BASACT(view_layer);
244  if (base && BASE_SELECTABLE(v3d, base)) {
245  Object *ob = base->object;
246  if (ob->type == OB_LAMP) {
247  Light *la = ob->data;
248  return (ELEM(la->type, LA_SUN, LA_SPOT, LA_AREA));
249  }
250 #if 0
251  else if (ob->type == OB_CAMERA) {
252  return true;
253  }
254 #endif
255  }
256  return false;
257 }
258 
260 {
261  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
262  wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_move_3d", gzgroup, NULL);
263  wmGizmo *gz = wwrapper->gizmo;
264 
265  gzgroup->customdata = wwrapper;
266 
269 
270  gz->scale_basis = 0.06f;
271 
272  wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_transform_axis_target", true);
273 
274  RNA_enum_set(
276 
277  WM_gizmo_operator_set(gz, 0, ot, NULL);
278 }
279 
281 {
282  wmGizmoWrapper *wwrapper = gzgroup->customdata;
283  ViewLayer *view_layer = CTX_data_view_layer(C);
284  Object *ob = OBACT(view_layer);
285  wmGizmo *gz = wwrapper->gizmo;
286 
288  unit_m4(gz->matrix_offset);
289 
290  if (ob->type == OB_LAMP) {
291  Light *la = ob->data;
292  if (la->type == LA_SPOT) {
293  /* Draw just past the light size angle gizmo. */
294  madd_v3_v3fl(gz->matrix_basis[3], gz->matrix_basis[2], -la->spotsize);
295  }
296  }
297  gz->matrix_offset[3][2] -= 23.0;
299 }
300 
302 {
303  gzgt->name = "Target Light Widgets";
304  gzgt->idname = "VIEW3D_GGT_light_target";
305 
307 
312 }
313 
typedef float(TangentPoint)[2]
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
General operations, lookup, etc. for blender objects.
#define BLI_assert(a)
Definition: BLI_assert.h:46
void unit_m4(float m[4][4])
Definition: rct.c:1090
void normalize_m4_m4(float R[4][4], const float M[4][4]) ATTR_NONNULL()
Definition: math_matrix.c:1965
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:77
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void negate_v3_v3(float r[3], const float a[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
#define UNUSED(x)
#define ELEM(...)
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
@ BASE_SELECTABLE
#define LA_AREA
#define LA_SPOT
#define LA_AREA_SQUARE
#define LA_AREA_ELLIPSE
#define LA_SUN
#define LA_AREA_DISK
#define LA_AREA_RECT
Object is a sort of wrapper for general info.
@ OB_CAMERA
@ OB_LAMP
#define BASACT(_view_layer)
#define OBACT(_view_layer)
@ V3D_GIZMO_SHOW_LIGHT_LOOK_AT
@ V3D_GIZMO_SHOW_LIGHT_SIZE
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_CONTEXT
@ ED_GIZMO_ARROW_XFORM_FLAG_INVERTED
@ ED_GIZMO_MOVE_DRAW_FLAG_FILL
@ ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW
@ ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE
@ ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void UI_GetThemeColor3fv(int colorid, float col[3])
Definition: resources.c:1165
@ TH_GIZMO_HI
Definition: UI_resources.h:304
@ TH_GIZMO_PRIMARY
Definition: UI_resources.h:305
@ TH_GIZMO_SECONDARY
Definition: UI_resources.h:306
@ WM_GIZMO_DRAW_HOVER
@ WM_GIZMO_DRAW_OFFSET_SCALE
@ WM_GIZMOGROUPTYPE_DEPTH_3D
@ WM_GIZMOGROUPTYPE_3D
@ WM_GIZMOGROUPTYPE_PERSISTENT
#define ND_LIGHTING_DRAW
Definition: WM_types.h:432
#define NC_LAMP
Definition: WM_types.h:332
void ED_gizmo_arrow3d_set_range_fac(wmGizmo *gz, const float range_fac)
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:5015
struct Object * object
float area_sizey
short area_shape
float spotsize
float area_size
short type
float obmat[4][4]
void * data
char gizmo_show_light
char gizmo_flag
wmGizmoGroupFnSetupKeymap setup_keymap
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
const char * name
wmGizmoGroupFnDrawPrepare draw_prepare
const struct wmGizmoPropertyType * type
struct wmGizmoProperty::@1185 custom_func
struct wmGizmo * gizmo
float matrix_basis[4][4]
float matrix_offset[4][4]
float color_hi[4]
float color[4]
struct PointerRNA * ptr
float scale_basis
static void WIDGETGROUP_light_spot_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static bool WIDGETGROUP_light_spot_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
static void WIDGETGROUP_light_target_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
void VIEW3D_GGT_light_spot(wmGizmoGroupType *gzgt)
static void WIDGETGROUP_light_spot_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
static void WIDGETGROUP_light_target_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
static void WIDGETGROUP_light_area_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_area_light_prop_matrix_set(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, const void *value_p)
void VIEW3D_GGT_light_area(wmGizmoGroupType *gzgt)
static void WIDGETGROUP_light_area_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
static bool WIDGETGROUP_light_target_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
static void gizmo_area_light_prop_matrix_get(const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop, void *value_p)
static bool WIDGETGROUP_light_area_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
void VIEW3D_GGT_light_target(wmGizmoGroupType *gzgt)
void WM_main_add_notifier(unsigned int type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3479
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition: wm_gizmo.c:203
void WM_gizmo_set_matrix_location(wmGizmo *gz, const float origin[3])
Definition: wm_gizmo.c:284
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:304
void WM_gizmo_set_matrix_rotation_from_z_axis(wmGizmo *gz, const float z_axis[3])
Definition: wm_gizmo.c:274
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:94
wmKeyMap * WM_gizmogroup_setup_keymap_generic_maybe_drag(const wmGizmoGroupType *UNUSED(gzgt), wmKeyConfig *kc)
void WM_gizmo_target_property_def_rna(wmGizmo *gz, const char *idname, PointerRNA *ptr, const char *propname, int index)
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)