Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MoyStick, a Joystick Control

0.00/5 (No votes)
2 Jun 2005 1  
An eight direction joystick control.

Sample Image

Introduction

This control emulates an eight direction joystick. Unlike conventional joysticks, this control also provides a magnitude measurement depending on how far the end of the "stick" is from the center of the control.

Using the code

Attributes you may want to be aware of:

  • BackColor sets the color of the control's circular region.
  • CircleDiameter sets the diameter (in pixels) of the control.
  • LineColor sets the color of the control's "stick".
  • LineShapeEnd sets the shape of the end of the "stick".
  • LineWidth sets the width of the "stick" (in pixels).

Using the mouse stick with your app requires the use of the MouseStickMoved event. The eventargs provides two attributes, Magnitude and Direction.

  • Magnitude will return an integer value between 0 and 15, depending on the distance between the end of your "stick" and the center of the control.
  • Direction returns an enumeration of type cPoint, which is defined as follows:
    public enum cPoint
    {
        north,
        northEast,
        east,
        southEast,
        south,
        southWest,
        west,
        northWest
    }

For example, in my demo program, I set my circle's acceleration and direction in the form's userControl11_MouseStickMoved event handler:

private void userControl11_MouseStickMoved(object sender, 
                              MoyStick.MouseStickEventArgs e)
{
    stbMouseStick.Text = "direction: " + e.Direction.ToString() + 
                         " magnitude: " + e.Magnitude.ToString();
    switch(e.Direction)
    {
        case MoyStick.cPoint.north:
            dY = e.Magnitude * (-1);
            dX = 0;
            break;
        case MoyStick.cPoint.northEast:
            dY = e.Magnitude * (-1);
            dX = e.Magnitude;
            break;
        //(And so on......)

Points of Interest

I used a timer to invalidate to eliminate intense flickering that I observed when I tried to invalidate every time the MouseMove event occurred.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here