xref: /haiku/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.cpp (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
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 <Catalog.h>
15 #include <Locale.h>
16 
17 #include "VectorPath.h"
18 
19 
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-FlipPointsCmd"
22 
23 
24 using std::nothrow;
25 
26 // constructor
27 FlipPointsCommand::FlipPointsCommand(VectorPath* path,
28 									 const int32* indices,
29 									 int32 count)
30 	: PathCommand(path),
31 	  fIndex(NULL),
32 	  fCount(0)
33 {
34 	if (indices && count > 0) {
35 		fIndex = new (nothrow) int32[count];
36 		fCount = count;
37 	}
38 
39 	if (InitCheck() < B_OK)
40 		return;
41 
42 	memcpy(fIndex, indices, count * sizeof(int32));
43 }
44 
45 // destructor
46 FlipPointsCommand::~FlipPointsCommand()
47 {
48 	delete[] fIndex;
49 }
50 
51 // InitCheck
52 status_t
53 FlipPointsCommand::InitCheck()
54 {
55 	status_t status = PathCommand::InitCheck();
56 	if (status < B_OK)
57 		return status;
58 	if (!fIndex)
59 		status = B_NO_MEMORY;
60 	return status;
61 }
62 
63 // Perform
64 status_t
65 FlipPointsCommand::Perform()
66 {
67 	status_t status = B_OK;
68 
69 	AutoNotificationSuspender _(fPath);
70 
71 	// NOTE: fCount guaranteed > 0
72 	for (int32 i = 0; i < fCount; i++) {
73 		BPoint point;
74 		BPoint pointIn;
75 		BPoint pointOut;
76 		bool connected;
77 
78 		if (fPath->GetPointsAt(fIndex[i], point, pointIn, pointOut, &connected)) {
79 BPoint p1(point - (pointOut - point));
80 printf("flip: (%.1f, %.1f) -> (%.1f, %.1f)\n", pointOut.x, pointOut.y, p1.x, p1.y);
81 			// flip "in" and "out" control points
82 			fPath->SetPoint(fIndex[i],
83 							point,
84 							point - (pointIn - point),
85 							point - (pointOut - point),
86 							connected);
87 		} else {
88 			status = B_ERROR;
89 			break;
90 		}
91 	}
92 
93 	return status;
94 }
95 
96 // Undo
97 status_t
98 FlipPointsCommand::Undo()
99 {
100 	status_t status = Perform();
101 
102 	if (status >= B_OK) {
103 		// restore selection
104 		_Select(fIndex, fCount);
105 	}
106 
107 	return status;
108 }
109 
110 // GetName
111 void
112 FlipPointsCommand::GetName(BString& name)
113 {
114 	if (fCount > 1)
115 		name << B_TRANSLATE("Flip Control Points");
116 	else
117 		name << B_TRANSLATE("Flip Control Point");
118 }
119 
120