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