This article has been dead for over three months
You
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Menu
{
public enum Axis
{
X, Y
}
public class LocationBounceEffect
{
public Axis BouncingAxis { get; private set; }
public int Offset { get; private set; }
public int Product { get; private set; }
public int StartX { get; private set; }
public int StartY { get; private set; }
public int X { get; private set; }
public int Y { get; private set; }
private Point Location;
private bool OffsetCompleted = false;
public LocationBounceEffect(Point Location, int Offset, Axis AxisToModify)
{
this.Location = Location;
this.StartX = Location.X;
this.StartY = Location.Y;
this.Offset = Offset;
this.BouncingAxis = AxisToModify;
}
public void PerformBounce()
{
if (!OffsetCompleted)
{
if (BouncingAxis == Axis.X && StartX + Product < StartX + Offset ||
BouncingAxis == Axis.Y && StartY + Product < StartY + Offset)
Product++;
else if (BouncingAxis == Axis.X && StartX + Product >= StartX + Offset ||
BouncingAxis == Axis.Y && StartY + Product >= StartY + Offset)
OffsetCompleted = true;
}
else
{
if (BouncingAxis == Axis.X && StartX + Product > StartX ||
BouncingAxis == Axis.Y && StartY + Product > StartY)
Product--;
else if (BouncingAxis == Axis.X && StartX + Product <= StartX ||
BouncingAxis == Axis.Y && StartY + Product <= StartY)
OffsetCompleted = false;
}
if (BouncingAxis == Axis.X)
Location = new Point(StartX + Product, Location.Y);
else
Location = new Point(Location.X, StartY + Product);
this.X = Location.X;
this.Y = Location.Y;
}
public void Reset()
{
this.Product = 0;
Location = new Point(StartX, StartY);
this.X = Location.X;
this.Y = Location.Y;
OffsetCompleted = false;
}
}
public class MenuEntry
{
public string Text = "";
public Point Location;
public Color Color;
public Font Font = new Font("OCR A Extended", 8.25f, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
public MenuEntry(string Text)
{
this.Text = Text;
Color = Color.White;
}
public MenuEntry(string Text, Color Color)
{
this.Text = Text;
this.Color = Color;
}
public MenuEntry(string Text, Font Font, Color Color)
{
this.Text = Text;
this.Font = Font;
this.Color = Color;
}
public MenuEntry(string Text, string FontName, float FontSize, Color Color)
{
this.Text = Text;
this.Font = new Font(FontName, FontSize, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
this.Color = Color;
}
}
public class Menu
{
private LocationBounceEffect[] LBE = new LocationBounceEffect[50];
private Input Input = new Input();
private List<MenuEntry> MenuItems;
public List<Label> Items { get; private set; }
public Point Location { get; private set; }
public int Length { get; private set; }
public int SelectedIndex { get; private set; }
public Color SelectionColor { get; private set; }
public Menu(Point Location)
{
MenuItems = new List<MenuEntry>();
this.Length = 0;
this.SelectedIndex = 0;
this.Location = Location;
this.SelectionColor = Color.White;
Items = new List<Label>();
}
public Menu(Point Location, Color SelectionColor)
{
MenuItems = new List<MenuEntry>();
this.Length = 0;
this.SelectedIndex = 0;
this.Location = Location;
this.SelectionColor = SelectionColor;
this.Items = new List<Label>();
}
public void AddEntry(MenuEntry Entry)
{
MenuItems.Add(Entry);
Items.Add(new Label());
if (this.Length == 0)
MenuItems[Length].Location = this.Location;
else
MenuItems[Length].Location = new Point(this.Location.X, MenuItems[Length - 1].Location.Y + Items[Length - 1].Size.Height + 5);
Items[Length].BackColor = Color.Transparent;
Items[Length].Text = Entry.Text;
Items[Length].Font = Entry.Font;
Items[Length].ForeColor = Entry.Color;
Items[Length].Location = MenuItems[Length].Location;
LBE[Length] = new LocationBounceEffect(Items[Length].Location, 10, Axis.X);
Length++;
}
public void Navigate(KeyEventArgs e)
{
if (Input.NavigatingUp(e))
{
if (SelectedIndex <= 0)
SelectedIndex = MenuItems.Count;
else
SelectedIndex--;
}
else if (Input.NavigatingDown(e))
{
if (SelectedIndex >= MenuItems.Count)
SelectedIndex = 0;
else
SelectedIndex++;
}
}
public void Update()
{
for (int i = 0; i < Items.Count; i++)
Items[i].ForeColor = MenuItems[i].Color;
Items[SelectedIndex].ForeColor = this.SelectionColor;
LBE[SelectedIndex].Reset();
}
public void UpdateBounce()
{
LBE[SelectedIndex].PerformBounce();
Items[SelectedIndex].Location = new Point(LBE[SelectedIndex].X, LBE[SelectedIndex].Y);
}
}
public class Input
{
private bool Up = false;
private bool Down = false;
public bool NavigatingUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
Up = true;
Thread.Sleep(10);
}
else
Up = false;
return Up;
}
public bool NavigatingDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
Down = true;
Thread.Sleep(10);
}
else
Down = false;
return Down;
}
}
}