2017-08-10 14:05:43 +10:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2018-07-14 23:16:34 +02:00
|
|
|
/** \file blender/editors/space_view3d/view3d_gizmo_empty.c
|
2017-08-10 14:05:43 +10:00
|
|
|
* \ingroup spview3d
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "BLI_blenlib.h"
|
|
|
|
#include "BLI_math.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
#include "BKE_context.h"
|
|
|
|
#include "BKE_object.h"
|
|
|
|
#include "BKE_image.h"
|
|
|
|
|
2018-10-09 14:36:15 +02:00
|
|
|
#include "DEG_depsgraph.h"
|
|
|
|
|
2017-08-10 14:05:43 +10:00
|
|
|
#include "DNA_object_types.h"
|
|
|
|
#include "DNA_lamp_types.h"
|
|
|
|
|
|
|
|
#include "ED_screen.h"
|
2018-07-14 23:16:34 +02:00
|
|
|
#include "ED_gizmo_library.h"
|
2017-08-10 14:05:43 +10:00
|
|
|
|
|
|
|
#include "UI_resources.h"
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "RNA_access.h"
|
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
#include "WM_types.h"
|
|
|
|
|
|
|
|
#include "view3d_intern.h" /* own include */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
2018-07-14 23:49:00 +02:00
|
|
|
/** \name Empty Image Gizmos
|
2017-08-10 14:05:43 +10:00
|
|
|
* \{ */
|
|
|
|
|
|
|
|
struct EmptyImageWidgetGroup {
|
2018-07-14 23:49:00 +02:00
|
|
|
wmGizmo *gizmo;
|
2017-08-10 14:05:43 +10:00
|
|
|
struct {
|
|
|
|
Object *ob;
|
|
|
|
float dims[2];
|
|
|
|
} state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* translate callbacks */
|
2018-07-14 23:49:00 +02:00
|
|
|
static void gizmo_empty_image_prop_matrix_get(
|
2018-07-15 14:24:10 +02:00
|
|
|
const wmGizmo *gz, wmGizmoProperty *gz_prop,
|
2017-08-10 14:05:43 +10:00
|
|
|
void *value_p)
|
|
|
|
{
|
2017-08-31 01:38:51 +10:00
|
|
|
float (*matrix)[4] = value_p;
|
2018-07-15 14:24:10 +02:00
|
|
|
BLI_assert(gz_prop->type->array_length == 16);
|
|
|
|
struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
|
|
|
|
const Object *ob = igzgroup->state.ob;
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2017-08-31 01:38:51 +10:00
|
|
|
unit_m4(matrix);
|
|
|
|
matrix[0][0] = ob->empty_drawsize;
|
|
|
|
matrix[1][1] = ob->empty_drawsize;
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2017-08-10 17:41:47 +02:00
|
|
|
float dims[2] = {0.0f, 0.0f};
|
2018-07-15 14:24:10 +02:00
|
|
|
RNA_float_get_array(gz->ptr, "dimensions", dims);
|
2017-08-10 14:05:43 +10:00
|
|
|
dims[0] *= ob->empty_drawsize;
|
|
|
|
dims[1] *= ob->empty_drawsize;
|
|
|
|
|
2017-08-31 01:38:51 +10:00
|
|
|
matrix[3][0] = (ob->ima_ofs[0] * dims[0]) + (0.5f * dims[0]);
|
|
|
|
matrix[3][1] = (ob->ima_ofs[1] * dims[1]) + (0.5f * dims[1]);
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
|
|
|
|
2018-07-14 23:49:00 +02:00
|
|
|
static void gizmo_empty_image_prop_matrix_set(
|
2018-07-15 14:24:10 +02:00
|
|
|
const wmGizmo *gz, wmGizmoProperty *gz_prop,
|
2017-08-10 14:05:43 +10:00
|
|
|
const void *value_p)
|
|
|
|
{
|
2017-08-31 01:38:51 +10:00
|
|
|
const float (*matrix)[4] = value_p;
|
2018-07-15 14:24:10 +02:00
|
|
|
BLI_assert(gz_prop->type->array_length == 16);
|
|
|
|
struct EmptyImageWidgetGroup *igzgroup = gz_prop->custom_func.user_data;
|
|
|
|
Object *ob = igzgroup->state.ob;
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2017-08-31 01:38:51 +10:00
|
|
|
ob->empty_drawsize = matrix[0][0];
|
2018-10-09 12:45:00 +00:00
|
|
|
DEG_id_tag_update(&ob->id, DEG_TAG_TRANSFORM);
|
2017-08-31 01:38:51 +10:00
|
|
|
|
2017-08-10 14:05:43 +10:00
|
|
|
float dims[2];
|
2018-07-15 14:24:10 +02:00
|
|
|
RNA_float_get_array(gz->ptr, "dimensions", dims);
|
2017-08-10 14:05:43 +10:00
|
|
|
dims[0] *= ob->empty_drawsize;
|
|
|
|
dims[1] *= ob->empty_drawsize;
|
|
|
|
|
2017-08-31 01:38:51 +10:00
|
|
|
ob->ima_ofs[0] = (matrix[3][0] - (0.5f * dims[0])) / dims[0];
|
|
|
|
ob->ima_ofs[1] = (matrix[3][1] - (0.5f * dims[1])) / dims[1];
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
static bool WIDGETGROUP_empty_image_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
|
2017-08-10 14:05:43 +10:00
|
|
|
{
|
2018-06-05 14:24:58 +02:00
|
|
|
View3D *v3d = CTX_wm_view3d(C);
|
2018-10-31 13:35:53 +01:00
|
|
|
RegionView3D *rv3d = CTX_wm_region_view3d(C);
|
2018-07-11 10:38:01 +02:00
|
|
|
|
|
|
|
if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
|
2018-07-15 14:24:10 +02:00
|
|
|
(v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
|
2018-07-11 10:38:01 +02:00
|
|
|
{
|
2018-06-05 14:24:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-10 14:05:43 +10:00
|
|
|
Object *ob = CTX_data_active_object(C);
|
|
|
|
|
|
|
|
if (ob && ob->type == OB_EMPTY) {
|
2018-11-01 07:31:49 +11:00
|
|
|
if (ob->empty_drawtype == OB_EMPTY_IMAGE) {
|
2018-11-19 19:33:09 +01:00
|
|
|
return BKE_image_empty_visible_in_view3d(ob, rv3d);
|
2018-10-31 13:35:53 +01:00
|
|
|
}
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
static void WIDGETGROUP_empty_image_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
|
2017-08-10 14:05:43 +10:00
|
|
|
{
|
2018-07-15 14:24:10 +02:00
|
|
|
struct EmptyImageWidgetGroup *igzgroup = MEM_mallocN(sizeof(struct EmptyImageWidgetGroup), __func__);
|
|
|
|
igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
|
|
|
|
wmGizmo *gz = igzgroup->gizmo;
|
|
|
|
RNA_enum_set(gz->ptr, "transform",
|
2018-07-14 23:49:00 +02:00
|
|
|
ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
gzgroup->customdata = igzgroup;
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
|
|
|
|
UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
static void WIDGETGROUP_empty_image_refresh(const bContext *C, wmGizmoGroup *gzgroup)
|
2017-08-10 14:05:43 +10:00
|
|
|
{
|
2018-07-15 14:24:10 +02:00
|
|
|
struct EmptyImageWidgetGroup *igzgroup = gzgroup->customdata;
|
2017-08-10 14:05:43 +10:00
|
|
|
Object *ob = CTX_data_active_object(C);
|
2018-07-15 14:24:10 +02:00
|
|
|
wmGizmo *gz = igzgroup->gizmo;
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
copy_m4_m4(gz->matrix_basis, ob->obmat);
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
RNA_enum_set(gz->ptr, "transform",
|
2018-07-14 23:49:00 +02:00
|
|
|
ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE |
|
|
|
|
ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE |
|
|
|
|
ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM);
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
igzgroup->state.ob = ob;
|
2017-08-10 14:05:43 +10:00
|
|
|
|
|
|
|
/* Use dimensions for aspect. */
|
|
|
|
if (ob->data != NULL) {
|
2017-08-31 02:36:47 +10:00
|
|
|
const Image *image = ob->data;
|
2017-08-10 14:05:43 +10:00
|
|
|
ImageUser iuser = *ob->iuser;
|
2017-08-31 02:36:47 +10:00
|
|
|
float size[2];
|
|
|
|
BKE_image_get_size_fl(ob->data, &iuser, size);
|
|
|
|
|
|
|
|
/* Get the image aspect even if the buffer is invalid */
|
|
|
|
if (image->aspx > image->aspy) {
|
|
|
|
size[1] *= image->aspy / image->aspx;
|
|
|
|
}
|
|
|
|
else if (image->aspx < image->aspy) {
|
|
|
|
size[0] *= image->aspx / image->aspy;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float dims_max = max_ff(size[0], size[1]);
|
2018-07-15 14:24:10 +02:00
|
|
|
igzgroup->state.dims[0] = size[0] / dims_max;
|
|
|
|
igzgroup->state.dims[1] = size[1] / dims_max;
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
|
|
|
else {
|
2018-07-15 14:24:10 +02:00
|
|
|
copy_v2_fl(igzgroup->state.dims, 1.0f);
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
2018-07-15 14:24:10 +02:00
|
|
|
RNA_float_set_array(gz->ptr, "dimensions", igzgroup->state.dims);
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-14 23:49:00 +02:00
|
|
|
WM_gizmo_target_property_def_func(
|
2018-07-15 14:24:10 +02:00
|
|
|
gz, "matrix",
|
2018-07-14 23:49:00 +02:00
|
|
|
&(const struct wmGizmoPropertyFnParams) {
|
|
|
|
.value_get_fn = gizmo_empty_image_prop_matrix_get,
|
|
|
|
.value_set_fn = gizmo_empty_image_prop_matrix_set,
|
2017-08-10 14:05:43 +10:00
|
|
|
.range_get_fn = NULL,
|
2018-07-15 14:24:10 +02:00
|
|
|
.user_data = igzgroup,
|
2017-08-10 14:05:43 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
void VIEW3D_GGT_empty_image(wmGizmoGroupType *gzgt)
|
2017-08-10 14:05:43 +10:00
|
|
|
{
|
2018-07-15 14:24:10 +02:00
|
|
|
gzgt->name = "Area Light Widgets";
|
|
|
|
gzgt->idname = "VIEW3D_GGT_empty_image";
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
gzgt->flag |= (WM_GIZMOGROUPTYPE_PERSISTENT |
|
2018-07-14 23:49:00 +02:00
|
|
|
WM_GIZMOGROUPTYPE_3D |
|
|
|
|
WM_GIZMOGROUPTYPE_DEPTH_3D);
|
2017-08-10 14:05:43 +10:00
|
|
|
|
2018-07-15 14:24:10 +02:00
|
|
|
gzgt->poll = WIDGETGROUP_empty_image_poll;
|
|
|
|
gzgt->setup = WIDGETGROUP_empty_image_setup;
|
|
|
|
gzgt->refresh = WIDGETGROUP_empty_image_refresh;
|
2017-08-10 14:05:43 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|