-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseMvcView.cs
205 lines (178 loc) · 5.38 KB
/
BaseMvcView.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#if NGUI
using System;
using System.Collections.Generic;
using UnityEngine;
using Renko.Utility;
using Renko.LapseFramework;
namespace Renko.MVCFramework
{
/// <summary>
/// The base class of all MVC view components.
/// </summary>
public class BaseMvcView : MonoBehaviour, IMvcView {
/// <summary>
/// Just keeping this value as a reference rather than hard-coding it every time an editor script needs it.
/// </summary>
public const string ClassName = "BaseMvcView";
/// <summary>
/// Whether to reset the UIPanel's rect based on current resolution.
/// </summary>
public bool ResizePanel = true;
/// <summary>
/// Backing field of ViewSize property.
/// </summary>
public ScreenAdaptor ViewSize;
/// <summary>
/// Whether the view is no longer being managed by MVC.
/// </summary>
private bool isDestroyed;
/// <summary>
/// Backing field of ViewId property.
/// </summary>
private int viewId;
/// <summary>
/// Backing field of Animations property for caching.
/// </summary>
private BaseMvcAnimation[] animations;
/// <summary>
/// The longest hide animation for temporary use.
/// </summary>
private BaseMvcAnimation longestHideAni;
/// <summary>
/// Returns the gameObject component of this view.
/// </summary>
public GameObject ViewObject {
get { return gameObject; }
}
/// <summary>
/// The unique id received from UIController upon creation.
/// </summary>
public int ViewId {
get { return viewId; }
}
/// <summary>
/// Returns whether this view is active.
/// If you're using Recycle method, MVC will check for this flag whether this view can be reused.
/// </summary>
public bool IsActive {
get { return gameObject.activeInHierarchy && !isDestroyed; }
}
/// <summary>
/// Array of animations included in this view.
/// </summary>
public BaseMvcAnimation[] Animations {
get {
if(animations == null)
animations = GetComponentsInChildren<BaseMvcAnimation>(true);
return animations;
}
}
/// <summary>
/// For integration with auto generated code with MVC base views.
/// You should use OnInitialize for the actual initialization process.
/// </summary>
public virtual void Awake() {
// Nothing to do!
}
/// <summary>
/// Use this method to resize UIPanel and handle view anchoring.
/// Called ONLY once after Awake() and before OnInitialize().
/// </summary>
public virtual void OnAdaptView(ScreenAdaptor viewSize, MvcRescaleType type) {
this.ViewSize = viewSize;
if(ResizePanel) {
Vector2 newSize = viewSize.GetScaledResolution((ScreenAdaptor.ScaleMode)(int)type);
UIPanel up = GetComponent<UIPanel>();
if(up != null) {
up.SetRect(0f, 0f, newSize.x, newSize.y);
}
}
}
/// <summary>
/// Use this method to handle initialization of fields, resources, etc.
/// Called ONLY once after Awake() and OnAdaptView().
/// </summary>
public virtual void OnInitialize(int viewId, MvcParameter param) {
this.viewId = viewId;
isDestroyed = false;
}
/// <summary>
/// Use this method to handle re-initialization of fields, resources, etc.
/// Will invoke OnViewShow() afterwards.
/// Called everytime this view is being recycled.
/// </summary>
public virtual void OnRecycle(int viewId, MvcParameter param) {
this.viewId = viewId;
isDestroyed = false;
}
/// <summary>
/// Use this method to handle view setup. Ideal place for a show animation, if any.
/// </summary>
public virtual void OnViewShow() {
PlayShowAnimations();
}
/// <summary>
/// Use this method to handle view hiding. Ideal place for a hide animation, if any.
/// You should return a MvcParameter value that represents a return data from this view.
/// If none, just return null.
/// Don't call this base method if you wish to handle animation yourself.
/// </summary>
public virtual MvcParameter OnViewHide() {
PlayHideAnimations();
return null;
}
/// <summary>
/// Use this method to dispose unused resources.
/// Called right before destruction/deactivation of the view for cleanup.
/// </summary>
public virtual void OnDisposeView() {
isDestroyed = true;
}
/// <summary>
/// Plays view animations flagged to view show event.
/// </summary>
public void PlayShowAnimations() {
var ani = Animations;
for(int i=0; i<ani.Length; i++)
ani[i].Play(MvcAnimationEvent.OnViewShow);
}
/// <summary>
/// Plays view animations flagged to view hide event.
/// Will also handle view disposal action.
/// </summary>
public void PlayHideAnimations() {
var ani = Animations;
longestHideAni = null;
for(int i=0; i<ani.Length; i++) {
var curAni = ani[i];
if(curAni.Play(MvcAnimationEvent.OnViewHide)) {
if(longestHideAni == null) {
longestHideAni = curAni;
}
else if(curAni.Duration > longestHideAni.Duration) {
longestHideAni = curAni;
}
}
else if(curAni.TargetEvent == MvcAnimationEvent.OnViewShow) {
curAni.FateAni.Stop();
}
}
// Raising view disposal event.
if(longestHideAni != null) {
longestHideAni.FateAni.AddEvent(FateEvents.OnEnd, OnHideAniEnded);
}
else {
MVC.DisposeView(this);
}
}
/// <summary>
/// Callback method after longestHideAni finishes.
/// </summary>
void OnHideAniEnded(IFateTimer item)
{
longestHideAni.FateAni.RemoveEvent(FateEvents.OnEnd, OnHideAniEnded);
MVC.DisposeView(this);
}
}
}
#endif