// @(#)root/gl:$Name:  $:$Id: TGLTransManip.cxx
// Author:  Richard Maunder  16/09/2005

/*************************************************************************
 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TGLTransManip.h"
#include "TGLPhysicalShape.h"
#include "TGLCamera.h"
#include "TGLIncludes.h"

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGLTransManip                                                        //
//                                                                      //
// Translation manipulator - attaches to physical shape and draws local //
// axes widgets with arrow heads. User can mouse over (turns yellow) and//
// L click/drag to translate along this axis.                           // 
// Widgets use standard 3D package axes colours: X red, Y green, Z blue.//
//////////////////////////////////////////////////////////////////////////

ClassImp(TGLTransManip)

//______________________________________________________________________________
 TGLTransManip::TGLTransManip(TGLViewer & viewer) : TGLManip(viewer)
{
   // Construct translation manipulator, attached to supplied TGLViewer 
   // 'viewer', not bound to any physical shape.
}

//______________________________________________________________________________
 TGLTransManip::TGLTransManip(TGLViewer & viewer, TGLPhysicalShape * shape) : 
   TGLManip(viewer, shape) 
{
   // Construct translation manipulator, attached to supplied TGLViewer 
   // 'viewer', bound to TGLPhysicalShape 'shape'.
}

//______________________________________________________________________________
 TGLTransManip::~TGLTransManip() 
{
   // Destory the translation manipulator
}
   
//______________________________________________________________________________
 void TGLTransManip::Draw(const TGLCamera & camera) const
{
   // Draw translation manipulator - tubes with arrow heads, in local axes of 
   // attached shape, in red(X), green(Y) and blue(Z), with white center sphere. 
   // If selected widget (mouse over) this is drawn in active colour (yellow).
   if (!fShape) {
      return;
   }

   const TGLBoundingBox & box = fShape->BoundingBox();
   Double_t widgetScale = CalcDrawScale(box, camera);

   // Get permitted manipulations on shape
   TGLPhysicalShape::EManip manip = fShape->GetManip();

   TGLVector3 translateAxes[3];
   for (UInt_t i = 0; i<3; i++) {
      if (box.IsEmpty()) {
         translateAxes[i] = box.Axis(i, kTRUE)*widgetScale*-10.0;
      } else {
         translateAxes[i] = box.Axis(i, kFALSE)*-0.51;
      }
   }

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glDisable(GL_CULL_FACE);

   // Draw three axis widgets out of bounding box where permitted
   // Not drawing will prevent interaction
   // GL name loading for hit testing - 0 reserved for no selection
   if (manip & TGLPhysicalShape::kTranslateX) {
      glPushName(1);
      TGLUtil::DrawLine(box.Center(), translateAxes[0], TGLUtil::kLineHeadArrow, 
                        widgetScale, fSelectedWidget == 1 ? fgYellow : fgRed);
      glPopName();
   } else {
      TGLUtil::DrawLine(box.Center(), translateAxes[0], TGLUtil::kLineHeadArrow, 
                        widgetScale, fgGrey);
   }
   if (manip & TGLPhysicalShape::kTranslateY) {
      glPushName(2);
      TGLUtil::DrawLine(box.Center(), translateAxes[1], TGLUtil::kLineHeadArrow, 
                        widgetScale, fSelectedWidget == 2 ? fgYellow : fgGreen);
      glPopName();
   } else {
      TGLUtil::DrawLine(box.Center(), translateAxes[1], TGLUtil::kLineHeadArrow, 
                        widgetScale, fgGrey);
   }
   if (manip & TGLPhysicalShape::kTranslateZ) {
      glPushName(3);
      TGLUtil::DrawLine(box.Center(), translateAxes[2], TGLUtil::kLineHeadArrow, 
                        widgetScale, fSelectedWidget == 3 ? fgYellow : fgBlue);
      glPopName();
   } else {
      TGLUtil::DrawLine(box.Center(), translateAxes[2], TGLUtil::kLineHeadArrow, 
                        widgetScale, fgGrey);
   }
   // Draw white center sphere
   TGLUtil::DrawSphere(box.Center(), widgetScale/2.0, fgWhite);

   glEnable(GL_CULL_FACE);
   glDisable(GL_BLEND);
}

//______________________________________________________________________________
 Bool_t TGLTransManip::HandleMotion(const Event_t * event, const TGLCamera & camera)
{
   // Handle mouse motion over manipulator - if active (selected widget) translate 
   // physical along selected widget (axis) of the manipulator, so it tracks mouse 
   // action. Returns kTRUE if redraw required kFALSE otherwise.
   if (fActive) {
      // Find mouse delta projected into world at attached object center
      TGLVector3 shift = camera.ViewportDeltaToWorld(fShape->BoundingBox().Center(), 
                                                     event->fX - fLastMouse.GetX(),
                                                     -event->fY + fLastMouse.GetY()); // Y inverted
      
      // Now project this delta onto the current widget (axis) to give
      // a constrained shift along this
      UInt_t axisIndex = fSelectedWidget - 1; // Ugg sort out axis / widget id mapping
      TGLVector3 widgetAxis = fShape->BoundingBox().Axis(axisIndex, kTRUE);
      TGLVector3 constrainedShift = widgetAxis * Dot(shift, widgetAxis);
      fShape->Translate(constrainedShift);

      fLastMouse.SetX(event->fX);
      fLastMouse.SetY(event->fY);

      return kTRUE;
   } else {
      return TGLManip::HandleMotion(event, camera);
   }
}



ROOT page - Class index - Class Hierarchy - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.