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
25using std::nothrow;
26
27// constructor
28FlipPointsCommand::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
47FlipPointsCommand::~FlipPointsCommand()
48{
49	delete[] fIndex;
50}
51
52// InitCheck
53status_t
54FlipPointsCommand::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
65status_t
66FlipPointsCommand::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)) {
80BPoint p1(point - (pointOut - point));
81printf("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
98status_t
99FlipPointsCommand::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
112void
113FlipPointsCommand::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