1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppcanvas.hxx"
30 
31 #include <com/sun/star/rendering/XSprite.hpp>
32 #include <com/sun/star/rendering/XAnimatedSprite.hpp>
33 
34 #include <basegfx/tools/canvastools.hxx>
35 #include <basegfx/polygon/b2dpolypolygon.hxx>
36 #include <canvas/canvastools.hxx>
37 
38 #include "implsprite.hxx"
39 
40 
41 using namespace ::com::sun::star;
42 
43 namespace cppcanvas
44 {
45     namespace internal
46     {
47 
48         ImplSprite::ImplSprite( const uno::Reference< rendering::XSpriteCanvas >& 		rParentCanvas,
49                                 const uno::Reference< rendering::XSprite >& 			rSprite,
50                                 const ImplSpriteCanvas::TransformationArbiterSharedPtr&	rTransformArbiter ) :
51             mxGraphicDevice(),
52             mxSprite( rSprite ),
53             mxAnimatedSprite(),
54 			mpTransformArbiter( rTransformArbiter )
55         {
56             // Avoiding ternary operator in initializer list (Solaris
57             // compiler bug, when function call and temporary is
58             // involved)
59             if( rParentCanvas.is() )
60                 mxGraphicDevice = rParentCanvas->getDevice();
61 
62             OSL_ENSURE( rParentCanvas.is() , "ImplSprite::ImplSprite(): Invalid canvas");
63             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::ImplSprite(): Invalid graphic device");
64             OSL_ENSURE( mxSprite.is(), "ImplSprite::ImplSprite(): Invalid sprite");
65             OSL_ENSURE( mpTransformArbiter.get(), "ImplSprite::ImplSprite(): Invalid transformation arbiter");
66         }
67 
68         ImplSprite::ImplSprite( const uno::Reference< rendering::XSpriteCanvas >& 		rParentCanvas,
69                                 const uno::Reference< rendering::XAnimatedSprite >& 	rSprite,
70                                 const ImplSpriteCanvas::TransformationArbiterSharedPtr&	rTransformArbiter ) :
71             mxGraphicDevice(),
72             mxSprite( uno::Reference< rendering::XSprite >(rSprite,
73                                                            uno::UNO_QUERY) ),
74             mxAnimatedSprite( rSprite ),
75 			mpTransformArbiter( rTransformArbiter )
76         {
77             // Avoiding ternary operator in initializer list (Solaris
78             // compiler bug, when function call and temporary is
79             // involved)
80             if( rParentCanvas.is() )
81                 mxGraphicDevice = rParentCanvas->getDevice();
82 
83             OSL_ENSURE( rParentCanvas.is() , "ImplSprite::ImplSprite(): Invalid canvas");
84             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::ImplSprite(): Invalid graphic device");
85             OSL_ENSURE( mxSprite.is(), "ImplSprite::ImplSprite(): Invalid sprite");
86             OSL_ENSURE( mpTransformArbiter.get(), "ImplSprite::ImplSprite(): Invalid transformation arbiter");
87         }
88 
89         ImplSprite::~ImplSprite()
90         {
91             // hide the sprite on the canvas. If we don't hide the
92             // sprite, it will stay on the canvas forever, since the
93             // canvas naturally keeps a list of visible sprites
94             // (otherwise, it wouldn't be able to paint them
95             // autonomously)
96             if( mxSprite.is() )
97                 mxSprite->hide();
98         }
99 
100         void ImplSprite::setAlpha( const double& rAlpha )
101         {
102             OSL_ENSURE( mxSprite.is(), "ImplSprite::setAlpha(): Invalid sprite");
103 
104             if( mxSprite.is() )
105                 mxSprite->setAlpha( rAlpha );
106         }
107 
108         void ImplSprite::movePixel( const ::basegfx::B2DPoint& rNewPos )
109         {
110             OSL_ENSURE( mxSprite.is(), "ImplSprite::movePixel(): Invalid sprite");
111 
112             if( mxSprite.is() )
113             {
114                 rendering::ViewState	aViewState;
115                 rendering::RenderState	aRenderState;
116 
117                 ::canvas::tools::initViewState( aViewState );
118                 ::canvas::tools::initRenderState( aRenderState );
119 
120                 mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ),
121                                 aViewState,
122                                 aRenderState );
123             }
124         }
125 
126         void ImplSprite::move( const ::basegfx::B2DPoint& rNewPos )
127         {
128             OSL_ENSURE( mxSprite.is(), "ImplSprite::move(): Invalid sprite");
129 
130             if( mxSprite.is() )
131             {
132                 rendering::ViewState	aViewState;
133                 rendering::RenderState	aRenderState;
134 
135                 ::canvas::tools::initViewState( aViewState );
136                 ::canvas::tools::initRenderState( aRenderState );
137 
138                 ::canvas::tools::setViewStateTransform( aViewState,
139                                                         mpTransformArbiter->getTransformation() );
140 
141                 mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ),
142                                 aViewState,
143                                 aRenderState );
144             }
145         }
146 
147         void ImplSprite::transform( const ::basegfx::B2DHomMatrix& rMatrix )
148         {
149             OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
150 
151             if( mxSprite.is() )
152             {
153                 geometry::AffineMatrix2D aMatrix;
154 
155                 mxSprite->transform( ::basegfx::unotools::affineMatrixFromHomMatrix( aMatrix,
156                                                                                      rMatrix ) );
157             }
158         }
159 
160         void ImplSprite::setClipPixel( const ::basegfx::B2DPolyPolygon& rClipPoly )
161         {
162             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
163             OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
164 
165             if( mxSprite.is() && mxGraphicDevice.is() )
166                 mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice,
167                                                                                          rClipPoly ) );
168         }
169 
170         void ImplSprite::setClip( const ::basegfx::B2DPolyPolygon& rClipPoly )
171         {
172             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
173             OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
174 
175             if( mxSprite.is() && mxGraphicDevice.is() )
176             {
177                 ::basegfx::B2DPolyPolygon 	aTransformedClipPoly( rClipPoly );
178 
179                 // extract linear part of canvas view transformation (linear means:
180                 // without translational components)
181                 ::basegfx::B2DHomMatrix		aViewTransform( mpTransformArbiter->getTransformation() );
182                 aViewTransform.set( 0, 2, 0.0 );
183                 aViewTransform.set( 1, 2, 0.0 );
184 
185                 // transform polygon from view to device coordinate space
186                 aTransformedClipPoly.transform( aViewTransform );
187 
188                 mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice,
189                                                                                      aTransformedClipPoly ) );
190             }
191         }
192 
193         void ImplSprite::setClip()
194         {
195             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
196             OSL_ENSURE( mxSprite.is(), "ImplSprite::setClip(): Invalid sprite");
197 
198             if( mxSprite.is() && mxGraphicDevice.is() )
199                 mxSprite->clip( uno::Reference< rendering::XPolyPolygon2D >() );
200         }
201 
202         void ImplSprite::show()
203         {
204             OSL_ENSURE( mxSprite.is(), "ImplSprite::show(): Invalid sprite");
205 
206             if( mxSprite.is() )
207                 mxSprite->show();
208         }
209 
210         void ImplSprite::hide()
211         {
212             OSL_ENSURE( mxSprite.is(), "ImplSprite::hide(): Invalid sprite");
213 
214             if( mxSprite.is() )
215                 mxSprite->hide();
216         }
217 
218         void ImplSprite::setPriority( double fPriority )
219         {
220             OSL_ENSURE( mxSprite.is(), "ImplSprite::setPriority(): Invalid sprite");
221 
222             if( mxSprite.is() )
223                 mxSprite->setPriority(fPriority);
224         }
225 
226         uno::Reference< rendering::XSprite > ImplSprite::getUNOSprite() const
227         {
228             return mxSprite;
229         }
230 
231         uno::Reference< rendering::XGraphicDevice > ImplSprite::getGraphicDevice() const
232         {
233             return mxGraphicDevice;
234         }
235     }
236 }
237