| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <basegfx/utils/canvastools.hxx> |
| 22 |
#include <basegfx/polygon/b2dpolypolygon.hxx> |
| 23 |
#include <canvas/canvastools.hxx> |
| 24 |
#include <osl/diagnose.h> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "implsprite.hxx" |
| 28 |
|
| 29 |
|
| 30 |
using namespace ::com::sun::star; |
| 31 |
|
| 32 |
namespace cppcanvas::internal |
| 33 |
{ |
| 34 |
|
| 35 |
ImplSprite::ImplSprite( const uno::Reference< rendering::XSpriteCanvas >& rParentCanvas, |
| 36 |
uno::Reference< rendering::XSprite > rSprite, |
| 37 |
ImplSpriteCanvas::TransformationArbiterSharedPtr xTransformArbiter ) : |
| 38 |
mxSprite(std::move( rSprite )), |
| 39 |
mpTransformArbiter(std::move( xTransformArbiter )) |
| 40 |
{ |
| 41 |
// Avoiding ternary operator in initializer list (Solaris |
| 42 |
// compiler bug, when function call and temporary is |
| 43 |
// involved) |
| 44 |
if( rParentCanvas.is() ) |
| 45 |
mxGraphicDevice = rParentCanvas->getDevice(); |
| 46 |
|
| 47 |
OSL_ENSURE( rParentCanvas.is() , "ImplSprite::ImplSprite(): Invalid canvas"); |
| 48 |
OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::ImplSprite(): Invalid graphic device"); |
| 49 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::ImplSprite(): Invalid sprite"); |
| 50 |
OSL_ENSURE( mpTransformArbiter, "ImplSprite::ImplSprite(): Invalid transformation arbiter"); |
| 51 |
} |
| 52 |
|
| 53 |
ImplSprite::~ImplSprite() |
| 54 |
{ |
| 55 |
// hide the sprite on the canvas. If we don't hide the |
| 56 |
// sprite, it will stay on the canvas forever, since the |
| 57 |
// canvas naturally keeps a list of visible sprites |
| 58 |
// (otherwise, it wouldn't be able to paint them |
| 59 |
// autonomously) |
| 60 |
if( mxSprite.is() ) |
| 61 |
mxSprite->hide(); |
| 62 |
} |
| 63 |
|
| 64 |
void ImplSprite::setAlpha( const double& rAlpha ) |
| 65 |
{ |
| 66 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::setAlpha(): Invalid sprite"); |
| 67 |
|
| 68 |
if( mxSprite.is() ) |
| 69 |
mxSprite->setAlpha( rAlpha ); |
| 70 |
} |
| 71 |
|
| 72 |
void ImplSprite::movePixel( const ::basegfx::B2DPoint& rNewPos ) |
| 73 |
{ |
| 74 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::movePixel(): Invalid sprite"); |
| 75 |
|
| 76 |
if( mxSprite.is() ) |
| 77 |
{ |
| 78 |
rendering::ViewState aViewState; |
| 79 |
rendering::RenderState aRenderState; |
| 80 |
|
| 81 |
::canvastools::initViewState( aViewState ); |
| 82 |
::canvastools::initRenderState( aRenderState ); |
| 83 |
|
| 84 |
mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ), |
| 85 |
aViewState, |
| 86 |
aRenderState ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void ImplSprite::move( const ::basegfx::B2DPoint& rNewPos ) |
| 91 |
{ |
| 92 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::move(): Invalid sprite"); |
| 93 |
|
| 94 |
if( !mxSprite.is() ) |
| 95 |
return; |
| 96 |
|
| 97 |
rendering::ViewState aViewState; |
| 98 |
rendering::RenderState aRenderState; |
| 99 |
|
| 100 |
::canvastools::initViewState( aViewState ); |
| 101 |
::canvastools::initRenderState( aRenderState ); |
| 102 |
|
| 103 |
::canvastools::setViewStateTransform( aViewState, |
| 104 |
mpTransformArbiter->getTransformation() ); |
| 105 |
|
| 106 |
mxSprite->move( ::basegfx::unotools::point2DFromB2DPoint( rNewPos ), |
| 107 |
aViewState, |
| 108 |
aRenderState ); |
| 109 |
} |
| 110 |
|
| 111 |
void ImplSprite::transform( const ::basegfx::B2DHomMatrix& rMatrix ) |
| 112 |
{ |
| 113 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite"); |
| 114 |
|
| 115 |
if( mxSprite.is() ) |
| 116 |
{ |
| 117 |
geometry::AffineMatrix2D aMatrix; |
| 118 |
|
| 119 |
mxSprite->transform( ::basegfx::unotools::affineMatrixFromHomMatrix( aMatrix, |
| 120 |
rMatrix ) ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void ImplSprite::setClipPixel( const ::basegfx::B2DPolyPolygon& rClipPoly ) |
| 125 |
{ |
| 126 |
OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas"); |
| 127 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite"); |
| 128 |
|
| 129 |
if( mxSprite.is() && mxGraphicDevice.is() ) |
| 130 |
mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice, |
| 131 |
rClipPoly ) ); |
| 132 |
} |
| 133 |
|
| 134 |
void ImplSprite::setClip( const ::basegfx::B2DPolyPolygon& rClipPoly ) |
| 135 |
{ |
| 136 |
OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas"); |
| 137 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::transform(): Invalid sprite"); |
| 138 |
|
| 139 |
if( !(mxSprite.is() && mxGraphicDevice.is()) ) |
| 140 |
return; |
| 141 |
|
| 142 |
::basegfx::B2DPolyPolygon aTransformedClipPoly( rClipPoly ); |
| 143 |
|
| 144 |
// extract linear part of canvas view transformation (linear means: |
| 145 |
// without translational components) |
| 146 |
::basegfx::B2DHomMatrix aViewTransform( mpTransformArbiter->getTransformation() ); |
| 147 |
aViewTransform.set( 0, 2, 0.0 ); |
| 148 |
aViewTransform.set( 1, 2, 0.0 ); |
| 149 |
|
| 150 |
// transform polygon from view to device coordinate space |
| 151 |
aTransformedClipPoly.transform( aViewTransform ); |
| 152 |
|
| 153 |
mxSprite->clip( ::basegfx::unotools::xPolyPolygonFromB2DPolyPolygon( mxGraphicDevice, |
| 154 |
aTransformedClipPoly ) ); |
| 155 |
} |
| 156 |
|
| 157 |
void ImplSprite::setClip() |
| 158 |
{ |
| 159 |
OSL_ENSURE( mxGraphicDevice.is(), "ImplSprite::setClip(): Invalid canvas"); |
| 160 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::setClip(): Invalid sprite"); |
| 161 |
|
| 162 |
if( mxSprite.is() && mxGraphicDevice.is() ) |
| 163 |
mxSprite->clip( uno::Reference< rendering::XPolyPolygon2D >() ); |
| 164 |
} |
| 165 |
|
| 166 |
void ImplSprite::show() |
| 167 |
{ |
| 168 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::show(): Invalid sprite"); |
| 169 |
|
| 170 |
if( mxSprite.is() ) |
| 171 |
mxSprite->show(); |
| 172 |
} |
| 173 |
|
| 174 |
void ImplSprite::hide() |
| 175 |
{ |
| 176 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::hide(): Invalid sprite"); |
| 177 |
|
| 178 |
if( mxSprite.is() ) |
| 179 |
mxSprite->hide(); |
| 180 |
} |
| 181 |
|
| 182 |
void ImplSprite::setPriority( double fPriority ) |
| 183 |
{ |
| 184 |
OSL_ENSURE( mxSprite.is(), "ImplSprite::setPriority(): Invalid sprite"); |
| 185 |
|
| 186 |
if( mxSprite.is() ) |
| 187 |
mxSprite->setPriority(fPriority); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |