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

public class ColorrPaletteParts : MonoBehaviour
{
	private void Awake()
	{
		Transform transform = base.transform;
		while (this.ui_camera_ == null && transform != null)
		{
			UIRoot component = transform.parent.GetComponent<UIRoot>();
			if (component != null)
			{
				this.ui_camera_ = component.GetComponentInChildren<Camera>();
			}
			transform = transform.parent;
		}
		Texture2D texture2D = Resources.Load("SceneEdit/ColorPalette/Texture/cm3d2_ColorPalette_PickerGray 2") as Texture2D;
		UIEventTrigger uieventTrigger = this.PaletteHitArea.GetComponent<UIEventTrigger>();
		if (uieventTrigger == null)
		{
			uieventTrigger = this.PaletteHitArea.gameObject.AddComponent<UIEventTrigger>();
		}
		EventDelegate.Add(uieventTrigger.onPress, new EventDelegate.Callback(this.OnMouseDownPalettePanel));
		EventDelegate.Add(uieventTrigger.onRelease, new EventDelegate.Callback(this.OnMouseUpPalettePanel));
		EventDelegate.Add(this.HueSlider.onChange, new EventDelegate.Callback(this.OnChangeHue));
		this.hsv_color_.s = 0f;
		this.hsv_color_.v = 1f;
	}

	public void SetColor(Color color)
	{
		this.hsv_color_ = new HSVColor(color);
		float num = this.hsv_color_.h / 360f;
		if (this.HueSlider.value != num)
		{
			this.HueSlider.value = num;
		}
		else
		{
			this.UIUpdate();
		}
	}

	public Color GetColor()
	{
		return this.hsv_color_.ToColor();
	}

	public void Update()
	{
		if (this.is_drag_pic_)
		{
			Vector3 position = this.ui_camera_.ScreenToWorldPoint(UICamera.lastTouchPosition);
			Vector3 v = this.PaletteHitArea.transform.InverseTransformPoint(position);
			Vector2 vector = this.ConvertToSelectorPos(v);
			Vector3 size = this.PaletteHitArea.size;
			this.hsv_color_.s = vector.x / size.x;
			this.hsv_color_.v = (size.y - vector.y) / size.y;
			this.UIUpdate();
		}
	}

	public void UIUpdate()
	{
		Vector3 size = this.PaletteHitArea.size;
		Vector2 v;
		v.x = size.x * this.hsv_color_.s;
		v.y = size.y * (1f - this.hsv_color_.v) * -1f;
		this.PaletteColorPic.transform.localPosition = v;
		float s = this.hsv_color_.s;
		float v2 = this.hsv_color_.v;
		this.hsv_color_.s = (this.hsv_color_.v = 1f);
		this.PaletteColorField.color = this.hsv_color_.ToColor();
		this.hsv_color_.s = s;
		this.hsv_color_.v = v2;
		if (this.onChangeValue != null && 0 < this.onChangeValue.Count)
		{
			for (int i = 0; i < this.onChangeValue.Count; i++)
			{
				this.onChangeValue[i](this.hsv_color_.ToColor());
			}
		}
	}

	private void OnMouseDownPalettePanel()
	{
		if (UICamera.currentTouchID == -1)
		{
			this.is_drag_pic_ = true;
		}
	}

	private void OnMouseUpPalettePanel()
	{
		this.is_drag_pic_ = false;
	}

	private void OnChangeHue()
	{
		this.hsv_color_.h = 360f * this.HueSlider.value;
		this.UIUpdate();
	}

	private Vector2 ConvertToSelectorPos(Vector2 pos)
	{
		Vector2 localSize = this.PaletteColorField.localSize;
		Vector2 localSize2 = this.PaletteColorPic.localSize;
		Vector2 zero = Vector2.zero;
		zero.x = (localSize.x + localSize2.x) / 2f + pos.x - localSize2.x / 2f;
		zero.y = (localSize.y + localSize2.y) / 2f - pos.y - localSize2.y / 2f;
		zero.x = Mathf.Clamp(zero.x, 0f, localSize.x);
		zero.y = Mathf.Clamp(zero.y, 0f, localSize.y);
		return zero;
	}

	public new bool enabled
	{
		get
		{
			return this.PaletteHitArea.enabled;
		}
		set
		{
			this.PaletteHitArea.enabled = value;
			UIButton[] componentsInChildren = base.GetComponentsInChildren<UIButton>();
			foreach (UIButton uibutton in componentsInChildren)
			{
				uibutton.isEnabled = value;
			}
			UISlider[] componentsInChildren2 = base.GetComponentsInChildren<UISlider>();
			foreach (UISlider uislider in componentsInChildren2)
			{
				uislider.enabled = value;
			}
		}
	}

	public bool visible
	{
		get
		{
			return base.gameObject.activeSelf;
		}
		set
		{
			base.gameObject.SetActive(value);
		}
	}

	public BoxCollider PaletteHitArea;

	public UISprite PaletteColorField;

	public UISprite PaletteColorPic;

	public UISlider HueSlider;

	public List<Action<Color>> onChangeValue = new List<Action<Color>>();

	private Camera ui_camera_;

	private bool is_drag_pic_;

	private HSVColor hsv_color_;
}
