Merge remote-tracking branch 'origin' into blender2.8
[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 "BLT_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 "DEG_depsgraph.h"
60
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63
64 #include "GPU_glew.h"
65 #include "GPU_matrix.h"
66
67 #include "IMB_colormanagement.h"
68 #include "IMB_imbuf_types.h"
69 #include "IMB_imbuf.h"
70
71 #include "RE_render_ext.h"
72
73 #include "ED_view3d.h"
74 #include "ED_screen.h"
75
76 #include "BLI_sys_types.h"
77 #include "ED_mesh.h" /* for face mask functions */
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 #include "paint_intern.h"
83
84 /* Convert the object-space axis-aligned bounding box (expressed as
85  * its minimum and maximum corners) into a screen-space rectangle,
86  * returns zero if the result is empty */
87 bool paint_convert_bb_to_rect(rcti *rect,
88                               const float bb_min[3],
89                               const float bb_max[3],
90                               const ARegion *ar,
91                               RegionView3D *rv3d,
92                               Object *ob)
93 {
94         float projection_mat[4][4];
95         int i, j, k;
96
97         BLI_rcti_init_minmax(rect);
98
99         /* return zero if the bounding box has non-positive volume */
100         if (bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
101                 return 0;
102
103         ED_view3d_ob_project_mat_get(rv3d, ob, projection_mat);
104
105         for (i = 0; i < 2; ++i) {
106                 for (j = 0; j < 2; ++j) {
107                         for (k = 0; k < 2; ++k) {
108                                 float vec[3], proj[2];
109                                 int proj_i[2];
110                                 vec[0] = i ? bb_min[0] : bb_max[0];
111                                 vec[1] = j ? bb_min[1] : bb_max[1];
112                                 vec[2] = k ? bb_min[2] : bb_max[2];
113                                 /* convert corner to screen space */
114                                 ED_view3d_project_float_v2_m4(ar, vec, proj, projection_mat);
115                                 /* expand 2D rectangle */
116
117                                 /* we could project directly to int? */
118                                 proj_i[0] = proj[0];
119                                 proj_i[1] = proj[1];
120
121                                 BLI_rcti_do_minmax_v(rect, proj_i);
122                         }
123                 }
124         }
125
126         /* return false if the rectangle has non-positive area */
127         return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
128 }
129
130 /* Get four planes in object-space that describe the projection of
131  * screen_rect from screen into object-space (essentially converting a
132  * 2D screens-space bounding box into four 3D planes) */
133 void paint_calc_redraw_planes(float planes[4][4],
134                               const ARegion *ar,
135                               Object *ob,
136                               const rcti *screen_rect)
137 {
138         BoundBox bb;
139         rcti rect;
140
141         /* use some extra space just in case */
142         rect = *screen_rect;
143         rect.xmin -= 2;
144         rect.xmax += 2;
145         rect.ymin -= 2;
146         rect.ymax += 2;
147
148         ED_view3d_clipping_calc(&bb, planes, ar, ob, &rect);
149         negate_m4(planes);
150 }
151
152 float paint_calc_object_space_radius(ViewContext *vc, const float center[3],
153                                      float pixel_radius)
154 {
155         Object *ob = vc->obact;
156         float delta[3], scale, loc[3];
157         const float mval_f[2] = {pixel_radius, 0.0f};
158         float zfac;
159
160         mul_v3_m4v3(loc, ob->obmat, center);
161
162         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
163         ED_view3d_win_to_delta(vc->ar, mval_f, delta, zfac);
164
165         scale = fabsf(mat4_to_scale(ob->obmat));
166         scale = (scale == 0.0f) ? 1.0f : scale;
167
168         return len_v3(delta) / scale;
169 }
170
171 float paint_get_tex_pixel(const MTex *mtex, float u, float v, struct ImagePool *pool, int thread)
172 {
173         float intensity, rgba[4];
174         float co[3] = {u, v, 0.0f};
175
176         externtex(mtex, co, &intensity,
177                   rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
178
179         return intensity;
180 }
181
182 void paint_get_tex_pixel_col(const MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace)
183 {
184         float co[3] = {u, v, 0.0f};
185         int hasrgb;
186         float intensity;
187
188         hasrgb = externtex(mtex, co, &intensity,
189                            rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
190         if (!hasrgb) {
191                 rgba[0] = intensity;
192                 rgba[1] = intensity;
193                 rgba[2] = intensity;
194                 rgba[3] = 1.0f;
195         }
196
197         if (convert_to_linear)
198                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, colorspace);
199
200         linearrgb_to_srgb_v3_v3(rgba, rgba);
201
202         CLAMP(rgba[0], 0.0f, 1.0f);
203         CLAMP(rgba[1], 0.0f, 1.0f);
204         CLAMP(rgba[2], 0.0f, 1.0f);
205         CLAMP(rgba[3], 0.0f, 1.0f);
206 }
207
208 void paint_stroke_operator_properties(wmOperatorType *ot)
209 {
210         static const EnumPropertyItem stroke_mode_items[] = {
211                 {BRUSH_STROKE_NORMAL, "NORMAL", 0, "Normal", "Apply brush normally"},
212                 {BRUSH_STROKE_INVERT, "INVERT", 0, "Invert", "Invert action of brush for duration of stroke"},
213                 {BRUSH_STROKE_SMOOTH, "SMOOTH", 0, "Smooth", "Switch brush to smooth mode for duration of stroke"},
214                 {0}
215         };
216
217         PropertyRNA *prop;
218
219         prop = RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
220         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
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(EvaluationContext *eval_ctx, Scene *scene, Object *ob, unsigned int faceindex, const int xy[2], float uv[2])
278 {
279         DerivedMesh *dm = mesh_get_derived_final(eval_ctx, scene, ob, CD_MASK_BAREMESH);
280         const int tottri = dm->getNumLoopTri(dm);
281         int i, findex;
282         float p[2], w[3], absw, minabsw;
283         float matrix[4][4], proj[4][4];
284         GLint view[4];
285         const eImagePaintMode mode = scene->toolsettings->imapaint.mode;
286         const MLoopTri *lt = dm->getLoopTriArray(dm);
287         const MPoly *mpoly = dm->getPolyArray(dm);
288         const MLoop *mloop = dm->getLoopArray(dm);
289         const int *index_mp_to_orig  = dm->getPolyDataArray(dm, CD_ORIGINDEX);
290
291         /* get the needed opengl matrices */
292         glGetIntegerv(GL_VIEWPORT, view);
293         gpuGetModelViewMatrix(matrix);
294         gpuGetProjectionMatrix(proj);
295         view[0] = view[1] = 0;
296         mul_m4_m4m4(matrix, matrix, ob->obmat);
297         mul_m4_m4m4(matrix, proj, matrix);
298
299         minabsw = 1e10;
300         uv[0] = uv[1] = 0.0;
301
302         /* test all faces in the derivedmesh with the original index of the picked face */
303         /* face means poly here, not triangle, indeed */
304         for (i = 0; i < tottri; i++, lt++) {
305                 findex = index_mp_to_orig ? index_mp_to_orig[lt->poly] : lt->poly;
306
307                 if (findex == faceindex) {
308                         const MLoopUV *mloopuv;
309                         const MPoly *mp = &mpoly[lt->poly];
310                         const MLoopUV *tri_uv[3];
311                         float  tri_co[3][3];
312
313                         dm->getVertCo(dm, mloop[lt->tri[0]].v, tri_co[0]);
314                         dm->getVertCo(dm, mloop[lt->tri[1]].v, tri_co[1]);
315                         dm->getVertCo(dm, mloop[lt->tri[2]].v, tri_co[2]);
316
317                         if (mode == IMAGEPAINT_MODE_MATERIAL) {
318                                 const Material *ma;
319                                 const TexPaintSlot *slot;
320
321                                 ma = dm->mat[mp->mat_nr];
322                                 slot = &ma->texpaintslot[ma->paint_active_slot];
323
324                                 if (!(slot && slot->uvname &&
325                                       (mloopuv = CustomData_get_layer_named(&dm->loopData, CD_MLOOPUV, slot->uvname))))
326                                 {
327                                         mloopuv = CustomData_get_layer(&dm->loopData, CD_MLOOPUV);
328                                 }
329                         }
330                         else {
331                                 mloopuv = CustomData_get_layer(&dm->loopData, CD_MLOOPUV);
332                         }
333
334                         tri_uv[0] = &mloopuv[lt->tri[0]];
335                         tri_uv[1] = &mloopuv[lt->tri[1]];
336                         tri_uv[2] = &mloopuv[lt->tri[2]];
337
338                         p[0] = xy[0];
339                         p[1] = xy[1];
340
341                         imapaint_tri_weights(matrix, view, UNPACK3(tri_co), p, w);
342                         absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
343                         if (absw < minabsw) {
344                                 uv[0] = tri_uv[0]->uv[0] * w[0] + tri_uv[1]->uv[0] * w[1] + tri_uv[2]->uv[0] * w[2];
345                                 uv[1] = tri_uv[0]->uv[1] * w[0] + tri_uv[1]->uv[1] * w[1] + tri_uv[2]->uv[1] * w[2];
346                                 minabsw = absw;
347                         }
348                 }
349         }
350
351         dm->release(dm);
352 }
353
354 /* returns 0 if not found, otherwise 1 */
355 static int imapaint_pick_face(
356         const struct EvaluationContext *eval_ctx, ViewContext *vc, const int mval[2],
357         unsigned int *r_index, unsigned int totpoly)
358 {
359         if (totpoly == 0)
360                 return 0;
361
362         /* sample only on the exact position */
363         *r_index = ED_view3d_backbuf_sample(eval_ctx, vc, mval[0], mval[1]);
364
365         if ((*r_index) == 0 || (*r_index) > (unsigned int)totpoly) {
366                 return 0;
367         }
368
369         (*r_index)--;
370
371         return 1;
372 }
373
374
375 static Image *imapaint_face_image(Object *ob, Mesh *me, int face_index)
376 {
377         Image *ima;
378         MPoly *mp = me->mpoly + face_index;
379         Material *ma = give_current_material(ob, mp->mat_nr + 1);
380         ima = ma && ma->texpaintslot ? ma->texpaintslot[ma->paint_active_slot].ima : NULL;
381
382         return ima;
383 }
384
385 /* Uses symm to selectively flip any axis of a coordinate. */
386 void flip_v3_v3(float out[3], const float in[3], const char symm)
387 {
388         if (symm & PAINT_SYMM_X)
389                 out[0] = -in[0];
390         else
391                 out[0] = in[0];
392         if (symm & PAINT_SYMM_Y)
393                 out[1] = -in[1];
394         else
395                 out[1] = in[1];
396         if (symm & PAINT_SYMM_Z)
397                 out[2] = -in[2];
398         else
399                 out[2] = in[2];
400 }
401
402 void flip_qt_qt(float out[4], const float in[4], const char symm)
403 {
404         float axis[3], angle;
405
406         quat_to_axis_angle(axis, &angle, in);
407         normalize_v3(axis);
408
409         if (symm & PAINT_SYMM_X) {
410                 axis[0] *= -1.0f;
411                 angle *= -1.0f;
412         }
413         if (symm & PAINT_SYMM_Y) {
414                 axis[1] *= -1.0f;
415                 angle *= -1.0f;
416         }
417         if (symm & PAINT_SYMM_Z) {
418                 axis[2] *= -1.0f;
419                 angle *= -1.0f;
420         }
421
422         axis_angle_normalized_to_quat(out, axis, angle);
423 }
424
425 /* used for both 3d view and image window */
426 void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_proj, bool use_palette)
427 {
428         Scene *scene = CTX_data_scene(C);
429         EvaluationContext eval_ctx;
430         Paint *paint = BKE_paint_get_active_from_context(C);
431         Palette *palette = BKE_paint_palette(paint);
432         PaletteColor *color = NULL;
433         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
434         unsigned int col;
435         const unsigned char *cp;
436
437         CTX_data_eval_ctx(C, &eval_ctx);
438
439         CLAMP(x, 0, ar->winx);
440         CLAMP(y, 0, ar->winy);
441         
442         if (use_palette) {
443                 if (!palette) {
444                         palette = BKE_palette_add(CTX_data_main(C), "Palette");
445                         BKE_paint_palette_set(paint, palette);
446                 }
447
448                 color = BKE_palette_color_add(palette);
449                 palette->active_color = BLI_listbase_count(&palette->colors) - 1;
450         }
451
452
453         if (CTX_wm_view3d(C) && texpaint_proj) {
454                 /* first try getting a colour directly from the mesh faces if possible */
455                 ViewLayer *view_layer = CTX_data_view_layer(C);
456                 Object *ob = OBACT(view_layer);
457                 bool sample_success = false;
458                 ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
459                 bool use_material = (imapaint->mode == IMAGEPAINT_MODE_MATERIAL);
460
461                 if (ob) {
462                         Mesh *me = (Mesh *)ob->data;
463                         DerivedMesh *dm = mesh_get_derived_final(&eval_ctx, scene, ob, CD_MASK_BAREMESH);
464
465                         ViewContext vc;
466                         const int mval[2] = {x, y};
467                         unsigned int faceindex;
468                         unsigned int totpoly = me->totpoly;
469
470                         if (dm->getLoopDataArray(dm, CD_MLOOPUV)) {
471                                 ED_view3d_viewcontext_init(C, &vc);
472
473                                 view3d_operator_needs_opengl(C);
474
475                                 if (imapaint_pick_face(&eval_ctx, &vc, mval, &faceindex, totpoly)) {
476                                         Image *image;
477                                         
478                                         if (use_material) 
479                                                 image = imapaint_face_image(ob, me, faceindex);
480                                         else
481                                                 image = imapaint->canvas;
482                                         
483                                         if (image) {
484                                                 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
485                                                 if (ibuf && ibuf->rect) {
486                                                         float uv[2];
487                                                         float u, v;
488                                                         imapaint_pick_uv(&eval_ctx, scene, ob, faceindex, mval, uv);
489                                                         sample_success = true;
490                                                         
491                                                         u = fmodf(uv[0], 1.0f);
492                                                         v = fmodf(uv[1], 1.0f);
493                                                         
494                                                         if (u < 0.0f) u += 1.0f;
495                                                         if (v < 0.0f) v += 1.0f;
496                                                         
497                                                         u = u * ibuf->x;
498                                                         v = v * ibuf->y;
499                                                         
500                                                         if (ibuf->rect_float) {
501                                                                 float rgba_f[4];
502                                                                 if (U.gameflags & USER_DISABLE_MIPMAP)
503                                                                         nearest_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v);
504                                                                 else
505                                                                         bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v);
506                                                                 straight_to_premul_v4(rgba_f);
507                                                                 if (use_palette) {
508                                                                         linearrgb_to_srgb_v3_v3(color->rgb, rgba_f);
509                                                                 }
510                                                                 else {
511                                                                         linearrgb_to_srgb_v3_v3(rgba_f, rgba_f);
512                                                                         BKE_brush_color_set(scene, br, rgba_f);
513                                                                 }
514                                                         }
515                                                         else {
516                                                                 unsigned char rgba[4];
517                                                                 if (U.gameflags & USER_DISABLE_MIPMAP)
518                                                                         nearest_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
519                                                                 else
520                                                                         bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
521                                                                 if (use_palette) {
522                                                                         rgb_uchar_to_float(color->rgb, rgba);
523                                                                 }
524                                                                 else {
525                                                                         float rgba_f[3];
526                                                                         rgb_uchar_to_float(rgba_f, rgba);
527                                                                         BKE_brush_color_set(scene, br, rgba_f);
528                                                                 }
529                                                         }
530                                                 }
531                                         
532                                                 BKE_image_release_ibuf(image, ibuf, NULL);
533                                         }
534                                 }
535                         }
536                         dm->release(dm);
537                 }
538
539                 if (!sample_success) {
540                         glReadBuffer(GL_FRONT);
541                         glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
542                         glReadBuffer(GL_BACK);
543                 }
544                 else
545                         return;
546         }
547         else {
548                 glReadBuffer(GL_FRONT);
549                 glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
550                 glReadBuffer(GL_BACK);
551         }
552         cp = (unsigned char *)&col;
553         
554         if (use_palette) {
555                 rgb_uchar_to_float(color->rgb, cp);
556         }
557         else {
558                 float rgba_f[3];
559                 rgb_uchar_to_float(rgba_f, cp);
560                 BKE_brush_color_set(scene, br, rgba_f);
561         }
562 }
563
564 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
565 {
566         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
567
568         if (br) {
569                 Scene *scene = CTX_data_scene(C);
570                 ViewLayer *view_layer = CTX_data_view_layer(C);
571                 BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
572                 BKE_paint_invalidate_cursor_overlay(scene, view_layer, br->curve);
573         }
574
575         return OPERATOR_FINISHED;
576 }
577
578 static int brush_curve_preset_poll(bContext *C)
579 {
580         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
581
582         return br && br->curve;
583 }
584
585 void BRUSH_OT_curve_preset(wmOperatorType *ot)
586 {
587         PropertyRNA *prop;
588         static const EnumPropertyItem prop_shape_items[] = {
589                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
590                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
591                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
592                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
593                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
594                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
595                 {0, NULL, 0, NULL, NULL}};
596
597         ot->name = "Preset";
598         ot->description = "Set brush shape";
599         ot->idname = "BRUSH_OT_curve_preset";
600
601         ot->exec = brush_curve_preset_exec;
602         ot->poll = brush_curve_preset_poll;
603
604         prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
605         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
606 }
607
608
609 /* face-select ops */
610 static int paint_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
611 {
612         paintface_select_linked(C, CTX_data_active_object(C), NULL, true);
613         ED_region_tag_redraw(CTX_wm_region(C));
614         return OPERATOR_FINISHED;
615 }
616
617 void PAINT_OT_face_select_linked(wmOperatorType *ot)
618 {
619         ot->name = "Select Linked";
620         ot->description = "Select linked faces";
621         ot->idname = "PAINT_OT_face_select_linked";
622
623         ot->exec = paint_select_linked_exec;
624         ot->poll = facemask_paint_poll;
625
626         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
627 }
628
629 static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
630 {
631         const bool select = !RNA_boolean_get(op->ptr, "deselect");
632         view3d_operator_needs_opengl(C);
633         paintface_select_linked(C, CTX_data_active_object(C), event->mval, select);
634         ED_region_tag_redraw(CTX_wm_region(C));
635         return OPERATOR_FINISHED;
636 }
637
638 void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
639 {
640         ot->name = "Select Linked Pick";
641         ot->description = "Select linked faces under the cursor";
642         ot->idname = "PAINT_OT_face_select_linked_pick";
643
644         ot->invoke = paint_select_linked_pick_invoke;
645         ot->poll = facemask_paint_poll;
646
647         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
648
649         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items");
650 }
651
652
653 static int face_select_all_exec(bContext *C, wmOperator *op)
654 {
655         Object *ob = CTX_data_active_object(C);
656         paintface_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
657         ED_region_tag_redraw(CTX_wm_region(C));
658         return OPERATOR_FINISHED;
659 }
660
661
662 void PAINT_OT_face_select_all(wmOperatorType *ot)
663 {
664         ot->name = "(De)select All";
665         ot->description = "Change selection for all faces";
666         ot->idname = "PAINT_OT_face_select_all";
667
668         ot->exec = face_select_all_exec;
669         ot->poll = facemask_paint_poll;
670
671         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
672
673         WM_operator_properties_select_all(ot);
674 }
675
676
677 static int vert_select_all_exec(bContext *C, wmOperator *op)
678 {
679         Object *ob = CTX_data_active_object(C);
680         paintvert_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
681         ED_region_tag_redraw(CTX_wm_region(C));
682         return OPERATOR_FINISHED;
683 }
684
685
686 void PAINT_OT_vert_select_all(wmOperatorType *ot)
687 {
688         ot->name = "(De)select All";
689         ot->description = "Change selection for all vertices";
690         ot->idname = "PAINT_OT_vert_select_all";
691
692         ot->exec = vert_select_all_exec;
693         ot->poll = vert_paint_poll;
694
695         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
696
697         WM_operator_properties_select_all(ot);
698 }
699
700
701 static int vert_select_ungrouped_exec(bContext *C, wmOperator *op)
702 {
703         Object *ob = CTX_data_active_object(C);
704         Mesh *me = ob->data;
705
706         if (BLI_listbase_is_empty(&ob->defbase) || (me->dvert == NULL)) {
707                 BKE_report(op->reports, RPT_ERROR, "No weights/vertex groups on object");
708                 return OPERATOR_CANCELLED;
709         }
710
711         paintvert_select_ungrouped(ob, RNA_boolean_get(op->ptr, "extend"), true);
712         ED_region_tag_redraw(CTX_wm_region(C));
713         return OPERATOR_FINISHED;
714 }
715
716 void PAINT_OT_vert_select_ungrouped(wmOperatorType *ot)
717 {
718         /* identifiers */
719         ot->name = "Select Ungrouped";
720         ot->idname = "PAINT_OT_vert_select_ungrouped";
721         ot->description = "Select vertices without a group";
722
723         /* api callbacks */
724         ot->exec = vert_select_ungrouped_exec;
725         ot->poll = vert_paint_poll;
726
727         /* flags */
728         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
729
730         RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
731 }
732
733 static int face_select_hide_exec(bContext *C, wmOperator *op)
734 {
735         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
736         Object *ob = CTX_data_active_object(C);
737         paintface_hide(ob, unselected);
738         ED_region_tag_redraw(CTX_wm_region(C));
739         return OPERATOR_FINISHED;
740 }
741
742 void PAINT_OT_face_select_hide(wmOperatorType *ot)
743 {
744         ot->name = "Face Select Hide";
745         ot->description = "Hide selected faces";
746         ot->idname = "PAINT_OT_face_select_hide";
747
748         ot->exec = face_select_hide_exec;
749         ot->poll = facemask_paint_poll;
750
751         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
752
753         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
754 }
755
756 static int face_select_reveal_exec(bContext *C, wmOperator *op)
757 {
758         const bool select = RNA_boolean_get(op->ptr, "select");
759         Object *ob = CTX_data_active_object(C);
760         paintface_reveal(ob, select);
761         ED_region_tag_redraw(CTX_wm_region(C));
762         return OPERATOR_FINISHED;
763 }
764
765 void PAINT_OT_face_select_reveal(wmOperatorType *ot)
766 {
767         ot->name = "Face Select Reveal";
768         ot->description = "Reveal hidden faces";
769         ot->idname = "PAINT_OT_face_select_reveal";
770
771         ot->exec = face_select_reveal_exec;
772         ot->poll = facemask_paint_poll;
773
774         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
775
776         RNA_def_boolean(ot->srna, "select", true, "Select", "");
777 }