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 #include <fucon3d.hxx>
21 
22 #include <svx/svxids.hrc>
23 #include <sfx2/dispatch.hxx>
24 #include <sfx2/viewfrm.hxx>
25 #include <tools/poly.hxx>
26 
27 #include <svx/xlineit0.hxx>
28 #include <svx/scene3d.hxx>
29 #include <svx/sphere3d.hxx>
30 #include <svx/cube3d.hxx>
31 #include <svx/lathe3d.hxx>
32 #include <svx/camera3d.hxx>
33 #include <svx/def3d.hxx>
34 
35 #include <vcl/weld.hxx>
36 
37 #include <app.hrc>
38 
39 #include <View.hxx>
40 #include <Window.hxx>
41 #include <ViewShell.hxx>
42 #include <drawdoc.hxx>
43 #include <ViewShellBase.hxx>
44 #include <ToolBarManager.hxx>
45 #include <svx/svx3ditems.hxx>
46 
47 #include <basegfx/polygon/b2dpolygontools.hxx>
48 
49 using namespace com::sun::star;
50 
51 namespace sd {
52 
53 
FuConstruct3dObject(ViewShell * pViewSh,::sd::Window * pWin,::sd::View * pView,SdDrawDocument * pDoc,SfxRequest & rReq)54 FuConstruct3dObject::FuConstruct3dObject (
55     ViewShell*  pViewSh,
56     ::sd::Window*       pWin,
57     ::sd::View*         pView,
58     SdDrawDocument* pDoc,
59     SfxRequest&     rReq)
60     : FuConstruct(pViewSh, pWin, pView, pDoc, rReq)
61 {
62 }
63 
Create(ViewShell * pViewSh,::sd::Window * pWin,::sd::View * pView,SdDrawDocument * pDoc,SfxRequest & rReq,bool bPermanent)64 rtl::Reference<FuPoor> FuConstruct3dObject::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq, bool bPermanent )
65 {
66     FuConstruct3dObject* pFunc;
67     rtl::Reference<FuPoor> xFunc( pFunc = new FuConstruct3dObject( pViewSh, pWin, pView, pDoc, rReq ) );
68     xFunc->DoExecute(rReq);
69     pFunc->SetPermanent(bPermanent);
70     return xFunc;
71 }
72 
DoExecute(SfxRequest & rReq)73 void FuConstruct3dObject::DoExecute( SfxRequest& rReq )
74 {
75     FuConstruct::DoExecute( rReq );
76     mpViewShell->GetViewShellBase().GetToolBarManager()->SetToolBar(
77         ToolBarManager::ToolBarGroup::Function,
78         ToolBarManager::msDrawingObjectToolBar);
79 }
80 
ImpCreateBasic3DShape()81 E3dCompoundObject* FuConstruct3dObject::ImpCreateBasic3DShape()
82 {
83     E3dCompoundObject* p3DObj = nullptr;
84 
85     switch (nSlotId)
86     {
87         default:
88         case SID_3D_CUBE:
89         {
90             p3DObj = new E3dCubeObj(
91                 mpView->getSdrModelFromSdrView(),
92                 mpView->Get3DDefaultAttributes(),
93                 ::basegfx::B3DPoint(-2500, -2500, -2500),
94                 ::basegfx::B3DVector(5000, 5000, 5000));
95             break;
96         }
97 
98         case SID_3D_SPHERE:
99         {
100             p3DObj = new E3dSphereObj(
101                 mpView->getSdrModelFromSdrView(),
102                 mpView->Get3DDefaultAttributes(),
103                 ::basegfx::B3DPoint(0, 0, 0),
104                 ::basegfx::B3DVector(5000, 5000, 5000));
105             break;
106         }
107 
108         case SID_3D_SHELL:
109         {
110             XPolygon aXPoly(Point (0, 1250), 2500, 2500, 0_deg100, 9000_deg100, false);
111             aXPoly.Scale(5.0, 5.0);
112 
113             ::basegfx::B2DPolygon aB2DPolygon(aXPoly.getB2DPolygon());
114             if(aB2DPolygon.areControlPointsUsed())
115             {
116                 aB2DPolygon = ::basegfx::utils::adaptiveSubdivideByAngle(aB2DPolygon);
117             }
118             p3DObj = new E3dLatheObj(
119                 mpView->getSdrModelFromSdrView(),
120                 mpView->Get3DDefaultAttributes(),
121                 ::basegfx::B2DPolyPolygon(aB2DPolygon));
122 
123             /* this is an open object, therefore it has to be handled double-
124                sided by default */
125             p3DObj->SetMergedItem(makeSvx3DDoubleSidedItem(true));
126             break;
127         }
128 
129         case SID_3D_HALF_SPHERE:
130         {
131             XPolygon aXPoly(Point (0, 1250), 2500, 2500, 0_deg100, 9000_deg100, false);
132             aXPoly.Scale(5.0, 5.0);
133 
134             aXPoly.Insert(0, Point (2400*5, 1250*5), PolyFlags::Normal);
135             aXPoly.Insert(0, Point (2000*5, 1250*5), PolyFlags::Normal);
136             aXPoly.Insert(0, Point (1500*5, 1250*5), PolyFlags::Normal);
137             aXPoly.Insert(0, Point (1000*5, 1250*5), PolyFlags::Normal);
138             aXPoly.Insert(0, Point (500*5, 1250*5), PolyFlags::Normal);
139             aXPoly.Insert(0, Point (250*5, 1250*5), PolyFlags::Normal);
140             aXPoly.Insert(0, Point (50*5, 1250*5), PolyFlags::Normal);
141             aXPoly.Insert(0, Point (0,    1250*5), PolyFlags::Normal);
142 
143             ::basegfx::B2DPolygon aB2DPolygon(aXPoly.getB2DPolygon());
144             if(aB2DPolygon.areControlPointsUsed())
145             {
146                 aB2DPolygon = ::basegfx::utils::adaptiveSubdivideByAngle(aB2DPolygon);
147             }
148             p3DObj = new E3dLatheObj(
149                 mpView->getSdrModelFromSdrView(),
150                 mpView->Get3DDefaultAttributes(),
151                 ::basegfx::B2DPolyPolygon(aB2DPolygon));
152             break;
153         }
154 
155         case SID_3D_TORUS:
156         {
157             ::basegfx::B2DPolygon aB2DPolygon(::basegfx::utils::createPolygonFromCircle(::basegfx::B2DPoint(1000.0, 0.0), 500.0));
158             if(aB2DPolygon.areControlPointsUsed())
159             {
160                 aB2DPolygon = ::basegfx::utils::adaptiveSubdivideByAngle(aB2DPolygon);
161             }
162             p3DObj = new E3dLatheObj(
163                 mpView->getSdrModelFromSdrView(),
164                 mpView->Get3DDefaultAttributes(),
165                 ::basegfx::B2DPolyPolygon(aB2DPolygon));
166             break;
167         }
168 
169         case SID_3D_CYLINDER:
170         {
171             ::basegfx::B2DPolygon aInnerPoly;
172 
173             aInnerPoly.append(::basegfx::B2DPoint(0, 1000*5));
174             aInnerPoly.append(::basegfx::B2DPoint(50*5, 1000*5));
175             aInnerPoly.append(::basegfx::B2DPoint(100*5, 1000*5));
176             aInnerPoly.append(::basegfx::B2DPoint(200*5, 1000*5));
177             aInnerPoly.append(::basegfx::B2DPoint(300*5, 1000*5));
178             aInnerPoly.append(::basegfx::B2DPoint(400*5, 1000*5));
179             aInnerPoly.append(::basegfx::B2DPoint(450*5, 1000*5));
180             aInnerPoly.append(::basegfx::B2DPoint(500*5, 1000*5));
181             aInnerPoly.append(::basegfx::B2DPoint(500*5, -1000*5));
182             aInnerPoly.append(::basegfx::B2DPoint(450*5, -1000*5));
183             aInnerPoly.append(::basegfx::B2DPoint(400*5, -1000*5));
184             aInnerPoly.append(::basegfx::B2DPoint(300*5, -1000*5));
185             aInnerPoly.append(::basegfx::B2DPoint(200*5, -1000*5));
186             aInnerPoly.append(::basegfx::B2DPoint(100*5, -1000*5));
187             aInnerPoly.append(::basegfx::B2DPoint(50*5, -1000*5));
188             aInnerPoly.append(::basegfx::B2DPoint(0,    -1000*5));
189             aInnerPoly.setClosed(true);
190 
191             p3DObj = new E3dLatheObj(
192                 mpView->getSdrModelFromSdrView(),
193                 mpView->Get3DDefaultAttributes(),
194                 ::basegfx::B2DPolyPolygon(aInnerPoly));
195             break;
196         }
197 
198         case SID_3D_CONE:
199         {
200             ::basegfx::B2DPolygon aInnerPoly;
201 
202             aInnerPoly.append(::basegfx::B2DPoint(0, -1000*5));
203             aInnerPoly.append(::basegfx::B2DPoint(25*5, -900*5));
204             aInnerPoly.append(::basegfx::B2DPoint(50*5, -800*5));
205             aInnerPoly.append(::basegfx::B2DPoint(100*5, -600*5));
206             aInnerPoly.append(::basegfx::B2DPoint(200*5, -200*5));
207             aInnerPoly.append(::basegfx::B2DPoint(300*5, 200*5));
208             aInnerPoly.append(::basegfx::B2DPoint(400*5, 600*5));
209             aInnerPoly.append(::basegfx::B2DPoint(500*5, 1000*5));
210             aInnerPoly.append(::basegfx::B2DPoint(400*5, 1000*5));
211             aInnerPoly.append(::basegfx::B2DPoint(300*5, 1000*5));
212             aInnerPoly.append(::basegfx::B2DPoint(200*5, 1000*5));
213             aInnerPoly.append(::basegfx::B2DPoint(100*5, 1000*5));
214             aInnerPoly.append(::basegfx::B2DPoint(50*5, 1000*5));
215             aInnerPoly.append(::basegfx::B2DPoint(0,    1000*5));
216             aInnerPoly.setClosed(true);
217 
218             p3DObj = new E3dLatheObj(
219                 mpView->getSdrModelFromSdrView(),
220                 mpView->Get3DDefaultAttributes(),
221                 ::basegfx::B2DPolyPolygon(aInnerPoly));
222             break;
223         }
224 
225         case SID_3D_PYRAMID:
226         {
227             ::basegfx::B2DPolygon aInnerPoly;
228 
229             aInnerPoly.append(::basegfx::B2DPoint(0, -1000*5));
230             aInnerPoly.append(::basegfx::B2DPoint(25*5, -900*5));
231             aInnerPoly.append(::basegfx::B2DPoint(50*5, -800*5));
232             aInnerPoly.append(::basegfx::B2DPoint(100*5, -600*5));
233             aInnerPoly.append(::basegfx::B2DPoint(200*5, -200*5));
234             aInnerPoly.append(::basegfx::B2DPoint(300*5, 200*5));
235             aInnerPoly.append(::basegfx::B2DPoint(400*5, 600*5));
236             aInnerPoly.append(::basegfx::B2DPoint(500*5, 1000*5));
237             aInnerPoly.append(::basegfx::B2DPoint(400*5, 1000*5));
238             aInnerPoly.append(::basegfx::B2DPoint(300*5, 1000*5));
239             aInnerPoly.append(::basegfx::B2DPoint(200*5, 1000*5));
240             aInnerPoly.append(::basegfx::B2DPoint(100*5, 1000*5));
241             aInnerPoly.append(::basegfx::B2DPoint(50*5, 1000*5));
242             aInnerPoly.append(::basegfx::B2DPoint(0, 1000*5));
243             aInnerPoly.setClosed(true);
244 
245             p3DObj = new E3dLatheObj(
246                 mpView->getSdrModelFromSdrView(),
247                 mpView->Get3DDefaultAttributes(),
248                 ::basegfx::B2DPolyPolygon(aInnerPoly));
249             p3DObj->SetMergedItem(makeSvx3DHorizontalSegmentsItem(4));
250             break;
251         }
252     }
253 
254     return p3DObj;
255 }
256 
ImpPrepareBasic3DShape(E3dCompoundObject const * p3DObj,E3dScene * pScene)257 void FuConstruct3dObject::ImpPrepareBasic3DShape(E3dCompoundObject const * p3DObj, E3dScene *pScene)
258 {
259     Camera3D aCamera  = pScene->GetCamera ();
260 
261     // get transformed BoundVolume of the new object
262     basegfx::B3DRange aBoundVol;
263     basegfx::B3DRange aObjVol(p3DObj->GetBoundVolume());
264     aObjVol.transform(p3DObj->GetTransform());
265     aBoundVol.expand(aObjVol);
266     double fDeepth(aBoundVol.getDepth());
267 
268     aCamera.SetPRP(::basegfx::B3DPoint(0.0, 0.0, 1000.0));
269     aCamera.SetPosition(::basegfx::B3DPoint(0.0, 0.0, mpView->GetDefaultCamPosZ() + fDeepth / 2));
270     aCamera.SetFocalLength(mpView->GetDefaultCamFocal());
271     pScene->SetCamera(aCamera);
272     basegfx::B3DHomMatrix aTransformation;
273 
274     switch (nSlotId)
275     {
276         case SID_3D_CUBE:
277         {
278             aTransformation.rotate(DEG2RAD(20), 0.0, 0.0);
279         }
280         break;
281 
282         case SID_3D_SPHERE:
283         {
284         }
285         break;
286 
287         case SID_3D_SHELL:
288         case SID_3D_HALF_SPHERE:
289         {
290             aTransformation.rotate(DEG2RAD(200), 0.0, 0.0);
291         }
292         break;
293 
294         case SID_3D_CYLINDER:
295         case SID_3D_CONE:
296         case SID_3D_PYRAMID:
297         {
298         }
299         break;
300 
301         case SID_3D_TORUS:
302         {
303             aTransformation.rotate(DEG2RAD(90), 0.0, 0.0);
304         }
305         break;
306 
307         default:
308         {
309         }
310         break;
311     }
312 
313     pScene->SetTransform(aTransformation * pScene->GetTransform());
314 
315     SfxItemSet aAttr (mpViewShell->GetPool());
316     pScene->SetMergedItemSetAndBroadcast(aAttr);
317 }
318 
MouseButtonDown(const MouseEvent & rMEvt)319 bool FuConstruct3dObject::MouseButtonDown(const MouseEvent& rMEvt)
320 {
321     bool bReturn = FuConstruct::MouseButtonDown(rMEvt);
322 
323     if ( rMEvt.IsLeft() && !mpView->IsAction() )
324     {
325         Point aPnt( mpWindow->PixelToLogic( rMEvt.GetPosPixel() ) );
326 
327         mpWindow->CaptureMouse();
328         sal_uInt16 nDrgLog = sal_uInt16 ( mpWindow->PixelToLogic(Size(DRGPIX,0)).Width() );
329 
330         weld::WaitObject aWait(mpViewShell->GetFrameWeld());
331 
332         E3dCompoundObject* p3DObj = ImpCreateBasic3DShape();
333         E3dScene* pScene = mpView->SetCurrent3DObj(p3DObj);
334 
335         ImpPrepareBasic3DShape(p3DObj, pScene);
336         bReturn = mpView->BegCreatePreparedObject(aPnt, nDrgLog, pScene);
337 
338         SdrObject* pObj = mpView->GetCreateObj();
339 
340         if (pObj)
341         {
342             SfxItemSet aAttr(mpDoc->GetPool());
343             SetStyleSheet(aAttr, pObj);
344 
345             // extract LineStyle
346             aAttr.Put(XLineStyleItem (drawing::LineStyle_NONE));
347 
348             pObj->SetMergedItemSet(aAttr);
349         }
350     }
351 
352     return bReturn;
353 }
354 
MouseButtonUp(const MouseEvent & rMEvt)355 bool FuConstruct3dObject::MouseButtonUp(const MouseEvent& rMEvt)
356 {
357     bool bReturn = false;
358 
359     if ( mpView->IsCreateObj() && rMEvt.IsLeft() )
360     {
361         mpView->EndCreateObj(SdrCreateCmd::ForceEnd);
362         bReturn = true;
363     }
364 
365     bReturn = FuConstruct::MouseButtonUp(rMEvt) || bReturn;
366 
367     if (!bPermanent)
368         mpViewShell->GetViewFrame()->GetDispatcher()->Execute(SID_OBJECT_SELECT, SfxCallMode::ASYNCHRON);
369 
370     return bReturn;
371 }
372 
Activate()373 void FuConstruct3dObject::Activate()
374 {
375     mpView->SetCurrentObj(OBJ_NONE);
376 
377     FuConstruct::Activate();
378 }
379 
CreateDefaultObject(const sal_uInt16 nID,const::tools::Rectangle & rRectangle)380 SdrObjectUniquePtr FuConstruct3dObject::CreateDefaultObject(const sal_uInt16 nID, const ::tools::Rectangle& rRectangle)
381 {
382 
383     E3dCompoundObject* p3DObj = ImpCreateBasic3DShape();
384 
385     // E3dView::SetCurrent3DObj part
386     // get transformed BoundVolume of the object
387     basegfx::B3DRange aObjVol(p3DObj->GetBoundVolume());
388     aObjVol.transform(p3DObj->GetTransform());
389     basegfx::B3DRange aVolume(aObjVol);
390     double fW(aVolume.getWidth());
391     double fH(aVolume.getHeight());
392     ::tools::Rectangle a3DRect(0, 0, static_cast<::tools::Long>(fW), static_cast<::tools::Long>(fH));
393     std::unique_ptr< E3dScene, SdrObjectFreeOp > pScene(new E3dScene(*mpDoc));
394 
395     // copied code from E3dView::InitScene
396     double fCamZ(aVolume.getMaxZ() + ((fW + fH) / 4.0));
397     Camera3D aCam(pScene->GetCamera());
398     aCam.SetAutoAdjustProjection(false);
399     aCam.SetViewWindow(- fW / 2, - fH / 2, fW, fH);
400     ::basegfx::B3DPoint aLookAt;
401     double fDefaultCamPosZ = mpView->GetDefaultCamPosZ();
402     ::basegfx::B3DPoint aCamPos(0.0, 0.0, fCamZ < fDefaultCamPosZ ? fDefaultCamPosZ : fCamZ);
403     aCam.SetPosAndLookAt(aCamPos, aLookAt);
404     aCam.SetFocalLength(mpView->GetDefaultCamFocal());
405     pScene->SetCamera(aCam);
406     pScene->InsertObject(p3DObj);
407     pScene->NbcSetSnapRect(a3DRect);
408     ImpPrepareBasic3DShape(p3DObj, pScene.get());
409     SfxItemSet aAttr(mpDoc->GetPool());
410     SetStyleSheet(aAttr, p3DObj);
411     aAttr.Put(XLineStyleItem (drawing::LineStyle_NONE));
412     p3DObj->SetMergedItemSet(aAttr);
413 
414     // make object interactive at once
415     pScene->SetRectsDirty();
416 
417     // Take care of restrictions for the rectangle
418     ::tools::Rectangle aRect(rRectangle);
419 
420     switch(nID)
421     {
422         case SID_3D_CUBE:
423         case SID_3D_SPHERE:
424         case SID_3D_TORUS:
425         {
426             // force quadratic
427             ImpForceQuadratic(aRect);
428             break;
429         }
430 
431         case SID_3D_SHELL:
432         case SID_3D_HALF_SPHERE:
433         {
434             // force horizontal layout
435             break;
436         }
437 
438         case SID_3D_CYLINDER:
439         case SID_3D_CONE:
440         case SID_3D_PYRAMID:
441         {
442             // force vertical layout
443             break;
444         }
445     }
446 
447     // use changed rectangle, not original one
448     pScene->SetLogicRect(aRect);
449 
450     return SdrObjectUniquePtr(pScene.release());
451 }
452 
453 } // end of namespace sd
454 
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
456