Blender  V2.93
view3d_gizmo_light.c
Go to the documentation of this file.
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 
21 #include "BLI_math.h"
22 #include "BLI_utildefines.h"
23 
24 #include "BKE_context.h"
25 #include "BKE_layer.h"
26 #include "BKE_object.h"
27 
28 #include "DEG_depsgraph.h"
29 
30 #include "DNA_light_types.h"
31 #include "DNA_object_types.h"
32 
33 #include "ED_gizmo_library.h"
34 #include "ED_screen.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 /* -------------------------------------------------------------------- */
52 {
53  View3D *v3d = CTX_wm_view3d(C);
55  return false;
56  }
57  if ((v3d->gizmo_show_light & V3D_GIZMO_SHOW_LIGHT_SIZE) == 0) {
58  return false;
59  }
60 
61  ViewLayer *view_layer = CTX_data_view_layer(C);
62  Base *base = BASACT(view_layer);
63  if (base && BASE_SELECTABLE(v3d, base)) {
64  Object *ob = base->object;
65  if (ob->type == OB_LAMP) {
66  Light *la = ob->data;
67  return (la->type == LA_SPOT);
68  }
69  }
70  return false;
71 }
72 
74 {
75  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
76 
77  wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_arrow_3d", gzgroup, NULL);
78  wmGizmo *gz = wwrapper->gizmo;
80 
81  gzgroup->customdata = wwrapper;
82 
84 
86 }
87 
89 {
90  wmGizmoWrapper *wwrapper = gzgroup->customdata;
91  wmGizmo *gz = wwrapper->gizmo;
92  ViewLayer *view_layer = CTX_data_view_layer(C);
93  Object *ob = OBACT(view_layer);
94  Light *la = ob->data;
95  float dir[3];
96 
97  negate_v3_v3(dir, ob->obmat[2]);
98 
101 
102  /* need to set property here for undo. TODO would prefer to do this in _init */
103  PointerRNA lamp_ptr;
104  const char *propname = "spot_size";
105  RNA_pointer_create(&la->id, &RNA_Light, la, &lamp_ptr);
106  WM_gizmo_target_property_def_rna(gz, "offset", &lamp_ptr, propname, -1);
107 }
108 
110 {
111  gzgt->name = "Spot Light Widgets";
112  gzgt->idname = "VIEW3D_GGT_light_spot";
113 
115 
120 }
121 
124 /* -------------------------------------------------------------------- */
128 /* scale callbacks */
130  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 :
139  la->area_size;
140 }
141 
143  wmGizmoProperty *gz_prop,
144  const void *value_p)
145 {
146  const float(*matrix)[4] = value_p;
147  BLI_assert(gz_prop->type->array_length == 16);
148  Light *la = gz_prop->custom_func.user_data;
149 
151  la->area_size = len_v3(matrix[0]);
152  la->area_sizey = len_v3(matrix[1]);
153  }
154  else {
155  la->area_size = len_v3(matrix[0]);
156  }
157 
160 }
161 
163 {
164  View3D *v3d = CTX_wm_view3d(C);
166  return false;
167  }
168  if ((v3d->gizmo_show_light & V3D_GIZMO_SHOW_LIGHT_SIZE) == 0) {
169  return false;
170  }
171 
172  ViewLayer *view_layer = CTX_data_view_layer(C);
173  Base *base = BASACT(view_layer);
174  if (base && BASE_SELECTABLE(v3d, base)) {
175  Object *ob = base->object;
176  if (ob->type == OB_LAMP) {
177  Light *la = ob->data;
178  return (la->type == LA_AREA);
179  }
180  }
181  return false;
182 }
183 
185 {
186  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
187  wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
188  wmGizmo *gz = wwrapper->gizmo;
190 
191  gzgroup->customdata = wwrapper;
192 
194 
197 }
198 
200 {
201  wmGizmoWrapper *wwrapper = gzgroup->customdata;
202  ViewLayer *view_layer = CTX_data_view_layer(C);
203  Object *ob = OBACT(view_layer);
204  Light *la = ob->data;
205  wmGizmo *gz = wwrapper->gizmo;
206 
207  copy_m4_m4(gz->matrix_basis, ob->obmat);
208 
212  }
213  RNA_enum_set(gz->ptr, "transform", flag);
214 
215  /* need to set property here for undo. TODO would prefer to do this in _init */
217  "matrix",
218  &(const struct wmGizmoPropertyFnParams){
219  .value_get_fn = gizmo_area_light_prop_matrix_get,
220  .value_set_fn = gizmo_area_light_prop_matrix_set,
221  .range_get_fn = NULL,
222  .user_data = la,
223  });
224 }
225 
227 {
228  gzgt->name = "Area Light Widgets";
229  gzgt->idname = "VIEW3D_GGT_light_area";
230 
232 
237 }
238 
241 /* -------------------------------------------------------------------- */
246 {
247  View3D *v3d = CTX_wm_view3d(C);
249  return false;
250  }
252  return false;
253  }
254 
255  ViewLayer *view_layer = CTX_data_view_layer(C);
256  Base *base = BASACT(view_layer);
257  if (base && BASE_SELECTABLE(v3d, base)) {
258  Object *ob = base->object;
259  if (ob->type == OB_LAMP) {
260  Light *la = ob->data;
261  return (ELEM(la->type, LA_SUN, LA_SPOT, LA_AREA));
262  }
263 #if 0
264  else if (ob->type == OB_CAMERA) {
265  return true;
266  }
267 #endif
268  }
269  return false;
270 }
271 
273 {
274  wmGizmoWrapper *wwrapper = MEM_mallocN(sizeof(wmGizmoWrapper), __func__);
275  wwrapper->gizmo = WM_gizmo_new("GIZMO_GT_move_3d", gzgroup, NULL);
276  wmGizmo *gz = wwrapper->gizmo;
277 
278  gzgroup->customdata = wwrapper;
279 
282 
283  gz->scale_basis = 0.06f;
284 
285  wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_transform_axis_target", true);
286 
287  RNA_enum_set(
289 
290  WM_gizmo_operator_set(gz, 0, ot, NULL);
291 }
292 
294 {
295  wmGizmoWrapper *wwrapper = gzgroup->customdata;
296  ViewLayer *view_layer = CTX_data_view_layer(C);
297  Object *ob = OBACT(view_layer);
298  wmGizmo *gz = wwrapper->gizmo;
299 
301  unit_m4(gz->matrix_offset);
302 
303  if (ob->type == OB_LAMP) {
304  Light *la = ob->data;
305  if (la->type == LA_SPOT) {
306  /* Draw just past the light size angle gizmo. */
307  madd_v3_v3fl(gz->matrix_basis[3], gz->matrix_basis[2], -la->spotsize);
308  }
309  }
310  gz->matrix_offset[3][2] -= 23.0;
312 }
313 
315 {
316  gzgt->name = "Target Light Widgets";
317  gzgt->idname = "VIEW3D_GGT_light_target";
318 
320 
325 }
326 
typedef float(TangentPoint)[2]
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
General operations, lookup, etc. for blender objects.
#define BLI_assert(a)
Definition: BLI_assert.h:58
void unit_m4(float m[4][4])
Definition: rct.c:1140
void normalize_m4_m4(float R[4][4], const float M[4][4]) ATTR_NONNULL()
Definition: math_matrix.c:1972
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
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:654
@ 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.
StructRNA RNA_Light
#define C
Definition: RandGen.cpp:39
void UI_GetThemeColor3fv(int colorid, float col[3])
Definition: resources.c:1191
@ TH_GIZMO_HI
Definition: UI_resources.h:320
@ TH_GIZMO_PRIMARY
Definition: UI_resources.h:321
@ TH_GIZMO_SECONDARY
Definition: UI_resources.h:322
@ 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:384
#define NC_LAMP
Definition: WM_types.h:283
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:47
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:146
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:6413
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
struct wmGizmoProperty::@1149 custom_func
const struct wmGizmoPropertyType * type
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:3156
PointerRNA * WM_gizmo_operator_set(wmGizmo *gz, int part_index, wmOperatorType *ot, IDProperty *properties)
Definition: wm_gizmo.c:232
void WM_gizmo_set_matrix_location(wmGizmo *gz, const float origin[3])
Definition: wm_gizmo.c:316
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:339
void WM_gizmo_set_matrix_rotation_from_z_axis(wmGizmo *gz, const float z_axis[3])
Definition: wm_gizmo.c:306
wmGizmo * WM_gizmo_new(const char *idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition: wm_gizmo.c:114
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)