xref: /haiku/src/apps/icon-o-matic/transformable/TransformCommand.cpp (revision f23596149e0d173463f70629581aa10cc305d32e)
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 "TransformCommand.h"
10 
11 #include <stdio.h>
12 
13 // constructor
14 TransformCommand::TransformCommand(BPoint pivot,
15 								   BPoint translation,
16 								   double rotation,
17 								   double xScale,
18 								   double yScale,
19 								   const char* actionName,
20 								   uint32 nameIndex)
21 	: Command(),
22 	  fOldPivot(pivot),
23 	  fOldTranslation(translation),
24 	  fOldRotation(rotation),
25 	  fOldXScale(xScale),
26 	  fOldYScale(yScale),
27 
28 	  fNewPivot(pivot),
29 	  fNewTranslation(translation),
30 	  fNewRotation(rotation),
31 	  fNewXScale(xScale),
32 	  fNewYScale(yScale),
33 
34 	  fName(actionName),
35 	  fNameIndex(nameIndex)
36 {
37 }
38 
39 // constructor
40 TransformCommand::TransformCommand(const char* actionName,
41 								   uint32 nameIndex)
42 	: Command(),
43 	  fOldPivot(B_ORIGIN),
44 	  fOldTranslation(B_ORIGIN),
45 	  fOldRotation(0.0),
46 	  fOldXScale(1.0),
47 	  fOldYScale(1.0),
48 
49 	  fNewPivot(B_ORIGIN),
50 	  fNewTranslation(B_ORIGIN),
51 	  fNewRotation(0.0),
52 	  fNewXScale(1.0),
53 	  fNewYScale(1.0),
54 
55 	  fName(actionName),
56 	  fNameIndex(nameIndex)
57 {
58 }
59 
60 // destructor
61 TransformCommand::~TransformCommand()
62 {
63 }
64 
65 // InitCheck
66 status_t
67 TransformCommand::InitCheck()
68 {
69 	if ((fNewPivot != fOldPivot
70 		 || fNewTranslation != fOldTranslation
71 		 || fNewRotation != fOldRotation
72 		 || fNewXScale != fOldXScale
73 		 || fNewYScale != fOldYScale))
74 		return B_OK;
75 	else
76 		return B_NO_INIT;
77 }
78 
79 // Perform
80 status_t
81 TransformCommand::Perform()
82 {
83 	// objects are already transformed
84 	return B_OK;
85 }
86 
87 // Undo
88 status_t
89 TransformCommand::Undo()
90 {
91 	status_t status = InitCheck();
92 	if (status >= B_OK) {
93 		_SetTransformation(fOldPivot,
94 						   fOldTranslation,
95 						   fOldRotation,
96 						   fOldXScale,
97 						   fOldYScale);
98 	}
99 	return status;
100 }
101 
102 // Redo
103 status_t
104 TransformCommand::Redo()
105 {
106 	status_t status = InitCheck();
107 	if (status >= B_OK) {
108 		_SetTransformation(fNewPivot,
109 						   fNewTranslation,
110 						   fNewRotation,
111 						   fNewXScale,
112 						   fNewYScale);
113 	}
114 	return status;
115 }
116 
117 // GetName
118 void
119 TransformCommand::GetName(BString& name)
120 {
121 	name << _GetString(fNameIndex, fName.String());
122 }
123 
124 // SetNewTransformation
125 void
126 TransformCommand::SetNewTransformation(BPoint pivot,
127 									   BPoint translation,
128 									   double rotation,
129 									   double xScale,
130 									   double yScale)
131 {
132 	fNewPivot = pivot;
133 	fNewTranslation = translation;
134 	fNewRotation = rotation;
135 	fNewXScale = xScale;
136 	fNewYScale = yScale;
137 }
138 
139 // SetNewTranslation
140 void
141 TransformCommand::SetNewTranslation(BPoint translation)
142 {
143 	// NOTE: convinience method for nudging
144 	fNewTranslation = translation;
145 }
146 
147 // SetName
148 void
149 TransformCommand::SetName(const char* actionName, uint32 nameIndex)
150 {
151 	fName.SetTo(actionName);
152 	fNameIndex = nameIndex;
153 }
154 
155