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

paint_curve.c « sculpt_paint « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c7c3b102e32dba09b0a701c1b5139688847c392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/sculpt_paint/paint_curve.c
 *  \ingroup edsculpt
 */

#include <string.h>
#include <limits.h>

#include "MEM_guardedalloc.h"

#include "DNA_brush_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_view3d_types.h"

#include "BKE_context.h"
#include "BKE_main.h"
#include "BKE_paint.h"

#include "BLI_math_vector.h"
#include "BLI_string.h"

#include "ED_paint.h"
#include "ED_view3d.h"

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

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

#include "UI_view2d.h"

#include "paint_intern.h"

#define PAINT_CURVE_SELECT_THRESHOLD 40.0f
#define PAINT_CURVE_POINT_SELECT(pcp, i) (*(&pcp->bez.f1 + i) = SELECT)


int paint_curve_poll(bContext *C)
{
	Object *ob = CTX_data_active_object(C);
	Paint *p;
	RegionView3D *rv3d = CTX_wm_region_view3d(C);
	SpaceImage *sima;

	if (rv3d && !(ob && ((ob->mode & OB_MODE_ALL_PAINT) != 0)))
		return false;

	sima = CTX_wm_space_image(C);

	if (sima && sima->mode != SI_MODE_PAINT)
		return false;

	p = BKE_paint_get_active_from_context(C);

	if (p && p->brush && (p->brush->flag & BRUSH_CURVE)) {
		return true;
	}

	return false;
}

/* Paint Curve Undo*/

typedef struct UndoCurve {
	struct UndoImageTile *next, *prev;

	PaintCurvePoint *points; /* points of curve */
	int tot_points;
	int active_point;

	char idname[MAX_ID_NAME];  /* name instead of pointer*/
} UndoCurve;

static void paintcurve_undo_restore(bContext *C, ListBase *lb)
{
	Paint *p = BKE_paint_get_active_from_context(C);
	UndoCurve *uc;
	PaintCurve *pc = NULL;

	if (p->brush) {
		pc = p->brush->paint_curve;
	}

	if (!pc)
		return;

	uc = (UndoCurve *)lb->first;

	if (strncmp(uc->idname, pc->id.name, BLI_strnlen(uc->idname, sizeof(uc->idname))) == 0) {
		SWAP(PaintCurvePoint *, pc->points, uc->points);
		SWAP(int, pc->tot_points, uc->tot_points);
		SWAP(int, pc->add_index, uc->active_point);
	}
}

static void paintcurve_undo_delete(ListBase *lb)
{
	UndoCurve *uc;
	uc = (UndoCurve *)lb->first;

	if (uc->points)
		MEM_freeN(uc->points);
	uc->points = NULL;
}


static void paintcurve_undo_begin(bContext *C, wmOperator *op, PaintCurve *pc)
{
	PaintMode mode = BKE_paintmode_get_active_from_context(C);
	ListBase *lb = NULL;
	int undo_stack_id;
	UndoCurve *uc;

	switch (mode) {
		case PAINT_TEXTURE_2D:
		case PAINT_TEXTURE_PROJECTIVE:
			undo_stack_id = UNDO_PAINT_IMAGE;
			break;

		case PAINT_SCULPT:
			undo_stack_id = UNDO_PAINT_MESH;
			break;

		default:
			/* do nothing, undo is handled by global */
			return;
	}


	ED_undo_paint_push_begin(undo_stack_id, op->type->name,
	                         paintcurve_undo_restore, paintcurve_undo_delete, NULL);
	lb = undo_paint_push_get_list(undo_stack_id);

	uc = MEM_callocN(sizeof(*uc), "Undo_curve");

	lb->first = uc;

	BLI_strncpy(uc->idname, pc->id.name, sizeof(uc->idname));
	uc->tot_points = pc->tot_points;
	uc->active_point = pc->add_index;
	uc->points = MEM_dupallocN(pc->points);

	undo_paint_push_count_alloc(undo_stack_id, sizeof(*uc) + sizeof(*pc->points) * pc->tot_points);

	ED_undo_paint_push_end(undo_stack_id);
}
#define SEL_F1 (1 << 0)
#define SEL_F2 (1 << 1)
#define SEL_F3 (1 << 2)

/* returns 0, 1, or 2 in point according to handle 1, pivot or handle 2 */
static PaintCurvePoint *paintcurve_point_get_closest(PaintCurve *pc, const float pos[2], bool ignore_pivot, const float threshold, char *point)
{
	PaintCurvePoint *pcp, *closest = NULL;
	int i;
	float dist, closest_dist = FLT_MAX;

	for (i = 0, pcp = pc->points; i < pc->tot_points; i++, pcp++) {
		dist = len_manhattan_v2v2(pos, pcp->bez.vec[0]);
		if (dist < threshold) {
			if (dist < closest_dist) {
				closest = pcp;
				closest_dist = dist;
				if (point)
					*point = SEL_F1;
			}
		}
		if (!ignore_pivot) {
			dist = len_manhattan_v2v2(pos, pcp->bez.vec[1]);
			if (dist < threshold) {
				if (dist < closest_dist) {
					closest = pcp;
					closest_dist = dist;
					if (point)
						*point = SEL_F2;
				}
			}
		}
		dist = len_manhattan_v2v2(pos, pcp->bez.vec[2]);
		if (dist < threshold) {
			if (dist < closest_dist) {
				closest = pcp;
				closest_dist = dist;
				if (point)
					*point = SEL_F3;
			}
		}
	}

	return closest;
}

static int paintcurve_point_co_index(char sel)
{
	char i = 0;
	while (sel != 1) {
		sel >>= 1;
		i++;
	}
	return i;
}

/******************* Operators *********************************/

static int paintcurve_new_exec(bContext *C, wmOperator *UNUSED(op))
{
	Paint *p = BKE_paint_get_active_from_context(C);
	Main *bmain = CTX_data_main(C);

	if (p && p->brush) {
		p->brush->paint_curve = BKE_paint_curve_add(bmain, "PaintCurve");
	}

	return OPERATOR_FINISHED;
}

void PAINTCURVE_OT_new(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add New Paint Curve";
	ot->description = "Add new paint curve";
	ot->idname = "PAINTCURVE_OT_new";

	/* api callbacks */
	ot->exec = paintcurve_new_exec;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

static void paintcurve_point_add(bContext *C,  wmOperator *op, const int loc[2])
{
	Paint *p = BKE_paint_get_active_from_context(C);
	Brush *br = p->brush;
	Main *bmain = CTX_data_main(C);
	PaintCurve *pc;
	PaintCurvePoint *pcp;
	wmWindow *window = CTX_wm_window(C);
	ARegion *ar = CTX_wm_region(C);
	float vec[3] = {loc[0], loc[1], 0.0};
	int add_index;
	int i;

	pc = br->paint_curve;
	if (!pc) {
		br->paint_curve = pc = BKE_paint_curve_add(bmain, "PaintCurve");
	}

	paintcurve_undo_begin(C, op, pc);

	pcp = MEM_mallocN((pc->tot_points + 1) * sizeof(PaintCurvePoint), "PaintCurvePoint");
	add_index = pc->add_index;

	if (pc->points) {
		if (add_index > 0)
			memcpy(pcp, pc->points, add_index * sizeof(PaintCurvePoint));
		if (add_index < pc->tot_points)
			memcpy(pcp + add_index + 1, pc->points + add_index, (pc->tot_points - add_index) * sizeof(PaintCurvePoint));

		MEM_freeN(pc->points);
	}
	pc->points = pcp;
	pc->tot_points++;

	/* initialize new point */
	memset(&pcp[add_index], 0, sizeof(PaintCurvePoint));
	copy_v3_v3(pcp[add_index].bez.vec[0], vec);
	copy_v3_v3(pcp[add_index].bez.vec[1], vec);
	copy_v3_v3(pcp[add_index].bez.vec[2], vec);

	/* last step, clear selection from all bezier handles expect the next */
	for (i = 0; i < pc->tot_points; i++) {
		pcp[i].bez.f1 = pcp[i].bez.f2 = pcp[i].bez.f3 = 0;
	}
	pcp[add_index].bez.f3 = SELECT;
	pcp[add_index].bez.h2 = HD_ALIGN;

	pc->add_index = add_index + 1;

	WM_paint_cursor_tag_redraw(window, ar);
}


static int paintcurve_add_point_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	int loc[2] = {event->mval[0], event->mval[1]};
	paintcurve_point_add(C, op, loc);
	RNA_int_set_array(op->ptr, "location", loc);
	return OPERATOR_FINISHED;
}

static int paintcurve_add_point_exec(bContext *C, wmOperator *op)
{
	int loc[2];

	if (RNA_struct_property_is_set(op->ptr, "location")) {
		RNA_int_get_array(op->ptr, "location", loc);
		paintcurve_point_add(C, op, loc);
		return OPERATOR_FINISHED;
	}

	return OPERATOR_CANCELLED;
}

void PAINTCURVE_OT_add_point(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add New Paint Curve Point";
	ot->description = "Add new paint curve point";
	ot->idname = "PAINTCURVE_OT_add_point";

	/* api callbacks */
	ot->invoke = paintcurve_add_point_invoke;
	ot->exec = paintcurve_add_point_exec;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER;

	/* properties */
	RNA_def_int_vector(ot->srna, "location", 2, NULL, 0, SHRT_MAX,
	                   "Location", "Location of vertex in area space", 0, SHRT_MAX);
}

static int paintcurve_delete_point_exec(bContext *C, wmOperator *op)
{
	Paint *p = BKE_paint_get_active_from_context(C);
	Brush *br = p->brush;
	PaintCurve *pc;
	PaintCurvePoint *pcp;
	wmWindow *window = CTX_wm_window(C);
	ARegion *ar = CTX_wm_region(C);
	int i;
	int tot_del = 0;
	pc = br->paint_curve;

	if (!pc || pc->tot_points == 0) {
		return OPERATOR_CANCELLED;
	}

	paintcurve_undo_begin(C, op, pc);

#define DELETE_TAG 2

	for (i = 0, pcp = pc->points; i < pc->tot_points; i++, pcp++) {
		if ((pcp->bez.f1 & SELECT) || (pcp->bez.f2 & SELECT) || (pcp->bez.f3 & SELECT)) {
			pcp->bez.f2 |= DELETE_TAG;
			tot_del++;
		}
	}

	if (tot_del > 0) {
		int j = 0;
		int new_tot = pc->tot_points - tot_del;
		PaintCurvePoint *points_new = NULL;
		if (new_tot > 0)
			points_new = MEM_mallocN(new_tot * sizeof(PaintCurvePoint), "PaintCurvePoint");

		for (i = 0, pcp = pc->points; i < pc->tot_points; i++, pcp++) {
			if (!(pcp->bez.f2 & DELETE_TAG)) {
				points_new[j] = pc->points[i];

				if ((i + 1) == pc->add_index) {
					pc->add_index = j + 1;
				}
				j++;
			}
			else if ((i + 1) == pc->add_index) {
				/* prefer previous point */
				pc->add_index = j;
			}
		}
		MEM_freeN(pc->points);

		pc->points = points_new;
		pc->tot_points = new_tot;
	}

#undef DELETE_TAG

	WM_paint_cursor_tag_redraw(window, ar);

	return OPERATOR_FINISHED;
}


void PAINTCURVE_OT_delete_point(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Add New Paint Curve Point";
	ot->description = "Add new paint curve point";
	ot->idname = "PAINTCURVE_OT_delete_point";

	/* api callbacks */
	ot->exec = paintcurve_delete_point_exec;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO;
}


static bool paintcurve_point_select(bContext *C, wmOperator *op, const int loc[2], bool toggle, bool extend)
{
	wmWindow *window = CTX_wm_window(C);
	ARegion *ar = CTX_wm_region(C);
	Paint *p = BKE_paint_get_active_from_context(C);
	Brush *br = p->brush;
	PaintCurve *pc;
	PaintCurvePoint *pcp;
	int i;
	const float loc_fl[2] = {UNPACK2(loc)};

	pc = br->paint_curve;

	if (!pc)
		return false;

	paintcurve_undo_begin(C, op, pc);

	pcp = pc->points;

	if (toggle) {
		char select = 0;
		bool selected = false;

		for (i = 0; i < pc->tot_points; i++) {
			if (pcp[i].bez.f1 || pcp[i].bez.f2 || pcp[i].bez.f3) {
				selected = true;
				break;
			}
		}

		if (!selected) {
			select = SELECT;
		}

		for (i = 0; i < pc->tot_points; i++) {
			pc->points[i].bez.f1 = pc->points[i].bez.f2 = pc->points[i].bez.f3 = select;
		}
	}
	else {
		PaintCurvePoint *pcp;
		char selflag;

		pcp = paintcurve_point_get_closest(pc, loc_fl, false, PAINT_CURVE_SELECT_THRESHOLD, &selflag);

		if (pcp) {
			pc->add_index = (pcp - pc->points) + 1;

			if (selflag == SEL_F2) {
				if (extend)
					pcp->bez.f2 ^= SELECT;
				else
					pcp->bez.f2 |= SELECT;
			}
			else if (selflag == SEL_F1) {
				if (extend)
					pcp->bez.f1 ^= SELECT;
				else
					pcp->bez.f1 |= SELECT;
			}
			else if (selflag == SEL_F3) {
				if (extend)
					pcp->bez.f3 ^= SELECT;
				else
					pcp->bez.f3 |= SELECT;
			}
		}

		/* clear selection for unselected points if not extending and if a point has been selected */
		if (!extend && pcp) {
			for (i = 0; i < pc->tot_points; i++) {
				pc->points[i].bez.f1 = pc->points[i].bez.f2 = pc->points[i].bez.f3 = 0;

				if ((pc->points + i) == pcp) {
					char index = paintcurve_point_co_index(selflag);
					PAINT_CURVE_POINT_SELECT(pcp, index);
				}
			}
		}

		if (!pcp)
			return false;
	}

	WM_paint_cursor_tag_redraw(window, ar);

	return true;
}


static int paintcurve_select_point_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	int loc[2] = {UNPACK2(event->mval)};
	bool toggle = RNA_boolean_get(op->ptr, "toggle");
	bool extend = RNA_boolean_get(op->ptr, "extend");
	if (paintcurve_point_select(C, op, loc, toggle, extend)) {
		RNA_int_set_array(op->ptr, "location", loc);
		return OPERATOR_FINISHED;
	}
	else {
		return OPERATOR_CANCELLED;
	}
}

static int paintcurve_select_point_exec(bContext *C, wmOperator *op)
{
	int loc[2];

	if (RNA_struct_property_is_set(op->ptr, "location")) {
		bool toggle = RNA_boolean_get(op->ptr, "toggle");
		bool extend = RNA_boolean_get(op->ptr, "extend");
		RNA_int_get_array(op->ptr, "location", loc);
		if (paintcurve_point_select(C, op, loc, toggle, extend))
			return OPERATOR_FINISHED;
	}

	return OPERATOR_CANCELLED;
}

void PAINTCURVE_OT_select(wmOperatorType *ot)
{
	PropertyRNA *prop;

	/* identifiers */
	ot->name = "Select Paint Curve Point";
	ot->description = "Select a paint curve point";
	ot->idname = "PAINTCURVE_OT_select";

	/* api callbacks */
	ot->invoke = paintcurve_select_point_invoke;
	ot->exec = paintcurve_select_point_exec;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO | OPTYPE_REGISTER;

	/* properties */
	RNA_def_int_vector(ot->srna, "location", 2, NULL, 0, SHRT_MAX,
	                   "Location", "Location of vertex in area space", 0, SHRT_MAX);
	prop = RNA_def_boolean(ot->srna, "toggle", false, "Toggle", "(De)select all");
	RNA_def_property_flag(prop, PROP_SKIP_SAVE);
	prop = RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend selection");
	RNA_def_property_flag(prop, PROP_SKIP_SAVE);
}

typedef struct PointSlideData {
	PaintCurvePoint *pcp;
	char select;
	int initial_loc[2];
	float point_initial_loc[3][2];
	int event;
	bool align;
} PointSlideData;

static int paintcurve_slide_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	Paint *p = BKE_paint_get_active_from_context(C);
	const float loc_fl[2] = {UNPACK2(event->mval)};
	char select;
	int i;
	bool do_select = RNA_boolean_get(op->ptr, "select");
	bool align = RNA_boolean_get(op->ptr, "align");
	Brush *br = p->brush;
	PaintCurve *pc = br->paint_curve;
	PaintCurvePoint *pcp;

	if (!pc)
		return OPERATOR_PASS_THROUGH;

	if (do_select) {
		pcp = paintcurve_point_get_closest(pc, loc_fl, align, PAINT_CURVE_SELECT_THRESHOLD, &select);
	}
	else {
		pcp = NULL;
		/* just find first selected point */
		for (i = 0; i < pc->tot_points; i++) {
			if (pc->points[i].bez.f1 || pc->points[i].bez.f2 || pc->points[i].bez.f3) {
				pcp = &pc->points[i];
				select = SEL_F3;
				break;
			}
		}
	}


	if (pcp) {
		ARegion *ar = CTX_wm_region(C);
		wmWindow *window = CTX_wm_window(C);
		PointSlideData *psd = MEM_mallocN(sizeof(PointSlideData), "PointSlideData");
		copy_v2_v2_int(psd->initial_loc, event->mval);
		psd->event = event->type;
		psd->pcp = pcp;
		psd->select = paintcurve_point_co_index(select);
		for (i = 0; i < 3; i++) {
			copy_v2_v2(psd->point_initial_loc[i], pcp->bez.vec[i]);
		}
		psd->align = align;
		op->customdata = psd;

		if (do_select)
			paintcurve_undo_begin(C, op, pc);

		/* first, clear all selection from points */
		for (i = 0; i < pc->tot_points; i++)
			pc->points[i].bez.f1 = pc->points[i].bez.f3 = pc->points[i].bez.f2 = 0;

		/* only select the active point */
		PAINT_CURVE_POINT_SELECT(pcp, psd->select);
		pc->add_index = (pcp - pc->points) + 1;

		WM_event_add_modal_handler(C, op);
		WM_paint_cursor_tag_redraw(window, ar);
		return OPERATOR_RUNNING_MODAL;
	}

	return OPERATOR_PASS_THROUGH;
}

static int paintcurve_slide_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
	PointSlideData *psd = op->customdata;

	if (event->type == psd->event && event->val == KM_RELEASE) {
		MEM_freeN(psd);
		return OPERATOR_FINISHED;
	}

	switch (event->type) {
		case MOUSEMOVE:
		{
			ARegion *ar = CTX_wm_region(C);
			wmWindow *window = CTX_wm_window(C);
			float diff[2] = {event->mval[0] - psd->initial_loc[0],
			                 event->mval[1] - psd->initial_loc[1]};
			if (psd->select == 1) {
				int i;
				for (i = 0; i < 3; i++)
					add_v2_v2v2(psd->pcp->bez.vec[i], diff, psd->point_initial_loc[i]);
			}
			else {
				add_v2_v2(diff, psd->point_initial_loc[psd->select]);
				copy_v2_v2(psd->pcp->bez.vec[psd->select], diff);

				if (psd->align) {
					char opposite = (psd->select == 0) ? 2 : 0;
					sub_v2_v2v2(diff, psd->pcp->bez.vec[1], psd->pcp->bez.vec[psd->select]);
					add_v2_v2v2(psd->pcp->bez.vec[opposite], psd->pcp->bez.vec[1], diff);
				}
			}
			WM_paint_cursor_tag_redraw(window, ar);
			break;
		}
		default:
			break;
	}

	return OPERATOR_RUNNING_MODAL;
}


void PAINTCURVE_OT_slide(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Slide Paint Curve Point";
	ot->description = "Select and slide paint curve point";
	ot->idname = "PAINTCURVE_OT_slide";

	/* api callbacks */
	ot->invoke = paintcurve_slide_invoke;
	ot->modal = paintcurve_slide_modal;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO;

	/* properties */
	RNA_def_boolean(ot->srna, "align", false, "Align Handles", "Aligns opposite point handle during transform");
	RNA_def_boolean(ot->srna, "select", true, "Select", "Attempt to select a point handle before transform");
}

static int paintcurve_draw_exec(bContext *C, wmOperator *UNUSED(op))
{
	PaintMode mode = BKE_paintmode_get_active_from_context(C);
	const char *name;

	switch (mode) {
		case PAINT_TEXTURE_2D:
		case PAINT_TEXTURE_PROJECTIVE:
			name = "PAINT_OT_image_paint";
			break;
		case PAINT_WEIGHT:
			name = "PAINT_OT_weight_paint";
			break;
		case PAINT_VERTEX:
			name = "PAINT_OT_vertex_paint";
			break;
		case PAINT_SCULPT:
			name = "SCULPT_OT_brush_stroke";
			break;
		default:
			return OPERATOR_PASS_THROUGH;
	}

	return WM_operator_name_call(C, name, WM_OP_INVOKE_DEFAULT, NULL);
}

void PAINTCURVE_OT_draw(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Draw Curve";
	ot->description = "Draw curve";
	ot->idname = "PAINTCURVE_OT_draw";

	/* api callbacks */
	ot->exec = paintcurve_draw_exec;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO;
}

static int paintcurve_cursor_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
{
	PaintMode mode = BKE_paintmode_get_active_from_context(C);
	
	switch (mode) {
		case PAINT_TEXTURE_2D:
		{
			ARegion *ar = CTX_wm_region(C);
			SpaceImage *sima = CTX_wm_space_image(C);
			float location[2];
			
			if (!sima)
				return OPERATOR_CANCELLED;
			
			UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
			copy_v2_v2(sima->cursor, location);
			WM_event_add_notifier(C, NC_SPACE | ND_SPACE_IMAGE, NULL);
			break;
		}
		default:
			ED_view3d_cursor3d_update(C, event->mval);
			break;
	}
	
	return OPERATOR_FINISHED;
}

void PAINTCURVE_OT_cursor(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Place Cursor";
	ot->description = "Place cursor";
	ot->idname = "PAINTCURVE_OT_cursor";

	/* api callbacks */
	ot->invoke = paintcurve_cursor_invoke;
	ot->poll = paint_curve_poll;

	/* flags */
	ot->flag = 0;
}