Blender  V2.93
view3d_gizmo_empty.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_image.h"
26 #include "BKE_layer.h"
27 #include "BKE_object.h"
28 
29 #include "DEG_depsgraph.h"
30 
31 #include "DNA_light_types.h"
32 #include "DNA_object_types.h"
33 
34 #include "ED_gizmo_library.h"
35 #include "ED_screen.h"
36 
37 #include "UI_resources.h"
38 
39 #include "MEM_guardedalloc.h"
40 
41 #include "RNA_access.h"
42 
43 #include "WM_api.h"
44 #include "WM_types.h"
45 
46 #include "view3d_intern.h" /* own include */
47 
48 /* -------------------------------------------------------------------- */
54  struct {
56  float dims[2];
57  } state;
58 };
59 
60 /* translate callbacks */
62  wmGizmoProperty *gz_prop,
63  void *value_p)
64 {
65  float(*matrix)[4] = value_p;
66  BLI_assert(gz_prop->type->array_length == 16);
67  struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
68  const Object *ob = igzgroup->state.ob;
69 
70  unit_m4(matrix);
71  matrix[0][0] = ob->empty_drawsize;
72  matrix[1][1] = ob->empty_drawsize;
73 
74  float dims[2] = {0.0f, 0.0f};
75  RNA_float_get_array(gz->ptr, "dimensions", dims);
76  dims[0] *= ob->empty_drawsize;
77  dims[1] *= ob->empty_drawsize;
78 
79  matrix[3][0] = (ob->ima_ofs[0] * dims[0]) + (0.5f * dims[0]);
80  matrix[3][1] = (ob->ima_ofs[1] * dims[1]) + (0.5f * dims[1]);
81 }
82 
84  wmGizmoProperty *gz_prop,
85  const void *value_p)
86 {
87  const float(*matrix)[4] = value_p;
88  BLI_assert(gz_prop->type->array_length == 16);
89  struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
90  Object *ob = igzgroup->state.ob;
91 
92  ob->empty_drawsize = matrix[0][0];
94 
95  float dims[2];
96  RNA_float_get_array(gz->ptr, "dimensions", dims);
97  dims[0] *= ob->empty_drawsize;
98  dims[1] *= ob->empty_drawsize;
99 
100  ob->ima_ofs[0] = (matrix[3][0] - (0.5f * dims[0])) / dims[0];
101  ob->ima_ofs[1] = (matrix[3][1] - (0.5f * dims[1])) / dims[1];
102 }
103 
105 {
106  View3D *v3d = CTX_wm_view3d(C);
108 
110  return false;
111  }
112  if ((v3d->gizmo_show_empty & V3D_GIZMO_SHOW_EMPTY_IMAGE) == 0) {
113  return false;
114  }
115 
116  ViewLayer *view_layer = CTX_data_view_layer(C);
117  Base *base = BASACT(view_layer);
118  if (base && BASE_SELECTABLE(v3d, base)) {
119  Object *ob = base->object;
120  if (ob->type == OB_EMPTY) {
121  if (ob->empty_drawtype == OB_EMPTY_IMAGE) {
123  }
124  }
125  }
126  return false;
127 }
128 
130 {
131  struct EmptyImageWidgetGroup *igzgroup = MEM_mallocN(sizeof(struct EmptyImageWidgetGroup),
132  __func__);
133  igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
134  wmGizmo *gz = igzgroup->gizmo;
136 
137  gzgroup->customdata = igzgroup;
138 
140 
143 }
144 
146 {
147  struct EmptyImageWidgetGroup *igzgroup = gzgroup->customdata;
148  wmGizmo *gz = igzgroup->gizmo;
149  ViewLayer *view_layer = CTX_data_view_layer(C);
150  Object *ob = OBACT(view_layer);
151 
153 
154  RNA_enum_set(gz->ptr,
155  "transform",
158 
159  igzgroup->state.ob = ob;
160 
161  /* Use dimensions for aspect. */
162  if (ob->data != NULL) {
163  const Image *image = ob->data;
164  ImageUser iuser = *ob->iuser;
165  float size[2];
166  BKE_image_get_size_fl(ob->data, &iuser, size);
167 
168  /* Get the image aspect even if the buffer is invalid */
169  if (image->aspx > image->aspy) {
170  size[1] *= image->aspy / image->aspx;
171  }
172  else if (image->aspx < image->aspy) {
173  size[0] *= image->aspx / image->aspy;
174  }
175 
176  const float dims_max = max_ff(size[0], size[1]);
177  igzgroup->state.dims[0] = size[0] / dims_max;
178  igzgroup->state.dims[1] = size[1] / dims_max;
179  }
180  else {
181  copy_v2_fl(igzgroup->state.dims, 1.0f);
182  }
183  RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
184 
186  "matrix",
187  &(const struct wmGizmoPropertyFnParams){
188  .value_get_fn = gizmo_empty_image_prop_matrix_get,
189  .value_set_fn = gizmo_empty_image_prop_matrix_set,
190  .range_get_fn = NULL,
191  .user_data = igzgroup,
192  });
193 }
194 
196 {
197  gzgt->name = "Area Light Widgets";
198  gzgt->idname = "VIEW3D_GGT_empty_image";
199 
201 
206 }
207 
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
struct RegionView3D * CTX_wm_region_view3d(const bContext *C)
Definition: context.c:769
void BKE_image_get_size_fl(struct Image *image, struct ImageUser *iuser, float r_size[2])
Definition: image.c:5535
General operations, lookup, etc. for blender objects.
bool BKE_object_empty_image_frame_is_visible_in_view3d(const struct Object *ob, const struct RegionView3D *rv3d)
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE float max_ff(float a, float b)
void unit_m4(float m[4][4])
Definition: rct.c:1140
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
MINLINE void copy_v2_fl(float r[2], float f)
#define UNUSED(x)
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_TRANSFORM
Definition: DNA_ID.h:599
@ BASE_SELECTABLE
Object is a sort of wrapper for general info.
@ OB_EMPTY_IMAGE
@ OB_EMPTY
#define BASACT(_view_layer)
#define OBACT(_view_layer)
@ V3D_GIZMO_SHOW_EMPTY_IMAGE
@ V3D_GIZMO_HIDE
@ V3D_GIZMO_HIDE_CONTEXT
@ ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE
@ ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE
@ ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM
Read Guarded memory(de)allocation.
#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
@ WM_GIZMO_DRAW_HOVER
@ WM_GIZMOGROUPTYPE_DEPTH_3D
@ WM_GIZMOGROUPTYPE_3D
@ WM_GIZMOGROUPTYPE_PERSISTENT
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
float aspy
float aspx
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
Definition: rna_access.c:6378
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:6413
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
Definition: rna_access.c:6390
struct Object * object
struct EmptyImageWidgetGroup::@547 state
char empty_drawtype
ImageUser * iuser
float empty_drawsize
float obmat[4][4]
void * data
float ima_ofs[2]
char gizmo_show_empty
char gizmo_flag
wmGizmoGroupFnSetupKeymap setup_keymap
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnInit setup
const char * idname
eWM_GizmoFlagGroupTypeFlag flag
wmGizmoGroupFnPoll poll
const char * name
struct wmGizmoProperty::@1149 custom_func
const struct wmGizmoPropertyType * type
float matrix_basis[4][4]
float color_hi[4]
float color[4]
struct PointerRNA * ptr
static void WIDGETGROUP_empty_image_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
static bool WIDGETGROUP_empty_image_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
static void WIDGETGROUP_empty_image_refresh(const bContext *C, wmGizmoGroup *gzgroup)
static void gizmo_empty_image_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
static void gizmo_empty_image_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition: wm_gizmo.c:339
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_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)