fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svx / source / engine3d / scene3d.cxx
blob059e61f0f85d9f7e7d5c51d19479eb43041a9a72
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 "svx/svdstr.hrc"
22 #include "svx/svdglob.hxx"
23 #include "svx/svditer.hxx"
25 #include <stdlib.h>
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 <svx/sdr/properties/e3dsceneproperties.hxx>
41 #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
42 #include <svx/svddrag.hxx>
43 #include <helperminimaldepth3d.hxx>
44 #include <algorithm>
45 #include <drawinglayer/geometry/viewinformation3d.hxx>
46 #include <basegfx/polygon/b2dpolypolygontools.hxx>
47 #include <svx/e3dsceneupdater.hxx>
49 //////////////////////////////////////////////////////////////////////////////
51 class ImpRemap3DDepth
53 sal_uInt32 mnOrdNum;
54 double mfMinimalDepth;
56 // bit field
57 bool mbIsScene : 1;
59 public:
60 ImpRemap3DDepth(sal_uInt32 nOrdNum, double fMinimalDepth);
61 ImpRemap3DDepth(sal_uInt32 nOrdNum);
62 ~ImpRemap3DDepth();
64 // for ::std::sort
65 bool operator<(const ImpRemap3DDepth& rComp) const;
67 sal_uInt32 GetOrdNum() const { return mnOrdNum; }
68 bool IsScene() const { return mbIsScene; }
71 ImpRemap3DDepth::ImpRemap3DDepth(sal_uInt32 nOrdNum, double fMinimalDepth)
72 : mnOrdNum(nOrdNum),
73 mfMinimalDepth(fMinimalDepth),
74 mbIsScene(false)
78 ImpRemap3DDepth::ImpRemap3DDepth(sal_uInt32 nOrdNum)
79 : mnOrdNum(nOrdNum),
80 mfMinimalDepth(0.0),
81 mbIsScene(true)
85 ImpRemap3DDepth::~ImpRemap3DDepth()
89 bool ImpRemap3DDepth::operator<(const ImpRemap3DDepth& rComp) const
91 if(IsScene())
93 return false;
95 else
97 if(rComp.IsScene())
99 return true;
101 else
103 return mfMinimalDepth < rComp.mfMinimalDepth;
108 // typedefs for a vector of ImpRemap3DDepths
109 typedef ::std::vector< ImpRemap3DDepth > ImpRemap3DDepthVector;
111 //////////////////////////////////////////////////////////////////////////////
113 class Imp3DDepthRemapper
115 ImpRemap3DDepthVector maVector;
117 public:
118 Imp3DDepthRemapper(E3dScene& rScene);
119 ~Imp3DDepthRemapper();
121 sal_uInt32 RemapOrdNum(sal_uInt32 nOrdNum) const;
124 Imp3DDepthRemapper::Imp3DDepthRemapper(E3dScene& rScene)
126 // only called when rScene.GetSubList() and nObjCount > 1L
127 SdrObjList* pList = rScene.GetSubList();
128 const sal_uInt32 nObjCount(pList->GetObjCount());
130 for(sal_uInt32 a(0L); a < nObjCount; a++)
132 SdrObject* pCandidate = pList->GetObj(a);
134 if(pCandidate)
136 if(pCandidate->ISA(E3dCompoundObject))
138 // single 3d object, calc depth
139 const double fMinimalDepth(getMinimalDepthInViewCoordinates(static_cast< const E3dCompoundObject& >(*pCandidate)));
140 ImpRemap3DDepth aEntry(a, fMinimalDepth);
141 maVector.push_back(aEntry);
143 else
145 // scene, use standard entry for scene
146 ImpRemap3DDepth aEntry(a);
147 maVector.push_back(aEntry);
152 // now, we need to sort the maVector by it's members minimal depth. The
153 // smaller, the nearer to the viewer.
154 ::std::sort(maVector.begin(), maVector.end());
157 Imp3DDepthRemapper::~Imp3DDepthRemapper()
161 sal_uInt32 Imp3DDepthRemapper::RemapOrdNum(sal_uInt32 nOrdNum) const
163 if(nOrdNum < maVector.size())
165 nOrdNum = maVector[(maVector.size() - 1) - nOrdNum].GetOrdNum();
168 return nOrdNum;
171 //////////////////////////////////////////////////////////////////////////////
172 // BaseProperties section
174 sdr::properties::BaseProperties* E3dScene::CreateObjectSpecificProperties()
176 return new sdr::properties::E3dSceneProperties(*this);
179 //////////////////////////////////////////////////////////////////////////////
180 // DrawContact section
182 sdr::contact::ViewContact* E3dScene::CreateObjectSpecificViewContact()
184 return new sdr::contact::ViewContactOfE3dScene(*this);
187 //////////////////////////////////////////////////////////////////////////////
189 TYPEINIT1(E3dScene, E3dObject);
191 E3dScene::E3dScene()
192 : E3dObject(),
193 aCamera(basegfx::B3DPoint(0.0, 0.0, 4.0), basegfx::B3DPoint()),
194 mp3DDepthRemapper(0L),
195 bDrawOnlySelected(false)
197 // Set defaults
198 E3dDefaultAttributes aDefault;
199 SetDefaultAttributes(aDefault);
202 E3dScene::E3dScene(E3dDefaultAttributes& rDefault)
203 : E3dObject(),
204 aCamera(basegfx::B3DPoint(0.0, 0.0, 4.0), basegfx::B3DPoint()),
205 mp3DDepthRemapper(0L),
206 bDrawOnlySelected(false)
208 // Set defaults
209 SetDefaultAttributes(rDefault);
212 void E3dScene::SetDefaultAttributes(E3dDefaultAttributes& /*rDefault*/)
214 // For WIN95/NT turn off the FP-Exceptions
215 #if defined(WNT)
216 _control87( _MCW_EM, _MCW_EM );
217 #endif
219 // Set defaults
220 aCamera.SetViewWindow(-2, -2, 4, 4);
221 aCameraSet.SetDeviceRectangle(-2, 2, -2, 2);
222 aCamera.SetDeviceWindow(Rectangle(0, 0, 10, 10));
223 Rectangle aRect(0, 0, 10, 10);
224 aCameraSet.SetViewportRectangle(aRect);
226 // set defaults for Camera from ItemPool
227 aCamera.SetProjection(GetPerspective());
228 basegfx::B3DPoint aActualPosition(aCamera.GetPosition());
229 double fNew = GetDistance();
231 if(fabs(fNew - aActualPosition.getZ()) > 1.0)
233 aCamera.SetPosition( basegfx::B3DPoint( aActualPosition.getX(), aActualPosition.getY(), fNew) );
236 fNew = GetFocalLength() / 100.0;
237 aCamera.SetFocalLength(fNew);
240 E3dScene::~E3dScene()
242 ImpCleanup3DDepthMapper();
245 basegfx::B2DPolyPolygon E3dScene::TakeXorPoly() const
247 const sdr::contact::ViewContactOfE3dScene& rVCScene = static_cast< sdr::contact::ViewContactOfE3dScene& >(GetViewContact());
248 const drawinglayer::geometry::ViewInformation3D aViewInfo3D(rVCScene.getViewInformation3D());
249 const basegfx::B3DPolyPolygon aCubePolyPolygon(CreateWireframe());
251 basegfx::B2DPolyPolygon aRetval(basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(aCubePolyPolygon,
252 aViewInfo3D.getObjectToView()));
253 aRetval.transform(rVCScene.getObjectTransformation());
255 return aRetval;
258 void E3dScene::ImpCleanup3DDepthMapper()
260 if(mp3DDepthRemapper)
262 delete mp3DDepthRemapper;
263 mp3DDepthRemapper = 0L;
267 sal_uInt32 E3dScene::RemapOrdNum(sal_uInt32 nNewOrdNum) const
269 if(!mp3DDepthRemapper)
271 const sal_uInt32 nObjCount(GetSubList() ? GetSubList()->GetObjCount() : 0L);
273 if(nObjCount > 1L)
275 ((E3dScene*)this)->mp3DDepthRemapper = new Imp3DDepthRemapper((E3dScene&)(*this));
279 if(mp3DDepthRemapper)
281 return mp3DDepthRemapper->RemapOrdNum(nNewOrdNum);
284 return nNewOrdNum;
287 sal_uInt16 E3dScene::GetObjIdentifier() const
289 return E3D_SCENE_ID;
292 void E3dScene::SetBoundRectDirty()
294 E3dScene* pScene = GetScene();
296 if(pScene == this)
298 // avoid resetting aOutRect which in case of a 3D scene used as 2d object
299 // is model data,not re-creatable view data
301 else
303 // if not the outmost scene it is used as group in 3d, call parent
304 E3dObject::SetBoundRectDirty();
308 void E3dScene::NbcSetSnapRect(const Rectangle& rRect)
310 SetRectsDirty();
311 E3dObject::NbcSetSnapRect(rRect);
312 aCamera.SetDeviceWindow(rRect);
313 aCameraSet.SetViewportRectangle((Rectangle&)rRect);
315 ImpCleanup3DDepthMapper();
318 void E3dScene::NbcMove(const Size& rSize)
320 Rectangle aNewSnapRect = GetSnapRect();
321 MoveRect(aNewSnapRect, rSize);
322 NbcSetSnapRect(aNewSnapRect);
325 void E3dScene::NbcResize(const Point& rRef, const Fraction& rXFact,
326 const Fraction& rYFact)
328 Rectangle aNewSnapRect = GetSnapRect();
329 ResizeRect(aNewSnapRect, rRef, rXFact, rYFact);
330 NbcSetSnapRect(aNewSnapRect);
333 // Set new camera, and thus mark the scene and if possible the bound volume
334 // as changed
336 void E3dScene::SetCamera(const Camera3D& rNewCamera)
338 // Set old camera
339 aCamera = rNewCamera;
340 ((sdr::properties::E3dSceneProperties&)GetProperties()).SetSceneItemsFromCamera();
342 SetRectsDirty();
344 // Fill new camera from old
345 Camera3D& rCam = (Camera3D&)GetCamera();
347 // Turn off ratio
348 if(rCam.GetAspectMapping() == AS_NO_MAPPING)
349 GetCameraSet().SetRatio(0.0);
351 // Set Imaging geometry
352 basegfx::B3DPoint aVRP(rCam.GetViewPoint());
353 basegfx::B3DVector aVPN(aVRP - rCam.GetVRP());
354 basegfx::B3DVector aVUV(rCam.GetVUV());
356 // use SetViewportValues() to set VRP, VPN and VUV as vectors, too.
357 // Else these values would not be exported/imported correctly.
358 GetCameraSet().SetViewportValues(aVRP, aVPN, aVUV);
360 // Set perspective
361 GetCameraSet().SetPerspective(rCam.GetProjection() == PR_PERSPECTIVE);
362 GetCameraSet().SetViewportRectangle((Rectangle&)rCam.GetDeviceWindow());
364 ImpCleanup3DDepthMapper();
367 void E3dScene::NewObjectInserted(const E3dObject* p3DObj)
369 E3dObject::NewObjectInserted(p3DObj);
371 if ( p3DObj == this )
372 return;
374 ImpCleanup3DDepthMapper();
377 // Inform parent of changes of a child
379 void E3dScene::StructureChanged()
381 E3dObject::StructureChanged();
382 SetRectsDirty();
384 ImpCleanup3DDepthMapper();
387 // Determine the overall scene object
389 E3dScene* E3dScene::GetScene() const
391 if(GetParentObj())
392 return GetParentObj()->GetScene();
393 else
394 return (E3dScene*)this;
397 void E3dScene::removeAllNonSelectedObjects()
399 E3DModifySceneSnapRectUpdater aUpdater(this);
401 for(sal_uInt32 a(0); a < maSubList.GetObjCount(); a++)
403 SdrObject* pObj = maSubList.GetObj(a);
405 if(pObj)
407 bool bRemoveObject(false);
409 if(pObj->ISA(E3dScene))
411 E3dScene* pScene = (E3dScene*)pObj;
413 // iterate over this sub-scene
414 pScene->removeAllNonSelectedObjects();
416 // check object count. Empty scenes can be deleted
417 const sal_uInt32 nObjCount(pScene->GetSubList() ? pScene->GetSubList()->GetObjCount() : 0);
419 if(!nObjCount)
421 // all objects removed, scene can be removed, too
422 bRemoveObject = true;
425 else if(pObj->ISA(E3dCompoundObject))
427 E3dCompoundObject* pCompound = (E3dCompoundObject*)pObj;
429 if(!pCompound->GetSelected())
431 bRemoveObject = true;
435 if(bRemoveObject)
437 maSubList.NbcRemoveObject(pObj->GetOrdNum());
438 a--;
439 SdrObject::Free(pObj);
445 E3dScene* E3dScene::Clone() const
447 return CloneHelper< E3dScene >();
450 E3dScene& E3dScene::operator=(const E3dScene& rObj)
452 if( this == &rObj )
453 return *this;
454 E3dObject::operator=(rObj);
456 const E3dScene& r3DObj = (const E3dScene&) rObj;
457 aCamera = r3DObj.aCamera;
459 aCameraSet = r3DObj.aCameraSet;
460 ((sdr::properties::E3dSceneProperties&)GetProperties()).SetSceneItemsFromCamera();
462 InvalidateBoundVolume();
463 RebuildLists();
464 SetRectsDirty();
466 ImpCleanup3DDepthMapper();
468 // #i101941#
469 // After a Scene as model object is cloned, the used
470 // ViewContactOfE3dScene is created and partially used
471 // to calculate Bound/SnapRects, but - since quite some
472 // values are buffered at the VC - not really well
473 // initialized. It would be possible to always watch for
474 // preconditions of buffered data, but this would be expensive
475 // and would create a lot of short living data structures.
476 // It is currently better to flush that data, e.g. by using
477 // ActionChanged at the VC which will for this class
478 // flush that cached data and initalize it's valid reconstruction
479 GetViewContact().ActionChanged();
480 return *this;
483 // Rebuild Light- and label- object lists rebuild (after loading, allocation)
485 void E3dScene::RebuildLists()
487 // first delete
488 SdrLayerID nCurrLayerID = GetLayer();
490 SdrObjListIter a3DIterator(maSubList, IM_FLAT);
492 // then examine all the objects in the scene
493 while ( a3DIterator.IsMore() )
495 E3dObject* p3DObj = (E3dObject*) a3DIterator.Next();
496 p3DObj->NbcSetLayer(nCurrLayerID);
497 NewObjectInserted(p3DObj);
501 SdrObjGeoData *E3dScene::NewGeoData() const
503 return new E3DSceneGeoData;
506 void E3dScene::SaveGeoData(SdrObjGeoData& rGeo) const
508 E3dObject::SaveGeoData (rGeo);
510 ((E3DSceneGeoData &) rGeo).aCamera = aCamera;
513 void E3dScene::RestGeoData(const SdrObjGeoData& rGeo)
515 // #i94832# removed E3DModifySceneSnapRectUpdater here.
516 // It should not be needed, is already part of E3dObject::RestGeoData
517 E3dObject::RestGeoData (rGeo);
518 SetCamera (((E3DSceneGeoData &) rGeo).aCamera);
521 // Something was changed in the style sheet, so change scene
523 void E3dScene::Notify(SfxBroadcaster &rBC, const SfxHint &rHint)
525 SetRectsDirty();
526 E3dObject::Notify(rBC, rHint);
529 void E3dScene::RotateScene (const Point& rRef, long /*nWink*/, double sn, double cs)
531 Point UpperLeft, LowerRight, Center, NewCenter;
533 UpperLeft = aOutRect.TopLeft();
534 LowerRight = aOutRect.BottomRight();
536 long dxOutRectHalf = labs(UpperLeft.X() - LowerRight.X());
537 dxOutRectHalf /= 2;
538 long dyOutRectHalf = labs(UpperLeft.Y() - LowerRight.Y());
539 dyOutRectHalf /= 2;
541 // Only the center is moved. The corners are moved by NbcMove. For the
542 // rotation a cartesian coordinate system is used in which the pivot
543 // point is the origin, and the y-axis increases upward, the X-axis to
544 // the right. This must be especially noted for the Y-values.
545 // (When considering a flat piece of paper the Y-axis pointing downwards
546 Center.X() = (UpperLeft.X() + dxOutRectHalf) - rRef.X();
547 Center.Y() = -((UpperLeft.Y() + dyOutRectHalf) - rRef.Y());
548 // A few special cases has to be dealt with first (n * 90 degrees n integer)
549 if (sn==1.0 && cs==0.0) { // 90deg
550 NewCenter.X() = -Center.Y();
551 NewCenter.Y() = -Center.X();
552 } else if (sn==0.0 && cs==-1.0) { // 180deg
553 NewCenter.X() = -Center.X();
554 NewCenter.Y() = -Center.Y();
555 } else if (sn==-1.0 && cs==0.0) { // 270deg
556 NewCenter.X() = Center.Y();
557 NewCenter.Y() = -Center.X();
559 else // Here it is rotated to any angle in the mathematically
560 // positive direction!
561 { // xnew = x * cos(alpha) - y * sin(alpha)
562 // ynew = x * sin(alpha) + y * cos(alpha)
563 // Bottom Right is not rotated: the pages of aOutRect must
564 // remain parallel to the coordinate axes.
565 NewCenter.X() = (long) (Center.X() * cs - Center.Y() * sn);
566 NewCenter.Y() = (long) (Center.X() * sn + Center.Y() * cs);
569 Size Differenz;
570 Point DiffPoint = (NewCenter - Center);
571 Differenz.Width() = DiffPoint.X();
572 Differenz.Height() = -DiffPoint.Y(); // Note that the Y-axis is counted ad positive downward.
573 NbcMove (Differenz); // Actually executes the coordinate transformation.
576 void E3dScene::TakeObjNameSingul(XubString& rName) const
578 rName=ImpGetResStr(STR_ObjNameSingulScene3d);
580 String aName( GetName() );
581 if(aName.Len())
583 rName += sal_Unicode(' ');
584 rName += sal_Unicode('\'');
585 rName += aName;
586 rName += sal_Unicode('\'');
590 void E3dScene::TakeObjNamePlural(XubString& rName) const
592 rName=ImpGetResStr(STR_ObjNamePluralScene3d);
595 // The NbcRotate routine overloads the one of the SdrObject. The idea is
596 // to be able to rotate the scene relative to the position of the scene
597 // and then the objects in the scene
599 void E3dScene::NbcSetTransform(const basegfx::B3DHomMatrix& rMatrix)
601 if(maTransformation != rMatrix)
603 // call parent
604 E3dObject::NbcSetTransform(rMatrix);
608 void E3dScene::SetTransform(const basegfx::B3DHomMatrix& rMatrix)
610 if(rMatrix != maTransformation)
612 // call parent
613 E3dObject::SetTransform(rMatrix);
617 void E3dScene::NbcRotate(const Point& rRef, long nWink, double sn, double cs)
619 // So currently the glue points are defined relative to the scene aOutRect.
620 // Before turning the glue points are defined relative to the page. They
621 // take no part in the rotation of the scene. To ensure this, there is the
622 // SetGlueReallyAbsolute(sal_True);
624 // So that was the scene, now the objects used in the scene
625 // 3D objects, if there is only one it can still have multiple surfaces but
626 // the surfaces do not hve to be connected. This allows you to access child
627 // objects. So going through the entire list and rotate around the Z axis
628 // through the enter of aOutRect's (Steiner's theorem), so RotateZ
630 RotateScene (rRef, nWink, sn, cs); // Rotates the scene
631 double fWinkelInRad = nWink/100 * F_PI180;
633 basegfx::B3DHomMatrix aRotation;
634 aRotation.rotate(0.0, 0.0, fWinkelInRad);
635 NbcSetTransform(aRotation * GetTransform());
637 SetRectsDirty(); // This forces a recalculation of all BoundRects
638 NbcRotateGluePoints(rRef,nWink,sn,cs); // Rotate the glue points (who still
639 // have coordinates relative to the
640 // original page)
641 SetGlueReallyAbsolute(sal_False); // from now they are again relative to BoundRect (that is defined as aOutRect)
642 SetRectsDirty();
645 void E3dScene::RecalcSnapRect()
647 E3dScene* pScene = GetScene();
649 if(pScene == this)
651 // The Scene is used as a 2D-Objekt, take the SnapRect from the
652 // 2D Display settings
653 Camera3D& rCam = (Camera3D&)pScene->GetCamera();
654 maSnapRect = rCam.GetDeviceWindow();
656 else
658 // The Scene itself is a member of another scene, get the SnapRect
659 // as a composite object
660 E3dObject::RecalcSnapRect();
664 bool E3dScene::IsBreakObjPossible()
666 // Break scene, if all members are able to break
667 SdrObjListIter a3DIterator(maSubList, IM_DEEPWITHGROUPS);
669 while ( a3DIterator.IsMore() )
671 E3dObject* pObj = (E3dObject*) a3DIterator.Next();
672 DBG_ASSERT(pObj->ISA(E3dObject), "only 3D objects are allowed in scenes!");
673 if(!pObj->IsBreakObjPossible())
674 return false;
677 return true;
680 basegfx::B2DPolyPolygon E3dScene::TakeCreatePoly(const SdrDragStat& /*rDrag*/) const
682 return TakeXorPoly();
685 bool E3dScene::BegCreate(SdrDragStat& rStat)
687 rStat.SetOrtho4Possible();
688 Rectangle aRect1(rStat.GetStart(), rStat.GetNow());
689 aRect1.Justify();
690 rStat.SetActionRect(aRect1);
691 NbcSetSnapRect(aRect1);
692 return true;
695 bool E3dScene::MovCreate(SdrDragStat& rStat)
697 Rectangle aRect1;
698 rStat.TakeCreateRect(aRect1);
699 aRect1.Justify();
700 rStat.SetActionRect(aRect1);
701 NbcSetSnapRect(aRect1);
702 SetBoundRectDirty();
703 bSnapRectDirty=sal_True;
704 return true;
707 bool E3dScene::EndCreate(SdrDragStat& rStat, SdrCreateCmd eCmd)
709 Rectangle aRect1;
710 rStat.TakeCreateRect(aRect1);
711 aRect1.Justify();
712 NbcSetSnapRect(aRect1);
713 SetRectsDirty();
714 return (eCmd==SDRCREATE_FORCEEND || rStat.GetPointAnz()>=2);
717 bool E3dScene::BckCreate(SdrDragStat& /*rStat*/)
719 return false;
722 void E3dScene::BrkCreate(SdrDragStat& /*rStat*/)
726 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */