xref: /haiku/src/apps/icon-o-matic/transformable/ResetTransformationCommand.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2006, 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 "ResetTransformationCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include "ChannelTransform.h"
15 
16 using std::nothrow;
17 
18 // constructor
19 ResetTransformationCommand::ResetTransformationCommand(
20 								Transformable** const objects,
21 								int32 count)
22 	: Command(),
23 	  fObjects(objects && count > 0 ?
24 	  		   new (nothrow) Transformable*[count] : NULL),
25 	  fOriginals(objects && count > 0 ?
26 	  			 new (nothrow) double[
27 	  			 	count * Transformable::matrix_size] : NULL),
28 	  fCount(count)
29 {
30 	if (!fObjects || !fOriginals)
31 		return;
32 
33 	memcpy(fObjects, objects, fCount * sizeof(Transformable*));
34 
35 	int32 matrixSize = Transformable::matrix_size;
36 	for (int32 i = 0; i < fCount; i++) {
37 		fObjects[i]->StoreTo(&fOriginals[matrixSize * i]);
38 	}
39 }
40 
41 // destructor
42 ResetTransformationCommand::~ResetTransformationCommand()
43 {
44 	delete[] fObjects;
45 	delete[] fOriginals;
46 }
47 
48 // InitCheck
49 status_t
50 ResetTransformationCommand::InitCheck()
51 {
52 	return fObjects && fOriginals ? B_OK : B_NO_INIT;
53 }
54 
55 // Perform
56 status_t
57 ResetTransformationCommand::Perform()
58 {
59 	// reset transformations
60 	for (int32 i = 0; i < fCount; i++) {
61 		if (fObjects[i])
62 			fObjects[i]->Reset();
63 	}
64 	return B_OK;
65 }
66 
67 // Undo
68 status_t
69 ResetTransformationCommand::Undo()
70 {
71 	// reset transformations
72 	int32 matrixSize = Transformable::matrix_size;
73 	for (int32 i = 0; i < fCount; i++) {
74 		if (fObjects[i])
75 			fObjects[i]->LoadFrom(&fOriginals[i * matrixSize]);
76 	}
77 	return B_OK;
78 }
79 
80 // GetName
81 void
82 ResetTransformationCommand::GetName(BString& name)
83 {
84 	if (fCount > 1)
85 		name << "Reset Transformations";
86 	else
87 		name << "Reset Transformation";
88 }
89