Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / cppcanvas / source / wrapper / implsprite.cxx
blob5f58ba210e30d9d537baa584468cfc050cb0c347
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/rendering/XSprite.hpp>
22 #include <com/sun/star/rendering/XAnimatedSprite.hpp>
24 #include <basegfx/tools/canvastools.hxx>
25 #include <basegfx/polygon/b2dpolypolygon.hxx>
26 #include <canvas/canvastools.hxx>
28 #include "implsprite.hxx"
31 using namespace ::com::sun::star;
33 namespace cppcanvas
35 namespace internal
38 ImplSprite::ImplSprite( const uno::Reference< rendering::XSpriteCanvas >& rParentCanvas,
39 const uno::Reference< rendering::XSprite >& rSprite,
40 const ImplSpriteCanvas::TransformationArbiterSharedPtr& rTransformArbiter ) :
41 mxGraphicDevice(),
42 mxSprite( rSprite ),
43 mxAnimatedSprite(),
44 mpTransformArbiter( rTransformArbiter )
46 // Avoiding ternary operator in initializer list (Solaris
47 // compiler bug, when function call and temporary is
48 // involved)
49 if( rParentCanvas.is() )
50 mxGraphicDevice = rParentCanvas->getDevice();
52 OSL_ENSURE( rParentCanvas.is() , "ImplSprite::ImplSprite(): Invalid canvas");
53 OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::ImplSprite(): Invalid graphic device");
54 OSL_ENSURE( mxSprite.is(), "ImplSprite::ImplSprite(): Invalid sprite");
55 OSL_ENSURE( mpTransformArbiter.get(), "ImplSprite::ImplSprite(): Invalid transformation arbiter");
58 ImplSprite::~ImplSprite()
60 // hide the sprite on the canvas. If we don't hide the
61 // sprite, it will stay on the canvas forever, since the
62 // canvas naturally keeps a list of visible sprites
63 // (otherwise, it wouldn't be able to paint them
64 // autonomously)
65 if( mxSprite.is() )
66 mxSprite->hide();
69 void ImplSprite::setAlpha( const double& rAlpha )
71 OSL_ENSURE( mxSprite.is(), "ImplSprite::setAlpha(): Invalid sprite");
73 if( mxSprite.is() )
74 mxSprite->setAlpha( rAlpha );
77 void ImplSprite::movePixel( const ::basegfx::B2DPoint& rNewPos )
79 OSL_ENSURE( mxSprite.is(), "ImplSprite::movePixel(): Invalid sprite");
81 if( mxSprite.is() )
83 rendering::ViewState aViewState;
84 rendering::RenderState aRenderState;
86 ::canvas::tools::initViewState( aViewState );
87 ::canvas::tools::initRenderState( aRenderState );
89 mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ),
90 aViewState,
91 aRenderState );
95 void ImplSprite::move( const ::basegfx::B2DPoint& rNewPos )
97 OSL_ENSURE( mxSprite.is(), "ImplSprite::move(): Invalid sprite");
99 if( mxSprite.is() )
101 rendering::ViewState aViewState;
102 rendering::RenderState aRenderState;
104 ::canvas::tools::initViewState( aViewState );
105 ::canvas::tools::initRenderState( aRenderState );
107 ::canvas::tools::setViewStateTransform( aViewState,
108 mpTransformArbiter->getTransformation() );
110 mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ),
111 aViewState,
112 aRenderState );
116 void ImplSprite::transform( const ::basegfx::B2DHomMatrix& rMatrix )
118 OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
120 if( mxSprite.is() )
122 geometry::AffineMatrix2D aMatrix;
124 mxSprite->transform( ::basegfx::unotools::affineMatrixFromHomMatrix( aMatrix,
125 rMatrix ) );
129 void ImplSprite::setClipPixel( const ::basegfx::B2DPolyPolygon& rClipPoly )
131 OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
132 OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
134 if( mxSprite.is() && mxGraphicDevice.is() )
135 mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice,
136 rClipPoly ) );
139 void ImplSprite::setClip( const ::basegfx::B2DPolyPolygon& rClipPoly )
141 OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
142 OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite");
144 if( mxSprite.is() && mxGraphicDevice.is() )
146 ::basegfx::B2DPolyPolygon aTransformedClipPoly( rClipPoly );
148 // extract linear part of canvas view transformation (linear means:
149 // without translational components)
150 ::basegfx::B2DHomMatrix aViewTransform( mpTransformArbiter->getTransformation() );
151 aViewTransform.set( 0, 2, 0.0 );
152 aViewTransform.set( 1, 2, 0.0 );
154 // transform polygon from view to device coordinate space
155 aTransformedClipPoly.transform( aViewTransform );
157 mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice,
158 aTransformedClipPoly ) );
162 void ImplSprite::setClip()
164 OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas");
165 OSL_ENSURE( mxSprite.is(), "ImplSprite::setClip(): Invalid sprite");
167 if( mxSprite.is() && mxGraphicDevice.is() )
168 mxSprite->clip( uno::Reference< rendering::XPolyPolygon2D >() );
171 void ImplSprite::show()
173 OSL_ENSURE( mxSprite.is(), "ImplSprite::show(): Invalid sprite");
175 if( mxSprite.is() )
176 mxSprite->show();
179 void ImplSprite::hide()
181 OSL_ENSURE( mxSprite.is(), "ImplSprite::hide(): Invalid sprite");
183 if( mxSprite.is() )
184 mxSprite->hide();
187 void ImplSprite::setPriority( double fPriority )
189 OSL_ENSURE( mxSprite.is(), "ImplSprite::setPriority(): Invalid sprite");
191 if( mxSprite.is() )
192 mxSprite->setPriority(fPriority);
195 uno::Reference< rendering::XSprite > ImplSprite::getUNOSprite() const
197 return mxSprite;
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */