home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-20 | 9.6 KB | 347 lines | [TEXT/CWIE] |
- //Copyright (c) 1997 Brian and Aidan Cully
- //All rights reserved
-
- #include "ConicContent.h"
- #include "Messages.h"
- #include <math.h>
- #include "CLLine.h"
- #include "CLStringShape.h"
- #include "CLColorStyle.h"
- #include "CLMouseHandler.h"
- #include "CLNullHandler.h"
- #include "CLKeyboardHandler.h"
- #include "CLRectShape.h"
- #include <Drag.h>
- #include "LMath.h"
- #include "CLBaseWindow.h"
-
- TConicContent::TConicContent( TLayoutBranch *super, MActionHandler *superHandler, Boolean hasDD,
- TConic *nconic, TPlane *nplane, float ntheta, float nphi ):
- TDocContent( super, hasDD, superHandler, nplane ),
- mTheta( ntheta ),
- mPhi( nphi )
- {
- mConic= nconic;
- d = 45000;
- D = 32767;
- S = 10;
- mNormCurs= ::GetCursor( 130 );
- mShifCurs= ::GetCursor( 131 );
- mOptCurs= ::GetCursor( 133 );
- ::MoveHHi( (Handle)mNormCurs );
- ::HLock( (Handle)mNormCurs );
- ::MoveHHi( (Handle)mShifCurs );
- ::HLock( (Handle)mShifCurs );
- ::MoveHHi( (Handle)mOptCurs );
- ::HLock( (Handle)mOptCurs );
- }
-
- p2d TConicContent::P3dT2d( p3d coord3d, Rect scaleRect )
- {
- p3d eyec;
- p2d coord2d;
- Point center;
- Rect tRect = scaleRect;
-
- ::OffsetRect( &tRect, -scaleRect.left, -scaleRect.top );
- center.h = tRect.right/2+scaleRect.left;
- center.v = tRect.bottom/2+scaleRect.top;
-
- eyec.x = -1 * coord3d.x*mTheta.GetSinAngle() + coord3d.y*mTheta.GetCosAngle();
- eyec.y = -1 * coord3d.x*mTheta.GetCosAngle()*mPhi.GetCosAngle() -
- coord3d.y*mTheta.GetSinAngle()*mPhi.GetCosAngle() + coord3d.z*mPhi.GetSinAngle();
- eyec.z = -1 * coord3d.x*mPhi.GetSinAngle()*mTheta.GetCosAngle() -
- coord3d.y*mPhi.GetSinAngle()*mTheta.GetSinAngle() - coord3d.z*mPhi.GetCosAngle() + D;
-
- coord2d.x = (d/S) * (eyec.x/eyec.z);
- coord2d.y = (d/S) * (eyec.y/eyec.z);
-
- coord2d.x = (coord2d.x * tRect.right/3)+center.h;
- coord2d.y = coord2d.y * -tRect.bottom/3+center.v;
-
- return(coord2d);
- }
-
- SInt8 TConicContent::DrawConic( TDrawSlate *gr )
- {
- int surf;
- p3d temp;
- p2d opoint,point,obpoint,bpoint, center;
- Rect drawRect = GetLocalRect();
- TColorStyle rim( 0, 0, 0xffff ), mid( 0xffff, 0, 0 );
-
- DrawAxis( gr );
- point = P3dT2d(mConic->mConic[0], drawRect);
- bpoint = P3dT2d(mConic->mConic[1], drawRect);
- temp.x = 0;
- temp.y = 0;
- temp.z = 0;
- center = P3dT2d( temp, drawRect );
-
- RGBColor col;
- for(surf = 0;surf<=CONICPARTS;surf+=2)
- {
- opoint = point;
- obpoint = bpoint;
- point = P3dT2d(mConic->mConic[surf], drawRect);
- bpoint = P3dT2d(mConic->mConic[surf+1], drawRect);
- TLine( &rim, opoint.x, opoint.y, point.x, point.y ).RenderOn( gr );
- TLine( &mid, point.x, point.y, center.x, center.y ).RenderOn( gr );
- TLine( &mid, center.x, center.y, bpoint.x, bpoint.y ).RenderOn( gr );
- TLine( &rim, obpoint.x, obpoint.y, bpoint.x, bpoint.y ).RenderOn( gr );
- }
- }
-
- SInt8 TConicContent::DrawPlaneAxis( TDrawSlate *gr )
- {
- TColorStyle xax( 0, 0x8000, 0xffff );
- TColorStyle yax( 0, 0, 0xffff );
- TColorStyle black( 0x8000, 0x8000, 0x8000 );
- p3d e1, e2, iter;
- p2d point, opoint;
- int surf;
- Rect drawRect= GetLocalRect();
-
- e1.x= (mPlane->mPlane[0].x+mPlane->mPlane[1].x)/2;
- e1.y= (mPlane->mPlane[0].y+mPlane->mPlane[1].y)/2;
- e1.z= (mPlane->mPlane[0].z+mPlane->mPlane[1].z)/2;
- point= P3dT2d( e1, drawRect );
- e2.x= (mPlane->mPlane[2].x+mPlane->mPlane[3].x)/2;
- e2.y= (mPlane->mPlane[2].y+mPlane->mPlane[3].y)/2;
- e2.z= (mPlane->mPlane[2].z+mPlane->mPlane[3].z)/2;
- opoint= P3dT2d( e2, drawRect );
- TLine( &yax, point.x, point.y, opoint.x, opoint.y ).RenderOn( gr );
- iter= e1;
- p3d temp= e2;
- int ofx, ofy;
- temp.x-= 0.5;
- p2d tpoint= P3dT2d( temp, drawRect );
- ofx= tpoint.x-opoint.x;
- ofy= tpoint.y-opoint.y;
- for( surf= 0; surf<12; surf++ ) {
- TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
- iter.x+= (e2.x-e1.x)/12;
- iter.y+= (e2.y-e1.y)/12;
- iter.z+= (e2.z-e1.z)/12;
- point= P3dT2d( iter, drawRect );
- }
- TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
- e1.x= (mPlane->mPlane[1].x+mPlane->mPlane[2].x)/2;
- e1.y= (mPlane->mPlane[1].y+mPlane->mPlane[2].y)/2;
- e1.z= (mPlane->mPlane[1].z+mPlane->mPlane[2].z)/2;
- point= P3dT2d( e1, drawRect );
- e2.x= (mPlane->mPlane[0].x+mPlane->mPlane[3].x)/2;
- e2.y= (mPlane->mPlane[0].y+mPlane->mPlane[3].y)/2;
- e2.z= (mPlane->mPlane[0].z+mPlane->mPlane[3].z)/2;
- opoint= P3dT2d( e2, drawRect );
- TLine( &xax, point.x, point.y, opoint.x, opoint.y ).RenderOn( gr );
- iter= e1;
- temp= e2;
- temp.y-= (mPlane->mPlane[3].y-mPlane->mPlane[0].y)/24;
- temp.z-= (mPlane->mPlane[3].z-mPlane->mPlane[0].z)/24;
- tpoint= P3dT2d( temp, drawRect );
- ofx= tpoint.x-opoint.x;
- ofy= tpoint.y-opoint.y;
- for( surf= 0; surf<12; surf++ ) {
- TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
- iter.x+= (e2.x-e1.x)/12;
- iter.y+= (e2.y-e1.y)/12;
- iter.z+= (e2.z-e1.z)/12;
- point= P3dT2d( iter, drawRect );
- }
- TLine( &black, point.x-ofx, point.y-ofy, point.x+ofx, point.y+ofy ).RenderOn( gr );
- }
-
- SInt8 TConicContent::DrawPlane( TDrawSlate *gr )
- {
- Rect drawRect = GetLocalRect();
- p2d point, opoint;
- int surf;
- TColorStyle plane( 0, 0x8000, 0 );
-
- DrawPlaneAxis( gr );
- point = P3dT2d(mPlane->mPlane[0], drawRect);
- //gr->SetColor( 0, 0, 0x8000 );
- for (surf = 0;surf<=3;surf++)
- {
- opoint = point;
- point = P3dT2d(mPlane->mPlane[surf], drawRect);
- TLine( &plane, opoint.x, opoint.y, point.x, point.y ).RenderOn( gr );
- }
- opoint = P3dT2d(mPlane->mPlane[0], drawRect);
- TLine( &plane, opoint.x, opoint.y, point.x, point.y ).RenderOn( gr );
- }
-
- SInt8 TConicContent::DrawAxis( TDrawSlate *gr )
- {
- p3d x,y,z,bx,by,bz;
- p2d xax,yax,zax,bxax,byax,bzax;
- unsigned pattern;
- Rect drawRect = GetLocalRect();
- RGBColor col;
- TColorStyle black( 0, 0, 0 );
- TColorStyle xcol( 0, 0x8000, 0xffff );
- TColorStyle ycol( 0, 0, 0xffff );
- TColorStyle zcol( 0xffff, 0, 0xffff );
-
- bx.x = -10; bx.y = 0; bx.z = 0;
- by.x = 0; by.y = -10; by.z = 0;
- bz.x = 0; bz.y = 0; bz.z = -10;
-
- x.x = 10; x.y = 0; x.z = 0;
- y.x = 0; y.y = 10; y.z = 0;
- z.x = 0; z.y = 0; z.z = 10;
-
- bxax = P3dT2d(bx, drawRect);
- xax = P3dT2d(x, drawRect);
- //color 0xff00000
- TLine( &xcol, bxax.x, bxax.y, xax.x, xax.y ).RenderOn( gr );
- TStringShape( &black, xax.x, xax.y, "x" ).RenderOn( gr );
-
- byax = P3dT2d(by, drawRect);
- yax = P3dT2d(y, drawRect);
- //color 0xff00
- //gr->SetColor( 0, 0xffff, 0 );
- TLine( &ycol, byax.x, byax.y, yax.x, yax.y ).RenderOn( gr );
- TStringShape( &black, yax.x, yax.y, "y" ).RenderOn( gr );
-
- bzax = P3dT2d(bz, drawRect);
- zax = P3dT2d(z, drawRect);
- //color 0xffff
- TLine( &zcol, bzax.x, bzax.y, zax.x, zax.y ).RenderOn( gr );
- TStringShape( &black, zax.x, zax.y, "z" ).RenderOn( gr );
- }
-
- void TConicContent::DrawSelf( TDrawSlate *gr )
- {
- TEraseRectShape( GetLocalRect() ).RenderOn( gr );
- DrawConic( gr );
- DrawPlane( gr );
- //gr->SetColor( 0, 0, 0 );
- }
-
- void TConicContent::HandleOptMouse()
- {
- float theta( mTheta.GetAngle() ), phi( mPhi.GetAngle() );
- float angle, y, x, dis;
- CursHandle curs= ::GetCursor( 132 );
- mTheta.SetAngle( 0 );
- mPhi.SetAngle( 270 );
- Rect localRect= GetLocalRect();
- Draw( mWindow );
- Point pt, opt;
- TMouseHandler *mouse= TMouseHandler::SGetMouse();
- mouse->GetPosition( pt );
- mWindow->GlobalToLocal( &pt );
- opt.h= pt.h+1;
- ::SetCursor( *curs );
- do {
- if( (pt.h!=opt.h)||(pt.v!=opt.v) ) {
- y= (pt.v-localRect.top)-(localRect.bottom-localRect.top)/2.0;
- x= (localRect.right-localRect.left)/2.0-(pt.h-localRect.left);
- //Yes, I know, this should all be accomplished with the atan2. Tell that to that
- //bloody MathLib I've got on my machine.
- if( fabs(fabs(y)-fabs(x))>0.000001 )
- angle= (atan2( x, y )*180/M_PI);
- else if( (y>0)&&(x>0) )
- angle= 45;
- else if( (y<0)&&(x<0) )
- angle= 225;
- else if( (y<0)&&(x>0) )
- angle= 135;
- else if( (y>0)&&(x<0) )
- angle= 315;
- dis= sqrt( (x*x)+(y*y) )/(localRect.right-localRect.left)*22;
- mPlane->SetAngle( angle );
- mPlane->SetDistance( dis );
- mConic->CalcIntersect( *mPlane );
- mPlane->SendMessage( (UINT32)kUpdateWindow );
- }
- opt= pt;
- mouse->GetPosition( pt );
- mWindow->GlobalToLocal( &pt );
- } while( mouse->GetButton() );
- mTheta.SetAngle( theta );
- mPhi.SetAngle( phi );
- Draw( mWindow );
- ::SetCursor( mPresCurs );
- }
-
- void TConicContent::HandleNormMouse( BOOLEAN shift )
- {
- Point pt, opt;
- CursHandle curs;
- int dir;
- TMouseHandler *mouse= TMouseHandler::SGetMouse();
-
- mouse->GetPosition( pt );
- if( shift ) {
- dir=0;
- curs= GetCursor( 129 );
- } else {
- dir=3;
- curs= GetCursor( 128 );
- }
- SetCursor( (*curs) );
- while( mouse->GetButton() ) {
- opt= pt;
- mouse->GetPosition( pt );
- if( (pt.h != opt.h) || (pt.v != opt.v) ) {
- if( shift&&!dir ) {
- if( (pt.h-opt.h)*((pt.h-opt.h)<0?-1:1)>(pt.v-opt.v)*((pt.v-opt.v)<0?-1:1) )
- dir= 1;
- else
- dir= 2;
- }
- if( dir&1 )
- mTheta.SetAngle( mTheta.GetAngle()-(pt.h-opt.h)/2.0 );
- if( dir&2 )
- mPhi.SetAngle( mPhi.GetAngle()+(pt.v-opt.v)/2.0 );
- Draw( mWindow );
- }
- }
- ::SetCursor( mPresCurs );
- }
-
- Boolean TConicContent::HandleMouseSelf( TMouseButtonEvent *ev )
- {
- if( !ev->button )
- return( true );
- UINT16 mods= TKeyboardHandler::GetKeyboard()->GetModifiers();
- if( mods&optionKey )
- HandleOptMouse();
- else if( mHasDragDrop&&(mods&cmdKey) )
- TDocContent::HandleMouseSelf( ev );
- else
- HandleNormMouse( (mods&shiftKey)!=0 );
- return( true );
- }
-
- void TConicContent::HandleMouseEnter()
- {
- TNullHandler::GetNullHandler()->AddPulsar( this );
- mState= 32767;
- }
-
- void TConicContent::HandleMouseExit()
- {
- TNullHandler::GetNullHandler()->RemovePulsar( this );
- ::SetCursor( &qd.arrow );
- }
-
- void TConicContent::Pulse( const TEvent *ev )
- {
- UINT16 modifiers= TKeyboardHandler::GetKeyboard()->GetModifiers();
- if( modifiers != mState ) {
- mState= modifiers;
- if( mState&optionKey )
- mPresCurs= *mOptCurs;
- else if( mState&shiftKey )
- mPresCurs= *mShifCurs;
- else if( mHasDragDrop&&(mState&cmdKey) )
- mPresCurs= &qd.arrow;
- else
- mPresCurs= *mNormCurs;
- ::SetCursor( mPresCurs );
- }
- }