示例#1
0
    /// <summary>
    /// fixed 90 degree rotation
    /// </summary>
    public void RotateItemFromButtonFixed()
    {
        if (selectedItem != null)
        {
            selectedItem.AddRotation();
            float rot = selectedItem.YRotation;
            //rounding rotation to closest angle (0, 90, 180, 270)
            float[] fixedAngles = new float[] { 0f, 90f, 180f, 270f, 360f };

            float distance     = 400f;
            float roundedValue = float.NaN;
            foreach (float f in fixedAngles)
            {
                float d = Mathf.Abs(rot - f);
                if (d < distance)
                {
                    distance     = d;
                    roundedValue = f;
                }
            }
            roundedValue = roundedValue == 360f ? 0 : roundedValue;
            selectedItem.Rotate(roundedValue);
        }
    }