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

grab3d_manipulator.c « manipulator_library « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c794c99160cb16d2ab117f78179eae1a7fca2dce (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
/*
 * ***** 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
 *
 * \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 "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, const int flag);

typedef struct GrabManipulator {
	wmManipulator manipulator;
	int style;
} GrabManipulator;

typedef struct GrabInteraction {
	float init_mval[2];

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

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

#define DIAL_WIDTH       1.0f
#define DIAL_RESOLUTION 32

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

static void grab_geom_draw(
        const GrabManipulator *grab3d, 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 bool filled = (grab3d->style == ED_MANIPULATOR_DIAL_STYLE_RING_FILLED);

	glLineWidth(grab3d->manipulator.line_width);

	VertexFormat *format = immVertexFormat();
	uint pos = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);

	immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

	immUniformColor4fv(col);

	if (filled) {
		imm_draw_circle_fill(pos, 0, 0, 1.0, DIAL_RESOLUTION);
	}
	else {
		imm_draw_circle_wire(pos, 0, 0, 1.0, DIAL_RESOLUTION);
	}

	immUnbindProgram();

	UNUSED_VARS(select);
#endif
}

static void grab3d_get_translate(
        const GrabManipulator *grab, const wmEvent *event, const ARegion *ar,
        float co_delta[3])
{
	GrabInteraction *inter = grab->manipulator.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 *C, GrabManipulator *grab,
        const bool select, const bool highlight)
{
	float mat[4][4];
	float col[4];

	BLI_assert(CTX_wm_area(C)->spacetype == SPACE_VIEW3D);

	manipulator_color_get(&grab->manipulator, highlight, col);

	copy_m4_m4(mat, grab->manipulator.matrix);
	mul_mat3_m4_fl(mat, grab->manipulator.scale);

	gpuPushMatrix();
	if (grab->manipulator.interaction_data) {
		GrabInteraction *inter = grab->manipulator.interaction_data;
		gpuTranslate3fv(inter->output.co_ofs);
	}
	gpuMultMatrix(mat);
	gpuMultMatrix(grab->manipulator.matrix_offset);
	glEnable(GL_BLEND);
	grab_geom_draw(grab, col, select);
	glDisable(GL_BLEND);
	gpuPopMatrix();

	if (grab->manipulator.interaction_data) {
		gpuPushMatrix();
		gpuMultMatrix(mat);
		gpuMultMatrix(grab->manipulator.matrix_offset);
		glEnable(GL_BLEND);
		grab_geom_draw(grab, (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 selectionbase)
{
	GrabManipulator *grab = (GrabManipulator *)mpr;

	GPU_select_load_id(selectionbase);
	grab3d_draw_intern(C, grab, true, false);
}

static void manipulator_grab_draw(const bContext *C, wmManipulator *mpr)
{
	GrabManipulator *grab = (GrabManipulator *)mpr;
	const bool active = mpr->state & WM_MANIPULATOR_STATE_ACTIVE;
	const bool highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0;

	BLI_assert(grab->style != -1);

	(void)active;

	glEnable(GL_BLEND);
	grab3d_draw_intern(C, grab, false, highlight);
	glDisable(GL_BLEND);
}

static void manipulator_grab_modal(bContext *C, wmManipulator *mpr, const wmEvent *event, const int UNUSED(flag))
{
	GrabManipulator *grab = (GrabManipulator *)mpr;

	GrabInteraction *inter = grab->manipulator.interaction_data;

	grab3d_get_translate(grab, event, CTX_wm_region(C), inter->output.co_ofs);

	add_v3_v3v3(inter->output.co_final, inter->init_prop_co, inter->output.co_ofs);

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

static void manipulator_grab_setup(wmManipulator *mpr)
{
	GrabManipulator *grab = (GrabManipulator *)mpr;

	grab->style = -1;
}

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];

	wmManipulatorProperty *mpr_prop = WM_manipulator_property_find(mpr, "offset");
	if (mpr_prop && WM_manipulator_property_is_valid(mpr_prop)) {
		WM_manipulator_property_value_get_array(mpr, mpr_prop, inter->init_prop_co, 3);
	}

	mpr->interaction_data = inter;
}

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

#define ASSERT_TYPE_CHECK(mpr) BLI_assert(mpr->type->draw == manipulator_grab_draw)

void ED_manipulator_grab3d_set_style(wmManipulator *mpr, int style)
{
	ASSERT_TYPE_CHECK(mpr);
	GrabManipulator *grab = (GrabManipulator *)mpr;
	grab->style = style;
}

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->setup = manipulator_grab_setup;
	wt->invoke = manipulator_grab_invoke;
	wt->modal = manipulator_grab_modal;

	wt->struct_size = sizeof(GrabManipulator);
}

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

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