diff --git a/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.cpp b/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.cpp new file mode 100644 index 0000000000..5df15d94fc --- /dev/null +++ b/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.cpp @@ -0,0 +1,111 @@ +/* + * Copyright 2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stephan Aßmus + */ + +#include "FlipPointsCommand.h" + +#include +#include + +#include "VectorPath.h" + +using std::nothrow; + +// constructor +FlipPointsCommand::FlipPointsCommand(VectorPath* path, + const int32* indices, + int32 count) + : PathCommand(path), + fIndex(NULL), + fCount(0) +{ + if (indices && count > 0) { + fIndex = new (nothrow) int32[count]; + fCount = count; + } + + if (InitCheck() < B_OK) + return; + + memcpy(fIndex, indices, count * sizeof(int32)); +} + +// destructor +FlipPointsCommand::~FlipPointsCommand() +{ + delete[] fIndex; +} + +// InitCheck +status_t +FlipPointsCommand::InitCheck() +{ + status_t status = PathCommand::InitCheck(); + if (status < B_OK) + return status; + if (!fIndex) + status = B_NO_MEMORY; + return status; +} + +// Perform +status_t +FlipPointsCommand::Perform() +{ + status_t status = B_OK; + + AutoNotificationSuspender _(fPath); + + // NOTE: fCount guaranteed > 0 + for (int32 i = 0; i < fCount; i++) { + BPoint point; + BPoint pointIn; + BPoint pointOut; + bool connected; + + if (fPath->GetPointsAt(fIndex[i], point, pointIn, pointOut, &connected)) { +BPoint p1(point - (pointOut - point)); +printf("flip: (%.1f, %.1f) -> (%.1f, %.1f)\n", pointOut.x, pointOut.y, p1.x, p1.y); + // flip "in" and "out" control points + fPath->SetPoint(fIndex[i], + point, + point - (pointIn - point), + point - (pointOut - point), + connected); + } else { + status = B_ERROR; + break; + } + } + + return status; +} + +// Undo +status_t +FlipPointsCommand::Undo() +{ + status_t status = Perform(); + + if (status >= B_OK) { + // restore selection + _Select(fIndex, fCount); + } + + return status; +} + +// GetName +void +FlipPointsCommand::GetName(BString& name) +{ + if (fCount > 1) + name << "Flip Control Points"; + else + name << "Flip Control Point"; +} + diff --git a/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.h b/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.h new file mode 100644 index 0000000000..274f8492a9 --- /dev/null +++ b/src/apps/icon-o-matic/shape/commands/FlipPointsCommand.h @@ -0,0 +1,35 @@ +/* + * Copyright 2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stephan Aßmus + */ + +#ifndef FLIP_POINTS_COMMAND_H +#define FLIP_POINTS_COMMAND_H + +#include "PathCommand.h" + +class BPoint; + +class FlipPointsCommand : public PathCommand { + public: + FlipPointsCommand(VectorPath* path, + const int32* indices, + int32 count); + virtual ~FlipPointsCommand(); + + virtual status_t InitCheck(); + + virtual status_t Perform(); + virtual status_t Undo(); + + virtual void GetName(BString& name); + + private: + int32* fIndex; + int32 fCount; +}; + +#endif // FLIP_POINTS_COMMAND_H