Fix T43427: Particle system children sometimes not generated on reload
[blender.git] / source / blender / editors / sculpt_paint / paint_utils.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/sculpt_paint/paint_utils.c
29  *  \ingroup edsculpt
30  */
31
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_material_types.h"
39
40 #include "DNA_scene_types.h"
41 #include "DNA_brush_types.h"
42
43 #include "BLI_math.h"
44 #include "BLI_math_color.h"
45 #include "BLI_utildefines.h"
46 #include "BLI_listbase.h"
47 #include "BLI_rect.h"
48
49 #include "BLF_translation.h"
50
51 #include "BKE_brush.h"
52 #include "BKE_context.h"
53 #include "BKE_DerivedMesh.h"
54 #include "BKE_image.h"
55 #include "BKE_material.h"
56 #include "BKE_paint.h"
57 #include "BKE_report.h"
58
59 #include "RNA_access.h"
60 #include "RNA_define.h"
61
62 #include "BIF_gl.h"
63 #include "BIF_glutil.h"
64
65 #include "IMB_colormanagement.h"
66 #include "IMB_imbuf_types.h"
67 #include "IMB_imbuf.h"
68
69 #include "RE_render_ext.h"
70
71 #include "ED_view3d.h"
72 #include "ED_screen.h"
73
74 #include "BLI_sys_types.h"
75 #include "ED_mesh.h" /* for face mask functions */
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "paint_intern.h"
81
82 /* Convert the object-space axis-aligned bounding box (expressed as
83  * its minimum and maximum corners) into a screen-space rectangle,
84  * returns zero if the result is empty */
85 bool paint_convert_bb_to_rect(rcti *rect,
86                               const float bb_min[3],
87                               const float bb_max[3],
88                               const ARegion *ar,
89                               RegionView3D *rv3d,
90                               Object *ob)
91 {
92         float projection_mat[4][4];
93         int i, j, k;
94
95         BLI_rcti_init_minmax(rect);
96
97         /* return zero if the bounding box has non-positive volume */
98         if (bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
99                 return 0;
100
101         ED_view3d_ob_project_mat_get(rv3d, ob, projection_mat);
102
103         for (i = 0; i < 2; ++i) {
104                 for (j = 0; j < 2; ++j) {
105                         for (k = 0; k < 2; ++k) {
106                                 float vec[3], proj[2];
107                                 int proj_i[2];
108                                 vec[0] = i ? bb_min[0] : bb_max[0];
109                                 vec[1] = j ? bb_min[1] : bb_max[1];
110                                 vec[2] = k ? bb_min[2] : bb_max[2];
111                                 /* convert corner to screen space */
112                                 ED_view3d_project_float_v2_m4(ar, vec, proj, projection_mat);
113                                 /* expand 2D rectangle */
114
115                                 /* we could project directly to int? */
116                                 proj_i[0] = proj[0];
117                                 proj_i[1] = proj[1];
118
119                                 BLI_rcti_do_minmax_v(rect, proj_i);
120                         }
121                 }
122         }
123
124         /* return false if the rectangle has non-positive area */
125         return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
126 }
127
128 /* Get four planes in object-space that describe the projection of
129  * screen_rect from screen into object-space (essentially converting a
130  * 2D screens-space bounding box into four 3D planes) */
131 void paint_calc_redraw_planes(float planes[4][4],
132                               const ARegion *ar,
133                               RegionView3D *rv3d,
134                               Object *ob,
135                               const rcti *screen_rect)
136 {
137         BoundBox bb;
138         bglMats mats;
139         rcti rect;
140
141         memset(&bb, 0, sizeof(BoundBox));
142         view3d_get_transformation(ar, rv3d, ob, &mats);
143
144         /* use some extra space just in case */
145         rect = *screen_rect;
146         rect.xmin -= 2;
147         rect.xmax += 2;
148         rect.ymin -= 2;
149         rect.ymax += 2;
150
151         ED_view3d_clipping_calc(&bb, planes, &mats, &rect);
152         negate_m4(planes);
153 }
154
155 float paint_calc_object_space_radius(ViewContext *vc, const float center[3],
156                                      float pixel_radius)
157 {
158         Object *ob = vc->obact;
159         float delta[3], scale, loc[3];
160         const float mval_f[2] = {pixel_radius, 0.0f};
161         float zfac;
162
163         mul_v3_m4v3(loc, ob->obmat, center);
164
165         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
166         ED_view3d_win_to_delta(vc->ar, mval_f, delta, zfac);
167
168         scale = fabsf(mat4_to_scale(ob->obmat));
169         scale = (scale == 0.0f) ? 1.0f : scale;
170
171         return len_v3(delta) / scale;
172 }
173
174 float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, int thread)
175 {
176         float intensity, rgba[4];
177         float co[3] = {u, v, 0.0f};
178
179         externtex(mtex, co, &intensity,
180                   rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false);
181
182         return intensity;
183 }
184
185 void paint_get_tex_pixel_col(MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace)
186 {
187         float co[3] = {u, v, 0.0f};
188         int hasrgb;
189         float intensity;
190
191         hasrgb = externtex(mtex, co, &intensity,
192                            rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false);
193         if (!hasrgb) {
194                 rgba[0] = intensity;
195                 rgba[1] = intensity;
196                 rgba[2] = intensity;
197                 rgba[3] = 1.0f;
198         }
199
200         if (convert_to_linear)
201                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, colorspace);
202
203         linearrgb_to_srgb_v3_v3(rgba, rgba);
204
205         CLAMP(rgba[0], 0.0f, 1.0f);
206         CLAMP(rgba[1], 0.0f, 1.0f);
207         CLAMP(rgba[2], 0.0f, 1.0f);
208         CLAMP(rgba[3], 0.0f, 1.0f);
209 }
210
211 void paint_stroke_operator_properties(wmOperatorType *ot)
212 {
213         static EnumPropertyItem stroke_mode_items[] = {
214                 {BRUSH_STROKE_NORMAL, "NORMAL", 0, "Normal", "Apply brush normally"},
215                 {BRUSH_STROKE_INVERT, "INVERT", 0, "Invert", "Invert action of brush for duration of stroke"},
216                 {BRUSH_STROKE_SMOOTH, "SMOOTH", 0, "Smooth", "Switch brush to smooth mode for duration of stroke"},
217                 {0}
218         };
219
220         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
221
222         RNA_def_enum(ot->srna, "mode", stroke_mode_items, BRUSH_STROKE_NORMAL, 
223                      "Stroke Mode",
224                      "Action taken when a paint stroke is made");
225         
226 }
227
228 /* 3D Paint */
229
230 static void imapaint_project(float matrix[4][4], const float co[3], float pco[4])
231 {
232         copy_v3_v3(pco, co);
233         pco[3] = 1.0f;
234
235         mul_m4_v4(matrix, pco);
236 }
237
238 static void imapaint_tri_weights(float matrix[4][4], GLint view[4],
239                                  const float v1[3], const float v2[3], const float v3[3],
240                                  const float co[2], float w[3])
241 {
242         float pv1[4], pv2[4], pv3[4], h[3], divw;
243         float wmat[3][3], invwmat[3][3];
244
245         /* compute barycentric coordinates */
246
247         /* project the verts */
248         imapaint_project(matrix, v1, pv1);
249         imapaint_project(matrix, v2, pv2);
250         imapaint_project(matrix, v3, pv3);
251
252         /* do inverse view mapping, see gluProject man page */
253         h[0] = (co[0] - view[0]) * 2.0f / view[2] - 1.0f;
254         h[1] = (co[1] - view[1]) * 2.0f / view[3] - 1.0f;
255         h[2] = 1.0f;
256
257         /* solve for (w1,w2,w3)/perspdiv in:
258          * h * perspdiv = Project * Model * (w1 * v1 + w2 * v2 + w3 * v3) */
259
260         wmat[0][0] = pv1[0];  wmat[1][0] = pv2[0];  wmat[2][0] = pv3[0];
261         wmat[0][1] = pv1[1];  wmat[1][1] = pv2[1];  wmat[2][1] = pv3[1];
262         wmat[0][2] = pv1[3];  wmat[1][2] = pv2[3];  wmat[2][2] = pv3[3];
263
264         invert_m3_m3(invwmat, wmat);
265         mul_m3_v3(invwmat, h);
266
267         copy_v3_v3(w, h);
268
269         /* w is still divided by perspdiv, make it sum to one */
270         divw = w[0] + w[1] + w[2];
271         if (divw != 0.0f) {
272                 mul_v3_fl(w, 1.0f / divw);
273         }
274 }
275
276 /* compute uv coordinates of mouse in face */
277 static void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const int xy[2], float uv[2])
278 {
279         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
280         MTFace *tf_base, *tf;
281         Material *ma;
282         TexPaintSlot *slot;
283         int numfaces = dm->getNumTessFaces(dm), a, findex;
284         float p[2], w[3], absw, minabsw;
285         MFace mf;
286         MVert mv[4];
287         float matrix[4][4], proj[4][4];
288         GLint view[4];
289         ImagePaintMode mode = scene->toolsettings->imapaint.mode;
290
291         /* compute barycentric coordinates */
292
293         /* double lookup */
294         const int *index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
295         const int *index_mp_to_orig  = dm->getPolyDataArray(dm, CD_ORIGINDEX);
296         if (index_mf_to_mpoly == NULL) {
297                 index_mp_to_orig = NULL;
298         }
299
300         /* get the needed opengl matrices */
301         glGetIntegerv(GL_VIEWPORT, view);
302         glGetFloatv(GL_MODELVIEW_MATRIX,  (float *)matrix);
303         glGetFloatv(GL_PROJECTION_MATRIX, (float *)proj);
304         view[0] = view[1] = 0;
305         mul_m4_m4m4(matrix, matrix, ob->obmat);
306         mul_m4_m4m4(matrix, proj, matrix);
307
308         minabsw = 1e10;
309         uv[0] = uv[1] = 0.0;
310
311         /* test all faces in the derivedmesh with the original index of the picked face */
312         for (a = 0; a < numfaces; a++) {
313                 findex = index_mf_to_mpoly ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
314
315                 if (findex == faceindex) {
316                         dm->getTessFace(dm, a, &mf);
317
318                         dm->getVert(dm, mf.v1, &mv[0]);
319                         dm->getVert(dm, mf.v2, &mv[1]);
320                         dm->getVert(dm, mf.v3, &mv[2]);
321                         if (mf.v4)
322                                 dm->getVert(dm, mf.v4, &mv[3]);
323
324                         if (mode == IMAGEPAINT_MODE_MATERIAL) {
325                                 ma = dm->mat[mf.mat_nr];
326                                 slot = &ma->texpaintslot[ma->paint_active_slot];
327
328                                 if (!(slot && slot->uvname && (tf_base = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, slot->uvname))))
329                                         tf_base = CustomData_get_layer(&dm->faceData, CD_MTFACE);
330
331                                 tf = &tf_base[a];
332                         }
333                         else {
334                                 tf_base = CustomData_get_layer(&dm->faceData, CD_MTFACE);
335                                 tf = &tf_base[a];
336                         }
337
338                         p[0] = xy[0];
339                         p[1] = xy[1];
340
341                         if (mf.v4) {
342                                 /* the triangle with the largest absolute values is the one
343                                  * with the most negative weights */
344                                 imapaint_tri_weights(matrix, view, mv[0].co, mv[1].co, mv[3].co, p, w);
345                                 absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
346                                 if (absw < minabsw) {
347                                         uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[3][0] * w[2];
348                                         uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[3][1] * w[2];
349                                         minabsw = absw;
350                                 }
351
352                                 imapaint_tri_weights(matrix, view, mv[1].co, mv[2].co, mv[3].co, p, w);
353                                 absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
354                                 if (absw < minabsw) {
355                                         uv[0] = tf->uv[1][0] * w[0] + tf->uv[2][0] * w[1] + tf->uv[3][0] * w[2];
356                                         uv[1] = tf->uv[1][1] * w[0] + tf->uv[2][1] * w[1] + tf->uv[3][1] * w[2];
357                                         minabsw = absw;
358                                 }
359                         }
360                         else {
361                                 imapaint_tri_weights(matrix, view, mv[0].co, mv[1].co, mv[2].co, p, w);
362                                 absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
363                                 if (absw < minabsw) {
364                                         uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[2][0] * w[2];
365                                         uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[2][1] * w[2];
366                                         minabsw = absw;
367                                 }
368                         }
369                 }
370         }
371
372         dm->release(dm);
373 }
374
375 /* returns 0 if not found, otherwise 1 */
376 static int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int *r_index, unsigned int totface)
377 {
378         if (totface == 0)
379                 return 0;
380
381         /* sample only on the exact position */
382         *r_index = view3d_sample_backbuf(vc, mval[0], mval[1]);
383
384         if ((*r_index) == 0 || (*r_index) > (unsigned int)totface) {
385                 return 0;
386         }
387
388         (*r_index)--;
389
390         return 1;
391 }
392
393
394 static Image *imapaint_face_image(Object *ob, Mesh *me, int face_index)
395 {
396         Image *ima;
397         MPoly *mp = me->mpoly + face_index;
398         Material *ma = give_current_material(ob, mp->mat_nr + 1);;
399         ima = ma && ma->texpaintslot ? ma->texpaintslot[ma->paint_active_slot].ima : NULL;
400
401         return ima;
402 }
403
404 /* Uses symm to selectively flip any axis of a coordinate. */
405 void flip_v3_v3(float out[3], const float in[3], const char symm)
406 {
407         if (symm & PAINT_SYMM_X)
408                 out[0] = -in[0];
409         else
410                 out[0] = in[0];
411         if (symm & PAINT_SYMM_Y)
412                 out[1] = -in[1];
413         else
414                 out[1] = in[1];
415         if (symm & PAINT_SYMM_Z)
416                 out[2] = -in[2];
417         else
418                 out[2] = in[2];
419 }
420
421 /* used for both 3d view and image window */
422 void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_proj, bool use_palette)
423 {
424         Scene *scene = CTX_data_scene(C);
425         Paint *paint = BKE_paint_get_active_from_context(C);
426         Palette *palette = BKE_paint_palette(paint);
427         PaletteColor *color;
428         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
429         unsigned int col;
430         const unsigned char *cp;
431
432         CLAMP(x, 0, ar->winx);
433         CLAMP(y, 0, ar->winy);
434         
435         if (use_palette) {
436                 if (!palette) {
437                         palette = BKE_palette_add(CTX_data_main(C), "Palette");
438                         BKE_paint_palette_set(paint, palette);
439                 }
440
441                 color = BKE_palette_color_add(palette);
442         }
443
444
445         if (CTX_wm_view3d(C) && texpaint_proj) {
446                 /* first try getting a colour directly from the mesh faces if possible */
447                 Object *ob = OBACT;
448                 bool sample_success = false;
449                 ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
450                 bool use_material = (imapaint->mode == IMAGEPAINT_MODE_MATERIAL);
451
452                 if (ob) {
453                         Mesh *me = (Mesh *)ob->data;
454                         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
455
456                         ViewContext vc;
457                         const int mval[2] = {x, y};
458                         unsigned int faceindex;
459                         unsigned int totface = me->totface;
460                         MTFace *dm_mtface = dm->getTessFaceDataArray(dm, CD_MTFACE);
461
462                         if (dm_mtface) {
463                                 view3d_set_viewcontext(C, &vc);
464
465                                 view3d_operator_needs_opengl(C);
466
467                                 if (imapaint_pick_face(&vc, mval, &faceindex, totface)) {
468                                         Image *image;
469                                         
470                                         if (use_material) 
471                                                 image = imapaint_face_image(ob, me, faceindex);
472                                         else
473                                                 image = imapaint->canvas;
474                                         
475                                         if (image) {
476                                                 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
477                                                 if (ibuf && ibuf->rect) {
478                                                         float uv[2];
479                                                         float u, v;
480                                                         imapaint_pick_uv(scene, ob, faceindex, mval, uv);
481                                                         sample_success = true;
482                                                         
483                                                         u = fmodf(uv[0], 1.0f);
484                                                         v = fmodf(uv[1], 1.0f);
485                                                         
486                                                         if (u < 0.0f) u += 1.0f;
487                                                         if (v < 0.0f) v += 1.0f;
488                                                         
489                                                         u = u * ibuf->x - 0.5f;
490                                                         v = v * ibuf->y - 0.5f;
491                                                         
492                                                         if (ibuf->rect_float) {
493                                                                 float rgba_f[4];
494                                                                 bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v);
495                                                                 straight_to_premul_v4(rgba_f);
496                                                                 if (use_palette) {
497                                                                         linearrgb_to_srgb_v3_v3(color->rgb, rgba_f);
498                                                                 }
499                                                                 else {
500                                                                         linearrgb_to_srgb_v3_v3(rgba_f, rgba_f);
501                                                                         BKE_brush_color_set(scene, br, rgba_f);
502                                                                 }
503                                                         }
504                                                         else {
505                                                                 unsigned char rgba[4];
506                                                                 bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
507                                                                 if (use_palette) {
508                                                                         rgb_uchar_to_float(color->rgb, rgba);
509                                                                 }
510                                                                 else {
511                                                                         float rgba_f[3];
512                                                                         rgb_uchar_to_float(rgba_f, rgba);
513                                                                         BKE_brush_color_set(scene, br, rgba_f);
514                                                                 }
515                                                         }
516                                                 }
517                                         
518                                                 BKE_image_release_ibuf(image, ibuf, NULL);
519                                         }
520                                 }
521                         }
522                         dm->release(dm);
523                 }
524
525                 if (!sample_success) {
526                         glReadBuffer(GL_FRONT);
527                         glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
528                         glReadBuffer(GL_BACK);
529                 }
530                 else
531                         return;
532         }
533         else {
534                 glReadBuffer(GL_FRONT);
535                 glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
536                 glReadBuffer(GL_BACK);
537         }
538         cp = (unsigned char *)&col;
539         
540         if (use_palette) {
541                 rgb_uchar_to_float(color->rgb, cp);
542         }
543         else {
544                 float rgba_f[3];
545                 rgb_uchar_to_float(rgba_f, cp);
546                 BKE_brush_color_set(scene, br, rgba_f);
547         }
548 }
549
550 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
551 {
552         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
553
554         if (br) {
555                 Scene *scene = CTX_data_scene(C);
556                 BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
557                 BKE_paint_invalidate_cursor_overlay(scene, br->curve);
558         }
559
560         return OPERATOR_FINISHED;
561 }
562
563 static int brush_curve_preset_poll(bContext *C)
564 {
565         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
566
567         return br && br->curve;
568 }
569
570 void BRUSH_OT_curve_preset(wmOperatorType *ot)
571 {
572         PropertyRNA *prop;
573         static EnumPropertyItem prop_shape_items[] = {
574                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
575                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
576                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
577                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
578                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
579                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
580                 {0, NULL, 0, NULL, NULL}};
581
582         ot->name = "Preset";
583         ot->description = "Set brush shape";
584         ot->idname = "BRUSH_OT_curve_preset";
585
586         ot->exec = brush_curve_preset_exec;
587         ot->poll = brush_curve_preset_poll;
588
589         prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
590         RNA_def_property_translation_context(prop, BLF_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
591 }
592
593
594 /* face-select ops */
595 static int paint_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
596 {
597         paintface_select_linked(C, CTX_data_active_object(C), NULL, true);
598         ED_region_tag_redraw(CTX_wm_region(C));
599         return OPERATOR_FINISHED;
600 }
601
602 void PAINT_OT_face_select_linked(wmOperatorType *ot)
603 {
604         ot->name = "Select Linked";
605         ot->description = "Select linked faces";
606         ot->idname = "PAINT_OT_face_select_linked";
607
608         ot->exec = paint_select_linked_exec;
609         ot->poll = facemask_paint_poll;
610
611         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
612 }
613
614 static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
615 {
616         const bool select = !RNA_boolean_get(op->ptr, "deselect");
617         view3d_operator_needs_opengl(C);
618         paintface_select_linked(C, CTX_data_active_object(C), event->mval, select);
619         ED_region_tag_redraw(CTX_wm_region(C));
620         return OPERATOR_FINISHED;
621 }
622
623 void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
624 {
625         ot->name = "Select Linked Pick";
626         ot->description = "Select linked faces under the cursor";
627         ot->idname = "PAINT_OT_face_select_linked_pick";
628
629         ot->invoke = paint_select_linked_pick_invoke;
630         ot->poll = facemask_paint_poll;
631
632         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
633
634         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items");
635 }
636
637
638 static int face_select_all_exec(bContext *C, wmOperator *op)
639 {
640         Object *ob = CTX_data_active_object(C);
641         paintface_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
642         ED_region_tag_redraw(CTX_wm_region(C));
643         return OPERATOR_FINISHED;
644 }
645
646
647 void PAINT_OT_face_select_all(wmOperatorType *ot)
648 {
649         ot->name = "(De)select All";
650         ot->description = "Change selection for all faces";
651         ot->idname = "PAINT_OT_face_select_all";
652
653         ot->exec = face_select_all_exec;
654         ot->poll = facemask_paint_poll;
655
656         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
657
658         WM_operator_properties_select_all(ot);
659 }
660
661
662 static int vert_select_all_exec(bContext *C, wmOperator *op)
663 {
664         Object *ob = CTX_data_active_object(C);
665         paintvert_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
666         ED_region_tag_redraw(CTX_wm_region(C));
667         return OPERATOR_FINISHED;
668 }
669
670
671 void PAINT_OT_vert_select_all(wmOperatorType *ot)
672 {
673         ot->name = "(De)select All";
674         ot->description = "Change selection for all vertices";
675         ot->idname = "PAINT_OT_vert_select_all";
676
677         ot->exec = vert_select_all_exec;
678         ot->poll = vert_paint_poll;
679
680         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
681
682         WM_operator_properties_select_all(ot);
683 }
684
685
686 static int vert_select_ungrouped_exec(bContext *C, wmOperator *op)
687 {
688         Object *ob = CTX_data_active_object(C);
689         Mesh *me = ob->data;
690
691         if (BLI_listbase_is_empty(&ob->defbase) || (me->dvert == NULL)) {
692                 BKE_report(op->reports, RPT_ERROR, "No weights/vertex groups on object");
693                 return OPERATOR_CANCELLED;
694         }
695
696         paintvert_select_ungrouped(ob, RNA_boolean_get(op->ptr, "extend"), true);
697         ED_region_tag_redraw(CTX_wm_region(C));
698         return OPERATOR_FINISHED;
699 }
700
701 void PAINT_OT_vert_select_ungrouped(wmOperatorType *ot)
702 {
703         /* identifiers */
704         ot->name = "Select Ungrouped";
705         ot->idname = "PAINT_OT_vert_select_ungrouped";
706         ot->description = "Select vertices without a group";
707
708         /* api callbacks */
709         ot->exec = vert_select_ungrouped_exec;
710         ot->poll = vert_paint_poll;
711
712         /* flags */
713         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
714
715         RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
716 }
717
718 static int face_select_hide_exec(bContext *C, wmOperator *op)
719 {
720         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
721         Object *ob = CTX_data_active_object(C);
722         paintface_hide(ob, unselected);
723         ED_region_tag_redraw(CTX_wm_region(C));
724         return OPERATOR_FINISHED;
725 }
726
727 void PAINT_OT_face_select_hide(wmOperatorType *ot)
728 {
729         ot->name = "Face Select Hide";
730         ot->description = "Hide selected faces";
731         ot->idname = "PAINT_OT_face_select_hide";
732
733         ot->exec = face_select_hide_exec;
734         ot->poll = facemask_paint_poll;
735
736         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
737
738         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
739 }
740
741 static int face_select_reveal_exec(bContext *C, wmOperator *UNUSED(op))
742 {
743         Object *ob = CTX_data_active_object(C);
744         paintface_reveal(ob);
745         ED_region_tag_redraw(CTX_wm_region(C));
746         return OPERATOR_FINISHED;
747 }
748
749 void PAINT_OT_face_select_reveal(wmOperatorType *ot)
750 {
751         ot->name = "Face Select Reveal";
752         ot->description = "Reveal hidden faces";
753         ot->idname = "PAINT_OT_face_select_reveal";
754
755         ot->exec = face_select_reveal_exec;
756         ot->poll = facemask_paint_poll;
757
758         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
759
760         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
761 }