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