Share:

Create AutoScroll V1_0_0 for WPF C# | Saatody | Amit Padhiyar

 Today, I will discuss about AutoScroll algorithm. This is version V1_0_0 of AutoScroll algorithm. First see the algorithm's API. Then we will discuss about connectivity of this API with your program.

namespace Saatody.Core.UI
{
    public class AutoScroll
    {
        private enum ValueTypes
        {
            Increment,
            Decrement
        }
        private enum SpeedLevels
        {
            None,
            Normal,
            Medium,
            Extreme
        }

        public double None { set; get; }
        public double Normal { set; get; }
        public double Medium { set; get; }
        public double Extreme { set; get; }
        
        public double NormalDistance { set; get; }
        public double MediumDistance { set; get; }
        public double ExtremeDistance { set; get; }

        public double Mouse { set; get; }
        public double Point1 { set; get; }
        public double Point2 { set; get; }
        
        public double Speed
        {
            get
            {
                return GetSpeed();
            }
        }

        private double GetSpeed()
        {
            double Speed = None;
            SpeedLevels SpeedLevel = SpeedLevels.None;
            ValueTypes ValueType = ValueTypes.Increment;

            double PlusNormalDistance = (NormalDistance);
            double PlusMediumDistance = (MediumDistance);
            double PlusExtremeDistance = (ExtremeDistance);
            double MinusNormalDistance = (-NormalDistance);
            double MinusMediumDistance = (-MediumDistance);
            double MinusExtremeDistance = (-ExtremeDistance);

            if ((Mouse > (Point1 + PlusNormalDistance)) && (Mouse < (Point2 - PlusNormalDistance)))
            {
                SpeedLevel = SpeedLevels.None;
                ValueType = ValueTypes.Increment;
            }
            else
            {
                if ((Mouse <= (Point1 + PlusNormalDistance)) && (Mouse >= (Point1 + PlusMediumDistance)))
                {
                    SpeedLevel = SpeedLevels.Normal;
                    ValueType = ValueTypes.Decrement;
                }
                else if ((Mouse <= (Point1 + PlusMediumDistance)) && (Mouse >= (Point1 + PlusExtremeDistance)))
                {
                    SpeedLevel = SpeedLevels.Medium;
                    ValueType = ValueTypes.Decrement;
                }
                else if ((Mouse < (Point1 + PlusExtremeDistance)))
                {
                    SpeedLevel = SpeedLevels.Extreme;
                    ValueType = ValueTypes.Decrement;
                }

                if ((Mouse >= (Point2 + MinusNormalDistance)) && (Mouse <= (Point2 + MinusMediumDistance)))
                {
                    SpeedLevel = SpeedLevels.Normal;
                    ValueType = ValueTypes.Increment;
                }
                else if ((Mouse >= (Point2 + MinusMediumDistance)) && (Mouse <= (Point2 + MinusExtremeDistance)))
                {
                    SpeedLevel = SpeedLevels.Medium;
                    ValueType = ValueTypes.Increment;
                }
                else if ((Mouse > (Point2 + MinusExtremeDistance)))
                {
                    SpeedLevel = SpeedLevels.Extreme;
                    ValueType = ValueTypes.Increment;
                }
            }

            if(SpeedLevel == SpeedLevels.None && ValueType == ValueTypes.Increment)
            {
                Speed = None;
            }
            else if (SpeedLevel == SpeedLevels.Normal && ValueType == ValueTypes.Decrement)
            {
                Speed = (-Normal);
            }
            else if (SpeedLevel == SpeedLevels.Medium && ValueType == ValueTypes.Decrement)
            {
                Speed = (-Medium);
            }
            else if (SpeedLevel == SpeedLevels.Extreme && ValueType == ValueTypes.Decrement)
            {
                Speed = (-Extreme);
            }
            else if (SpeedLevel == SpeedLevels.Normal && ValueType == ValueTypes.Increment)
            {
                Speed = (Normal);
            }
            else if (SpeedLevel == SpeedLevels.Medium && ValueType == ValueTypes.Increment)
            {
                Speed = (Medium);
            }
            else if (SpeedLevel == SpeedLevels.Extreme && ValueType == ValueTypes.Increment)
            {
                Speed = (Extreme);
            }

            return Speed;
        }
    }
}
Here is full API of AutoScroll. Now let's discuss about API connectivity.
// Create object of AutoScroll
AutoScroll AutoScroll = new AutoScroll();

// Set the speed with different states
AutoScroll.None    = 0;
AutoScroll.Normal  = 1;
AutoScroll.Medium  = 2;
AutoScroll.Extreme = 5;

// Set the distance with different states
AutoScroll.NormalDistance = 20;
AutoScroll.MediumDistance = 10;
AutoScroll.ExtremeDistance = 5;

// Set object size
AutoScroll.Point1 = 0.0;
AutoScroll.Point2 = Grid.ActualWidth;

// Set mouse position continuously
AutoScroll.Mouse = Mouse.X;

// Get the speed continuously
SpeedX = AutoScroll.Speed;

Finally, We connected all properties to our class. Now, I describe in details about all properties.

Create object of AutoScroll

Creating an object for AutoScroll class is very simple.

// Create object of AutoScroll
AutoScroll AutoScroll = new AutoScroll();

Set the speed with different states

The speed could be use for set margin of grid or for set value for scroll when mouse position is particular distance state. There is four types of speed states. 1] None, 2] Normal, 3] Medium, And 4] Extreme.
// Set the speed with different states
AutoScroll.None    = 0;
AutoScroll.Normal  = 1;
AutoScroll.Medium  = 2;
AutoScroll.Extreme = 5;

Set the distance with different states

The distance state is one type of range that calculate mouse position on your grid is in which state? After the detecting this will give you output in speed form.

Here is three distance states. 1] Normal, 2] Medium, And 3] Extreme.
// Set the distance with different states
AutoScroll.NormalDistance = 20;
AutoScroll.MediumDistance = 10;
AutoScroll.ExtremeDistance = 5;

Set object size

If you want to give AutoScroll size of grid on horizontal or vertical mouse position perspective then there are two different points. 1] Point1, And 2] Point2

// Set object size
AutoScroll.Point1 = 0.0;
AutoScroll.Point2 = Grid.ActualWidth;

Set mouse position continuously

Set mouse position with mouse move event. This will give you continuously speed.
// Set mouse position continuously
AutoScroll.Mouse = Mouse.X;

Get the speed continuously

Yes here is also we need to get speed of AutoScroll continuously. So, This way we can set margin or origin on our grid or graphics objects.
// Get the speed continuously
SpeedX = AutoScroll.Speed;

Now little question is, How I can get continuously output without any loop, thread and background workers.

So, Let's below code.

bool AllowAutoScroll = false;

// Create object of AutoScroll
AutoScroll AutoScroll = new AutoScroll();

// Set the speed with different states
AutoScroll.None    = 0;
AutoScroll.Normal  = 1;
AutoScroll.Medium  = 2;
AutoScroll.Extreme = 5;

// Set the distance with different states
AutoScroll.NormalDistance = 20;
AutoScroll.MediumDistance = 10;
AutoScroll.ExtremeDistance = 5;

Grid.MouseEnter += (sender, e) =>
{
    AllowAutoScroll = true;
};
Grid.MouseLeave += (sender, e) =>
{
    AllowAutoScroll = false;
};
CompositionTarget.Rendering += (sender, e) =>
{
    if (AllowAutoScroll == true)
    {
        try
        {
	    // Set mouse position continuously
	    Point Point = Mouse.GetPosition(Grid);
	    AutoScroll.Mouse = Point.X;

            // Set object size
	    AutoScroll.Point1 = 0.0;
            AutoScroll.Point2 = Grid.ActualWidth;

	    // Get the speed continuously
	    double SpeedX = AutoScroll.Speed;
			
            if (SpeedX != 0)
            {
                Grid.Margin = new Thickness(Grid.Margin.Left - SpeedX,0,0,0);
            }
        }
        catch (Exception Ex)
        {

        }
    }
};

Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#