563abdcd51a99f4995472e5d8d406e0cbcb7dbc1
[occt.git] / samples / mfc / standard / 09_Animation / src / AnimationView3D.cpp
1 // AnimationView3D.cpp : implementation of the CAnimationView3D class
2 //
3
4 #include "stdafx.h"
5
6 #include "AnimationView3D.h"
7
8 #include "AnimationApp.h"
9 #include "ShadingDialog.h"
10 #include "AnimationDoc.h"
11
12 #include "Sensitivity.h"
13
14 #ifdef _DEBUG
15 //#define new DEBUG_NEW by CasCade
16 #undef THIS_FILE
17 static char THIS_FILE[] = __FILE__;
18 #endif
19 static int rotCount = 0;
20
21 // for elastic bean selection
22 #define ValZWMin 1
23
24
25 /////////////////////////////////////////////////////////////////////////////
26 // CAnimationView3D
27
28 IMPLEMENT_DYNCREATE(CAnimationView3D, CView)
29
30 BEGIN_MESSAGE_MAP(CAnimationView3D, CView)
31         //{{AFX_MSG_MAP(CAnimationView3D)
32         ON_COMMAND(ID_BUTTONAxo, OnBUTTONAxo)
33         ON_COMMAND(ID_BUTTONBack, OnBUTTONBack)
34         ON_COMMAND(ID_BUTTONBottom, OnBUTTONBottom)
35         ON_COMMAND(ID_BUTTONFront, OnBUTTONFront)
36         ON_COMMAND(ID_BUTTONHlrOff, OnBUTTONHlrOff)
37         ON_COMMAND(ID_BUTTONHlrOn, OnBUTTONHlrOn)
38         ON_COMMAND(ID_BUTTONLeft, OnBUTTONLeft)
39         ON_COMMAND(ID_BUTTONPan, OnBUTTONPan)
40         ON_COMMAND(ID_BUTTONPanGlo, OnBUTTONPanGlo)
41         ON_COMMAND(ID_BUTTONReset, OnBUTTONReset)
42         ON_COMMAND(ID_BUTTONRight, OnBUTTONRight)
43         ON_COMMAND(ID_BUTTONRot, OnBUTTONRot)
44         ON_COMMAND(ID_BUTTONTop, OnBUTTONTop)
45         ON_COMMAND(ID_BUTTONZoomAll, OnBUTTONZoomAll)
46     ON_COMMAND(ID_FILE_EXPORT_IMAGE, OnFileExportImage)
47         ON_WM_SIZE()
48         ON_COMMAND(ID_BUTTONZoomProg, OnBUTTONZoomProg)
49         ON_COMMAND(ID_BUTTONZoomWin, OnBUTTONZoomWin)
50         ON_WM_LBUTTONDOWN()
51         ON_WM_LBUTTONUP()
52         ON_WM_MBUTTONDOWN()
53         ON_WM_MBUTTONUP()
54         ON_WM_MOUSEMOVE()
55         ON_WM_RBUTTONDOWN()
56         ON_WM_RBUTTONUP()
57         ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOff, OnUpdateBUTTONHlrOff)
58         ON_UPDATE_COMMAND_UI(ID_BUTTONHlrOn, OnUpdateBUTTONHlrOn)
59         ON_UPDATE_COMMAND_UI(ID_BUTTONPanGlo, OnUpdateBUTTONPanGlo)
60         ON_UPDATE_COMMAND_UI(ID_BUTTONPan, OnUpdateBUTTONPan)
61         ON_UPDATE_COMMAND_UI(ID_BUTTONZoomProg, OnUpdateBUTTONZoomProg)
62         ON_UPDATE_COMMAND_UI(ID_BUTTONZoomWin, OnUpdateBUTTONZoomWin)
63         ON_UPDATE_COMMAND_UI(ID_BUTTONRot, OnUpdateBUTTONRot)
64
65         ON_COMMAND(ID_Modify_ChangeBackground     , OnChangeBackground)
66         ON_WM_TIMER()
67         ON_COMMAND(ID_STOP, OnStop)
68         ON_COMMAND(ID_RESTART, OnRestart)
69
70         ON_COMMAND(ID_SENSITIVITY, OnSensitivity)
71         ON_COMMAND(ID_BUTTONFly, OnBUTTONFly)
72         ON_COMMAND(ID_BUTTONTurn, OnBUTTONTurn)
73         ON_UPDATE_COMMAND_UI(ID_BUTTONFly, OnUpdateBUTTONFly)
74         ON_UPDATE_COMMAND_UI(ID_BUTTONTurn, OnUpdateBUTTONTurn)
75         ON_COMMAND(ID_VIEW_DISPLAYSTATUS, OnViewDisplaystatus)
76         ON_UPDATE_COMMAND_UI(ID_VIEW_DISPLAYSTATUS, OnUpdateViewDisplaystatus)
77         //}}AFX_MSG_MAP
78 // CasCade
79
80 END_MESSAGE_MAP()
81
82 /////////////////////////////////////////////////////////////////////////////
83 // CAnimationView3D construction/destruction
84
85 CAnimationView3D::CAnimationView3D()
86 : myXmin (0),
87   myYmin (0),
88   myXmax (0),
89   myYmax (0),
90   myCurZoom (0.0),
91   myHlrModeIsOn (Standard_False),
92   myCurrentMode  (CurrentAction3d_Nothing),
93   m_FlySens  (500.0),
94   m_TurnSens (M_PI / 40.0),
95   m_Pen (NULL)
96 {
97   // TODO: add construction code here
98 }
99
100 CAnimationView3D::~CAnimationView3D()
101 {
102     myView->Remove();
103     if (m_Pen) delete m_Pen;
104 }
105
106 BOOL CAnimationView3D::PreCreateWindow(CREATESTRUCT& cs)
107 {
108   // TODO: Modify the Window class or styles here by modifying
109   //  the CREATESTRUCT cs
110   cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC, ::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
111   return CView::PreCreateWindow(cs);
112 }
113
114 /////////////////////////////////////////////////////////////////////////////
115 // CAnimationView3D drawing
116
117 void CAnimationView3D::OnDraw(CDC* /*pDC*/)
118 {
119         CAnimationDoc* pDoc = GetDocument();
120         ASSERT_VALID(pDoc);
121
122         // TODO: add draw code for native data here
123
124           myView->Redraw();
125
126 }
127 void CAnimationView3D::OnInitialUpdate() 
128 {
129   CView::OnInitialUpdate();
130
131   // TODO: Add your specialized code here and/or call the base class
132   //    myView = GetDocument()->GetViewer()->CreateView();
133
134   Handle(V3d_Viewer) aViewer;
135
136   aViewer = GetDocument()->GetViewer();
137   aViewer->SetDefaultTypeOfView (V3d_PERSPECTIVE);
138
139   myView = aViewer->CreateView();
140
141   // store for restore state after rotation (witch is in Degenerated mode)
142   myHlrModeIsOn = myView->ComputedMode();
143
144   Handle(WNT_Window) aWNTWindow = new WNT_Window (GetSafeHwnd());
145   myView->SetWindow(aWNTWindow);
146   if (!aWNTWindow->IsMapped()) aWNTWindow->Map();
147
148   // store the mode ( nothing , dynamic zooming, dynamic ... )
149   myCurrentMode = CurrentAction3d_Nothing;
150   CFrameWnd* pParentFrm = GetParentFrame();
151   pParentFrm->ActivateFrame(SW_SHOWMAXIMIZED);
152
153   Standard_Integer w=100 , h=100 ;   /* Debug Matrox                         */
154   aWNTWindow->Size (w,h) ;           /* Keeps me unsatisfied (rlb).....      */
155                                    /* Resize is not supposed to be done on */
156                                    /* Matrox                               */
157                                    /* I suspect another problem elsewhere  */
158   ::PostMessage ( GetSafeHwnd () , WM_SIZE , SIZE_RESTORED , w + h*65536 ) ;
159
160   m_Tune.Create ( IDD_TUNE , NULL ) ;
161
162   RECT dlgrect;
163   m_Tune.GetWindowRect(&dlgrect);
164   LONG width = dlgrect.right-dlgrect.left;
165   LONG height = dlgrect.bottom-dlgrect.top;
166   RECT MainWndRect;
167   AfxGetApp()->m_pMainWnd->GetWindowRect(&MainWndRect);
168   LONG left = MainWndRect.left+3;
169   LONG top = MainWndRect.top + 112;
170   m_Tune.MoveWindow(left,top,width,height);
171
172   m_Tune.m_pView = this ;
173
174   m_Tune.ShowWindow ( SW_HIDE );
175
176   // store the mode ( nothing , dynamic zooming, dynamic ... )
177
178   myCurrentMode = CurrentAction3d_Nothing;
179
180   ReloadData () ;
181
182 }
183
184 void CAnimationView3D::DisplayTuneDialog()
185 {
186         m_Tune.Create ( IDD_TUNE , NULL ) ;
187         
188         RECT dlgrect;
189         m_Tune.GetWindowRect(&dlgrect);
190         LONG width = dlgrect.right-dlgrect.left;
191         LONG height = dlgrect.bottom-dlgrect.top;
192         RECT MainWndRect;
193         AfxGetApp()->m_pMainWnd->GetWindowRect(&MainWndRect);
194         LONG left = MainWndRect.left+3;
195         LONG top = MainWndRect.top + 112;
196         m_Tune.MoveWindow(left,top,width,height);
197         
198         m_Tune.m_pView = this ;
199 }
200
201
202 /////////////////////////////////////////////////////////////////////////////
203 // CAnimationView3D diagnostics
204
205 #ifdef _DEBUG
206 void CAnimationView3D::AssertValid() const
207 {
208         CView::AssertValid();
209 }
210
211 void CAnimationView3D::Dump(CDumpContext& dc) const
212 {
213         CView::Dump(dc);
214 }
215
216 CAnimationDoc* CAnimationView3D::GetDocument() // non-debug version is inline
217 {
218         ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAnimationDoc)));
219         return (CAnimationDoc*)m_pDocument;
220 }
221 #endif //_DEBUG
222
223 /////////////////////////////////////////////////////////////////////////////
224 // CAnimationView3D message handlers
225 void CAnimationView3D::OnFileExportImage()
226 {
227   GetDocument()->ExportView (myView);
228 }
229
230 void CAnimationView3D::OnSize(UINT nType, int cx, int cy)
231 {
232   CView::OnSize (nType, cx, cy);
233   m_cx = cx ;
234   m_cy = cy ;
235   if (!myView.IsNull())
236     myView->MustBeResized();
237 }
238
239 void CAnimationView3D::OnBUTTONBack() 
240 { myView->SetProj(V3d_Ypos); } // See the back View
241 void CAnimationView3D::OnBUTTONFront() 
242 { myView->SetProj(V3d_Yneg); } // See the front View
243
244 void CAnimationView3D::OnBUTTONBottom() 
245 { myView->SetProj(V3d_Zneg); } // See the bottom View
246 void CAnimationView3D::OnBUTTONTop() 
247 { myView->SetProj(V3d_Zpos); } // See the top View      
248
249 void CAnimationView3D::OnBUTTONLeft() 
250 { myView->SetProj(V3d_Xneg); } // See the left View     
251 void CAnimationView3D::OnBUTTONRight() 
252 { myView->SetProj(V3d_Xpos); } // See the right View
253
254 void CAnimationView3D::OnBUTTONAxo() 
255 { myView->SetProj(V3d_XposYnegZpos); } // See the axonometric View
256
257 void CAnimationView3D::OnBUTTONHlrOff() 
258 {
259   myHlrModeIsOn = Standard_False;
260   myView->SetComputedMode (myHlrModeIsOn);
261 }
262
263 void CAnimationView3D::OnBUTTONHlrOn() 
264 {
265   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
266   myHlrModeIsOn = Standard_True;
267   myView->SetComputedMode (myHlrModeIsOn);
268   SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
269 }
270
271 void CAnimationView3D::OnBUTTONPan() 
272 {  
273         myCurrentMode = CurrentAction3d_DynamicPanning; 
274 }
275
276 void CAnimationView3D::OnBUTTONPanGlo() 
277 {
278   // save the current zoom value 
279   myCurZoom = myView->Scale();
280   // Do a Global Zoom 
281   myView->FitAll();
282   // Set the mode 
283   myCurrentMode = CurrentAction3d_GlobalPanning;
284 }
285
286 void CAnimationView3D::OnBUTTONReset() 
287 {   myView->Reset(); 
288         ReloadData();
289 }
290
291 void CAnimationView3D::OnBUTTONRot() 
292 {   myCurrentMode = CurrentAction3d_DynamicRotation; }
293
294
295 void CAnimationView3D::OnBUTTONZoomAll() 
296 {
297         SetDimensions();
298         myView->FitAll();
299         myView->ZFitAll();
300 }
301
302 void CAnimationView3D::OnBUTTONZoomProg() 
303 {  myCurrentMode = CurrentAction3d_DynamicZooming; }
304
305 void CAnimationView3D::OnBUTTONZoomWin() 
306 {  myCurrentMode = CurrentAction3d_WindowZooming; }
307
308 void CAnimationView3D::OnBUTTONFly() 
309 {  myCurrentMode = CurrentAction3d_Fly; }
310
311 void CAnimationView3D::OnBUTTONTurn() 
312 {  myCurrentMode = CurrentAction3d_Turn; }
313
314
315 void CAnimationView3D::OnLButtonDown(UINT nFlags, CPoint point) 
316 {
317   //  save the current mouse coordinate in min 
318   myXmin=point.x;  myYmin=point.y;
319   myXmax=point.x;  myYmax=point.y;
320
321   if ( nFlags & MK_CONTROL ) 
322           {
323             // Button MB1 down Control :start zomming 
324         // SetCursor(AfxGetApp()->LoadStandardCursor());
325           }
326         else // if ( Ctrl )
327           {
328         switch (myCurrentMode)
329         {
330          case CurrentAction3d_Nothing : // start a drag
331            if (nFlags & MK_SHIFT)
332                 GetDocument()->ShiftDragEvent(myXmax,myYmax,-1,myView);
333            else
334                 GetDocument()->DragEvent(myXmax,myYmax,-1,myView);
335         break;
336          break;
337          case CurrentAction3d_DynamicZooming : // noting
338              // SetCursor(AfxGetApp()->LoadStandardCursor());
339          break;
340          case CurrentAction3d_WindowZooming : // noting
341          break;
342          case CurrentAction3d_DynamicPanning :// noting
343          break;
344          case CurrentAction3d_GlobalPanning :// noting
345         break;
346         case  CurrentAction3d_DynamicRotation :
347           if (myHlrModeIsOn)
348           {
349             myView->SetComputedMode (Standard_False);
350           }
351           myView->StartRotation (point.x, point.y);
352         break;
353                 case  CurrentAction3d_Fly :
354                         KillTimer (1) ;
355                         SetTimer ( 1 , 100 , NULL ) ;
356                 break ;
357         case  CurrentAction3d_Turn :
358                         KillTimer (1) ;
359                         SetTimer ( 1 , 100 , NULL ) ;
360                 break ;
361         default :
362            Standard_Failure::Raise(" incompatible Current Mode ");
363         break;
364         }
365     }
366 }
367
368 void CAnimationView3D::OnLButtonUp(UINT nFlags, CPoint point) 
369 {
370    if ( nFlags & MK_CONTROL ) 
371           {
372         return;
373           }
374         else // if ( Ctrl )
375           {
376         switch (myCurrentMode)
377         {
378          case CurrentAction3d_Nothing :
379          if (point.x == myXmin && point.y == myYmin)
380          { // no offset between down and up --> selectEvent
381             myXmax=point.x;  
382             myYmax=point.y;
383             if (nFlags & MK_SHIFT )
384               GetDocument()->ShiftInputEvent(point.x,point.y,myView);
385             else
386               GetDocument()->InputEvent     (point.x,point.y,myView);
387          } else
388          {
389             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
390             myXmax=point.x;  
391             myYmax=point.y;
392                     if (nFlags & MK_SHIFT)
393                                 GetDocument()->ShiftDragEvent(point.x,point.y,1,myView);
394                         else
395                                 GetDocument()->DragEvent(point.x,point.y,1,myView);
396          }
397          break;
398          case CurrentAction3d_DynamicZooming :
399              // SetCursor(AfxGetApp()->LoadStandardCursor());         
400                myCurrentMode = CurrentAction3d_Nothing;
401          break;
402          case CurrentAction3d_WindowZooming :
403            myXmax=point.x;            myYmax=point.y;
404             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
405                if ((abs(myXmin-myXmax)>ValZWMin) || (abs(myYmin-myYmax)>ValZWMin))
406                                          // Test if the zoom window is greater than a minimale window.
407                         {
408                           // Do the zoom window between Pmin and Pmax
409                           myView->WindowFitAll(myXmin,myYmin,myXmax,myYmax);  
410                         }  
411                myCurrentMode = CurrentAction3d_Nothing;
412          break;
413          case CurrentAction3d_DynamicPanning :
414            myCurrentMode = CurrentAction3d_Nothing;
415          break;
416          case CurrentAction3d_GlobalPanning :
417                myView->Place(point.x,point.y,myCurZoom); 
418                myCurrentMode = CurrentAction3d_Nothing;
419         break;
420         case  CurrentAction3d_DynamicRotation :
421                myCurrentMode = CurrentAction3d_Nothing;
422         break;
423                 case  CurrentAction3d_Fly :
424                         KillTimer ( 1 ) ;
425         case  CurrentAction3d_Turn :
426                         KillTimer ( 1 ) ;
427                 break;
428         default :
429            Standard_Failure::Raise(" incompatible Current Mode ");
430         break;
431         } //switch (myCurrentMode)
432     } //        else // if ( Ctrl )
433 }
434
435 void CAnimationView3D::OnMButtonDown(UINT nFlags, CPoint /*point*/) 
436 {
437    if ( nFlags & MK_CONTROL ) 
438           {
439         // Button MB2 down Control : panning init  
440         // SetCursor(AfxGetApp()->LoadStandardCursor());   
441           }
442 }
443
444 void CAnimationView3D::OnMButtonUp(UINT nFlags, CPoint /*point*/) 
445 {
446    if ( nFlags & MK_CONTROL ) 
447           {
448         // Button MB2 down Control : panning init  
449         // SetCursor(AfxGetApp()->LoadStandardCursor());   
450           }
451 }
452
453 void CAnimationView3D::OnRButtonDown(UINT nFlags, CPoint point) 
454 {
455   if ( nFlags & MK_CONTROL )
456   {
457     // SetCursor(AfxGetApp()->LoadStandardCursor());
458     if (myHlrModeIsOn)
459     {
460       myView->SetComputedMode (Standard_False);
461     }
462     myView->StartRotation (point.x, point.y);
463   }
464   else // if ( Ctrl )
465   {
466     GetDocument()->Popup(point.x,point.y,myView);
467   }
468 }
469
470 void CAnimationView3D::OnRButtonUp(UINT /*nFlags*/, CPoint /*point*/) 
471 {
472     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
473     myView->SetComputedMode (myHlrModeIsOn);
474     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
475 }
476
477 void CAnimationView3D::OnMouseMove(UINT nFlags, CPoint point) 
478 {
479     //   ============================  LEFT BUTTON =======================
480   m_curx = point.x ;
481   m_cury = point.y ;
482
483   if ( nFlags & MK_LBUTTON)
484     {
485      if ( nFlags & MK_CONTROL ) 
486           {
487             // move with MB1 and Control : on the dynamic zooming  
488             // Do the zoom in function of mouse's coordinates  
489             myView->Zoom(myXmax,myYmax,point.x,point.y); 
490             // save the current mouse coordinate in min 
491                 myXmax = point.x; 
492         myYmax = point.y;       
493           }
494           else // if ( Ctrl )
495           {
496         switch (myCurrentMode)
497         {
498          case CurrentAction3d_Nothing :
499                    myXmax = point.x;            myYmax = point.y;
500             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False);
501            if (nFlags & MK_SHIFT)               
502              GetDocument()->ShiftDragEvent(myXmax,myYmax,0,myView);
503            else
504              GetDocument()->DragEvent(myXmax,myYmax,0,myView);
505             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True);
506           break;
507          case CurrentAction3d_DynamicZooming :
508                myView->Zoom(myXmax,myYmax,point.x,point.y); 
509                // save the current mouse coordinate in min \n";
510                myXmax=point.x;  myYmax=point.y;
511          break;
512          case CurrentAction3d_WindowZooming :
513                    myXmax = point.x; myYmax = point.y;  
514             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_False,LongDash);
515             DrawRectangle(myXmin,myYmin,myXmax,myYmax,Standard_True,LongDash);
516          break;
517          case CurrentAction3d_DynamicPanning :
518                    myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
519                    myXmax = point.x; myYmax = point.y;  
520          break;
521          case CurrentAction3d_GlobalPanning : // nothing           
522         break;
523         case  CurrentAction3d_DynamicRotation :
524           myView->Rotation(point.x,point.y);
525               myView->Redraw();
526         break;
527                 case CurrentAction3d_Fly :
528                         break ;
529                 case CurrentAction3d_Turn :
530                         break ;
531         default :
532            Standard_Failure::Raise(" incompatible Current Mode ");
533         break;
534         }//  switch (myCurrentMode)
535       }// if ( nFlags & MK_CONTROL )  else 
536     } else //   if ( nFlags & MK_LBUTTON) 
537     //   ============================  MIDDLE BUTTON =======================
538     if ( nFlags & MK_MBUTTON)
539     {
540      if ( nFlags & MK_CONTROL ) 
541           {
542                 myView->Pan(point.x-myXmax,myYmax-point.y); // Realize the panning
543                 myXmax = point.x; myYmax = point.y;     
544
545           }
546     } else //  if ( nFlags & MK_MBUTTON)
547     //   ============================  RIGHT BUTTON =======================
548     if ( nFlags & MK_RBUTTON)
549     {
550      if ( nFlags & MK_CONTROL ) 
551           {
552              rotCount++;
553          myView->Rotation(point.x,point.y);
554           }
555     }else //if ( nFlags & MK_RBUTTON)
556     //   ============================  NO BUTTON =======================
557     {  // No buttons 
558           myXmax = point.x; myYmax = point.y;   
559           if (nFlags & MK_SHIFT)
560                 GetDocument()->ShiftMoveEvent(point.x,point.y,myView);
561           else
562                 GetDocument()->MoveEvent(point.x,point.y,myView);
563    }
564 }
565
566 void CAnimationView3D::OnUpdateBUTTONHlrOff(CCmdUI* pCmdUI) 
567 {
568   pCmdUI->SetCheck (!myHlrModeIsOn);
569   pCmdUI->Enable   (myHlrModeIsOn);
570 }
571
572 void CAnimationView3D::OnUpdateBUTTONHlrOn(CCmdUI* pCmdUI) 
573 {
574   pCmdUI->SetCheck (myHlrModeIsOn);
575   pCmdUI->Enable   (!myHlrModeIsOn);
576 }
577
578 void CAnimationView3D::OnUpdateBUTTONPanGlo(CCmdUI* pCmdUI) 
579 {
580     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_GlobalPanning);
581         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_GlobalPanning);      
582         
583 }
584
585 void CAnimationView3D::OnUpdateBUTTONPan(CCmdUI* pCmdUI) 
586 {
587     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_DynamicPanning);
588         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_DynamicPanning );    
589 }
590
591 void CAnimationView3D::OnUpdateBUTTONZoomProg(CCmdUI* pCmdUI) 
592 {
593     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_DynamicZooming );
594         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_DynamicZooming);     
595 }
596
597 void CAnimationView3D::OnUpdateBUTTONZoomWin(CCmdUI* pCmdUI) 
598 {
599     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_WindowZooming);
600         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_WindowZooming);      
601 }
602
603 void CAnimationView3D::OnUpdateBUTTONRot(CCmdUI* pCmdUI) 
604 {
605     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_DynamicRotation);
606         pCmdUI->Enable   (myCurrentMode != CurrentAction3d_DynamicRotation);    
607 }
608
609 void CAnimationView3D::OnUpdateBUTTONFly(CCmdUI* pCmdUI) 
610 {
611         pCmdUI->Enable(GetDocument()->m_bIsGridLoaded);
612     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_Fly);
613 }
614
615 void CAnimationView3D::OnUpdateBUTTONTurn(CCmdUI* pCmdUI) 
616 {
617         pCmdUI->Enable(GetDocument()->m_bIsGridLoaded);
618     pCmdUI->SetCheck (myCurrentMode == CurrentAction3d_Turn);
619 }
620
621 void CAnimationView3D::OnChangeBackground() 
622 {
623         Standard_Real R1;
624         Standard_Real G1;
625         Standard_Real B1;
626     myView->BackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
627         COLORREF m_clr ;
628         m_clr = RGB(R1*255,G1*255,B1*255);
629
630         CColorDialog dlgColor(m_clr);
631         if (dlgColor.DoModal() == IDOK)
632         {
633                 m_clr = dlgColor.GetColor();
634                 R1 = GetRValue(m_clr)/255.;
635                 G1 = GetGValue(m_clr)/255.;
636                 B1 = GetBValue(m_clr)/255.;
637         myView->SetBackgroundColor(Quantity_TOC_RGB,R1,G1,B1);
638         }
639     myView->Redraw();
640 }
641
642 //==========================================================================================
643 //==========================================================================================
644 //==========================================================================================
645
646 //-----------------------------------------------------------------------------------------
647 //
648 //-----------------------------------------------------------------------------------------
649 void CAnimationView3D::DrawRectangle(const Standard_Integer  MinX    ,
650                                                             const Standard_Integer  MinY    ,
651                                         const Standard_Integer  MaxX ,
652                                                             const Standard_Integer  MaxY ,
653                                                             const Standard_Boolean  Draw , 
654                                         const LineStyle aLineStyle)
655 {
656     static int m_DrawMode;
657     if  (!m_Pen && aLineStyle ==Solid )
658         {m_Pen = new CPen(PS_SOLID, 1, RGB(0,0,0)); m_DrawMode = R2_MERGEPENNOT;}
659     else if (!m_Pen && aLineStyle ==Dot )
660         {m_Pen = new CPen(PS_DOT, 1, RGB(0,0,0));   m_DrawMode = R2_XORPEN;}
661     else if (!m_Pen && aLineStyle == ShortDash)
662         {m_Pen = new CPen(PS_DASH, 1, RGB(255,0,0));    m_DrawMode = R2_XORPEN;}
663     else if (!m_Pen && aLineStyle == LongDash)
664         {m_Pen = new CPen(PS_DASH, 1, RGB(0,0,0));      m_DrawMode = R2_NOTXORPEN;}
665     else if (aLineStyle == Default) 
666         { m_Pen = NULL; m_DrawMode = R2_MERGEPENNOT;}
667
668     CPen* aOldPen = NULL;
669     CClientDC clientDC(this);
670     if (m_Pen) aOldPen = clientDC.SelectObject(m_Pen);
671     clientDC.SetROP2(m_DrawMode);
672
673     static              Standard_Integer StoredMinX, StoredMaxX, StoredMinY, StoredMaxY;
674     static              Standard_Boolean m_IsVisible;
675
676     if ( m_IsVisible && !Draw) // move or up  : erase at the old position 
677     {
678      clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
679      clientDC.LineTo(StoredMaxX,StoredMaxY); 
680          clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
681      m_IsVisible = false;
682     }
683
684     StoredMinX = Min ( MinX, MaxX );
685     StoredMinY = Min ( MinY, MaxY );
686     StoredMaxX = Max ( MinX, MaxX );
687     StoredMaxY = Max ( MinY, MaxY);
688
689     if (Draw) // move : draw
690     {
691      clientDC.MoveTo(StoredMinX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMaxY); 
692      clientDC.LineTo(StoredMaxX,StoredMaxY); 
693          clientDC.LineTo(StoredMaxX,StoredMinY); clientDC.LineTo(StoredMinX,StoredMinY);
694      m_IsVisible = true;
695    }
696
697    if (m_Pen) 
698        clientDC.SelectObject(aOldPen);
699 }
700 void CAnimationView3D::OnStop() 
701 {
702         KillTimer(GetDocument()->myCount);            
703 }
704
705 void CAnimationView3D::OnRestart() 
706 {
707         KillTimer(GetDocument()->myCount);            
708         SetTimer(GetDocument()->myCount, 1 , NULL); 
709 }
710
711 /*
712 void CAnimationView3D::OnTimer(UINT nIDEvent) 
713 {
714         // TODO: Add your message handler code here and/or call default
715         GetDocument()->OnMyTimer();
716         CView::OnTimer(nIDEvent);
717 }
718 */
719
720
721 /*********************************************************************************
722 **************  W A L K  T H R O U G H  ******************************************
723 /********************************************************************************/
724
725 void CAnimationView3D::OnTimer(UINT_PTR nIDEvent) 
726 {
727         if ( !GetDocument()->m_bIsGridLoaded )
728         {
729                 // TODO: Add your message handler code here and/or call default
730                 GetDocument()->OnMyTimer();
731                 CView::OnTimer(nIDEvent);
732         }
733         else
734         {
735                 CView::OnTimer(nIDEvent);
736                 if ( nIDEvent == 1 ) {
737                   myView->SetImmediateUpdate ( Standard_False ) ;
738                   if ( myCurrentMode == CurrentAction3d_Fly ) {
739
740                          Fly  ( m_curx , m_cury ) ;
741                          if ( m_bShift )
742                            Roll ( m_curx , m_cury ) ;
743                          else
744                            Turn ( m_curx , m_cury ) ;
745
746                         myView->SetAt  ( m_Atx  , m_Aty  , m_Atz  ) ;
747                         myView->SetEye ( m_Eyex , m_Eyey , m_Eyez ) ;
748
749                   }
750                   else if ( myCurrentMode == CurrentAction3d_Turn ) {
751                            Twist ( m_curx , m_cury ) ;
752                   }
753                   else
754                           KillTimer (1) ;
755
756
757                   myView->SetImmediateUpdate ( Standard_True ) ;
758
759                   myView->Update ();
760                 }
761
762                 ReloadData () ;
763         }
764 }
765
766 void CAnimationView3D::OnSensitivity() 
767 {
768         CSensitivity dial ;
769
770         dial.m_SensFly   = m_FlySens  ;
771         dial.m_SensTurn  = m_TurnSens ;
772         if ( dial.DoModal () ) {
773                 m_FlySens  = dial.m_SensFly   ;
774         m_TurnSens = dial.m_SensTurn  ;
775         }
776 }
777
778 void CAnimationView3D::Fly (int /*x*/ , int y)
779 {
780         double v [3] ;
781         double l ;
782         double sens ;
783         int    i     ;
784
785         sens = (double) myYmin - (double) y ;
786         sens /= (double) m_cy ;
787         sens *= m_FlySens ;
788
789         v [0] = m_Atx - m_Eyex ;
790         v [1] = m_Aty - m_Eyey ;
791         v [2] = m_Atz - m_Eyez ;
792         l = sqrt ( v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ) ;
793         if ( l > 1.e-3 ) {
794                 for ( i=0 ; i<3 ; i++ )
795           v [i] = v [i] / l * sens ;
796
797                 m_Atx += v [0] ;
798                 m_Aty += v [1] ;
799                 m_Atz += v [2] ;
800
801                 m_Eyex += v [0] ;
802                 m_Eyey += v [1] ;
803                 m_Eyez += v [2] ;
804
805         }
806 }
807
808 /* Rotation */
809
810 void CAnimationView3D::Turn (int x , int /*y*/)
811 {
812         gp_Vec z (0.,0.,1.) ;
813
814         double v [3] ;
815         double sens ;
816         double aX , aY , aZ ;
817
818         sens = (double) x - (double) myXmin ;
819         sens /= (double) m_cx ;
820         sens *= m_TurnSens ;
821
822         v [0] = m_Atx - m_Eyex ;
823         v [1] = m_Aty - m_Eyey ;
824         v [2] = m_Atz - m_Eyez ;
825
826         gp_Pnt eye ( m_Eyex , m_Eyey , m_Eyez ) ;
827
828         gp_Vec reg (v[0],v[1],v[2] );
829
830         gp_Vec vert = reg ^ z ;
831         gp_Vec haut = vert ^ reg ;
832
833         gp_Dir dh (haut) ;
834         gp_Ax1 rot (eye,dh);
835
836         reg.Rotate (rot,sens) ;
837
838         reg.Coord ( aX , aY , aZ ) ;
839
840         m_Atx = m_Eyex + aX ;
841         m_Aty = m_Eyey + aY ;
842         m_Atz = m_Eyez + aZ ;
843 }
844
845 void CAnimationView3D::Roll (int x , int /*y*/)
846 {
847         gp_Vec z (0.,0.,1.) ;
848
849         double v [3] ;
850         double sens ;
851         double aX , aY , aZ ;
852
853         sens = (double) x - (double) myXmin ;
854         sens /= (double) m_cx ;
855         sens *= m_TurnSens ;
856
857         v [0] = m_Atx - m_Eyex ;
858         v [1] = m_Aty - m_Eyey ;
859         v [2] = m_Atz - m_Eyez ;
860
861         gp_Pnt eye ( m_Eyex , m_Eyey , m_Eyez ) ;
862
863         gp_Vec reg (v[0],v[1],v[2] );
864
865         gp_Vec vert = reg ^ z ;
866
867         gp_Dir dh (vert) ;
868         gp_Ax1 rot (eye,dh);
869
870         reg.Rotate (rot,sens) ;
871
872         reg.Coord ( aX , aY , aZ ) ;
873
874         m_Atx = m_Eyex + aX ;
875         m_Aty = m_Eyey + aY ;
876         m_Atz = m_Eyez + aZ ;
877 }
878
879 void CAnimationView3D::Twist (int x , int /*y*/)
880 {
881         double sens ;
882         double a ;
883         
884         a = myView->Twist () ;
885
886         sens = (double) x - (double) myXmin ;
887         sens /= (double) m_cx ;
888         sens *= m_TurnSens ;
889
890         a += sens ;
891
892         myView->SetTwist (a) ;
893 }
894
895 //=============================================================================
896 // function: SetFocal
897 // purpose:
898 //=============================================================================
899 void CAnimationView3D::SetFocal (double theFocus, double theAngle)
900 {
901
902   Handle(Graphic3d_Camera) aCamera = myView->Camera();
903
904   gp_Pnt anAt  = aCamera->Center();
905   gp_Pnt anEye = aCamera->Eye();
906
907   gp_Vec aLook (anAt, anEye);
908
909   if (aCamera->Distance() > 1.e-3)
910   {
911     aLook = aLook / aCamera->Distance() * theFocus;
912
913     m_Focus = theFocus;
914
915     anAt.SetX (aLook.X() + anEye.X());
916     anAt.SetY (aLook.Y() + anEye.Y());
917     anAt.SetZ (aLook.Z() + anEye.Z());
918
919     m_dAngle = theAngle;
920
921     aCamera->SetCenter (anAt);
922     aCamera->SetFOVy (theAngle);
923
924     myView->Update();
925   }
926 }
927
928 void CAnimationView3D::ReloadData()
929 {
930         myView->At  ( m_Atx  , m_Aty  , m_Atz  ) ;
931         myView->Eye ( m_Eyex , m_Eyey , m_Eyez ) ;
932   double dTwist = myView->Twist() * 180. / M_PI;
933
934   CString aMsg;
935   aMsg.Format (L"%lf", m_Atx);
936         m_Tune.GetDlgItem (IDC_XAT)->SetWindowText (aMsg);
937   aMsg.Format (L"%lf", m_Aty);
938         m_Tune.GetDlgItem (IDC_YAT)->SetWindowText (aMsg);
939         aMsg.Format (L"%lf", m_Atz);
940         m_Tune.GetDlgItem (IDC_ZAT)->SetWindowText (aMsg);
941
942   aMsg.Format (L"%lf", m_Eyex);
943         m_Tune.GetDlgItem (IDC_XEYE)->SetWindowText (aMsg);
944         aMsg.Format (L"%lf", m_Eyey);
945         m_Tune.GetDlgItem (IDC_YEYE)->SetWindowText (aMsg);
946         aMsg.Format (L"%lf", m_Eyez);
947         m_Tune.GetDlgItem (IDC_ZEYE)->SetWindowText (aMsg);
948
949   aMsg.Format (L"%lf", dTwist);
950         m_Tune.GetDlgItem (IDC_TWIST)->SetWindowText (aMsg);
951
952         double dx,dy,dz ;
953         dx = m_Atx - m_Eyex ;
954         dy = m_Aty - m_Eyey ;
955         dz = m_Atz - m_Eyez ;
956
957   m_Focus = sqrt (dx * dx + dy * dy + dz * dz);
958
959   m_dAngle = myView->Camera()->FOVy();
960
961         m_Tune.m_dAngle = m_dAngle ;
962         m_Tune.m_dFocus = m_Focus  ;
963         m_Tune.UpdateData ( FALSE ) ;
964 }
965
966 void CAnimationView3D::SetDimensions()
967 {
968
969   CAnimationDoc* pDoc = GetDocument();
970
971   myView->SetImmediateUpdate ( Standard_False ) ;
972
973   m_Atx  = ( pDoc->m_Xmin + pDoc->m_Xmax ) / 2. ;
974   m_Aty  = ( pDoc->m_Ymin + pDoc->m_Ymax ) / 2. ;
975   m_Atz  = ( pDoc->m_Zmin + pDoc->m_Zmax ) / 2. ;
976   m_Eyex = pDoc->m_Xmax ;
977   m_Eyey = pDoc->m_Ymax ;
978   m_Eyez = pDoc->m_Zmax ;
979
980   myView->SetAt    ( m_Atx  , m_Aty  , m_Atz  ) ;
981   myView->SetEye   ( m_Eyex , m_Eyey , m_Eyez ) ;
982   myView->SetTwist (0.) ;
983
984   myView->SetImmediateUpdate ( Standard_False ) ;
985   myView->FitAll();
986   myView->SetImmediateUpdate ( Standard_False ) ;
987   myView->ZFitAll();
988
989   myView->SetImmediateUpdate ( Standard_True ) ;
990
991   ReloadData () ;
992   myView->Update ();
993 }
994
995 void CAnimationView3D::OnViewDisplaystatus() 
996 {
997         // TODO: Add your command handler code here
998
999         if ( m_Tune.IsWindowVisible () ) {
1000
1001         }
1002         else {
1003                 m_Tune.ShowWindow ( SW_SHOWNORMAL ) ;
1004         }
1005 }
1006
1007 void CAnimationView3D::OnUpdateViewDisplaystatus(CCmdUI* pCmdUI) 
1008 {
1009         // TODO: Add your command update UI handler code here
1010
1011         if ( m_Tune.IsWindowVisible () ) {
1012                 pCmdUI->SetCheck ( 1 ) ;
1013         }
1014         else {
1015                 pCmdUI->SetCheck ( 0 ) ;
1016         }
1017 }