2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * ***** END GPL LICENSE BLOCK *****
21 /** \file blender/editors/space_view3d/view3d_gizmo_empty.c
26 #include "BLI_blenlib.h"
28 #include "BLI_utildefines.h"
30 #include "BKE_context.h"
31 #include "BKE_object.h"
32 #include "BKE_image.h"
34 #include "DEG_depsgraph.h"
36 #include "DNA_object_types.h"
37 #include "DNA_lamp_types.h"
39 #include "ED_screen.h"
40 #include "ED_gizmo_library.h"
42 #include "UI_resources.h"
44 #include "MEM_guardedalloc.h"
46 #include "RNA_access.h"
51 #include "view3d_intern.h" /* own include */
53 /* -------------------------------------------------------------------- */
55 /** \name Empty Image Gizmos
58 struct EmptyImageWidgetGroup {
66 /* translate callbacks */
67 static void gizmo_empty_image_prop_matrix_get(
68 const wmGizmo *gz, wmGizmoProperty *gz_prop,
71 float (*matrix)[4] = value_p;
72 BLI_assert(gz_prop->type->array_length == 16);
73 struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
74 const Object *ob = igzgroup->state.ob;
77 matrix[0][0] = ob->empty_drawsize;
78 matrix[1][1] = ob->empty_drawsize;
80 float dims[2] = {0.0f, 0.0f};
81 RNA_float_get_array(gz->ptr, "dimensions", dims);
82 dims[0] *= ob->empty_drawsize;
83 dims[1] *= ob->empty_drawsize;
85 matrix[3][0] = (ob->ima_ofs[0] * dims[0]) + (0.5f * dims[0]);
86 matrix[3][1] = (ob->ima_ofs[1] * dims[1]) + (0.5f * dims[1]);
89 static void gizmo_empty_image_prop_matrix_set(
90 const wmGizmo *gz, wmGizmoProperty *gz_prop,
93 const float (*matrix)[4] = value_p;
94 BLI_assert(gz_prop->type->array_length == 16);
95 struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
96 Object *ob = igzgroup->state.ob;
98 ob->empty_drawsize = matrix[0][0];
99 DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
102 RNA_float_get_array(gz->ptr, "dimensions", dims);
103 dims[0] *= ob->empty_drawsize;
104 dims[1] *= ob->empty_drawsize;
106 ob->ima_ofs[0] = (matrix[3][0] - (0.5f * dims[0])) / dims[0];
107 ob->ima_ofs[1] = (matrix[3][1] - (0.5f * dims[1])) / dims[1];
110 static bool WIDGETGROUP_empty_image_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
112 View3D *v3d = CTX_wm_view3d(C);
113 RegionView3D *rv3d = CTX_wm_region_view3d(C);
115 if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
116 (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
121 ViewLayer *view_layer = CTX_data_view_layer(C);
122 Base *base = BASACT(view_layer);
123 if (base && BASE_SELECTABLE(v3d, base)) {
124 Object *ob = base->object;
125 if (ob->type == OB_EMPTY) {
126 if (ob->empty_drawtype == OB_EMPTY_IMAGE) {
127 return BKE_object_empty_image_is_visible_in_view3d(ob, rv3d);
134 static void WIDGETGROUP_empty_image_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
136 struct EmptyImageWidgetGroup *igzgroup = MEM_mallocN(sizeof(struct EmptyImageWidgetGroup), __func__);
137 igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
138 wmGizmo *gz = igzgroup->gizmo;
139 RNA_enum_set(gz->ptr, "transform",
140 ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
142 gzgroup->customdata = igzgroup;
144 WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
146 UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
147 UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
150 static void WIDGETGROUP_empty_image_refresh(const bContext *C, wmGizmoGroup *gzgroup)
152 struct EmptyImageWidgetGroup *igzgroup = gzgroup->customdata;
153 wmGizmo *gz = igzgroup->gizmo;
154 ViewLayer *view_layer = CTX_data_view_layer(C);
155 Object *ob = OBACT(view_layer);
157 copy_m4_m4(gz->matrix_basis, ob->obmat);
159 RNA_enum_set(gz->ptr, "transform",
160 ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
161 ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
162 ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM);
164 igzgroup->state.ob = ob;
166 /* Use dimensions for aspect. */
167 if (ob->data != NULL) {
168 const Image *image = ob->data;
169 ImageUser iuser = *ob->iuser;
171 BKE_image_get_size_fl(ob->data, &iuser, size);
173 /* Get the image aspect even if the buffer is invalid */
174 if (image->aspx > image->aspy) {
175 size[1] *= image->aspy / image->aspx;
177 else if (image->aspx < image->aspy) {
178 size[0] *= image->aspx / image->aspy;
181 const float dims_max = max_ff(size[0], size[1]);
182 igzgroup->state.dims[0] = size[0] / dims_max;
183 igzgroup->state.dims[1] = size[1] / dims_max;
186 copy_v2_fl(igzgroup->state.dims, 1.0f);
188 RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
190 WM_gizmo_target_property_def_func(
192 &(const struct wmGizmoPropertyFnParams) {
193 .value_get_fn = gizmo_empty_image_prop_matrix_get,
194 .value_set_fn = gizmo_empty_image_prop_matrix_set,
195 .range_get_fn = NULL,
196 .user_data = igzgroup,
200 void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
202 gzgt->name = "Area Light Widgets";
203 gzgt->idname = "VIEW3D_GGT_empty_image";
205 gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
206 WM_GIZMOGROUPTYPE_3D |
207 WM_GIZMOGROUPTYPE_DEPTH_3D);
209 gzgt->poll = WIDGETGROUP_empty_image_poll;
210 gzgt->setup = WIDGETGROUP_empty_image_setup;
211 gzgt->refresh = WIDGETGROUP_empty_image_refresh;