Fix [#22965] Icons for new brushes won't get sticky
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_brush_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "WM_types.h"
41 #include "WM_api.h"
42
43 #include "RNA_access.h"
44
45 #include "BLI_math.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_brush.h"
50 #include "BKE_colortools.h"
51 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_paint.h"
56 #include "BKE_texture.h"
57 #include "BKE_icons.h"
58
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
61
62 #include "RE_render_ext.h" /* externtex */
63 #include "RE_shader_ext.h"
64
65 /* Datablock add/copy/free/make_local */
66
67 Brush *add_brush(const char *name)
68 {
69         Brush *brush;
70
71         brush= alloc_libblock(&G.main->brush, ID_BR, name);
72
73         /* BRUSH SCULPT TOOL SETTINGS */
74         brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
75         brush->size= 35; /* radius of the brush in pixels */
76         brush->alpha= 0.5f; /* brush strength/intensity probably variable should be renamed? */
77         brush->autosmooth_factor= 0.0f;
78         brush->crease_pinch_factor= 0.5f;
79         brush->sculpt_plane = SCULPT_DISP_DIR_VIEW;
80         brush->plane_offset= 0.0f; /* how far above or below the plane that is found by averaging the faces */
81         brush->plane_trim= 0.5f;
82         brush->clone.alpha= 0.5f;
83         brush->normal_weight= 0.0f;
84
85         /* BRUSH PAINT TOOL SETTINGS */
86         brush->rgb[0]= 1.0f; /* default rgb color of the brush when painting - white */
87         brush->rgb[1]= 1.0f;
88         brush->rgb[2]= 1.0f;
89
90         /* BRUSH STROKE SETTINGS */
91         brush->flag |= (BRUSH_SPACE|BRUSH_SPACE_ATTEN);
92         brush->spacing= 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
93
94         brush->smooth_stroke_radius= 75;
95         brush->smooth_stroke_factor= 0.9f;
96
97         brush->rate= 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
98
99         brush->jitter= 0.0f;
100
101         /* BRUSH TEXTURE SETTINGS */
102         default_mtex(&brush->mtex);
103
104         brush->texture_sample_bias= 0; /* value to added to texture samples */
105
106         /* brush appearance  */
107
108         brush->add_col[0]= 1.00; /* add mode color is light red */
109         brush->add_col[1]= 0.39;
110         brush->add_col[2]= 0.39;
111
112         brush->sub_col[0]= 0.39; /* subtract mode color is light blue */
113         brush->sub_col[1]= 0.39;
114         brush->sub_col[2]= 1.00;
115
116          /* the default alpha falloff curve */
117         brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
118
119         /* enable fake user by default */
120         brush->id.flag |= LIB_FAKEUSER;
121         brush_toggled_fake_user(brush);
122
123         return brush;
124 }
125
126 Brush *copy_brush(Brush *brush)
127 {
128         Brush *brushn;
129         
130         brushn= copy_libblock(brush);
131
132         if (brush->mtex.tex)
133                 id_us_plus((ID*)brush->mtex.tex);
134
135         brushn->preview= NULL;
136
137         if (brush->icon_imbuf)
138                 brushn->icon_imbuf= IMB_dupImBuf(brush->icon_imbuf);
139
140         brushn->curve= curvemapping_copy(brush->curve);
141
142         /* enable fake user by default */
143         if (!(brushn->id.flag & LIB_FAKEUSER)) {
144                 brushn->id.flag |= LIB_FAKEUSER;
145                 brush_toggled_fake_user(brushn);
146         }
147         
148         return brushn;
149 }
150
151 /* not brush itself */
152 void free_brush(Brush *brush)
153 {
154         if (brush->mtex.tex)
155                 brush->mtex.tex->id.us--;
156
157         if (brush->icon_imbuf)
158                 IMB_freeImBuf(brush->icon_imbuf);
159
160         BKE_previewimg_free(&(brush->preview));
161
162         curvemapping_free(brush->curve);
163 }
164
165 void make_local_brush(Brush *brush)
166 {
167         /* - only lib users: do nothing
168                 * - only local users: set flag
169                 * - mixed: make copy
170                 */
171         
172         Brush *brushn;
173         Scene *scene;
174         int local= 0, lib= 0;
175
176         if(brush->id.lib==0) return;
177
178         if(brush->clone.image) {
179                 /* special case: ima always local immediately */
180                 brush->clone.image->id.lib= 0;
181                 brush->clone.image->id.flag= LIB_LOCAL;
182                 new_id(0, (ID *)brush->clone.image, 0);
183         }
184
185         for(scene= G.main->scene.first; scene; scene=scene->id.next)
186                 if(paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
187                         if(scene->id.lib) lib= 1;
188                         else local= 1;
189                 }
190
191         if(local && lib==0) {
192                 brush->id.lib= 0;
193                 brush->id.flag= LIB_LOCAL;
194                 new_id(0, (ID *)brush, 0);
195
196                 /* enable fake user by default */
197                 if (!(brush->id.flag & LIB_FAKEUSER)) {
198                         brush->id.flag |= LIB_FAKEUSER;
199                         brush_toggled_fake_user(brush);
200                 }
201         }
202         else if(local && lib) {
203                 brushn= copy_brush(brush);
204                 brushn->id.us= 1; /* only keep fake user */
205                 brushn->id.flag |= LIB_FAKEUSER;
206                 
207                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
208                         if(paint_brush(&scene->toolsettings->imapaint.paint)==brush)
209                                 if(scene->id.lib==0) {
210                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brushn);
211                                         brushn->id.us++;
212                                         brush->id.us--;
213                                 }
214         }
215 }
216
217 /* Library Operations */
218
219 int brush_set_nr(Brush **current_brush, int nr, const char *name)
220 {
221         ID *idtest, *id;
222         
223         id= (ID*)(*current_brush);
224         idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
225         
226         if(idtest==0) { /* new brush */
227                 if(id) idtest= (ID *)copy_brush((Brush *)id);
228                 else idtest= (ID *)add_brush(name);
229                 idtest->us--;
230         }
231         if(idtest!=id) {
232                 brush_delete(current_brush);
233                 *current_brush= (Brush *)idtest;
234                 id_us_plus(idtest);
235
236                 return 1;
237         }
238
239         return 0;
240 }
241
242 int brush_delete(Brush **current_brush)
243 {
244         if (*current_brush) {
245                 (*current_brush)->id.us--;
246                 *current_brush= NULL;
247
248                 return 1;
249         }
250
251         return 0;
252 }
253
254 void brush_toggled_fake_user(Brush *brush)
255 {
256         ID *id= (ID*)brush;
257         if(id) {
258                 if(id->flag & LIB_FAKEUSER) {
259                         id_us_plus(id);
260                 } else {
261                         id->us--;
262                 }
263         }
264 }
265
266 void brush_curve_preset(Brush *b, /*CurveMappingPreset*/int preset)
267 {
268         CurveMap *cm = NULL;
269
270         if(!b->curve)
271                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
272
273         cm = b->curve->cm;
274         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
275
276         b->curve->preset = preset;
277         curvemap_reset(cm, &b->curve->clipr, b->curve->preset);
278         curvemapping_changed(b->curve, 0);
279 }
280
281 static MTex *brush_active_texture(Brush *brush)
282 {
283         if(brush)
284                 return &brush->mtex;
285         return NULL;
286 }
287
288 int brush_texture_set_nr(Brush *brush, int nr)
289 {
290         ID *idtest, *id=NULL;
291
292         id= (ID *)brush->mtex.tex;
293
294         idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
295         if(idtest==0) { /* new tex */
296                 if(id) idtest= (ID *)copy_texture((Tex *)id);
297                 else idtest= (ID *)add_texture("Tex");
298                 idtest->us--;
299         }
300         if(idtest!=id) {
301                 brush_texture_delete(brush);
302
303                 brush->mtex.tex= (Tex*)idtest;
304                 id_us_plus(idtest);
305
306                 return 1;
307         }
308
309         return 0;
310 }
311
312 int brush_texture_delete(Brush *brush)
313 {
314         if(brush->mtex.tex)
315                 brush->mtex.tex->id.us--;
316
317         return 1;
318 }
319
320 int brush_clone_image_set_nr(Brush *brush, int nr)
321 {
322         if(brush && nr > 0) {
323                 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
324
325                 if(ima) {
326                         brush_clone_image_delete(brush);
327                         brush->clone.image= ima;
328                         id_us_plus(&ima->id);
329                         brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
330
331                         return 1;
332                 }
333         }
334
335         return 0;
336 }
337
338 int brush_clone_image_delete(Brush *brush)
339 {
340         if (brush && brush->clone.image) {
341                 brush->clone.image->id.us--;
342                 brush->clone.image= NULL;
343                 return 1;
344         }
345
346         return 0;
347 }
348
349 void brush_check_exists(Brush **brush, const char *name)
350 {
351         if(*brush==NULL)
352                 brush_set_nr(brush, 1, name);
353 }
354
355 /* Brush Sampling */
356 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
357 {
358         MTex *mtex= &brush->mtex;
359
360         if (mtex && mtex->tex) {
361                 float co[3], tin, tr, tg, tb, ta;
362                 int hasrgb;
363                 const int radius= brush_size(brush);
364
365                 co[0]= xy[0]/radius;
366                 co[1]= xy[1]/radius;
367                 co[2]= 0.0f;
368
369                 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
370
371                 if (hasrgb) {
372                         rgba[0]= tr;
373                         rgba[1]= tg;
374                         rgba[2]= tb;
375                         rgba[3]= ta;
376                 }
377                 else {
378                         rgba[0]= tin;
379                         rgba[1]= tin;
380                         rgba[2]= tin;
381                         rgba[3]= 1.0f;
382                 }
383         }
384         else if (rgba)
385                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
386 }
387
388
389 void brush_imbuf_new(Brush *brush, short flt, short texfall, int bufsize, ImBuf **outbuf)
390 {
391         ImBuf *ibuf;
392         float xy[2], dist, rgba[4], *dstf;
393         int x, y, rowbytes, xoff, yoff, imbflag;
394         const int radius= brush_size(brush);
395         char *dst, crgb[3];
396         const float alpha= brush_alpha(brush);
397
398         imbflag= (flt)? IB_rectfloat: IB_rect;
399         xoff = -bufsize/2.0f + 0.5f;
400         yoff = -bufsize/2.0f + 0.5f;
401         rowbytes= bufsize*4;
402
403         if (*outbuf)
404                 ibuf= *outbuf;
405         else
406                 ibuf= IMB_allocImBuf(bufsize, bufsize, 32, imbflag, 0);
407
408         if (flt) {
409                 for (y=0; y < ibuf->y; y++) {
410                         dstf = ibuf->rect_float + y*rowbytes;
411
412                         for (x=0; x < ibuf->x; x++, dstf+=4) {
413                                 xy[0] = x + xoff;
414                                 xy[1] = y + yoff;
415
416                                 if (texfall == 0) {
417                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
418
419                                         VECCOPY(dstf, brush->rgb);
420                                         dstf[3]= alpha*brush_curve_strength_clamp(brush, dist, radius);
421                                 }
422                                 else if (texfall == 1) {
423                                         brush_sample_tex(brush, xy, dstf);
424                                 }
425                                 else {
426                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
427
428                                         brush_sample_tex(brush, xy, rgba);
429
430                                         dstf[0] = rgba[0]*brush->rgb[0];
431                                         dstf[1] = rgba[1]*brush->rgb[1];
432                                         dstf[2] = rgba[2]*brush->rgb[2];
433                                         dstf[3] = rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius);
434                                 }
435                         }
436                 }
437         }
438         else {
439                 crgb[0]= FTOCHAR(brush->rgb[0]);
440                 crgb[1]= FTOCHAR(brush->rgb[1]);
441                 crgb[2]= FTOCHAR(brush->rgb[2]);
442
443                 for (y=0; y < ibuf->y; y++) {
444                         dst = (char*)ibuf->rect + y*rowbytes;
445
446                         for (x=0; x < ibuf->x; x++, dst+=4) {
447                                 xy[0] = x + xoff;
448                                 xy[1] = y + yoff;
449
450                                 if (texfall == 0) {
451                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
452
453                                         dst[0]= crgb[0];
454                                         dst[1]= crgb[1];
455                                         dst[2]= crgb[2];
456                                         dst[3]= FTOCHAR(alpha*brush_curve_strength(brush, dist, radius));
457                                 }
458                                 else if (texfall == 1) {
459                                         brush_sample_tex(brush, xy, rgba);
460                                         dst[0]= FTOCHAR(rgba[0]);
461                                         dst[1]= FTOCHAR(rgba[1]);
462                                         dst[2]= FTOCHAR(rgba[2]);
463                                         dst[3]= FTOCHAR(rgba[3]);
464                                 }
465                                 else {
466                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
467
468                                         brush_sample_tex(brush, xy, rgba);
469                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
470                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
471                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
472                                         dst[3] = FTOCHAR(rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius));
473                                 }
474                         }
475                 }
476         }
477
478         *outbuf= ibuf;
479 }
480
481 /* Brush Painting */
482
483 typedef struct BrushPainterCache {
484         short enabled;
485
486         int size;                       /* size override, if 0 uses brush_size(brush) */
487         short flt;                      /* need float imbuf? */
488         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
489
490         int lastsize;
491         float lastalpha;
492         float lastjitter;
493
494         ImBuf *ibuf;
495         ImBuf *texibuf;
496         ImBuf *maskibuf;
497 } BrushPainterCache;
498
499 struct BrushPainter {
500         Brush *brush;
501
502         float lastmousepos[2];  /* mouse position of last paint call */
503
504         float accumdistance;    /* accumulated distance of brush since last paint op */
505         float lastpaintpos[2];  /* position of last paint op */
506         float startpaintpos[2]; /* position of first paint */
507
508         double accumtime;               /* accumulated time since last paint op (airbrush) */
509         double lasttime;                /* time of last update */
510
511         float lastpressure;
512
513         short firsttouch;               /* first paint op */
514
515         float startsize;
516         float startalpha;
517         float startjitter;
518         float startspacing;
519
520         BrushPainterCache cache;
521 };
522
523 BrushPainter *brush_painter_new(Brush *brush)
524 {
525         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
526
527         painter->brush= brush;
528         painter->firsttouch= 1;
529         painter->cache.lastsize= -1; /* force ibuf create in refresh */
530
531         painter->startsize = brush_size(brush);
532         painter->startalpha = brush_alpha(brush);
533         painter->startjitter = brush->jitter;
534         painter->startspacing = brush->spacing;
535
536         return painter;
537 }
538
539 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
540 {
541         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
542                 ((painter->cache.texonly != texonly) && texonly)) {
543                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
544                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
545                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
546                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
547         }
548
549         if (painter->cache.flt != flt) {
550                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
551                 painter->cache.texibuf= NULL;
552                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
553         }
554
555         painter->cache.size= size;
556         painter->cache.flt= flt;
557         painter->cache.texonly= texonly;
558         painter->cache.enabled= 1;
559 }
560
561 void brush_painter_free(BrushPainter *painter)
562 {
563         Brush *brush = painter->brush;
564
565         brush_set_size(brush, painter->startsize);
566         brush_set_alpha(brush, painter->startalpha);
567         brush->jitter = painter->startjitter;
568         brush->spacing = painter->startspacing;
569
570         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
571         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
572         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
573         MEM_freeN(painter);
574 }
575
576 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
577 {
578         Brush *brush= painter->brush;
579         ImBuf *ibuf, *maskibuf, *texibuf;
580         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
581         char *b, *m, *t, *ot= NULL;
582         int dotexold, origx= x, origy= y;
583         const int radius= brush_size(brush);
584
585         xoff = -radius + 0.5f;
586         yoff = -radius + 0.5f;
587         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
588         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
589
590         ibuf = painter->cache.ibuf;
591         texibuf = painter->cache.texibuf;
592         maskibuf = painter->cache.maskibuf;
593
594         dotexold = (oldtexibuf != NULL);
595
596         if (painter->cache.flt) {
597                 for (; y < h; y++) {
598                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
599                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
600                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
601
602                         if (dotexold)
603                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
604
605                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
606                                 if (dotexold) {
607                                         VECCOPY(tf, otf);
608                                         tf[3] = otf[3];
609                                         otf += 4;
610                                 }
611                                 else {
612                                         xy[0] = x + xoff;
613                                         xy[1] = y + yoff;
614
615                                         brush_sample_tex(brush, xy, tf);
616                                 }
617
618                                 bf[0] = tf[0]*mf[0];
619                                 bf[1] = tf[1]*mf[1];
620                                 bf[2] = tf[2]*mf[2];
621                                 bf[3] = tf[3]*mf[3];
622                         }
623                 }
624         }
625         else {
626                 for (; y < h; y++) {
627                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
628                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
629                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
630
631                         if (dotexold)
632                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
633
634                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
635                                 if (dotexold) {
636                                         t[0] = ot[0];
637                                         t[1] = ot[1];
638                                         t[2] = ot[2];
639                                         t[3] = ot[3];
640                                         ot += 4;
641                                 }
642                                 else {
643                                         xy[0] = x + xoff;
644                                         xy[1] = y + yoff;
645
646                                         brush_sample_tex(brush, xy, rgba);
647                                         t[0]= FTOCHAR(rgba[0]);
648                                         t[1]= FTOCHAR(rgba[1]);
649                                         t[2]= FTOCHAR(rgba[2]);
650                                         t[3]= FTOCHAR(rgba[3]);
651                                 }
652
653                                 b[0] = t[0]*m[0]/255;
654                                 b[1] = t[1]*m[1]/255;
655                                 b[2] = t[2]*m[2]/255;
656                                 b[3] = t[3]*m[3]/255;
657                         }
658                 }
659         }
660 }
661
662 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
663 {
664         Brush *brush= painter->brush;
665         BrushPainterCache *cache= &painter->cache;
666         ImBuf *oldtexibuf, *ibuf;
667         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
668         const int diameter= 2*brush_size(brush);
669
670         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
671         if (!cache->ibuf)
672                 cache->ibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag, 0);
673         ibuf= cache->ibuf;
674
675         oldtexibuf= cache->texibuf;
676         cache->texibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag, 0);
677
678         if (oldtexibuf) {
679                 srcx= srcy= 0;
680                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
681                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
682                 w= oldtexibuf->x;
683                 h= oldtexibuf->y;
684
685                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
686         }
687         else {
688                 srcx= srcy= 0;
689                 destx= desty= 0;
690                 w= h= 0;
691         }
692         
693         x1= destx;
694         y1= desty;
695         x2= destx+w;
696         y2= desty+h;
697
698         /* blend existing texture in new position */
699         if ((x1 < x2) && (y1 < y2))
700                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
701
702         if (oldtexibuf)
703                 IMB_freeImBuf(oldtexibuf);
704
705         /* sample texture in new areas */
706         if ((0 < x1) && (0 < ibuf->y))
707                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
708         if ((x2 < ibuf->x) && (0 < ibuf->y))
709                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
710         if ((x1 < x2) && (0 < y1))
711                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
712         if ((x1 < x2) && (y2 < ibuf->y))
713                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
714 }
715
716 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
717 {
718         Brush *brush= painter->brush;
719         BrushPainterCache *cache= &painter->cache;
720         MTex *mtex= &brush->mtex;
721         int size;
722         short flt;
723         const int radius= brush_size(brush);
724         const float alpha= brush_alpha(brush);
725
726         if (radius != cache->lastsize ||
727                 alpha != cache->lastalpha ||
728                 brush->jitter != cache->lastjitter)
729         {
730                 if (cache->ibuf) {
731                         IMB_freeImBuf(cache->ibuf);
732                         cache->ibuf= NULL;
733                 }
734                 if (cache->maskibuf) {
735                         IMB_freeImBuf(cache->maskibuf);
736                         cache->maskibuf= NULL;
737                 }
738
739                 flt= cache->flt;
740                 size= (cache->size)? cache->size: radius;
741
742                 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
743                         brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
744                 }
745                 else if (brush->flag & BRUSH_FIXED_TEX) {
746                         brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
747                         brush_painter_fixed_tex_partial_update(painter, pos);
748                 }
749                 else
750                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
751
752                 cache->lastsize= radius;
753                 cache->lastalpha= alpha;
754                 cache->lastjitter= brush->jitter;
755         }
756         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
757                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
758                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
759
760                 if ((dx != 0) || (dy != 0))
761                         brush_painter_fixed_tex_partial_update(painter, pos);
762         }
763 }
764
765 void brush_painter_break_stroke(BrushPainter *painter)
766 {
767         painter->firsttouch= 1;
768 }
769
770 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
771 {
772         if (brush_use_alpha_pressure(brush)) 
773                 brush_set_alpha(brush, MAX2(0.0, painter->startalpha*pressure));
774         if (brush_use_size_pressure(brush))
775                 brush_set_size(brush, MAX2(1.0, painter->startsize*pressure));
776         if (brush->flag & BRUSH_JITTER_PRESSURE)
777                 brush->jitter = MAX2(0.0, painter->startjitter*pressure);
778         if (brush->flag & BRUSH_SPACING_PRESSURE)
779                 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
780 }
781
782 void brush_jitter_pos(Brush *brush, float *pos, float *jitterpos)
783 {
784         if(brush->jitter){
785                 float rand_pos[2];
786                 const int radius= brush_size(brush);
787
788                 // find random position within a circle of diameter 1
789                 do {
790                         rand_pos[0] = BLI_frand()-0.5f;
791                         rand_pos[1] = BLI_frand()-0.5f;
792                 } while (len_v2(rand_pos) > 0.5f);
793
794                 jitterpos[0] = pos[0] + 2*rand_pos[0]*radius*brush->jitter;
795                 jitterpos[1] = pos[1] + 2*rand_pos[1]*radius*brush->jitter;
796         }
797         else {
798                 VECCOPY2D(jitterpos, pos);
799         }
800 }
801
802 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
803 {
804         Brush *brush= painter->brush;
805         int totpaintops= 0;
806
807         if (pressure == 0.0f) {
808                 if(painter->lastpressure) // XXX - hack, operator misses
809                         pressure= painter->lastpressure;
810                 else
811                         pressure = 1.0f;        /* zero pressure == not using tablet */
812         }
813         if (painter->firsttouch) {
814                 /* paint exactly once on first touch */
815                 painter->startpaintpos[0]= pos[0];
816                 painter->startpaintpos[1]= pos[1];
817
818                 brush_apply_pressure(painter, brush, pressure);
819                 if (painter->cache.enabled)
820                         brush_painter_refresh_cache(painter, pos);
821                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
822                 
823                 painter->lasttime= time;
824                 painter->firsttouch= 0;
825                 painter->lastpaintpos[0]= pos[0];
826                 painter->lastpaintpos[1]= pos[1];
827         }
828 #if 0
829         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
830                 float spacing, step, paintpos[2], dmousepos[2], len;
831                 double starttime, curtime= time;
832
833                 /* compute brush spacing adapted to brush size */
834                 spacing= brush->rate; //brush_size(brush)*brush->spacing*0.01f;
835
836                 /* setup starting time, direction vector and accumulated time */
837                 starttime= painter->accumtime;
838                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
839                 len= normalize_v2(dmousepos);
840                 painter->accumtime += curtime - painter->lasttime;
841
842                 /* do paint op over unpainted time distance */
843                 while (painter->accumtime >= spacing) {
844                         step= (spacing - starttime)*len;
845                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
846                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
847
848                         if (painter->cache.enabled)
849                                 brush_painter_refresh_cache(painter);
850                         totpaintops += func(user, painter->cache.ibuf,
851                                 painter->lastpaintpos, paintpos);
852
853                         painter->lastpaintpos[0]= paintpos[0];
854                         painter->lastpaintpos[1]= paintpos[1];
855                         painter->accumtime -= spacing;
856                         starttime -= spacing;
857                 }
858                 
859                 painter->lasttime= curtime;
860         }
861 #endif
862         else {
863                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
864                 float t, len, press;
865                 const int radius= brush_size(brush);
866
867                 /* compute brush spacing adapted to brush radius, spacing may depend
868                    on pressure, so update it */
869                 brush_apply_pressure(painter, brush, painter->lastpressure);
870                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
871
872                 /* setup starting distance, direction vector and accumulated distance */
873                 startdistance= painter->accumdistance;
874                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
875                 len= normalize_v2(dmousepos);
876                 painter->accumdistance += len;
877
878                 /* do paint op over unpainted distance */
879                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
880                         step= spacing - startdistance;
881                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
882                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
883
884                         t = step/len;
885                         press= (1.0f-t)*painter->lastpressure + t*pressure;
886                         brush_apply_pressure(painter, brush, press);
887                         spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
888
889                         brush_jitter_pos(brush, paintpos, finalpos);
890
891                         if (painter->cache.enabled)
892                                 brush_painter_refresh_cache(painter, finalpos);
893
894                         totpaintops +=
895                                 func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
896
897                         painter->lastpaintpos[0]= paintpos[0];
898                         painter->lastpaintpos[1]= paintpos[1];
899                         painter->accumdistance -= spacing;
900                         startdistance -= spacing;
901                 }
902
903                 /* do airbrush paint ops, based on the number of paint ops left over
904                    from regular painting. this is a temporary solution until we have
905                    accurate time stamps for mouse move events */
906                 if (brush->flag & BRUSH_AIRBRUSH) {
907                         double curtime= time;
908                         double painttime= brush->rate*totpaintops;
909
910                         painter->accumtime += curtime - painter->lasttime;
911                         if (painter->accumtime <= painttime)
912                                 painter->accumtime= 0.0;
913                         else
914                                 painter->accumtime -= painttime;
915
916                         while (painter->accumtime >= brush->rate) {
917                                 brush_apply_pressure(painter, brush, pressure);
918
919                                 brush_jitter_pos(brush, pos, finalpos);
920
921                                 if (painter->cache.enabled)
922                                         brush_painter_refresh_cache(painter, finalpos);
923
924                                 totpaintops +=
925                                         func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
926                                 painter->accumtime -= brush->rate;
927                         }
928
929                         painter->lasttime= curtime;
930                 }
931         }
932
933         painter->lastmousepos[0]= pos[0];
934         painter->lastmousepos[1]= pos[1];
935         painter->lastpressure= pressure;
936
937         brush_set_alpha(brush, painter->startalpha);
938         brush_set_size(brush, painter->startsize);
939         brush->jitter = painter->startjitter;
940         brush->spacing = painter->startspacing;
941
942         return totpaintops;
943 }
944
945 /* Uses the brush curve control to find a strength value between 0 and 1 */
946 float brush_curve_strength_clamp(Brush *br, float p, const float len)
947 {
948         if(p >= len)    return 0;
949         else                    p= p/len;
950
951         p= curvemapping_evaluateF(br->curve, 0, p);
952         if(p < 0.0)                     p= 0.0f;
953         else if(p > 1.0f)       p= 1.0f;
954         return p;
955 }
956 /* same as above but can return negative values if the curve enables
957  * used for sculpt only */
958 float brush_curve_strength(Brush *br, float p, const float len)
959 {
960     if(p >= len)
961         p= 1.0f;
962     else
963         p= p/len;
964
965     return curvemapping_evaluateF(br->curve, 0, p);
966 }
967
968 /* TODO: should probably be unified with BrushPainter stuff? */
969 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
970 {
971         unsigned int *texcache = NULL;
972         MTex *mtex = &br->mtex;
973         TexResult texres;
974         int hasrgb, ix, iy;
975         int side = half_side * 2;
976
977         memset(&texres, 0, sizeof(TexResult));
978         
979         if(mtex->tex) {
980                 float x, y, step = 2.0 / side, co[3];
981
982                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
983
984                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
985                 
986                 /*do normalized cannonical view coords for texture*/
987                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
988                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
989                                 co[0]= x;
990                                 co[1]= y;
991                                 co[2]= 0.0f;
992                                 
993                                 /* This is copied from displace modifier code */
994                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres);
995                         
996                                 /* if the texture gave an RGB value, we assume it didn't give a valid
997                                  * intensity, so calculate one (formula from do_material_tex).
998                                  * if the texture didn't give an RGB value, copy the intensity across
999                                  */
1000                                 if(hasrgb & TEX_RGB)
1001                                         texres.tin = (0.35 * texres.tr + 0.45 *
1002                                                                   texres.tg + 0.2 * texres.tb);
1003
1004                                 texres.tin = texres.tin * 255.0;
1005                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1006                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1007                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1008                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1009                         }
1010                 }
1011         }
1012
1013         return texcache;
1014 }
1015
1016 /**** Radial Control ****/
1017 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
1018 {
1019         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1020         unsigned int *texcache;
1021         int side = 128;
1022         int half = side / 2;
1023         int i, j;
1024
1025         texcache = brush_gen_texture_cache(br, half);
1026         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1027         im->x = im->y = side;
1028
1029         for(i=0; i<side; ++i) {
1030                 for(j=0; j<side; ++j) {
1031                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1032                         im->rect_float[i*side + j]= brush_curve_strength_clamp(br, magn, half);
1033                 }
1034         }
1035
1036         /* Modulate curve with texture */
1037         if(texcache) {
1038                 for(i=0; i<side; ++i) {
1039                         for(j=0; j<side; ++j) {
1040                                 const int col= texcache[i*side+j];
1041                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1042                         }
1043                 }
1044
1045                 MEM_freeN(texcache);
1046         }
1047
1048         return im;
1049 }
1050
1051 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
1052 {
1053         int mode = RNA_int_get(op->ptr, "mode");
1054         float original_value= 0;
1055
1056         if(mode == WM_RADIALCONTROL_SIZE)
1057                 original_value = brush_size(br) * size_weight;
1058         else if(mode == WM_RADIALCONTROL_STRENGTH)
1059                 original_value = brush_alpha(br);
1060         else if(mode == WM_RADIALCONTROL_ANGLE) {
1061                 MTex *mtex = brush_active_texture(br);
1062                 if(mtex)
1063                         original_value = mtex->rot;
1064         }
1065
1066         RNA_float_set(op->ptr, "initial_value", original_value);
1067         op->customdata = brush_gen_radial_control_imbuf(br);
1068 }
1069
1070 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1071 {
1072         int mode = RNA_int_get(op->ptr, "mode");
1073         float new_value = RNA_float_get(op->ptr, "new_value");
1074         const float conv = 0.017453293;
1075
1076         if(mode == WM_RADIALCONTROL_SIZE)
1077                 if (brush_use_locked_size(br)) {
1078                         float initial_value = RNA_float_get(op->ptr, "initial_value");
1079                         const float unprojected_radius = brush_unprojected_radius(br);
1080                         brush_set_unprojected_radius(br, unprojected_radius * new_value/initial_value * size_weight);
1081                 }
1082                 else
1083                         brush_set_size(br, new_value * size_weight);
1084         else if(mode == WM_RADIALCONTROL_STRENGTH)
1085                 brush_set_alpha(br, new_value);
1086         else if(mode == WM_RADIALCONTROL_ANGLE) {
1087                 MTex *mtex = brush_active_texture(br);
1088                 if(mtex)
1089                         mtex->rot = new_value * conv;
1090         }
1091
1092         return OPERATOR_FINISHED;
1093 }
1094
1095 /* Unified Size and Strength */
1096
1097 static void set_unified_settings(Brush *brush, short flag, int value)
1098 {
1099         Scene *sce;
1100         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1101                 if (sce->toolsettings && 
1102                         ELEM4(brush,
1103                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1104                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1105                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1106                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1107                 {
1108                         if (value)
1109                                 sce->toolsettings->sculpt_paint_settings |= flag;
1110                         else
1111                                 sce->toolsettings->sculpt_paint_settings &= ~flag;
1112                 }
1113         }
1114 }
1115
1116 static short unified_settings(Brush *brush)
1117 {
1118         Scene *sce;
1119         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1120                 if (sce->toolsettings && 
1121                         ELEM4(brush,
1122                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1123                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1124                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1125                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1126                 {
1127                         return sce->toolsettings->sculpt_paint_settings;
1128                 }
1129         }
1130
1131         return 0;
1132 }
1133
1134 static void set_unified_size(Brush *brush, int value)
1135 {
1136         Scene *sce;
1137         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1138                 if (sce->toolsettings && 
1139                         ELEM4(brush,
1140                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1141                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1142                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1143                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1144                 {
1145                         sce->toolsettings->sculpt_paint_unified_size= value;
1146                 }
1147         }
1148 }
1149
1150 static int unified_size(Brush *brush)
1151 {
1152         Scene *sce;
1153         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1154                 if (sce->toolsettings && 
1155                         ELEM4(brush,
1156                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1157                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1158                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1159                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1160                 {
1161                         return sce->toolsettings->sculpt_paint_unified_size;
1162                 }
1163         }
1164
1165         return 35; // XXX magic number
1166 }
1167
1168 static void set_unified_alpha(Brush *brush, float value)
1169 {
1170         Scene *sce;
1171         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1172                 if (sce->toolsettings && 
1173                         ELEM4(brush,
1174                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1175                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1176                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1177                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1178                 {
1179                         sce->toolsettings->sculpt_paint_unified_alpha= value;
1180                 }
1181         }
1182 }
1183
1184 static float unified_alpha(Brush *brush)
1185 {
1186         Scene *sce;
1187         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1188                 if (sce->toolsettings && 
1189                         ELEM4(brush,
1190                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1191                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1192                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1193                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1194                 {
1195                         return sce->toolsettings->sculpt_paint_unified_alpha;
1196                 }
1197         }
1198
1199         return 0.5f; // XXX magic number
1200 }
1201
1202 static void set_unified_unprojected_radius(Brush *brush, float value)
1203 {
1204         Scene *sce;
1205         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1206                 if (sce->toolsettings && 
1207                         ELEM4(brush,
1208                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1209                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1210                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1211                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1212                 {
1213                         sce->toolsettings->sculpt_paint_unified_unprojected_radius= value;
1214                 }
1215         }
1216 }
1217
1218 static float unified_unprojected_radius(Brush *brush)
1219 {
1220         Scene *sce;
1221         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1222                 if (sce->toolsettings && 
1223                         ELEM4(brush,
1224                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1225                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1226                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1227                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1228                 {
1229                         return sce->toolsettings->sculpt_paint_unified_unprojected_radius;
1230                 }
1231         }
1232
1233         return 0.125f; // XXX magic number
1234 }
1235 void brush_set_size(Brush *brush, int size)
1236 {
1237         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1238                 set_unified_size(brush, size);
1239         else
1240                 brush->size= size;
1241
1242         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1243 }
1244
1245 int brush_size(Brush *brush)
1246 {
1247         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_size(brush) : brush->size;
1248 }
1249
1250 void brush_set_use_locked_size(Brush *brush, int value)
1251 {
1252         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1253                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE, value);
1254         }
1255         else {
1256                 if (value)
1257                         brush->flag |= BRUSH_LOCK_SIZE;
1258                 else
1259                         brush->flag &= ~BRUSH_LOCK_SIZE;
1260         }
1261
1262         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1263 }
1264
1265 int brush_use_locked_size(Brush *brush)
1266 {
1267         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE) : (brush->flag & BRUSH_LOCK_SIZE);
1268 }
1269
1270 void brush_set_use_size_pressure(Brush *brush, int value)
1271 {
1272         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1273                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_SIZE_PRESSURE, value);
1274         }
1275         else {
1276                 if (value)
1277                         brush->flag |= BRUSH_SIZE_PRESSURE;
1278                 else
1279                         brush->flag &= ~BRUSH_SIZE_PRESSURE;
1280         }
1281
1282         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1283 }
1284
1285 int brush_use_size_pressure(Brush *brush)
1286 {
1287         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_SIZE_PRESSURE) : (brush->flag & BRUSH_SIZE_PRESSURE);
1288 }
1289
1290 void brush_set_use_alpha_pressure(Brush *brush, int value)
1291 {
1292         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) {
1293                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE, value);
1294         }
1295         else {
1296                 if (value)
1297                         brush->flag |= BRUSH_ALPHA_PRESSURE;
1298                 else
1299                         brush->flag &= ~BRUSH_ALPHA_PRESSURE;
1300         }
1301
1302         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1303 }
1304
1305 int brush_use_alpha_pressure(Brush *brush)
1306 {
1307         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE) : (brush->flag & BRUSH_ALPHA_PRESSURE);
1308 }
1309
1310 void brush_set_unprojected_radius(Brush *brush, float unprojected_radius)
1311 {
1312         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1313                 set_unified_unprojected_radius(brush, unprojected_radius);
1314         else
1315                 brush->unprojected_radius= unprojected_radius;
1316
1317         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1318 }
1319
1320 float brush_unprojected_radius(Brush *brush)
1321 {
1322         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_unprojected_radius(brush) : brush->unprojected_radius;
1323 }
1324
1325 void brush_set_alpha(Brush *brush, float alpha)
1326 {
1327         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) 
1328                 set_unified_alpha(brush, alpha);
1329         else
1330                 brush->alpha= alpha;
1331
1332         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1333 }
1334
1335 float brush_alpha(Brush *brush)
1336 {
1337         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? unified_alpha(brush) : brush->alpha;
1338 }