Blender V5.3
view3d_gizmo_empty.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_listbase.hh"
10#include "BLI_math_matrix_c.hh"
11#include "BLI_math_vector_c.hh"
12
13#include "BKE_context.hh"
14#include "BKE_image.hh"
15#include "BKE_layer.hh"
16#include "BKE_lib_id.hh"
17#include "BKE_object.hh"
18
19#include "DEG_depsgraph.hh"
20
21#include "DNA_object_types.h"
22
23#include "ED_gizmo_library.hh"
24
25#include "UI_resources.hh"
26
27#include "MEM_guardedalloc.h"
28
29#include "RNA_access.hh"
30
31#include "WM_api.hh"
32#include "WM_types.hh"
33
34#include "view3d_intern.hh" /* own include */
35
36namespace blender {
37
38/* -------------------------------------------------------------------- */
41
44 struct {
45 Object *ob;
46 float dims[2];
48};
49
50/* translate callbacks */
52 wmGizmoProperty *gz_prop,
53 void *value_p)
54{
55 float (*matrix)[4] = static_cast<float (*)[4]>(value_p);
56 BLI_assert(gz_prop->type->array_length == 16);
57 EmptyImageWidgetGroup *igzgroup = static_cast<EmptyImageWidgetGroup *>(
58 gz_prop->custom_func.user_data);
59 const Object *ob = igzgroup->state.ob;
60
61 unit_m4(matrix);
62 matrix[0][0] = ob->empty_drawsize;
63 matrix[1][1] = ob->empty_drawsize;
64
65 float dims[2] = {0.0f, 0.0f};
66 RNA_float_get_array(gz->ptr, "dimensions", dims);
67 dims[0] *= ob->empty_drawsize;
68 dims[1] *= ob->empty_drawsize;
69
70 matrix[3][0] = (ob->ima_ofs[0] * dims[0]) + (0.5f * dims[0]);
71 matrix[3][1] = (ob->ima_ofs[1] * dims[1]) + (0.5f * dims[1]);
72}
73
75 wmGizmoProperty *gz_prop,
76 const void *value_p)
77{
78 const float (*matrix)[4] = static_cast<const float (*)[4]>(value_p);
79 BLI_assert(gz_prop->type->array_length == 16);
80 EmptyImageWidgetGroup *igzgroup = static_cast<EmptyImageWidgetGroup *>(
81 gz_prop->custom_func.user_data);
82 Object *ob = igzgroup->state.ob;
83
84 ob->empty_drawsize = matrix[0][0];
87
88 float dims[2];
89 RNA_float_get_array(gz->ptr, "dimensions", dims);
90 dims[0] *= ob->empty_drawsize;
91 dims[1] *= ob->empty_drawsize;
92
93 ob->ima_ofs[0] = (matrix[3][0] - (0.5f * dims[0])) / dims[0];
94 ob->ima_ofs[1] = (matrix[3][1] - (0.5f * dims[1])) / dims[1];
95}
96
98{
99 View3D *v3d = CTX_wm_view3d(C);
101
103 return false;
104 }
106 return false;
107 }
108
109 const Main *bmain = CTX_data_main(C);
110 const Scene *scene = CTX_data_scene(C);
111 ViewLayer *view_layer = CTX_data_view_layer(C);
112 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
113 Base *base = BKE_view_layer_active_base_get(view_layer);
114 if (base && BASE_SELECTABLE(v3d, base)) {
115 const Object *ob = base->object;
116 if (ob->type == OB_EMPTY) {
117 if (ob->empty_drawtype == OB_EMPTY_IMAGE) {
119 if (BKE_id_is_editable(CTX_data_main(C), &ob->id)) {
120 return true;
121 }
122 }
123 }
124 }
125 }
126 return false;
127}
128
129static void WIDGETGROUP_empty_image_setup(const bContext * /*C*/, wmGizmoGroup *gzgroup)
130{
132 igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, nullptr);
133 wmGizmo *gz = igzgroup->gizmo;
135
136 gzgroup->customdata = igzgroup;
137
139
142
143 /* All gizmos must perform undo. */
144 for (wmGizmo &gz : gzgroup->gizmos) {
146 }
147}
148
150{
151 EmptyImageWidgetGroup *igzgroup = static_cast<EmptyImageWidgetGroup *>(gzgroup->customdata);
152 wmGizmo *gz = igzgroup->gizmo;
153 const Main *bmain = CTX_data_main(C);
154 const Scene *scene = CTX_data_scene(C);
155 ViewLayer *view_layer = CTX_data_view_layer(C);
156 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
158
159 copy_m4_m4(gz->matrix_basis, ob->object_to_world().ptr());
160
161 RNA_enum_set(gz->ptr,
162 "transform",
165
166 igzgroup->state.ob = ob;
167
168 /* Use dimensions for aspect. */
169 if (ob->data != nullptr) {
170 const Image *image = id_cast<const Image *>(ob->data);
171 ImageUser iuser = *ob->iuser;
172 float size[2];
173 BKE_image_get_size_fl(id_cast<Image *>(ob->data), &iuser, size);
174
175 /* Get the image aspect even if the buffer is invalid */
176 if (image->aspx > image->aspy) {
177 size[1] *= image->aspy / image->aspx;
178 }
179 else if (image->aspx < image->aspy) {
180 size[0] *= image->aspx / image->aspy;
181 }
182
183 const float dims_max = max_ff(size[0], size[1]);
184 igzgroup->state.dims[0] = size[0] / dims_max;
185 igzgroup->state.dims[1] = size[1] / dims_max;
186 }
187 else {
188 copy_v2_fl(igzgroup->state.dims, 1.0f);
189 }
190 RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
191
195 params.range_get_fn = nullptr;
196 params.user_data = igzgroup;
197 WM_gizmo_target_property_def_func(gz, "matrix", &params);
198}
199
212
214
215} // namespace blender
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_TRANSFORM
Definition WM_types.hh:475
#define NC_OBJECT
Definition WM_types.hh:392
const int state
void get_color_3fv(int colorid, float col[3])
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
bool BKE_object_empty_image_frame_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
Definition object.cc:3877
MINLINE void copy_v2_fl(float r[2], float f)
void WM_gizmo_target_property_def_func(wmGizmo *gz, const char *idname, const wmGizmoPropertyFnParams *params)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
@ ED_GIZMO_CAGE_XFORM_FLAG_SCALE
@ ED_GIZMO_CAGE_XFORM_FLAG_TRANSLATE
@ ED_GIZMO_CAGE_XFORM_FLAG_SCALE_UNIFORM
static bool WIDGETGROUP_empty_image_poll(const bContext *C, wmGizmoGroupType *)
ViewLayer * CTX_data_view_layer(const bContext *C)
static void WIDGETGROUP_empty_image_setup(const bContext *, wmGizmoGroup *gzgroup)
void WM_gizmo_set_flag(wmGizmo *gz, const int flag, const bool enable)
Definition wm_gizmo.cc:303
void DEG_id_tag_update(ID *id, unsigned int flags)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void unit_m4(float m[4][4])
RegionView3D * CTX_wm_region_view3d(const bContext *C)
static void gizmo_empty_image_prop_matrix_set(const wmGizmo *gz, wmGizmoProperty *gz_prop, const void *value_p)
wmGizmo * WM_gizmo_new(const StringRef idname, wmGizmoGroup *gzgroup, PointerRNA *properties)
Definition wm_gizmo.cc:97
@ V3D_GIZMO_SHOW_EMPTY_IMAGE
static void gizmo_empty_image_prop_matrix_get(const wmGizmo *gz, wmGizmoProperty *gz_prop, void *value_p)
@ WM_GIZMOGROUPTYPE_DEPTH_3D
@ WM_GIZMOGROUPTYPE_PERSISTENT
@ WM_GIZMO_DRAW_HOVER
@ WM_GIZMO_NEEDS_UNDO
Main * CTX_data_main(const bContext *C)
MINLINE float max_ff(float a, float b)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
Scene * CTX_data_scene(const bContext *C)
wmKeyMap * WM_gizmogroup_setup_keymap_generic_maybe_drag(const wmGizmoGroupType *, wmKeyConfig *kc)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void BKE_image_get_size_fl(Image *image, ImageUser *iuser, float r_size[2])
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
static void WIDGETGROUP_empty_image_refresh(const bContext *C, wmGizmoGroup *gzgroup)
View3D * CTX_wm_view3d(const bContext *C)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct Object * object
struct blender::EmptyImageWidgetGroup::@275155311047377302006037347202115052362157010340 state
eObject_EmptyDrawType empty_drawtype
eView3D_GizmoShowEmpty gizmo_show_empty
eView3D_GizmoFlag gizmo_flag
wmGizmoGroupFnRefresh refresh
wmGizmoGroupFnSetupKeymap setup_keymap
eWM_GizmoFlagGroupTypeFlag flag
ListBaseT< wmGizmo > gizmos
wmGizmoPropertyFnSet value_set_fn
wmGizmoPropertyFnRangeGet range_get_fn
wmGizmoPropertyFnGet value_get_fn
const wmGizmoPropertyType * type
struct blender::wmGizmoProperty::@216011055122204070127377170176223170047224321165 custom_func
float matrix_basis[4][4]