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