Welcome to mirror list, hosted at ThFree Co, Russian Federation.

value2d_gizmo_group.c « gizmo_group_types « gizmo_library « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 274d35269d19be7203889f69aaab07c077439ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * ***** 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 *****
 */

/** \file value2d_gizmo_group.c
 *  \ingroup edgizmolib
 *
 * \name 2D Value Gizmo
 *
 * \brief Gizmo that edits a value for operator redo.
 */

#include "BLI_utildefines.h"

#include "BKE_context.h"

#include "ED_undo.h"
#include "ED_screen.h"
#include "ED_gizmo_library.h"

#include "UI_resources.h"

#include "MEM_guardedalloc.h"

#include "RNA_access.h"

#include "WM_api.h"
#include "WM_types.h"

/* -------------------------------------------------------------------- */
/** \name Value Gizmo
 * \{ */

struct ValueOpRedoGroup {
	wmGizmo *gizmo;
	struct {
		const bContext *context;  /* needed for redo. */
		wmOperator *op;
	} state;
};

static void gizmo_op_redo_exec(struct ValueOpRedoGroup *igzgroup)
{
	wmOperator *op = igzgroup->state.op;
	if (op == WM_operator_last_redo((bContext *)igzgroup->state.context)) {
		ED_undo_operator_repeat((bContext *)igzgroup->state.context, op);
	}
}

/* translate callbacks */
static void gizmo_value_operator_redo_value_get(
        const wmGizmo *gz, wmGizmoProperty *gz_prop,
        void *value_p)
{
	float *value = value_p;
	BLI_assert(gz_prop->type->array_length == 1);
	UNUSED_VARS_NDEBUG(gz_prop);

	struct ValueOpRedoGroup *igzgroup = gz->parent_gzgroup->customdata;
	wmOperator *op = igzgroup->state.op;
	*value = RNA_property_float_get(op->ptr, op->type->prop);
}

static void gizmo_value_operator_redo_value_set(
        const wmGizmo *gz, wmGizmoProperty *gz_prop,
        const void *value_p)
{
	const float *value = value_p;
	BLI_assert(gz_prop->type->array_length == 1);
	UNUSED_VARS_NDEBUG(gz_prop);

	struct ValueOpRedoGroup *igzgroup = gz->parent_gzgroup->customdata;
	wmOperator *op = igzgroup->state.op;
	RNA_property_float_set(op->ptr, op->type->prop, *value);
	gizmo_op_redo_exec(igzgroup);
}

static void WIDGETGROUP_value_operator_redo_modal_from_setup(
        const bContext *C, wmGizmoGroup *gzgroup)
{
	/* Start off dragging. */
	wmWindow *win = CTX_wm_window(C);
	wmGizmo *gz = gzgroup->gizmos.first;
	wmGizmoMap *gzmap = gzgroup->parent_gzmap;
	WM_gizmo_modal_set_from_setup(
	        gzmap, (bContext *)C, gz, 0, win->eventstate);
}

static void WIDGETGROUP_value_operator_redo_setup(const bContext *C, wmGizmoGroup *gzgroup)
{
	struct ValueOpRedoGroup *igzgroup = MEM_mallocN(sizeof(struct ValueOpRedoGroup), __func__);

	igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_value_2d", gzgroup, NULL);
	wmGizmo *gz = igzgroup->gizmo;

	igzgroup->state.context = C;
	igzgroup->state.op = WM_operator_last_redo(C);

	gzgroup->customdata = igzgroup;

	UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
	UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);

	WM_gizmo_target_property_def_func(
	        gz, "offset",
	        &(const struct wmGizmoPropertyFnParams) {
	            .value_get_fn = gizmo_value_operator_redo_value_get,
	            .value_set_fn = gizmo_value_operator_redo_value_set,
	            .range_get_fn = NULL,
	            .user_data = igzgroup,
	        });

	/* Become modal as soon as it's started. */
	WIDGETGROUP_value_operator_redo_modal_from_setup(C, gzgroup);
}

static void WIDGETGROUP_value_operator_redo_refresh(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
{
	struct ValueOpRedoGroup *igzgroup = gzgroup->customdata;
	wmGizmo *gz = igzgroup->gizmo;
	wmOperator *op = WM_operator_last_redo((bContext *)igzgroup->state.context);
	wmGizmoMap *gzmap = gzgroup->parent_gzmap;

	/* FIXME */
	extern struct wmGizmo *wm_gizmomap_modal_get(struct wmGizmoMap *gzmap);
	if ((op != igzgroup->state.op) ||
	    (wm_gizmomap_modal_get(gzmap) != gz))
	{
		WM_gizmo_group_type_unlink_delayed_ptr(gzgroup->type);
	}
}

static void WM_GGT_value_operator_redo(wmGizmoGroupType *gzgt)
{
	gzgt->name = "Value Operator Redo";
	gzgt->idname = "WM_GGT_value_operator_redo";

	/* FIXME, allow multiple. */
	gzgt->flag = WM_GIZMOGROUPTYPE_3D | WM_GIZMOGROUPTYPE_TOOL_INIT;

	gzgt->gzmap_params.spaceid = SPACE_VIEW3D;
	gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;


	gzgt->setup = WIDGETGROUP_value_operator_redo_setup;
	gzgt->refresh = WIDGETGROUP_value_operator_redo_refresh;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Public API
 * \{ */

void ED_gizmogrouptypes_value_2d(void)
{
	WM_gizmogrouptype_append(WM_GGT_value_operator_redo);
}

/** \} */