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