using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Speedometer : MonoBehaviour {

	public Texture2D DialTexture, NeedleTexture;
	public Vector2 _dialPosition;
    public float _topSpeed, _stopAngle, _topSpeedAngle;
    [Range(0, 200)] public float Speed;

	void OnGUI()
	{
		//GUI.DrawTexture(new Rect(_dialPosition.x, _dialPosition.y, DialTexture.width, DialTexture.height), DialTexture);
		//Vector2 centre = new Vector2(_dialPosition.x + DialTexture.width / 2, _dialPosition.y + DialTexture.height / 2);
		Vector2 centre = new Vector2(_dialPosition.x + DialTexture.width / 2, _dialPosition.y + DialTexture.height / 1);
		Matrix4x4 savedMatrix = GUI.matrix;
		float speedFraction = Speed / _topSpeed;
		float needleAngle = Mathf.Lerp(_stopAngle, _topSpeedAngle, speedFraction);
		GUIUtility.RotateAroundPivot(needleAngle, centre);
		//GUI.DrawTexture(new Rect(centre.x, centre.y - NeedleTexture.height / 2, NeedleTexture.width, NeedleTexture.height), NeedleTexture);
		GUI.DrawTexture(new Rect(centre.x, centre.y - NeedleTexture.height / 4, NeedleTexture.width, NeedleTexture.height), NeedleTexture);
		GUI.matrix = savedMatrix;
	}

	void Update()
	{
		if (Input.GetKey (KeyCode.UpArrow)) {
			Speed += 1F;
		} else if (Input.GetKey (KeyCode.DownArrow)){
			Speed -= 1F;
		}
	}

}
