2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
9 #include "FreezeTransformationCommand.h"
18 #include "GradientTransformable.h"
21 #include "VectorPath.h"
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-FreezeTransformationCmd"
31 FreezeTransformationCommand::FreezeTransformationCommand(
35 fShapes(shapes
&& count
> 0 ? new (nothrow
) Shape
*[count
] : NULL
),
36 fOriginalTransformations(count
> 0 ? new (nothrow
) double[
37 count
* Transformable::matrix_size
]
41 if (!fShapes
|| !fOriginalTransformations
)
44 memcpy(fShapes
, shapes
, sizeof(Shape
*) * fCount
);
48 for (int32 i
= 0; i
< fCount
; i
++) {
51 if (!fShapes
[i
]->IsIdentity())
53 fShapes
[i
]->StoreTo(&fOriginalTransformations
[
54 i
* Transformable::matrix_size
]);
60 delete[] fOriginalTransformations
;
61 fOriginalTransformations
= NULL
;
66 FreezeTransformationCommand::~FreezeTransformationCommand()
69 delete[] fOriginalTransformations
;
74 FreezeTransformationCommand::InitCheck()
76 return fShapes
&& fOriginalTransformations
? B_OK
: B_NO_INIT
;
81 FreezeTransformationCommand::Perform()
83 for (int32 i
= 0; i
< fCount
; i
++) {
84 if (!fShapes
[i
] || fShapes
[i
]->IsIdentity())
87 _ApplyTransformation(fShapes
[i
], *(fShapes
[i
]));
96 FreezeTransformationCommand::Undo()
98 for (int32 i
= 0; i
< fCount
; i
++) {
102 // restore original transformation
103 fShapes
[i
]->LoadFrom(&fOriginalTransformations
[
104 i
* Transformable::matrix_size
]);
106 Transformable
transform(*(fShapes
[i
]));
107 if (!transform
.IsValid() || transform
.IsIdentity())
111 _ApplyTransformation(fShapes
[i
], transform
);
119 FreezeTransformationCommand::GetName(BString
& name
)
122 name
<< B_TRANSLATE("Freeze Shapes");
124 name
<< B_TRANSLATE("Freeze Shape");
129 // _ApplyTransformation
131 FreezeTransformationCommand::_ApplyTransformation(Shape
* shape
,
132 const Transformable
& transform
)
134 // apply inverse of old shape transformation to every assigned path
135 int32 pathCount
= shape
->Paths()->CountPaths();
136 for (int32 i
= 0; i
< pathCount
; i
++) {
137 VectorPath
* path
= shape
->Paths()->PathAtFast(i
);
139 int32 listeners
= path
->CountListeners();
140 for (int32 j
= 0; j
< listeners
; j
++) {
141 if (dynamic_cast<Shape
*>(path
->ListenerAtFast(j
)))
144 // only freeze transformation of path if only one
145 // shape has it assigned
147 path
->ApplyTransform(transform
);
149 printf("Not transfering transformation of \"%s\" onto "
150 "path \"%s\", because %" B_PRId32
" other shapes "
151 "have it assigned.\n", shape
->Name(), path
->Name(),
155 // take care of style too
156 if (shape
->Style() && shape
->Style()->Gradient()) {
157 // TODO: not if more than one shape have this style assigned!
158 shape
->Style()->Gradient()->Multiply(transform
);