|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionAdobe Photoshop has two very professional controls that are used for effects such as Drop Shadow and Bevel and Emboss: one being the angle selector control and the other being the angle and altitude selector control. In this article, we will go through the process of creating two C# custom controls to mimic the appearance and behavior of those in Photoshop. The Foundation - Math to KnowPythagoras TheoremThe Pythagoras Theorem lets us calculate the hypotenuse of a right triangle (the longest side). The formula is Unit CircleSince we will be working with angles and circles, it will be helpful to be familiar with the format of a unit circle. The unit circle is simply a circle with a radius of 1 centered at the point TrigonometryWe will only need the three basic trig functions: Also remember that the inverse trig functions are used to calculate an unknown angle. Common FunctionsBoth of our custom controls will share two important functions:
The first function is the simpler of the two: private PointF DegreesToXY(float degrees, float radius, Point origin)
{
PointF xy = new PointF();
double radians = degrees * Math.PI / 180.0;
xy.X = (float)Math.Cos(radians) * radius + origin.X;
xy.Y = (float)Math.Sin(-radians) * radius + origin.Y;
return xy;
}
The thing to note is we first need to convert the angle into radians. (Converting Degrees into Radians.) But, for the general concept, we only need to visualize our unit circle:
The function has the angle and radius, so using trig, we can figure out the Also notice that the function uses the opposite value of the angle for the The second function is so the user can click on our control and the point can be converted into a matching angle. It is only trickier because we need to add some things into consideration. To keep the article at a reasonable length, I’ll post part of it: private float XYToDegrees(Point xy, Point origin)
{
double angle = 0.0;
if (xy.Y < origin.Y)
{
if (xy.X > origin.X)
{
angle = (double)(xy.X - origin.X) / (double)(origin.Y - xy.Y);
angle = Math.Atan(angle);
angle = 90.0 - angle * 180.0 / Math.PI;
}
else if (xy.X < origin.X)
{
//etc.
}
}
else if (xy.Y > origin.Y)
{
//etc.
}
if (angle > 180) angle -= 360;
return (float)angle;
}
The main gist of the function is to check the mouse’s position against the center of the control to determine on which of the four unit circle quadrants the point is in. Once we know the quadrant, we can use trig (the inverse of tangent) to figure out the angle. For these controls, I subtract Building the ControlDrawing the ControlThe background for both controls will be the same:
protected override void OnPaint(PaintEventArgs e)
{
//...
//Draw
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawEllipse(outline, drawRegion);
g.FillEllipse(fill, drawRegion);
//...Marker
g.SmoothingMode = SmoothingMode.HighSpeed;
g.FillRectangle(Brushes.Black, originSquare);
//...
}
The important part to note is the The marker part of the drawing depends on the control. The simple angle selector needs only to create a line from the origin to the point returned by our Handling User ClicksHandling the user clicks is extremely simple, thanks to our private int findNearestAngle(Point mouseXY)
{
int thisAngle = (int)XYToDegrees(mouseXY, origin);
if (thisAngle != 0)
return thisAngle;
else
return -1;
}
The altitude control will need an additional step, which is to find the distance between the center of the custom control and the mouse’s click point: private int findAltitude(Point mouseXY)
{
float distance = getDistance(mouseXY, origin);
int alt = 90 - (int)(90.0f * (distance / origin.X));
if (alt < 0) alt = 0;
return alt;
}
Altitude, as measured in Photoshop, is Custom EventsTo make the custom controls even more professional, they need to programmatically be able to alert when one of its values changes. This is where custom events come in. For example, to create an event for when the angle changes, first it’s defined like so: public delegate void AngleChangedDelegate();
public event AngleChangedDelegate AngleChanged;
Then, all we need to do is call Limitations and ImprovementsFlickeringThere is none! The .NET Framework 2.0 and up comes with the SizeSince both controls rely on math that assumes the radius to be equal at all times, the width and height dimensions must always be equal for the control to look and work correctly. ColorsI did not include properties for the background and outline colors of the control since I was going for the Photoshop look. However, look over the code and see that it wouldn’t be too hard to change the colors to your liking or even make them dynamic. ConclusionI recommend you download the project files (or at least the sample application) so you can see how well the controls come together. As for the uses and applications of the controls, that is up to your imagination.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||