xref: /haiku/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.cpp (revision 93a78ecaa45114d68952d08c4778f073515102f2)
1 /*
2  * Copyright 2007, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "FlipPointsCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include "VectorPath.h"
15 
16 using std::nothrow;
17 
18 // constructor
19 FlipPointsCommand::FlipPointsCommand(VectorPath* path,
20 									 const int32* indices,
21 									 int32 count)
22 	: PathCommand(path),
23 	  fIndex(NULL),
24 	  fCount(0)
25 {
26 	if (indices && count > 0) {
27 		fIndex = new (nothrow) int32[count];
28 		fCount = count;
29 	}
30 
31 	if (InitCheck() < B_OK)
32 		return;
33 
34 	memcpy(fIndex, indices, count * sizeof(int32));
35 }
36 
37 // destructor
38 FlipPointsCommand::~FlipPointsCommand()
39 {
40 	delete[] fIndex;
41 }
42 
43 // InitCheck
44 status_t
45 FlipPointsCommand::InitCheck()
46 {
47 	status_t status = PathCommand::InitCheck();
48 	if (status < B_OK)
49 		return status;
50 	if (!fIndex)
51 		status = B_NO_MEMORY;
52 	return status;
53 }
54 
55 // Perform
56 status_t
57 FlipPointsCommand::Perform()
58 {
59 	status_t status = B_OK;
60 
61 	AutoNotificationSuspender _(fPath);
62 
63 	// NOTE: fCount guaranteed > 0
64 	for (int32 i = 0; i < fCount; i++) {
65 		BPoint point;
66 		BPoint pointIn;
67 		BPoint pointOut;
68 		bool connected;
69 
70 		if (fPath->GetPointsAt(fIndex[i], point, pointIn, pointOut, &connected)) {
71 BPoint p1(point - (pointOut - point));
72 printf("flip: (%.1f, %.1f) -> (%.1f, %.1f)\n", pointOut.x, pointOut.y, p1.x, p1.y);
73 			// flip "in" and "out" control points
74 			fPath->SetPoint(fIndex[i],
75 							point,
76 							point - (pointIn - point),
77 							point - (pointOut - point),
78 							connected);
79 		} else {
80 			status = B_ERROR;
81 			break;
82 		}
83 	}
84 
85 	return status;
86 }
87 
88 // Undo
89 status_t
90 FlipPointsCommand::Undo()
91 {
92 	status_t status = Perform();
93 
94 	if (status >= B_OK) {
95 		// restore selection
96 		_Select(fIndex, fCount);
97 	}
98 
99 	return status;
100 }
101 
102 // GetName
103 void
104 FlipPointsCommand::GetName(BString& name)
105 {
106 	if (fCount > 1)
107 		name << "Flip Control Points";
108 	else
109 		name << "Flip Control Point";
110 }
111 
112