1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppcanvas.hxx"
26 
27 #include <com/sun/star/rendering/XSprite.hpp>
28 #include <com/sun/star/rendering/XAnimatedSprite.hpp>
29 
30 #include <basegfx/tools/canvastools.hxx>
31 #include <basegfx/polygon/b2dpolypolygon.hxx>
32 #include <canvas/canvastools.hxx>
33 
34 #include "implsprite.hxx"
35 
36 
37 using namespace ::com::sun::star;
38 
39 namespace cppcanvas
40 {
41     namespace internal
42     {
43 
ImplSprite(const uno::Reference<rendering::XSpriteCanvas> & rParentCanvas,const uno::Reference<rendering::XSprite> & rSprite,const ImplSpriteCanvas::TransformationArbiterSharedPtr & rTransformArbiter)44         ImplSprite::ImplSprite( const uno::Reference< rendering::XSpriteCanvas >& 		rParentCanvas,
45                                 const uno::Reference< rendering::XSprite >& 			rSprite,
46                                 const ImplSpriteCanvas::TransformationArbiterSharedPtr&	rTransformArbiter ) :
47             mxGraphicDevice(),
48             mxSprite( rSprite ),
49             mxAnimatedSprite(),
50 			mpTransformArbiter( rTransformArbiter )
51         {
52             // Avoiding ternary operator in initializer list (Solaris
53             // compiler bug, when function call and temporary is
54             // involved)
55             if( rParentCanvas.is() )
56                 mxGraphicDevice = rParentCanvas->getDevice();
57 
58             OSL_ENSURE( rParentCanvas.is() , "ImplSprite::ImplSprite(): Invalid canvas");
59             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::ImplSprite(): Invalid graphic device");
60             OSL_ENSURE( mxSprite.is(), "ImplSprite::ImplSprite(): Invalid sprite");
61             OSL_ENSURE( mpTransformArbiter.get(), "ImplSprite::ImplSprite(): Invalid transformation arbiter");
62         }
63 
ImplSprite(const uno::Reference<rendering::XSpriteCanvas> & rParentCanvas,const uno::Reference<rendering::XAnimatedSprite> & rSprite,const ImplSpriteCanvas::TransformationArbiterSharedPtr & rTransformArbiter)64         ImplSprite::ImplSprite( const uno::Reference< rendering::XSpriteCanvas >& 		rParentCanvas,
65                                 const uno::Reference< rendering::XAnimatedSprite >& 	rSprite,
66                                 const ImplSpriteCanvas::TransformationArbiterSharedPtr&	rTransformArbiter ) :
67             mxGraphicDevice(),
68             mxSprite( uno::Reference< rendering::XSprite >(rSprite,
69                                                            uno::UNO_QUERY) ),
70             mxAnimatedSprite( rSprite ),
71 			mpTransformArbiter( rTransformArbiter )
72         {
73             // Avoiding ternary operator in initializer list (Solaris
74             // compiler bug, when function call and temporary is
75             // involved)
76             if( rParentCanvas.is() )
77                 mxGraphicDevice = rParentCanvas->getDevice();
78 
79             OSL_ENSURE( rParentCanvas.is() , "ImplSprite::ImplSprite(): Invalid canvas");
80             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::ImplSprite(): Invalid graphic device");
81             OSL_ENSURE( mxSprite.is(), "ImplSprite::ImplSprite(): Invalid sprite");
82             OSL_ENSURE( mpTransformArbiter.get(), "ImplSprite::ImplSprite(): Invalid transformation arbiter");
83         }
84 
~ImplSprite()85         ImplSprite::~ImplSprite()
86         {
87             // hide the sprite on the canvas. If we don't hide the
88             // sprite, it will stay on the canvas forever, since the
89             // canvas naturally keeps a list of visible sprites
90             // (otherwise, it wouldn't be able to paint them
91             // autonomously)
92             if( mxSprite.is() )
93                 mxSprite->hide();
94         }
95 
setAlpha(const double & rAlpha)96         void ImplSprite::setAlpha( const double& rAlpha )
97         {
98             OSL_ENSURE( mxSprite.is(), "ImplSprite::setAlpha(): Invalid sprite");
99 
100             if( mxSprite.is() )
101                 mxSprite->setAlpha( rAlpha );
102         }
103 
movePixel(const::basegfx::B2DPoint & rNewPos)104         void ImplSprite::movePixel( const ::basegfx::B2DPoint& rNewPos )
105         {
106             OSL_ENSURE( mxSprite.is(), "ImplSprite::movePixel(): Invalid sprite");
107 
108             if( mxSprite.is() )
109             {
110                 rendering::ViewState	aViewState;
111                 rendering::RenderState	aRenderState;
112 
113                 ::canvas::tools::initViewState( aViewState );
114                 ::canvas::tools::initRenderState( aRenderState );
115 
116                 mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ),
117                                 aViewState,
118                                 aRenderState );
119             }
120         }
121 
move(const::basegfx::B2DPoint & rNewPos)122         void ImplSprite::move( const ::basegfx::B2DPoint& rNewPos )
123         {
124             OSL_ENSURE( mxSprite.is(), "ImplSprite::move(): Invalid sprite");
125 
126             if( mxSprite.is() )
127             {
128                 rendering::ViewState	aViewState;
129                 rendering::RenderState	aRenderState;
130 
131                 ::canvas::tools::initViewState( aViewState );
132                 ::canvas::tools::initRenderState( aRenderState );
133 
134                 ::canvas::tools::setViewStateTransform( aViewState,
135                                                         mpTransformArbiter->getTransformation() );
136 
137                 mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ),
138                                 aViewState,
139                                 aRenderState );
140             }
141         }
142 
transform(const::basegfx::B2DHomMatrix & rMatrix)143         void ImplSprite::transform( const ::basegfx::B2DHomMatrix& rMatrix )
144         {
145             OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
146 
147             if( mxSprite.is() )
148             {
149                 geometry::AffineMatrix2D aMatrix;
150 
151                 mxSprite->transform( ::basegfx::unotools::affineMatrixFromHomMatrix( aMatrix,
152                                                                                      rMatrix ) );
153             }
154         }
155 
setClipPixel(const::basegfx::B2DPolyPolygon & rClipPoly)156         void ImplSprite::setClipPixel( const ::basegfx::B2DPolyPolygon& rClipPoly )
157         {
158             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
159             OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
160 
161             if( mxSprite.is() && mxGraphicDevice.is() )
162                 mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice,
163                                                                                          rClipPoly ) );
164         }
165 
setClip(const::basegfx::B2DPolyPolygon & rClipPoly)166         void ImplSprite::setClip( const ::basegfx::B2DPolyPolygon& rClipPoly )
167         {
168             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
169             OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
170 
171             if( mxSprite.is() && mxGraphicDevice.is() )
172             {
173                 ::basegfx::B2DPolyPolygon 	aTransformedClipPoly( rClipPoly );
174 
175                 // extract linear part of canvas view transformation (linear means:
176                 // without translational components)
177                 ::basegfx::B2DHomMatrix		aViewTransform( mpTransformArbiter->getTransformation() );
178                 aViewTransform.set( 0, 2, 0.0 );
179                 aViewTransform.set( 1, 2, 0.0 );
180 
181                 // transform polygon from view to device coordinate space
182                 aTransformedClipPoly.transform( aViewTransform );
183 
184                 mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice,
185                                                                                      aTransformedClipPoly ) );
186             }
187         }
188 
setClip()189         void ImplSprite::setClip()
190         {
191             OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
192             OSL_ENSURE( mxSprite.is(), "ImplSprite::setClip(): Invalid sprite");
193 
194             if( mxSprite.is() && mxGraphicDevice.is() )
195                 mxSprite->clip( uno::Reference< rendering::XPolyPolygon2D >() );
196         }
197 
show()198         void ImplSprite::show()
199         {
200             OSL_ENSURE( mxSprite.is(), "ImplSprite::show(): Invalid sprite");
201 
202             if( mxSprite.is() )
203                 mxSprite->show();
204         }
205 
hide()206         void ImplSprite::hide()
207         {
208             OSL_ENSURE( mxSprite.is(), "ImplSprite::hide(): Invalid sprite");
209 
210             if( mxSprite.is() )
211                 mxSprite->hide();
212         }
213 
setPriority(double fPriority)214         void ImplSprite::setPriority( double fPriority )
215         {
216             OSL_ENSURE( mxSprite.is(), "ImplSprite::setPriority(): Invalid sprite");
217 
218             if( mxSprite.is() )
219                 mxSprite->setPriority(fPriority);
220         }
221 
getUNOSprite() const222         uno::Reference< rendering::XSprite > ImplSprite::getUNOSprite() const
223         {
224             return mxSprite;
225         }
226 
getGraphicDevice() const227         uno::Reference< rendering::XGraphicDevice > ImplSprite::getGraphicDevice() const
228         {
229             return mxGraphicDevice;
230         }
231     }
232 }
233