xref: /haiku/src/apps/icon-o-matic/shape/commands/ChangePointCommand.cpp (revision e10dd8e99a9d7b730c79e6d59d1d288fc6c6dc9d)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "ChangePointCommand.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-ChangePointCmd"
22 
23 
24 using std::nothrow;
25 
26 // constructor
27 ChangePointCommand::ChangePointCommand(VectorPath* path,
28 									  int32 index,
29  									  const int32* selected,
30 									  int32 count)
31 	: PathCommand(path),
32 	  fIndex(index),
33 	  fOldSelection(NULL),
34 	  fOldSelectionCount(count)
35 {
36 	if (fPath && !fPath->GetPointsAt(fIndex, fPoint, fPointIn, fPointOut, &fConnected))
37 		fPath = NULL;
38 	if (fOldSelectionCount > 0 && selected) {
39 		fOldSelection = new (nothrow) int32[fOldSelectionCount];
40 		memcpy(fOldSelection, selected, fOldSelectionCount * sizeof(int32));
41 	}
42 }
43 
44 // destructor
45 ChangePointCommand::~ChangePointCommand()
46 {
47 	delete[] fOldSelection;
48 }
49 
50 // InitCheck
51 status_t
52 ChangePointCommand::InitCheck()
53 {
54 	// TODO: figure out if selection changed!!!
55 	// (this command is also used to undo changes to the selection)
56 	// (but tracking the selection does not yet work in Icon-O-Matic)
57 
58 	status_t ret = PathCommand::InitCheck();
59 	if (ret < B_OK)
60 		return ret;
61 
62 	BPoint point;
63 	BPoint pointIn;
64 	BPoint pointOut;
65 	bool connected;
66 	if (!fPath->GetPointsAt(fIndex, point, pointIn, pointOut, &connected))
67 		return B_ERROR;
68 
69 	if (point != fPoint || pointIn != fPointIn
70 		|| pointOut != fPointOut || connected != fConnected)
71 		return B_OK;
72 
73 	return B_ERROR;
74 }
75 
76 // Perform
77 status_t
78 ChangePointCommand::Perform()
79 {
80 	// path point is already changed
81 	return B_OK;
82 }
83 
84 // Undo
85 status_t
86 ChangePointCommand::Undo()
87 {
88 	status_t status = InitCheck();
89 	if (status < B_OK)
90 		return status;
91 
92 	// set the point to the remembered state and
93 	// save the previous state of the point
94 	BPoint point;
95 	BPoint pointIn;
96 	BPoint pointOut;
97 	bool connected;
98 	if (fPath->GetPointsAt(fIndex, point, pointIn, pointOut, &connected)
99 		&& fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, fConnected)) {
100 		// toggle the remembered settings
101 		fPoint = point;
102 		fPointIn = pointIn;
103 		fPointOut = pointOut;
104 		fConnected = connected;
105 		// restore old selection
106 		_Select(fOldSelection, fOldSelectionCount);
107 	} else {
108 		status = B_ERROR;
109 	}
110 
111 	return status;
112 }
113 
114 // Redo
115 status_t
116 ChangePointCommand::Redo()
117 {
118 	status_t status = Undo();
119 	if (status >= B_OK)
120 		_Select(&fIndex, 1);
121 	return status;
122 }
123 
124 // GetName
125 void
126 ChangePointCommand::GetName(BString& name)
127 {
128 	name << B_TRANSLATE("Modify vertex");
129 }
130