summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs
blob: 863e89e745d22ba4a4c2fe9f3d91fe005b3b6267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public struct PropWidgetParam
{
	// Ö÷¶¯µã»÷
	public System.Action<PropWidget> onSelected;
	public PropBase prop;
}

public class PropWidget : UIGridPropBase
{
	public Image Image_Icon;
	public Image Image_SelectBg;

	public Image Image_Use;

	private System.Action<PropWidget> onSelected;

	public PropBase prop { get { return m_Prop; } }
	private PropBase m_Prop;

	private Coroutine m_CoUse;

	private int m_PendingUse = 0;

	public void SetSelectBg(bool selected)
	{
		Image_SelectBg.gameObject.SetActive(selected);
	}

	public override void Set(object param)
	{
		PropWidgetParam info = (PropWidgetParam)param;
		onSelected = info.onSelected;
		m_Prop = info.prop;

		Image_Icon.sprite = ResourceManager.Instance.Load<Sprite>(info.prop.iconPath);

		SetSelectBg(false);

		Image_Use.gameObject.SetActive(false);
	}

	public void OnSelectCallback()
	{
		SetSelectBg(true);
	}

	public void OnDeselectCallback()
	{
		SetSelectBg(false);
	}

	public void OnUseCallback()
	{
		m_PendingUse++;
		PlayUseAnimation();
	}

	private void PlayUseAnimation()
	{
		if (m_CoUse != null)
			return;
		Image_Use.gameObject.SetActive(true);
		m_CoUse = StartCoroutine(CoUseAnimation());
	}

	IEnumerator CoUseAnimation()
	{
		float speed = 7f;

		while(m_PendingUse > 0)
		{
			Image_Use.fillOrigin = (int)Image.OriginVertical.Bottom;
			float t = 0;
			while (true)
			{
				t += speed * Time.deltaTime;

				if (t > 1)
					break;

				Image_Use.fillAmount = Mathf.Lerp(0, 1, t);

				yield return null;
			}

			Image_Use.fillOrigin = (int)Image.OriginVertical.Top;
			t = 0;
			while (true)
			{
				t += speed * Time.deltaTime;

				if (t > 1)
					break;

				Image_Use.fillAmount = Mathf.Lerp(1, 0, t);

				yield return null;
			}
			m_PendingUse--;
		}

		Image_Use.gameObject.SetActive(false);

		m_CoUse = null;

		yield break;
	}

	private void StopUseAnimation()
	{
		if (m_CoUse != null)
		{
			StopCoroutine(m_CoUse);
		}
		Image_Use.gameObject.SetActive(false);
	}

}