70001186 Action that begins a user animation

Article 70001186
Type Wish
Product Engine
Version 7012
Date Added 10/5/2017
Fixed 7.7014.0.1 (11/22/2017)
Submitted by Karthik Bala

Summary

Action that begins a user animation

Solution

In version 7014.0.1 a new class VectorDraw.Professional.ActionUtility.Utility.AnimationAction was defined .
Developer can implement a new object that overrides this class in order to display an animated scene.


Example:

//create and start a new animation action
BaseAction action = new MyAnimationAction(document, 0);
document.ActionAdd(action);

    /// 
    /// Example of a new user define AnimationAction.
    /// 
    /// 
    /// When this action is started, begins an animation that view the drawing by walking on a circular path around the drawing center.
    /// 
    public class MyAnimationAction : VectorDraw.Professional.ActionUtility.AnimationAction
    {
        //variable that keep the current position in the animation path.
        double pos = 0;
        /// 
        /// Initialize a new instance of the object.
        /// 
        /// The  that reference to this object.
        /// Sets minimum the time in milliseconds between each scene redraw.
        public MyAnimationAction(vdDocument doc, int interval) : base(doc,interval)
        {
        }
        /// 
        /// Fires when the user presses a keyboard key.
        /// 
        /// KeyDown event arguments.
        public override void KeyDown(KeyEventArgs e)
        {
            if (e.KeyValue == (int)Keys.Escape) FinishAction(this); //finish the action if the Esc is pressed
            else if (e.KeyValue == (int)Keys.P)//Play/Pause the action if the 'P' key is pressed.
            {
                if (IsPaused) 
                    Play();
                else
                    Pause();
            }
            
        }
        /// 
        /// Called when the action is activated .
        /// We override this in order to select our user define cursor
        /// 
        public override void Show()
        {
            base.Show();
            ShowCursor(Cursors.SizeWE);//select a default cursor for this action
        }
        /// 
        /// Most importand method that overrides the drawing scene for each timer passed period according to our logic.
        /// Implements our logic that view the drawing by walking on a circular path around the drawing center.
        /// 
        public override void drawscene(vdRender render)
        {
            //get the drawing extents in world coordinate system
            Box extents = render.DrawingExtents;
            //if no entities exist then do nothing.
            if (!extents.IsNormal) return;
            
            //calculate the radius of the circular path depend on the drawing extents so the entire drawing to be visible while walking throw the path
            double radius = Math.Sqrt(extents.Width * extents.Width + extents.Height * extents.Height) / 2.0;
            if (Globals.AreEqualLength(radius, 0.0)) return;

            double LensAnRads = VectorDraw.Geometry.Globals.DegreesToRadians(render.LensAngle) / 2;
            double boxHeight = extents.Thickness / 2.0;
            double extendsNear =Math.Max(radius, boxHeight) / Math.Tan(LensAnRads);
            radius += extendsNear;

            //calculate the velocity so a complete circle to be done in one minute(60000 miliseconds)
            double velocity = (radius * Globals.VD_TWOPI) / 60000;//du per minite.60000 = time in miliseconds to make a full circle(60000 : 1 minite to walk full circle)
            
            //calculate the angle view relative to the circle center depend on the previus postion and the velocity.
            double angle = (pos + velocity * Period / radius) % (radius * Globals.VD_TWOPI);
            pos = angle;//change the pos variable to the next step.
           
            //calculate the World to View matrix 
            gPoint target = extents.MidPoint;
            gPoint eyePos = target.Polar(angle, radius);
            eyePos.z += boxHeight * 0.5;
            Matrix m = new Matrix();
            m.SetLookAt(eyePos, target, 0.0);
            render.CurrentMatrix = m;

            //set the view center to zero.World to view matrix includes the view center transformation.
            render.ViewCenter = new gPoint();
            //set the view size used by orthographic render modes enought big to fit the drawing.
            render.ViewSize = extents.Thickness * 1.5;

            

            
        }


        
        //Temporary mtext used to draw align to view text on the lower left corner
        vdText mtext = null;
        /// 
        /// Returns the text that is used to display the user information over the drawing scene.
        /// 
        vdText Text
        {
            get
            {
                if (mtext != null) return mtext;
                mtext = new vdText(document)
                { 
                    TextString = "",
                    InsertionPoint = new gPoint(),
                    HorJustify = VectorDraw.Professional.Constants.VdConstHorJust.VdTextHorLeft,
                    VerJustify = VectorDraw.Professional.Constants.VdConstVerJust.VdTextVerBottom, 
                    Draw3DFlag = Draw3DFlagEnum.ExcludeFromList, 
                    AlignToViewSize = 8, 
                    PenColor = new vdColor(Color.SkyBlue),
                    BackGroundMask = true,
                    BackgroundMaskBorder = true, 
                    BackGroundMaskOffset = 0.2, 
                    BackGroundMaskColor = new vdColor(Color.LightGray)
                };

                
                
                return mtext;
            }
        }
        /// 
        /// Override this method in order to display some info to the user on the screen.
        /// 
        public override void drawoverall(vdRender render, ref bool cancel)
        {
            //Ensure that the perspective mode is off in order the text to be drawn properly
            vdRender.VdConstPerspectiveMod mode = render.PerspectiveMod;
            render.PerspectiveMod = vdRender.VdConstPerspectiveMod.PerspectOFF;
            

            //draw aligned to view text
            
            //calculate the text insertion point to be near to the lower left corner of the screen
            gPoint lowerleft = render.ClipBounds.ToBox().Min + new gPoint(render.PixelSize * 25, render.PixelSize * 25, 0);//offset the text origin 25,25 pixels 
            lowerleft.z = render.CurrentMatrix.Transform(render.DrawingExtents.MidPoint).z;

            //set the text display string us the number of frames that displayed per second 
            Text.TextString = string.Format("{0,3} FPS",((int)(1000.0 / Period)).ToString());
            Text.InsertionPoint = lowerleft;
            Text.Update();
            

            render.PushToViewMatrix();//use this in order the text to be drawn over the view plane.
            
            
            Text.Draw(render);//draw the text object.
            
            render.PopMatrix();


            render.PerspectiveMod = mode;//restore the perspective mode.
            

            cancel = true;//ignore default overall render from VectorDraw Framework.Only the text is drawn over the scene.
        }
    }

Send comments on this topic.