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