Line data Source code
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 <sax/tools/converter.hxx>
22 :
23 : #include "ximp3dscene.hxx"
24 : #include <xmloff/xmluconv.hxx>
25 : #include "xexptran.hxx"
26 : #include <xmloff/xmltoken.hxx>
27 : #include "xmloff/xmlnmspe.hxx"
28 : #include <com/sun/star/drawing/Direction3D.hpp>
29 : #include <com/sun/star/drawing/CameraGeometry.hpp>
30 : #include "eventimp.hxx"
31 : #include "descriptionimp.hxx"
32 :
33 :
34 : using namespace ::com::sun::star;
35 : using namespace ::xmloff::token;
36 :
37 : //////////////////////////////////////////////////////////////////////////////
38 : // dr3d:3dlight context
39 :
40 0 : SdXML3DLightContext::SdXML3DLightContext(
41 : SvXMLImport& rImport,
42 : sal_uInt16 nPrfx,
43 : const OUString& rLName,
44 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttrList)
45 : : SvXMLImportContext( rImport, nPrfx, rLName),
46 : maDiffuseColor(0x00000000),
47 : maDirection(0.0, 0.0, 1.0),
48 : mbEnabled(false),
49 0 : mbSpecular(false)
50 : {
51 : // read attributes for the 3DScene
52 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
53 0 : for(sal_Int16 i=0; i < nAttrCount; i++)
54 : {
55 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
56 0 : OUString aLocalName;
57 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
58 0 : OUString sValue = xAttrList->getValueByIndex( i );
59 0 : const SvXMLTokenMap& rAttrTokenMap = GetImport().GetShapeImport()->Get3DLightAttrTokenMap();
60 :
61 0 : switch(rAttrTokenMap.Get(nPrefix, aLocalName))
62 : {
63 : case XML_TOK_3DLIGHT_DIFFUSE_COLOR:
64 : {
65 0 : ::sax::Converter::convertColor(maDiffuseColor, sValue);
66 0 : break;
67 : }
68 : case XML_TOK_3DLIGHT_DIRECTION:
69 : {
70 0 : GetImport().GetMM100UnitConverter().convertB3DVector(maDirection, sValue);
71 0 : break;
72 : }
73 : case XML_TOK_3DLIGHT_ENABLED:
74 : {
75 0 : ::sax::Converter::convertBool(mbEnabled, sValue);
76 0 : break;
77 : }
78 : case XML_TOK_3DLIGHT_SPECULAR:
79 : {
80 0 : ::sax::Converter::convertBool(mbSpecular, sValue);
81 0 : break;
82 : }
83 : }
84 0 : }
85 0 : }
86 :
87 0 : SdXML3DLightContext::~SdXML3DLightContext()
88 : {
89 0 : }
90 :
91 : //////////////////////////////////////////////////////////////////////////////
92 :
93 0 : TYPEINIT1( SdXML3DSceneShapeContext, SdXMLShapeContext );
94 :
95 0 : SdXML3DSceneShapeContext::SdXML3DSceneShapeContext(
96 : SvXMLImport& rImport,
97 : sal_uInt16 nPrfx,
98 : const OUString& rLocalName,
99 : const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList>& xAttrList,
100 : uno::Reference< drawing::XShapes >& rShapes,
101 : sal_Bool bTemporaryShapes)
102 0 : : SdXMLShapeContext( rImport, nPrfx, rLocalName, xAttrList, rShapes, bTemporaryShapes ), SdXML3DSceneAttributesHelper( rImport )
103 : {
104 0 : }
105 :
106 : //////////////////////////////////////////////////////////////////////////////
107 :
108 0 : SdXML3DSceneShapeContext::~SdXML3DSceneShapeContext()
109 : {
110 0 : }
111 :
112 : //////////////////////////////////////////////////////////////////////////////
113 :
114 0 : void SdXML3DSceneShapeContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList)
115 : {
116 : // create new 3DScene shape and add it to rShapes, use it
117 : // as base for the new 3DScene import
118 0 : AddShape( "com.sun.star.drawing.Shape3DSceneObject" );
119 0 : if( mxShape.is() )
120 : {
121 0 : SetStyle();
122 :
123 0 : mxChildren = uno::Reference< drawing::XShapes >::query( mxShape );
124 0 : if( mxChildren.is() )
125 0 : GetImport().GetShapeImport()->pushGroupForSorting( mxChildren );
126 :
127 0 : SetLayer();
128 :
129 : // set pos, size, shear and rotate
130 0 : SetTransformation();
131 : }
132 :
133 : // read attributes for the 3DScene
134 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
135 0 : for(sal_Int16 i=0; i < nAttrCount; i++)
136 : {
137 0 : OUString sAttrName = xAttrList->getNameByIndex( i );
138 0 : OUString aLocalName;
139 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
140 0 : OUString sValue = xAttrList->getValueByIndex( i );
141 0 : processSceneAttribute( nPrefix, aLocalName, sValue );
142 0 : }
143 :
144 : // #91047# call parent function is missing here, added it
145 0 : if(mxShape.is())
146 : {
147 : // call parent
148 0 : SdXMLShapeContext::StartElement(xAttrList);
149 : }
150 0 : }
151 :
152 : //////////////////////////////////////////////////////////////////////////////
153 :
154 0 : void SdXML3DSceneShapeContext::EndElement()
155 : {
156 0 : if(mxShape.is())
157 : {
158 0 : uno::Reference< beans::XPropertySet > xPropSet(mxShape, uno::UNO_QUERY);
159 0 : if(xPropSet.is())
160 : {
161 0 : setSceneAttributes( xPropSet );
162 : }
163 :
164 0 : if( mxChildren.is() )
165 0 : GetImport().GetShapeImport()->popGroupAndSort();
166 :
167 : // call parent
168 0 : SdXMLShapeContext::EndElement();
169 : }
170 0 : }
171 :
172 : //////////////////////////////////////////////////////////////////////////////
173 :
174 0 : SvXMLImportContext* SdXML3DSceneShapeContext::CreateChildContext( sal_uInt16 nPrefix,
175 : const OUString& rLocalName,
176 : const uno::Reference< xml::sax::XAttributeList>& xAttrList )
177 : {
178 0 : SvXMLImportContext* pContext = 0L;
179 :
180 : // #i68101#
181 0 : if( nPrefix == XML_NAMESPACE_SVG &&
182 0 : (IsXMLToken( rLocalName, XML_TITLE ) || IsXMLToken( rLocalName, XML_DESC ) ) )
183 : {
184 0 : pContext = new SdXMLDescriptionContext( GetImport(), nPrefix, rLocalName, xAttrList, mxShape );
185 : }
186 0 : else if( nPrefix == XML_NAMESPACE_OFFICE && IsXMLToken( rLocalName, XML_EVENT_LISTENERS ) )
187 : {
188 0 : pContext = new SdXMLEventsContext( GetImport(), nPrefix, rLocalName, xAttrList, mxShape );
189 : }
190 : // look for local light context first
191 0 : else if(nPrefix == XML_NAMESPACE_DR3D && IsXMLToken( rLocalName, XML_LIGHT ) )
192 : {
193 : // dr3d:light inside dr3d:scene context
194 0 : pContext = create3DLightContext( nPrefix, rLocalName, xAttrList );
195 : }
196 :
197 : // call GroupChildContext function at common ShapeImport
198 0 : if(!pContext)
199 : {
200 0 : pContext = GetImport().GetShapeImport()->Create3DSceneChildContext(
201 0 : GetImport(), nPrefix, rLocalName, xAttrList, mxChildren);
202 : }
203 :
204 : // call parent when no own context was created
205 0 : if(!pContext)
206 : {
207 : pContext = SvXMLImportContext::CreateChildContext(
208 0 : nPrefix, rLocalName, xAttrList);
209 : }
210 :
211 0 : return pContext;
212 : }
213 :
214 : //////////////////////////////////////////////////////////////////////////////
215 :
216 22 : SdXML3DSceneAttributesHelper::SdXML3DSceneAttributesHelper( SvXMLImport& rImporter )
217 : : mrImport( rImporter ),
218 : mbSetTransform( sal_False ),
219 : mxPrjMode(drawing::ProjectionMode_PERSPECTIVE),
220 : mnDistance(1000),
221 : mnFocalLength(1000),
222 : mnShadowSlant(0),
223 : mxShadeMode(drawing::ShadeMode_SMOOTH),
224 : maAmbientColor(0x00666666),
225 : mbLightingMode(false),
226 : maVRP(0.0, 0.0, 1.0),
227 : maVPN(0.0, 0.0, 1.0),
228 : maVUP(0.0, 1.0, 0.0),
229 : mbVRPUsed(sal_False),
230 : mbVPNUsed(sal_False),
231 22 : mbVUPUsed(sal_False)
232 : {
233 22 : }
234 :
235 44 : SdXML3DSceneAttributesHelper::~SdXML3DSceneAttributesHelper()
236 : {
237 : // release remembered light contexts, they are no longer needed
238 44 : for ( size_t i = maList.size(); i > 0; )
239 0 : maList[ --i ]->ReleaseRef();
240 22 : maList.clear();
241 22 : }
242 :
243 : /** creates a 3d ligth context and adds it to the internal list for later processing */
244 0 : SvXMLImportContext * SdXML3DSceneAttributesHelper::create3DLightContext( sal_uInt16 nPrfx, const OUString& rLName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttrList)
245 : {
246 0 : SvXMLImportContext* pContext = new SdXML3DLightContext(mrImport, nPrfx, rLName, xAttrList);
247 :
248 : // remember SdXML3DLightContext for later evaluation
249 0 : if(pContext)
250 : {
251 0 : pContext->AddRef();
252 0 : maList.push_back( (SdXML3DLightContext*)pContext );
253 : }
254 :
255 0 : return pContext;
256 : }
257 :
258 : /** this should be called for each scene attribute */
259 0 : void SdXML3DSceneAttributesHelper::processSceneAttribute( sal_uInt16 nPrefix, const OUString& rLocalName, const OUString& rValue )
260 : {
261 0 : if( XML_NAMESPACE_DR3D == nPrefix )
262 : {
263 0 : if( IsXMLToken( rLocalName, XML_TRANSFORM ) )
264 : {
265 0 : SdXMLImExTransform3D aTransform(rValue, mrImport.GetMM100UnitConverter());
266 0 : if(aTransform.NeedsAction())
267 0 : mbSetTransform = aTransform.GetFullHomogenTransform(mxHomMat);
268 0 : return;
269 : }
270 0 : else if( IsXMLToken( rLocalName, XML_VRP ) )
271 : {
272 0 : ::basegfx::B3DVector aNewVec;
273 0 : mrImport.GetMM100UnitConverter().convertB3DVector(aNewVec, rValue);
274 :
275 0 : if(aNewVec != maVRP)
276 : {
277 0 : maVRP = aNewVec;
278 0 : mbVRPUsed = sal_True;
279 : }
280 0 : return;
281 : }
282 0 : else if( IsXMLToken( rLocalName, XML_VPN ) )
283 : {
284 0 : ::basegfx::B3DVector aNewVec;
285 0 : mrImport.GetMM100UnitConverter().convertB3DVector(aNewVec, rValue);
286 :
287 0 : if(aNewVec != maVPN)
288 : {
289 0 : maVPN = aNewVec;
290 0 : mbVPNUsed = sal_True;
291 : }
292 0 : return;
293 : }
294 0 : else if( IsXMLToken( rLocalName, XML_VUP ) )
295 : {
296 0 : ::basegfx::B3DVector aNewVec;
297 0 : mrImport.GetMM100UnitConverter().convertB3DVector(aNewVec, rValue);
298 :
299 0 : if(aNewVec != maVUP)
300 : {
301 0 : maVUP = aNewVec;
302 0 : mbVUPUsed = sal_True;
303 : }
304 0 : return;
305 : }
306 0 : else if( IsXMLToken( rLocalName, XML_PROJECTION ) )
307 : {
308 0 : if( IsXMLToken( rValue, XML_PARALLEL ) )
309 0 : mxPrjMode = drawing::ProjectionMode_PARALLEL;
310 : else
311 0 : mxPrjMode = drawing::ProjectionMode_PERSPECTIVE;
312 0 : return;
313 : }
314 0 : else if( IsXMLToken( rLocalName, XML_DISTANCE ) )
315 : {
316 0 : mrImport.GetMM100UnitConverter().convertMeasureToCore(mnDistance,
317 0 : rValue);
318 0 : return;
319 : }
320 0 : else if( IsXMLToken( rLocalName, XML_FOCAL_LENGTH ) )
321 : {
322 0 : mrImport.GetMM100UnitConverter().convertMeasureToCore(mnFocalLength,
323 0 : rValue);
324 0 : return;
325 : }
326 0 : else if( IsXMLToken( rLocalName, XML_SHADOW_SLANT ) )
327 : {
328 0 : ::sax::Converter::convertNumber(mnShadowSlant, rValue);
329 0 : return;
330 : }
331 0 : else if( IsXMLToken( rLocalName, XML_SHADE_MODE ) )
332 : {
333 0 : if( IsXMLToken( rValue, XML_FLAT ) )
334 0 : mxShadeMode = drawing::ShadeMode_FLAT;
335 0 : else if( IsXMLToken( rValue, XML_PHONG ) )
336 0 : mxShadeMode = drawing::ShadeMode_PHONG;
337 0 : else if( IsXMLToken( rValue, XML_GOURAUD ) )
338 0 : mxShadeMode = drawing::ShadeMode_SMOOTH;
339 : else
340 0 : mxShadeMode = drawing::ShadeMode_DRAFT;
341 0 : return;
342 : }
343 0 : else if( IsXMLToken( rLocalName, XML_AMBIENT_COLOR ) )
344 : {
345 0 : ::sax::Converter::convertColor(maAmbientColor, rValue);
346 0 : return;
347 : }
348 0 : else if( IsXMLToken( rLocalName, XML_LIGHTING_MODE ) )
349 : {
350 0 : ::sax::Converter::convertBool(mbLightingMode, rValue);
351 0 : return;
352 : }
353 : }
354 : }
355 :
356 : /** this sets the scene attributes at this propertyset */
357 0 : void SdXML3DSceneAttributesHelper::setSceneAttributes( const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet >& xPropSet )
358 : {
359 0 : uno::Any aAny;
360 :
361 : // world transformation
362 0 : if(mbSetTransform)
363 : {
364 0 : aAny <<= mxHomMat;
365 0 : xPropSet->setPropertyValue("D3DTransformMatrix", aAny);
366 : }
367 :
368 : // distance
369 0 : aAny <<= mnDistance;
370 0 : xPropSet->setPropertyValue("D3DSceneDistance", aAny);
371 :
372 : // focalLength
373 0 : aAny <<= mnFocalLength;
374 0 : xPropSet->setPropertyValue("D3DSceneFocalLength", aAny);
375 :
376 : // shadowSlant
377 0 : aAny <<= (sal_Int16)mnShadowSlant;
378 0 : xPropSet->setPropertyValue("D3DSceneShadowSlant", aAny);
379 :
380 : // shadeMode
381 0 : aAny <<= mxShadeMode;
382 0 : xPropSet->setPropertyValue("D3DSceneShadeMode", aAny);
383 :
384 : // ambientColor
385 0 : aAny <<= maAmbientColor;
386 0 : xPropSet->setPropertyValue("D3DSceneAmbientColor", aAny);
387 :
388 : // lightingMode
389 0 : aAny <<= mbLightingMode;
390 0 : xPropSet->setPropertyValue("D3DSceneTwoSidedLighting", aAny);
391 :
392 0 : if( !maList.empty() )
393 : {
394 0 : uno::Any aAny2;
395 0 : uno::Any aAny3;
396 :
397 : // set lights
398 0 : for( size_t a = 0; a < maList.size(); a++)
399 : {
400 0 : SdXML3DLightContext* pCtx = (SdXML3DLightContext*)maList[ a ];
401 :
402 : // set anys
403 0 : aAny <<= pCtx->GetDiffuseColor();
404 0 : drawing::Direction3D xLightDir;
405 0 : xLightDir.DirectionX = pCtx->GetDirection().getX();
406 0 : xLightDir.DirectionY = pCtx->GetDirection().getY();
407 0 : xLightDir.DirectionZ = pCtx->GetDirection().getZ();
408 0 : aAny2 <<= xLightDir;
409 0 : aAny3 <<= pCtx->GetEnabled();
410 :
411 0 : switch(a)
412 : {
413 : case 0:
414 : {
415 0 : xPropSet->setPropertyValue("D3DSceneLightColor1", aAny);
416 0 : xPropSet->setPropertyValue("D3DSceneLightDirection1", aAny2);
417 0 : xPropSet->setPropertyValue("D3DSceneLightOn1", aAny3);
418 0 : break;
419 : }
420 : case 1:
421 : {
422 0 : xPropSet->setPropertyValue("D3DSceneLightColor2", aAny);
423 0 : xPropSet->setPropertyValue("D3DSceneLightDirection2", aAny2);
424 0 : xPropSet->setPropertyValue("D3DSceneLightOn2", aAny3);
425 0 : break;
426 : }
427 : case 2:
428 : {
429 0 : xPropSet->setPropertyValue("D3DSceneLightColor3", aAny);
430 0 : xPropSet->setPropertyValue("D3DSceneLightDirection3", aAny2);
431 0 : xPropSet->setPropertyValue("D3DSceneLightOn3", aAny3);
432 0 : break;
433 : }
434 : case 3:
435 : {
436 0 : xPropSet->setPropertyValue("D3DSceneLightColor4", aAny);
437 0 : xPropSet->setPropertyValue("D3DSceneLightDirection4", aAny2);
438 0 : xPropSet->setPropertyValue("D3DSceneLightOn4", aAny3);
439 0 : break;
440 : }
441 : case 4:
442 : {
443 0 : xPropSet->setPropertyValue("D3DSceneLightColor5", aAny);
444 0 : xPropSet->setPropertyValue("D3DSceneLightDirection5", aAny2);
445 0 : xPropSet->setPropertyValue("D3DSceneLightOn5", aAny3);
446 0 : break;
447 : }
448 : case 5:
449 : {
450 0 : xPropSet->setPropertyValue("D3DSceneLightColor6", aAny);
451 0 : xPropSet->setPropertyValue("D3DSceneLightDirection6", aAny2);
452 0 : xPropSet->setPropertyValue("D3DSceneLightOn6", aAny3);
453 0 : break;
454 : }
455 : case 6:
456 : {
457 0 : xPropSet->setPropertyValue("D3DSceneLightColor7", aAny);
458 0 : xPropSet->setPropertyValue("D3DSceneLightDirection7", aAny2);
459 0 : xPropSet->setPropertyValue("D3DSceneLightOn7", aAny3);
460 0 : break;
461 : }
462 : case 7:
463 : {
464 0 : xPropSet->setPropertyValue("D3DSceneLightColor8", aAny);
465 0 : xPropSet->setPropertyValue("D3DSceneLightDirection8", aAny2);
466 0 : xPropSet->setPropertyValue("D3DSceneLightOn8", aAny3);
467 0 : break;
468 : }
469 : }
470 0 : }
471 : }
472 :
473 : // CameraGeometry and camera settings
474 0 : drawing::CameraGeometry aCamGeo;
475 0 : aCamGeo.vrp.PositionX = maVRP.getX();
476 0 : aCamGeo.vrp.PositionY = maVRP.getY();
477 0 : aCamGeo.vrp.PositionZ = maVRP.getZ();
478 0 : aCamGeo.vpn.DirectionX = maVPN.getX();
479 0 : aCamGeo.vpn.DirectionY = maVPN.getY();
480 0 : aCamGeo.vpn.DirectionZ = maVPN.getZ();
481 0 : aCamGeo.vup.DirectionX = maVUP.getX();
482 0 : aCamGeo.vup.DirectionY = maVUP.getY();
483 0 : aCamGeo.vup.DirectionZ = maVUP.getZ();
484 0 : aAny <<= aCamGeo;
485 0 : xPropSet->setPropertyValue("D3DCameraGeometry", aAny);
486 :
487 : // #91047# set drawing::ProjectionMode AFTER camera geometry is set
488 : // projection "D3DScenePerspective" drawing::ProjectionMode
489 0 : aAny <<= mxPrjMode;
490 0 : xPropSet->setPropertyValue("D3DScenePerspective", aAny);
491 0 : }
492 :
493 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|