using System;
using UnityEngine;

public class UI_ParticleImageFit : UI_ScreenFitBase
{
	protected override void Start()
	{
		this.m_MyParticle = base.GetComponent<ParticleSystem>();
		this.m_FirstSpeed = this.m_MyParticle.main.startSpeedMultiplier;
		this.m_FirstLifeTime = this.m_MyParticle.main.startLifetimeMultiplier;
		base.Start();
	}

	protected override void Update()
	{
		base.Update();
	}

	protected override void FitAction()
	{
		if (!this.FitImage)
		{
			return;
		}
		switch (this.m_ParticleType)
		{
		case UI_ParticleImageFit.ParticleType.Box:
			this.FitBox();
			break;
		case UI_ParticleImageFit.ParticleType.Edge:
			this.FitEdge();
			break;
		}
	}

	private void FitBox()
	{
		ParticleSystem.ShapeModule shape = this.m_MyParticle.shape;
		Vector3 vector = this.NguiToWorldSize();
		Vector3 box = vector;
		if (this.m_AsspectRatio.x != 0f && this.m_AsspectRatio.y != 0f)
		{
			UI_ParticleImageFit.FitType fitType = this.m_FitType;
			if (fitType != UI_ParticleImageFit.FitType.Horizontal)
			{
				if (fitType == UI_ParticleImageFit.FitType.Vertical)
				{
					box.x = vector.y * (this.m_AsspectRatio.x / this.m_AsspectRatio.y);
				}
			}
			else
			{
				box.y = vector.x * (this.m_AsspectRatio.y / this.m_AsspectRatio.x);
			}
		}
		shape.box = box;
	}

	private void FitEdge()
	{
		ParticleSystem.MainModule main = this.m_MyParticle.main;
		Vector3 vector = this.NguiToWorldSize();
		if (this.m_SpeedAdjust)
		{
			if (this.m_FitType == UI_ParticleImageFit.FitType.Horizontal)
			{
				main.startSpeedMultiplier = vector.x / this.m_FirstLifeTime;
			}
			else
			{
				main.startSpeedMultiplier = vector.y / this.m_FirstLifeTime;
			}
			main.startLifetimeMultiplier = this.m_FirstLifeTime;
		}
		else
		{
			if (this.m_FitType == UI_ParticleImageFit.FitType.Horizontal)
			{
				main.startLifetimeMultiplier = vector.x / this.m_FirstSpeed;
			}
			else
			{
				main.startLifetimeMultiplier = vector.y / this.m_FirstSpeed;
			}
			main.startSpeedMultiplier = this.m_FirstSpeed;
		}
	}

	private Vector3 NguiToWorldSize()
	{
		Vector3 zero = Vector3.zero;
		switch (this.m_FitType)
		{
		case UI_ParticleImageFit.FitType.Horizontal:
			zero.x = (float)Screen.width;
			break;
		case UI_ParticleImageFit.FitType.Vertical:
			zero.y = (float)Screen.height;
			break;
		case UI_ParticleImageFit.FitType.All:
			zero.x = (float)Screen.width;
			zero.y = (float)Screen.height;
			break;
		}
		Vector3 vector = UI_ScreenFitBase.PointToScreenPos(zero) * 2f;
		Vector3 zero2 = Vector3.zero;
		switch (this.m_FitType)
		{
		case UI_ParticleImageFit.FitType.Horizontal:
			zero2.x = (float)Screen.width * ((float)this.FitImage.width / vector.x + 0.5f);
			break;
		case UI_ParticleImageFit.FitType.Vertical:
			zero2.y = (float)Screen.height * ((float)this.FitImage.height / vector.y + 0.5f);
			break;
		case UI_ParticleImageFit.FitType.All:
			zero2.x = (float)Screen.width * ((float)this.FitImage.width / vector.x + 0.5f);
			zero2.y = (float)Screen.height * ((float)this.FitImage.height / vector.y + 0.5f);
			break;
		}
		Vector3 vector2 = UI_ScreenFitBase.m_NGUICamera.ScreenToWorldPoint(zero2) - UI_ScreenFitBase.m_NGUICamera.transform.position;
		if (this.m_UseFillAmount)
		{
			if (this.m_FillReverse)
			{
				vector2 *= 1f - this.FitImage.fillAmount;
			}
			else
			{
				vector2 *= this.FitImage.fillAmount;
			}
		}
		else if (this.m_UseSizeRate)
		{
			vector2.x *= this.SizeRateX;
			vector2.y *= this.SizeRateY;
		}
		return vector2;
	}

	public UIBasicSprite FitImage;

	[SerializeField]
	private UI_ParticleImageFit.FitType m_FitType;

	[SerializeField]
	private UI_ParticleImageFit.ParticleType m_ParticleType;

	[SerializeField]
	[Header("スプライトのFillAmountを使うかどうか")]
	private bool m_UseFillAmount;

	[SerializeField]
	private bool m_FillReverse;

	[SerializeField]
	[Header("画像に対して一定の割合を使うか")]
	private bool m_UseSizeRate;

	[Range(0f, 1f)]
	public float SizeRateX = 1f;

	[Range(0f, 1f)]
	public float SizeRateY = 1f;

	[Header("ParticleType->Box時の設定項目")]
	[SerializeField]
	[Header("パーティクルの縦横比率((0, 0)で比率固定されない)")]
	private Vector2 m_AsspectRatio = Vector2.zero;

	[Header("ParticleType->Edge時の設定項目")]
	[SerializeField]
	[Header("速度で調整するか(しない場合は生存時間で調整)")]
	private bool m_SpeedAdjust;

	private float m_FirstSpeed;

	private float m_FirstLifeTime;

	private ParticleSystem m_MyParticle;

	private enum FitType
	{
		Horizontal,
		Vertical,
		All
	}

	private enum ParticleType
	{
		Box,
		Edge,
		Other
	}
}
