#include "TGLScaleManip.h"
#include "TGLPhysicalShape.h"
#include "TGLCamera.h"
#include "TGLIncludes.h"
ClassImp(TGLScaleManip)
TGLScaleManip::TGLScaleManip()
{
}
TGLScaleManip::TGLScaleManip(TGLPhysicalShape * shape) :
TGLManip(shape)
{
}
TGLScaleManip::~TGLScaleManip()
{
}
void TGLScaleManip::Draw(const TGLCamera & camera) const
{
if (!fShape) {
return;
}
const TGLBoundingBox & box = fShape->BoundingBox();
Double_t baseScale;
TGLVector3 axisScale[3];
CalcDrawScale(box, camera, baseScale, axisScale);
TGLPhysicalShape::EManip manip = fShape->GetManip();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
if (manip & TGLPhysicalShape::kScaleX) {
glPushName(1);
TGLUtil::DrawLine(box.Center(), axisScale[0], TGLUtil::kLineHeadBox,
baseScale, fSelectedWidget == 1 ? fgYellow : fgRed);
glPopName();
} else {
TGLUtil::DrawLine(box.Center(), axisScale[0], TGLUtil::kLineHeadBox,
baseScale, fgGrey);
}
if (manip & TGLPhysicalShape::kScaleY) {
glPushName(2);
TGLUtil::DrawLine(box.Center(), axisScale[1], TGLUtil::kLineHeadBox,
baseScale, fSelectedWidget == 2 ? fgYellow : fgGreen);
glPopName();
} else {
TGLUtil::DrawLine(box.Center(), axisScale[1], TGLUtil::kLineHeadBox,
baseScale, fgGrey);
}
if (manip & TGLPhysicalShape::kScaleZ) {
glPushName(3);
TGLUtil::DrawLine(box.Center(), axisScale[2], TGLUtil::kLineHeadBox,
baseScale, fSelectedWidget == 3 ? fgYellow : fgBlue);
glPopName();
} else {
TGLUtil::DrawLine(box.Center(), axisScale[2], TGLUtil::kLineHeadBox,
baseScale, fgGrey);
}
TGLUtil::DrawSphere(box.Center(), baseScale/2.0, fgWhite);
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
Bool_t TGLScaleManip::HandleButton(const Event_t & event,
const TGLCamera & camera)
{
if (event.fType == kButtonPress && fSelectedWidget != 0) {
fStartScale = fShape->GetScale();
}
return TGLManip::HandleButton(event, camera);
}
Bool_t TGLScaleManip::HandleMotion(const Event_t & event,
const TGLCamera & camera)
{
if (fActive) {
TGLVector3 shift = camera.ViewportDeltaToWorld(fShape->BoundingBox().Center(),
event.fX - fFirstMouse.GetX(),
-event.fY + fFirstMouse.GetY());
UInt_t axisIndex = fSelectedWidget - 1;
TGLVector3 widgetAxis = fShape->BoundingBox().Axis(axisIndex, kTRUE);
TGLVector3 screenScale = camera.ViewportDeltaToWorld(fShape->BoundingBox().Center(), 500, 500);
Double_t factor = -5.0*Dot(shift, widgetAxis) / screenScale.Mag();
TGLVector3 newScale = fStartScale;
newScale[axisIndex] += factor;
LimitScale(newScale[axisIndex]);
fShape->Scale(newScale);
fLastMouse.SetX(event.fX);
fLastMouse.SetY(event.fY);
return kTRUE;
}
return kFALSE;
}
void TGLScaleManip::LimitScale(Double_t & factor) const
{
if (factor < 1e-4) {
factor = 1e-4;
}
if (factor > 1e+4) {
factor = 1e+4;
}
}