1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "svx/svdstr.hrc"
22 #include "svdglob.hxx"
23 #include "svx/svditer.hxx"
26 #include <svx/globl3d.hxx>
27 #include <svx/svdpage.hxx>
28 #include <svl/style.hxx>
29 #include <svx/scene3d.hxx>
30 #include <svx/e3dundo.hxx>
31 #include <svx/svdtrans.hxx>
32 #include <svx/svxids.hrc>
33 #include <editeng/colritem.hxx>
34 #include <svx/e3ditem.hxx>
35 #include <svx/xlntrit.hxx>
36 #include <svx/xfltrit.hxx>
37 #include <svx/svx3ditems.hxx>
38 #include <svl/whiter.hxx>
39 #include <svx/xflftrit.hxx>
40 #include <sdr/properties/e3dsceneproperties.hxx>
41 #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
42 #include <svx/svddrag.hxx>
43 #include <helperminimaldepth3d.hxx>
45 #include <drawinglayer/geometry/viewinformation3d.hxx>
46 #include <basegfx/polygon/b2dpolypolygontools.hxx>
47 #include <svx/e3dsceneupdater.hxx>
48 #include <svx/svdmodel.hxx>
55 double mfMinimalDepth
;
61 ImpRemap3DDepth(sal_uInt32 nOrdNum
, double fMinimalDepth
);
62 ImpRemap3DDepth(sal_uInt32 nOrdNum
);
66 bool operator<(const ImpRemap3DDepth
& rComp
) const;
68 sal_uInt32
GetOrdNum() const { return mnOrdNum
; }
69 bool IsScene() const { return mbIsScene
; }
72 ImpRemap3DDepth::ImpRemap3DDepth(sal_uInt32 nOrdNum
, double fMinimalDepth
)
74 mfMinimalDepth(fMinimalDepth
),
79 ImpRemap3DDepth::ImpRemap3DDepth(sal_uInt32 nOrdNum
)
86 ImpRemap3DDepth::~ImpRemap3DDepth()
90 bool ImpRemap3DDepth::operator<(const ImpRemap3DDepth
& rComp
) const
104 return mfMinimalDepth
< rComp
.mfMinimalDepth
;
109 // typedefs for a vector of ImpRemap3DDepths
110 typedef ::std::vector
< ImpRemap3DDepth
> ImpRemap3DDepthVector
;
114 class Imp3DDepthRemapper
116 ImpRemap3DDepthVector maVector
;
119 Imp3DDepthRemapper(E3dScene
& rScene
);
120 ~Imp3DDepthRemapper();
122 sal_uInt32
RemapOrdNum(sal_uInt32 nOrdNum
) const;
125 Imp3DDepthRemapper::Imp3DDepthRemapper(E3dScene
& rScene
)
127 // only called when rScene.GetSubList() and nObjCount > 1L
128 SdrObjList
* pList
= rScene
.GetSubList();
129 const size_t nObjCount(pList
->GetObjCount());
131 for(size_t a
= 0; a
< nObjCount
; ++a
)
133 SdrObject
* pCandidate
= pList
->GetObj(a
);
137 if(pCandidate
->ISA(E3dCompoundObject
))
139 // single 3d object, calc depth
140 const double fMinimalDepth(getMinimalDepthInViewCoordinates(static_cast< const E3dCompoundObject
& >(*pCandidate
)));
141 ImpRemap3DDepth
aEntry(a
, fMinimalDepth
);
142 maVector
.push_back(aEntry
);
146 // scene, use standard entry for scene
147 ImpRemap3DDepth
aEntry(a
);
148 maVector
.push_back(aEntry
);
153 // now, we need to sort the maVector by it's members minimal depth. The
154 // smaller, the nearer to the viewer.
155 ::std::sort(maVector
.begin(), maVector
.end());
158 Imp3DDepthRemapper::~Imp3DDepthRemapper()
162 sal_uInt32
Imp3DDepthRemapper::RemapOrdNum(sal_uInt32 nOrdNum
) const
164 if(nOrdNum
< maVector
.size())
166 nOrdNum
= maVector
[(maVector
.size() - 1) - nOrdNum
].GetOrdNum();
173 // BaseProperties section
175 sdr::properties::BaseProperties
* E3dScene::CreateObjectSpecificProperties()
177 return new sdr::properties::E3dSceneProperties(*this);
181 // DrawContact section
183 sdr::contact::ViewContact
* E3dScene::CreateObjectSpecificViewContact()
185 return new sdr::contact::ViewContactOfE3dScene(*this);
190 TYPEINIT1(E3dScene
, E3dObject
);
194 aCamera(basegfx::B3DPoint(0.0, 0.0, 4.0), basegfx::B3DPoint()),
195 mp3DDepthRemapper(0L),
196 bDrawOnlySelected(false)
199 E3dDefaultAttributes aDefault
;
200 SetDefaultAttributes(aDefault
);
203 E3dScene::E3dScene(E3dDefaultAttributes
& rDefault
)
205 aCamera(basegfx::B3DPoint(0.0, 0.0, 4.0), basegfx::B3DPoint()),
206 mp3DDepthRemapper(0L),
207 bDrawOnlySelected(false)
210 SetDefaultAttributes(rDefault
);
213 void E3dScene::SetDefaultAttributes(E3dDefaultAttributes
& /*rDefault*/)
215 // For WIN95/NT turn off the FP-Exceptions
217 _control87( _MCW_EM
, _MCW_EM
);
221 aCamera
.SetViewWindow(-2, -2, 4, 4);
222 aCameraSet
.SetDeviceRectangle(-2, 2, -2, 2);
223 aCamera
.SetDeviceWindow(Rectangle(0, 0, 10, 10));
224 Rectangle
aRect(0, 0, 10, 10);
225 aCameraSet
.SetViewportRectangle(aRect
);
227 // set defaults for Camera from ItemPool
228 aCamera
.SetProjection(GetPerspective());
229 basegfx::B3DPoint
aActualPosition(aCamera
.GetPosition());
230 double fNew
= GetDistance();
232 if(fabs(fNew
- aActualPosition
.getZ()) > 1.0)
234 aCamera
.SetPosition( basegfx::B3DPoint( aActualPosition
.getX(), aActualPosition
.getY(), fNew
) );
237 fNew
= GetFocalLength() / 100.0;
238 aCamera
.SetFocalLength(fNew
);
241 E3dScene::~E3dScene()
243 ImpCleanup3DDepthMapper();
246 basegfx::B2DPolyPolygon
E3dScene::TakeXorPoly() const
248 const sdr::contact::ViewContactOfE3dScene
& rVCScene
= static_cast< sdr::contact::ViewContactOfE3dScene
& >(GetViewContact());
249 const drawinglayer::geometry::ViewInformation3D
aViewInfo3D(rVCScene
.getViewInformation3D());
250 const basegfx::B3DPolyPolygon
aCubePolyPolygon(CreateWireframe());
252 basegfx::B2DPolyPolygon
aRetval(basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(aCubePolyPolygon
,
253 aViewInfo3D
.getObjectToView()));
254 aRetval
.transform(rVCScene
.getObjectTransformation());
259 void E3dScene::ImpCleanup3DDepthMapper()
261 if(mp3DDepthRemapper
)
263 delete mp3DDepthRemapper
;
264 mp3DDepthRemapper
= 0L;
268 sal_uInt32
E3dScene::RemapOrdNum(sal_uInt32 nNewOrdNum
) const
270 if(!mp3DDepthRemapper
)
272 const size_t nObjCount(GetSubList() ? GetSubList()->GetObjCount() : 0);
276 const_cast<E3dScene
*>(this)->mp3DDepthRemapper
= new Imp3DDepthRemapper((E3dScene
&)(*this));
280 if(mp3DDepthRemapper
)
282 return mp3DDepthRemapper
->RemapOrdNum(nNewOrdNum
);
288 sal_uInt16
E3dScene::GetObjIdentifier() const
293 void E3dScene::SetBoundRectDirty()
295 E3dScene
* pScene
= GetScene();
299 // avoid resetting aOutRect which in case of a 3D scene used as 2d object
300 // is model data,not re-creatable view data
304 // if not the outmost scene it is used as group in 3d, call parent
305 E3dObject::SetBoundRectDirty();
309 void E3dScene::NbcSetSnapRect(const Rectangle
& rRect
)
312 E3dObject::NbcSetSnapRect(rRect
);
313 aCamera
.SetDeviceWindow(rRect
);
314 aCameraSet
.SetViewportRectangle((Rectangle
&)rRect
);
316 ImpCleanup3DDepthMapper();
319 void E3dScene::NbcMove(const Size
& rSize
)
321 Rectangle aNewSnapRect
= GetSnapRect();
322 MoveRect(aNewSnapRect
, rSize
);
323 NbcSetSnapRect(aNewSnapRect
);
326 void E3dScene::NbcResize(const Point
& rRef
, const Fraction
& rXFact
,
327 const Fraction
& rYFact
)
329 Rectangle aNewSnapRect
= GetSnapRect();
330 ResizeRect(aNewSnapRect
, rRef
, rXFact
, rYFact
);
331 NbcSetSnapRect(aNewSnapRect
);
334 // Set new camera, and thus mark the scene and if possible the bound volume
337 void E3dScene::SetCamera(const Camera3D
& rNewCamera
)
340 aCamera
= rNewCamera
;
341 static_cast<sdr::properties::E3dSceneProperties
&>(GetProperties()).SetSceneItemsFromCamera();
345 // Fill new camera from old
346 Camera3D
& rCam
= (Camera3D
&)GetCamera();
349 if(rCam
.GetAspectMapping() == AS_NO_MAPPING
)
350 GetCameraSet().SetRatio(0.0);
352 // Set Imaging geometry
353 basegfx::B3DPoint
aVRP(rCam
.GetViewPoint());
354 basegfx::B3DVector
aVPN(aVRP
- rCam
.GetVRP());
355 basegfx::B3DVector
aVUV(rCam
.GetVUV());
357 // use SetViewportValues() to set VRP, VPN and VUV as vectors, too.
358 // Else these values would not be exported/imported correctly.
359 GetCameraSet().SetViewportValues(aVRP
, aVPN
, aVUV
);
362 GetCameraSet().SetPerspective(rCam
.GetProjection() == PR_PERSPECTIVE
);
363 GetCameraSet().SetViewportRectangle((Rectangle
&)rCam
.GetDeviceWindow());
365 ImpCleanup3DDepthMapper();
368 void E3dScene::NewObjectInserted(const E3dObject
* p3DObj
)
370 E3dObject::NewObjectInserted(p3DObj
);
372 if ( p3DObj
== this )
375 ImpCleanup3DDepthMapper();
378 // Inform parent of changes of a child
380 void E3dScene::StructureChanged()
382 E3dObject::StructureChanged();
385 ImpCleanup3DDepthMapper();
388 // Determine the overall scene object
390 E3dScene
* E3dScene::GetScene() const
393 return GetParentObj()->GetScene();
395 return const_cast<E3dScene
*>(this);
398 void E3dScene::removeAllNonSelectedObjects()
400 E3DModifySceneSnapRectUpdater
aUpdater(this);
402 for(size_t a
= 0; a
< maSubList
.GetObjCount(); ++a
)
404 SdrObject
* pObj
= maSubList
.GetObj(a
);
408 bool bRemoveObject(false);
410 if(pObj
->ISA(E3dScene
))
412 E3dScene
* pScene
= static_cast<E3dScene
*>(pObj
);
414 // iterate over this sub-scene
415 pScene
->removeAllNonSelectedObjects();
417 // check object count. Empty scenes can be deleted
418 const size_t nObjCount(pScene
->GetSubList() ? pScene
->GetSubList()->GetObjCount() : 0);
422 // all objects removed, scene can be removed, too
423 bRemoveObject
= true;
426 else if(pObj
->ISA(E3dCompoundObject
))
428 E3dCompoundObject
* pCompound
= static_cast<E3dCompoundObject
*>(pObj
);
430 if(!pCompound
->GetSelected())
432 bRemoveObject
= true;
438 maSubList
.NbcRemoveObject(pObj
->GetOrdNum());
440 SdrObject::Free(pObj
);
446 E3dScene
* E3dScene::Clone() const
448 return CloneHelper
< E3dScene
>();
451 E3dScene
& E3dScene::operator=(const E3dScene
& rObj
)
455 E3dObject::operator=(rObj
);
457 const E3dScene
& r3DObj
= (const E3dScene
&) rObj
;
458 aCamera
= r3DObj
.aCamera
;
460 aCameraSet
= r3DObj
.aCameraSet
;
461 static_cast<sdr::properties::E3dSceneProperties
&>(GetProperties()).SetSceneItemsFromCamera();
463 InvalidateBoundVolume();
467 ImpCleanup3DDepthMapper();
470 // After a Scene as model object is cloned, the used
471 // ViewContactOfE3dScene is created and partially used
472 // to calculate Bound/SnapRects, but - since quite some
473 // values are buffered at the VC - not really well
474 // initialized. It would be possible to always watch for
475 // preconditions of buffered data, but this would be expensive
476 // and would create a lot of short living data structures.
477 // It is currently better to flush that data, e.g. by using
478 // ActionChanged at the VC which will for this class
479 // flush that cached data and initalize it's valid reconstruction
480 GetViewContact().ActionChanged();
484 // Rebuild Light- and label- object lists rebuild (after loading, allocation)
486 void E3dScene::RebuildLists()
489 SdrLayerID nCurrLayerID
= GetLayer();
491 SdrObjListIter
a3DIterator(maSubList
, IM_FLAT
);
493 // then examine all the objects in the scene
494 while ( a3DIterator
.IsMore() )
496 E3dObject
* p3DObj
= static_cast<E3dObject
*>(a3DIterator
.Next());
497 p3DObj
->NbcSetLayer(nCurrLayerID
);
498 NewObjectInserted(p3DObj
);
502 SdrObjGeoData
*E3dScene::NewGeoData() const
504 return new E3DSceneGeoData
;
507 void E3dScene::SaveGeoData(SdrObjGeoData
& rGeo
) const
509 E3dObject::SaveGeoData (rGeo
);
511 static_cast<E3DSceneGeoData
&>(rGeo
).aCamera
= aCamera
;
514 void E3dScene::RestGeoData(const SdrObjGeoData
& rGeo
)
516 // #i94832# removed E3DModifySceneSnapRectUpdater here.
517 // It should not be needed, is already part of E3dObject::RestGeoData
518 E3dObject::RestGeoData (rGeo
);
519 SetCamera (static_cast<const E3DSceneGeoData
&>(rGeo
).aCamera
);
522 // Something was changed in the style sheet, so change scene
524 void E3dScene::Notify(SfxBroadcaster
&rBC
, const SfxHint
&rHint
)
527 E3dObject::Notify(rBC
, rHint
);
530 void E3dScene::RotateScene (const Point
& rRef
, long /*nAngle*/, double sn
, double cs
)
532 Point UpperLeft
, LowerRight
, Center
, NewCenter
;
534 UpperLeft
= aOutRect
.TopLeft();
535 LowerRight
= aOutRect
.BottomRight();
537 long dxOutRectHalf
= labs(UpperLeft
.X() - LowerRight
.X());
539 long dyOutRectHalf
= labs(UpperLeft
.Y() - LowerRight
.Y());
542 // Only the center is moved. The corners are moved by NbcMove. For the
543 // rotation a cartesian coordinate system is used in which the pivot
544 // point is the origin, and the y-axis increases upward, the X-axis to
545 // the right. This must be especially noted for the Y-values.
546 // (When considering a flat piece of paper the Y-axis pointing downwards
547 Center
.X() = (UpperLeft
.X() + dxOutRectHalf
) - rRef
.X();
548 Center
.Y() = -((UpperLeft
.Y() + dyOutRectHalf
) - rRef
.Y());
549 // A few special cases has to be dealt with first (n * 90 degrees n integer)
550 if (sn
==1.0 && cs
==0.0) { // 90deg
551 NewCenter
.X() = -Center
.Y();
552 NewCenter
.Y() = -Center
.X();
553 } else if (sn
==0.0 && cs
==-1.0) { // 180deg
554 NewCenter
.X() = -Center
.X();
555 NewCenter
.Y() = -Center
.Y();
556 } else if (sn
==-1.0 && cs
==0.0) { // 270deg
557 NewCenter
.X() = Center
.Y();
558 NewCenter
.Y() = -Center
.X();
560 else // Here it is rotated to any angle in the mathematically
561 // positive direction!
562 { // xnew = x * cos(alpha) - y * sin(alpha)
563 // ynew = x * sin(alpha) + y * cos(alpha)
564 // Bottom Right is not rotated: the pages of aOutRect must
565 // remain parallel to the coordinate axes.
566 NewCenter
.X() = (long) (Center
.X() * cs
- Center
.Y() * sn
);
567 NewCenter
.Y() = (long) (Center
.X() * sn
+ Center
.Y() * cs
);
571 Point DiffPoint
= (NewCenter
- Center
);
572 Differenz
.Width() = DiffPoint
.X();
573 Differenz
.Height() = -DiffPoint
.Y(); // Note that the Y-axis is counted ad positive downward.
574 NbcMove (Differenz
); // Actually executes the coordinate transformation.
577 OUString
E3dScene::TakeObjNameSingul() const
579 OUStringBuffer
sName(ImpGetResStr(STR_ObjNameSingulScene3d
));
581 OUString
aName(GetName());
582 if (!aName
.isEmpty())
589 return sName
.makeStringAndClear();
592 OUString
E3dScene::TakeObjNamePlural() const
594 return ImpGetResStr(STR_ObjNamePluralScene3d
);
597 // The NbcRotate routine overrides the one of the SdrObject. The idea is
598 // to be able to rotate the scene relative to the position of the scene
599 // and then the objects in the scene
601 void E3dScene::NbcSetTransform(const basegfx::B3DHomMatrix
& rMatrix
)
603 if(maTransformation
!= rMatrix
)
606 E3dObject::NbcSetTransform(rMatrix
);
610 void E3dScene::SetTransform(const basegfx::B3DHomMatrix
& rMatrix
)
612 if(rMatrix
!= maTransformation
)
615 E3dObject::SetTransform(rMatrix
);
619 void E3dScene::NbcRotate(const Point
& rRef
, long nAngle
, double sn
, double cs
)
621 // So currently the glue points are defined relative to the scene aOutRect.
622 // Before turning the glue points are defined relative to the page. They
623 // take no part in the rotation of the scene. To ensure this, there is the
624 // SetGlueReallyAbsolute(sal_True);
626 // So that was the scene, now the objects used in the scene
627 // 3D objects, if there is only one it can still have multiple surfaces but
628 // the surfaces do not hve to be connected. This allows you to access child
629 // objects. So going through the entire list and rotate around the Z axis
630 // through the enter of aOutRect's (Steiner's theorem), so RotateZ
632 RotateScene (rRef
, nAngle
, sn
, cs
); // Rotates the scene
633 double fWinkelInRad
= nAngle
/100.0 * F_PI180
;
635 basegfx::B3DHomMatrix aRotation
;
636 aRotation
.rotate(0.0, 0.0, fWinkelInRad
);
637 NbcSetTransform(aRotation
* GetTransform());
639 SetRectsDirty(); // This forces a recalculation of all BoundRects
640 NbcRotateGluePoints(rRef
,nAngle
,sn
,cs
); // Rotate the glue points (who still
641 // have coordinates relative to the
643 SetGlueReallyAbsolute(false); // from now they are again relative to BoundRect (that is defined as aOutRect)
647 void E3dScene::RecalcSnapRect()
649 E3dScene
* pScene
= GetScene();
653 // The Scene is used as a 2D-Objekt, take the SnapRect from the
654 // 2D Display settings
655 Camera3D
& rCam
= (Camera3D
&)pScene
->GetCamera();
656 maSnapRect
= rCam
.GetDeviceWindow();
660 // The Scene itself is a member of another scene, get the SnapRect
661 // as a composite object
662 E3dObject::RecalcSnapRect();
666 bool E3dScene::IsBreakObjPossible()
668 // Break scene, if all members are able to break
669 SdrObjListIter
a3DIterator(maSubList
, IM_DEEPWITHGROUPS
);
671 while ( a3DIterator
.IsMore() )
673 E3dObject
* pObj
= static_cast<E3dObject
*>(a3DIterator
.Next());
674 DBG_ASSERT(pObj
->ISA(E3dObject
), "only 3D objects are allowed in scenes!");
675 if(!pObj
->IsBreakObjPossible())
682 basegfx::B2DPolyPolygon
E3dScene::TakeCreatePoly(const SdrDragStat
& /*rDrag*/) const
684 return TakeXorPoly();
687 bool E3dScene::BegCreate(SdrDragStat
& rStat
)
689 rStat
.SetOrtho4Possible();
690 Rectangle
aRect1(rStat
.GetStart(), rStat
.GetNow());
692 rStat
.SetActionRect(aRect1
);
693 NbcSetSnapRect(aRect1
);
697 bool E3dScene::MovCreate(SdrDragStat
& rStat
)
700 rStat
.TakeCreateRect(aRect1
);
702 rStat
.SetActionRect(aRect1
);
703 NbcSetSnapRect(aRect1
);
709 bool E3dScene::EndCreate(SdrDragStat
& rStat
, SdrCreateCmd eCmd
)
712 rStat
.TakeCreateRect(aRect1
);
714 NbcSetSnapRect(aRect1
);
716 return (eCmd
==SDRCREATE_FORCEEND
|| rStat
.GetPointAnz()>=2);
719 bool E3dScene::BckCreate(SdrDragStat
& /*rStat*/)
724 void E3dScene::BrkCreate(SdrDragStat
& /*rStat*/)
728 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */