//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace Gauges
{
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;
///
/// Select which gauge to use.
///
public partial class GaugeSelector : Form
{
///
/// Gets Selected gauge.
///
/// 'null' if no gauge selected.
///
///
public uint? selectedItem { get; private set; }
///
/// Coordinates for gauges.
///
private static readonly int[][] coords =
{
new int[] {116, 93, 313, 290},
new int[] {329, 93, 525, 290},
new int[] { 13, 14, 100, 100},
new int[] { 13, 115, 100, 202},
new int[] { 13, 218, 100, 305},
new int[] {541, 14, 627, 100},
new int[] {541, 115, 627, 202},
new int[] {541, 218, 627, 305},
};
///
/// Current gauge.
///
private uint currentItem;
///
/// Used gauges.
///
private uint[] usedItems;
///
/// Initializes a new instance of the class.
///
/// Current gauge.
/// Used gauges.
public GaugeSelector(uint currentItem, uint[] usedItems)
{
this.selectedItem = currentItem;
this.currentItem = currentItem;
this.usedItems = usedItems;
this.InitializeComponent();
}
///
/// Gets a list of all gauges.
///
/// List of all gauges.
public static IList allGauges()
{
IList gauges = new List();
for (uint i = 0; i < coords.Length; i++)
{
gauges.Add(i);
}
return gauges;
}
///
/// Draws the pointer.
///
/// Event arguments.
protected override void OnPaint(PaintEventArgs e)
{
if (e != null)
{
base.OnPaint(e);
if (this.usedItems != null)
{
// Graphics gr = e.Graphics;
Graphics gr = Graphics.FromImage(this.pictureBox1.Image);
Color c2 = Color.FromArgb(63, Color.Green);
for (int i = 0; i < coords.Length; i++)
{
if (i == this.currentItem)
{
using (SolidBrush brush = new SolidBrush(c2))
{
gr.FillRectangle(brush, coords[i][0], coords[i][1], coords[i][2] - coords[i][0], coords[i][3] - coords[i][1]);
}
}
}
Color c1 = Color.FromArgb(127, Color.Red);
foreach (uint item in this.usedItems)
{
if (item != this.currentItem)
{
using (SolidBrush brush = new SolidBrush(c1))
{
gr.FillRectangle(brush, coords[item][0], coords[item][1], coords[item][2] - coords[item][0], coords[item][3] - coords[item][1]);
}
}
}
}
}
}
///
/// Mouse clicked.
///
/// Sending object.
/// Event data.
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Point p1 = e.Location;
this.selectedItem = null;
for (uint i = 0; i < coords.Length; i++)
{
if (coords[i][0] < p1.X
&& coords[i][2] > p1.X
&& coords[i][1] < p1.Y
&& coords[i][3] > p1.Y)
{
bool available = true;
if (this.usedItems != null)
{
for (int j = 0; available && j < this.usedItems.Length; j++)
{
if (this.usedItems[j] == i && i != this.currentItem)
{
available = false;
}
}
}
if (available)
{
this.selectedItem = i;
break;
}
}
}
this.textBox1.Text = this.selectedItem.ToString();
this.okButton.Enabled = (this.selectedItem >= 0);
}
///
/// OK button clicked.
///
/// Sending object.
/// Event data.
private void okButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
///
/// Cancel button clicked.
///
/// Sending object.
/// Event data.
private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}