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

grab3d_manipulator.c « manipulator_types « manipulator_library « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 605c089f818e38fb724b783f8c61daf0a3c1ecad (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * ***** 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 grab3d_manipulator.c
 *  \ingroup wm
 *
 * \name Grab Manipulator
 *
 * 3D Manipulator, also works in 2D views.
 *
 * \brief Simple manipulator to grab and translate.
 *
 * - `matrix[0]` is derived from Y and Z.
 * - `matrix[1]` currently not used.
 * - `matrix[2]` is the widget direction (for all manipulators).
 *
 */

#include "BIF_gl.h"
#include "BIF_glutil.h"

#include "BKE_context.h"

#include "BLI_math.h"

#include "ED_screen.h"
#include "ED_view3d.h"
#include "ED_manipulator_library.h"

#include "GPU_select.h"

#include "GPU_matrix.h"

#include "GPU_immediate.h"
#include "GPU_immediate_util.h"

#include "MEM_guardedalloc.h"

#include "RNA_access.h"
#include "RNA_define.h"

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

/* own includes */
#include "../manipulator_geometry.h"
#include "../manipulator_library_intern.h"

static void manipulator_grab_modal(
        bContext *C, wmManipulator *mpr, const wmEvent *event,
        eWM_ManipulatorTweak tweak_flag);

typedef struct GrabInteraction {
	float init_mval[2];

	/* only for when using properties */
	float init_prop_co[3];

	float init_matrix_basis[4][4];
	float init_scale_final;

	/* final output values, used for drawing */
	struct {
		float co_final[3];
	} output;
} GrabInteraction;

#define DIAL_RESOLUTION 32

/* -------------------------------------------------------------------- */

static void grab_geom_draw(
        const wmManipulator *mpr, const float col[4], const bool select)
{
#ifdef USE_MANIPULATOR_CUSTOM_DIAL
	UNUSED_VARS(grab3d, col, axis_modal_mat);
	wm_manipulator_geometryinfo_draw(&wm_manipulator_geom_data_grab3d, select);
#else
	const int draw_style = RNA_enum_get(mpr->ptr, "draw_style");
	const int draw_options = RNA_enum_get(mpr->ptr, "draw_options");
	const bool filled = (draw_options & ED_MANIPULATOR_GRAB_DRAW_FLAG_FILL) != 0;

	glLineWidth(mpr->line_width);

	Gwn_VertFormat *format = immVertexFormat();
	uint pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);

	immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

	immUniformColor4fv(col);

	if (draw_style == ED_MANIPULATOR_GRAB_STYLE_RING) {
		if (filled) {
			imm_draw_circle_fill(pos, 0, 0, 1.0f, DIAL_RESOLUTION);
		}
		else {
			imm_draw_circle_wire(pos, 0, 0, 1.0f, DIAL_RESOLUTION);
		}
	}
	else if (draw_style == ED_MANIPULATOR_GRAB_STYLE_CROSS) {
		immBegin(GWN_PRIM_LINES, 4);
		immVertex2f(pos,  1.0f,  1.0f);
		immVertex2f(pos, -1.0f, -1.0f);

		immVertex2f(pos, -1.0f,  1.0f);
		immVertex2f(pos,  1.0f, -1.0f);
		immEnd();
	}

	immUnbindProgram();

	UNUSED_VARS(select);
#endif
}

static void grab3d_get_translate(
        const wmManipulator *mpr, const wmEvent *event, const ARegion *ar,
        float co_delta[3])
{
	GrabInteraction *inter = mpr->interaction_data;
	const float mval_delta[2] = {
	    event->mval[0] - inter->init_mval[0],
	    event->mval[1] - inter->init_mval[1],
	};

	RegionView3D *rv3d = ar->regiondata;
	const float *co_ref = inter->init_prop_co;
	const float zfac = ED_view3d_calc_zfac(rv3d, co_ref, NULL);

	ED_view3d_win_to_delta(ar, mval_delta, co_delta, zfac);
}

static void grab3d_draw_intern(
        const bContext *UNUSED(C), wmManipulator *mpr,
        const bool select, const bool highlight)
{
	float col[4];
	float final_matrix[4][4];

	manipulator_color_get(mpr, highlight, col);

	copy_m4_m4(final_matrix, mpr->matrix_basis);
	if (mpr->flag & WM_MANIPULATOR_DRAW_OFFSET_SCALE) {
		mul_mat3_m4_fl(final_matrix, mpr->scale_final);
	}
	mul_m4_m4m4(final_matrix, final_matrix, mpr->matrix_offset);
	if ((mpr->flag & WM_MANIPULATOR_DRAW_OFFSET_SCALE) == 0) {
		mul_mat3_m4_fl(final_matrix, mpr->scale_final);
	}

	gpuPushMatrix();
	gpuMultMatrix(mpr->matrix_space);
	gpuMultMatrix(final_matrix);
	glEnable(GL_BLEND);

	grab_geom_draw(mpr, col, select);
	glDisable(GL_BLEND);
	gpuPopMatrix();

	if (mpr->interaction_data) {
		GrabInteraction *inter = mpr->interaction_data;

		copy_m4_m4(final_matrix, inter->init_matrix_basis);
		if (mpr->flag & WM_MANIPULATOR_DRAW_OFFSET_SCALE) {
			mul_mat3_m4_fl(final_matrix, inter->init_scale_final);
		}
		mul_m4_m4m4(final_matrix, final_matrix, mpr->matrix_offset);
		if ((mpr->flag & WM_MANIPULATOR_DRAW_OFFSET_SCALE) == 0) {
			mul_mat3_m4_fl(final_matrix, inter->init_scale_final);
		}

		gpuPushMatrix();
		gpuMultMatrix(mpr->matrix_space);
		gpuMultMatrix(final_matrix);
		glEnable(GL_BLEND);
		grab_geom_draw(mpr, (const float [4]){0.5f, 0.5f, 0.5f, 0.5f}, select);
		glDisable(GL_BLEND);
		gpuPopMatrix();
	}
}

static void manipulator_grab_draw_select(const bContext *C, wmManipulator *mpr, int select_id)
{
	GPU_select_load_id(select_id);
	grab3d_draw_intern(C, mpr, true, false);
}

static void manipulator_grab_draw(const bContext *C, wmManipulator *mpr)
{
	const bool is_modal = mpr->state & WM_MANIPULATOR_STATE_MODAL;
	const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0;

	(void)is_modal;

	glEnable(GL_BLEND);
	grab3d_draw_intern(C, mpr, false, is_highlight);
	glDisable(GL_BLEND);
}

static void manipulator_grab_modal(
        bContext *C, wmManipulator *mpr, const wmEvent *event,
        eWM_ManipulatorTweak UNUSED(tweak_flag))
{
	GrabInteraction *inter = mpr->interaction_data;

	if (CTX_wm_area(C)->spacetype == SPACE_VIEW3D) {
		grab3d_get_translate(mpr, event, CTX_wm_region(C), mpr->matrix_basis[3]);
	}
	else {
		float mval_proj_init[2], mval_proj_curr[2];
		if ((manipulator_window_project_2d(
		         C, mpr, inter->init_mval, 2, false, mval_proj_init) == false) ||
		    (manipulator_window_project_2d(
		         C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, mval_proj_curr) == false))
		{
			return;
		}
		sub_v2_v2v2(mpr->matrix_basis[3], mval_proj_curr, mval_proj_init);
		mpr->matrix_basis[3][2] = 0.0f;
	}

	add_v3_v3v3(inter->output.co_final, inter->init_prop_co, mpr->matrix_basis[3]);

	/* set the property for the operator and call its modal function */
	wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset");
	if (WM_manipulator_target_property_is_valid(mpr_prop)) {
		WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, inter->output.co_final);
	}
}

static void manipulator_grab_invoke(
        bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *event)
{
	GrabInteraction *inter = MEM_callocN(sizeof(GrabInteraction), __func__);

	inter->init_mval[0] = event->mval[0];
	inter->init_mval[1] = event->mval[1];

	copy_m4_m4(inter->init_matrix_basis, mpr->matrix_basis);
	inter->init_scale_final = mpr->scale_final;

	wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset");
	if (WM_manipulator_target_property_is_valid(mpr_prop)) {
		WM_manipulator_target_property_value_get_array(mpr, mpr_prop, inter->init_prop_co);
	}

	mpr->interaction_data = inter;
}


static int manipulator_grab_test_select(
        bContext *C, wmManipulator *mpr, const wmEvent *event)
{
	float point_local[2];

	if (manipulator_window_project_2d(
	        C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, true, point_local) == false)
	{
		return 0;
	}

	if (len_squared_v2(point_local) < SQUARE(mpr->scale_final)) {
		return true;
	}

	return 0;
}

static void manipulator_grab_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop)
{
	WM_manipulator_target_property_value_get_array(mpr, mpr_prop, mpr->matrix_basis[3]);
}

static int manipulator_grab_cursor_get(wmManipulator *UNUSED(mpr))
{
	return BC_HANDCURSOR;
}

/* -------------------------------------------------------------------- */
/** \name Grab Manipulator API
 *
 * \{ */

static void MANIPULATOR_WT_grab_3d(wmManipulatorType *wt)
{
	/* identifiers */
	wt->idname = "MANIPULATOR_WT_grab_3d";

	/* api callbacks */
	wt->draw = manipulator_grab_draw;
	wt->draw_select = manipulator_grab_draw_select;
	wt->test_select = manipulator_grab_test_select;
	wt->invoke = manipulator_grab_invoke;
	wt->property_update = manipulator_grab_property_update;
	wt->modal = manipulator_grab_modal;
	wt->cursor_get = manipulator_grab_cursor_get;

	wt->struct_size = sizeof(wmManipulator);

	/* rna */
	static EnumPropertyItem rna_enum_draw_style[] = {
		{ED_MANIPULATOR_GRAB_STYLE_RING, "RING", 0, "Ring", ""},
		{0, NULL, 0, NULL, NULL}
	};
	static EnumPropertyItem rna_enum_draw_options[] = {
		{ED_MANIPULATOR_GRAB_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""},
		{0, NULL, 0, NULL, NULL}
	};

	RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_GRAB_STYLE_RING, "Draw Style", "");
	RNA_def_enum_flag(wt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", "");

	WM_manipulatortype_target_property_def(wt, "offset", PROP_FLOAT, 3);
}

void ED_manipulatortypes_grab_3d(void)
{
	WM_manipulatortype_append(MANIPULATOR_WT_grab_3d);
}

/** \} */ // Grab Manipulator API