Touch Zone Interface
This code was developed to allow for location-dependent input on a touch screen. Capacitive touch screens are sensitive to small position changes, but they have a margin of error for any given touch location from a large, blunt human finger. This interface allows a program to define a region of any given shape and size. Touch input methods can then detect the presence of touches and gestures within that region.
This example shows a good purpose for interfaces in place of class inheritance.
public interface IZone
{
bool ContainsPoint(Vector2 point);
}
public class Vector2Zone: IZone
{
public Vector2 position1;
public Vector2 position2;
public Vector2Zone(Vector2 pos1, Vector2 pos2)
{
position1 = pos1;
position2 = pos2;
}
public bool ContainsPoint(Vector2 point)
{
if ((point.X >= Math.Min(position1.X, position2.X)) &&
(point.X <= Math.Max(position1.X, position2.X)) &&
(point.Y >= Math.Min(position1.Y, position2.Y)) &&
(point.Y <= Math.Max(position1.Y, position2.Y)))
return true;
else
return false;
}
}
public class CircleZone: IZone
{
public float radius;
public Vector2 center;
public CircleZone(Vector2 center, float radius)
{
this.center = center;
this.radius = radius;
}
public bool ContainsPoint(Vector2 point)
{
if (Vector2.DistanceSquared(center, point) <= (radius * radius))
return true;
else
return false;
//AKA return (Vector2.DistanceSquared(center, point) <= (radius * radius));
}
}The above code may be expanded or combined to define more complex shapes. Semicircles would be a combination of being inside a circle but not inside a rectangle. Rings would be a larger circle and not an inner circle.
This interface allows the design of input functions that disregard shape and location:
public bool IsLocationTouch(IZone zone)
{
for (int i = 0; i < touchPanelCapabilities.MaximumTouchCount; i++)
{
if (IsLocationTouch(i,zone)) return true;
}
return false;
}
public bool IsLocationTouch(int touchNumber, IZone zone)
{
return zone.ContainsPoint(touchPositions[touchNumber]);
}