1
+ // This code is part of the Fungus library (https://github.com/snozbot/fungus)
2
+ // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
3
+
4
+ using UnityEngine ;
5
+ using System . Collections ;
6
+ using UnityEngine . UI ;
7
+ using System . Collections . Generic ;
8
+
9
+ public enum RotateZoomType
10
+ {
11
+ /// <summary> Fade In effect sequence. </summary>
12
+ SetCharacterToDefault ,
13
+ None
14
+ }
15
+ namespace Fungus
16
+ {
17
+ /// <summary>
18
+ /// Moves the camera to a location specified by a View object.
19
+ /// </summary>
20
+ [ CommandInfo ( "Animation" ,
21
+ "Zoom Flip Character" ,
22
+ "Zoom the character on Stage. MUST BE PLACED UNDER PORTRAIT if there's portrait change or at least not too close" ) ]
23
+ [ AddComponentMenu ( "" ) ]
24
+ public class CharacterZoom : Command
25
+ {
26
+ [ SerializeField ] RotateZoomType status = RotateZoomType . None ;
27
+ [ Tooltip ( "Character to zoom & scale" ) ]
28
+ [ SerializeField ] protected Character character ;
29
+ [ Tooltip ( "Scale character" ) ]
30
+ [ SerializeField ] protected Vector3 scaleCharacterUI = new Vector3 ( 1f , 1f , 1f ) ;
31
+ [ Tooltip ( "Wait until the fade has finished before executing next command" ) ]
32
+ [ SerializeField ] protected bool flipCharacter = false ;
33
+ [ Tooltip ( "Enable scale" ) ]
34
+ [ SerializeField ] protected bool enableScale = true ;
35
+ [ Tooltip ( "Enable move" ) ]
36
+ [ SerializeField ] protected bool enableMove = false ;
37
+ [ SerializeField ] protected bool enableRotate = false ;
38
+ [ Tooltip ( "Move character" ) ]
39
+ [ SerializeField ] protected Vector3 moveCharacterUI = new Vector3 ( 0f , 0f , 0f ) ;
40
+ [ Tooltip ( "Rotate angle in floats" ) ]
41
+ [ SerializeField ] protected float rotateAngle = 0f ;
42
+
43
+ [ Tooltip ( "Duration" ) ]
44
+ [ SerializeField ] protected float scaleMoveDuration = 0.3f ;
45
+ [ Tooltip ( "Ease type for the scale tween." ) ]
46
+ [ SerializeField ] protected LeanTweenType easeType ;
47
+
48
+ [ Tooltip ( "Wait until the fade has finished before executing next command" ) ]
49
+ [ SerializeField ] protected bool waitUntilFinished = true ;
50
+ private static List < GameObject > cacheChar = new List < GameObject > ( ) ;
51
+ protected bool isCompleted = false ;
52
+ protected bool moveIsCompleted = false ; protected bool moveIsCompleted1 = false ; protected bool moveIsCompleted2 = false ; protected bool moveIsCompleted3 = false ; protected bool rotIsTru = false ;
53
+ protected virtual void SetDefaultCharScale ( )
54
+ {
55
+ for ( int i = 0 ; i < cacheChar . Count ; i ++ )
56
+ {
57
+ Canvas . ForceUpdateCanvases ( ) ;
58
+ var b = cacheChar [ i ] ;
59
+ var c = b . GetComponent < RectTransform > ( ) ;
60
+
61
+ LeanTween . scale ( c , Vector3 . one , scaleMoveDuration ) . setRecursive ( true ) . setEase ( easeType ) . setOnComplete ( ( ) =>
62
+ {
63
+ isCompleted = false ;
64
+ } ) ;
65
+ //Reset character rotation if any
66
+ LeanTween . rotateAround ( c , Vector3 . forward , 0 , scaleMoveDuration ) . setEaseOutQuad ( ) . setOnComplete ( ( ) =>
67
+ {
68
+ c . rotation = Quaternion . identity ;
69
+ rotIsTru = false ;
70
+ } ) ;
71
+ }
72
+ }
73
+ protected virtual void SetDefaultCharPosition ( )
74
+ {
75
+ Canvas . ForceUpdateCanvases ( ) ;
76
+ for ( int i = 0 ; i < cacheChar . Count ; i ++ )
77
+ {
78
+ var b = cacheChar [ i ] ;
79
+ var c = b . GetComponent < RectTransform > ( ) ;
80
+
81
+ float newOffsetMinnX = c . offsetMin . x ;
82
+ float newOffsetMinnY = c . offsetMin . y ;
83
+
84
+ float newOffsetMaxxX = c . offsetMax . x ;
85
+ float newOffsetMaxxY = c . offsetMax . y ;
86
+
87
+ LeanTween . value ( b , newOffsetMinnX , 0f , scaleMoveDuration ) . setOnUpdate ( ( float val ) =>
88
+ {
89
+ c . offsetMin = new Vector2 ( val , c . offsetMin . y ) ;
90
+ } ) . setOnComplete ( ( ) =>
91
+ {
92
+ moveIsCompleted = false ;
93
+ } ) ;
94
+ LeanTween . value ( b , newOffsetMinnY , 0f , scaleMoveDuration ) . setOnUpdate ( ( float val ) =>
95
+ {
96
+ c . offsetMin = new Vector2 ( c . offsetMin . x , val ) ;
97
+ } ) . setOnComplete ( ( ) =>
98
+ {
99
+ moveIsCompleted1 = false ;
100
+ } ) ;
101
+
102
+ LeanTween . value ( b , newOffsetMaxxX , 0f , scaleMoveDuration ) . setOnUpdate ( ( float val ) =>
103
+ {
104
+ c . offsetMax = new Vector2 ( val , c . offsetMax . y ) ;
105
+ } ) . setOnComplete ( ( ) =>
106
+ {
107
+ moveIsCompleted2 = false ;
108
+ } ) ;
109
+ LeanTween . value ( b , newOffsetMaxxY , 0f , scaleMoveDuration ) . setOnUpdate ( ( float val ) =>
110
+ {
111
+ c . offsetMax = new Vector2 ( c . offsetMax . x , val ) ;
112
+ } ) . setOnComplete ( ( ) =>
113
+ {
114
+ moveIsCompleted3 = false ;
115
+ } ) ;
116
+
117
+ StartCoroutine ( FinalStop ( ) ) ;
118
+ }
119
+ }
120
+ protected virtual IEnumerator FinalStop ( )
121
+ {
122
+ while ( true )
123
+ {
124
+ yield return null ;
125
+ if ( ! moveIsCompleted && ! moveIsCompleted1 && ! moveIsCompleted2 && ! moveIsCompleted3 && ! isCompleted && ! rotIsTru )
126
+ {
127
+ cacheChar = new List < GameObject > ( ) ;
128
+ break ;
129
+ }
130
+ }
131
+ Stops ( ) ;
132
+ }
133
+ protected virtual void Stops ( )
134
+ {
135
+ for ( int i = character . State . holder . transform . childCount - 1 ; i >= 0 ; i -- )
136
+ {
137
+ var b = character . State . holder . transform . GetChild ( i ) . gameObject ;
138
+ b . GetComponent < RectTransform > ( ) . anchoredPosition = Vector3 . zero ;
139
+ }
140
+ Continue ( ) ;
141
+ StopAllCoroutines ( ) ;
142
+ }
143
+ protected virtual void Continues ( )
144
+ {
145
+ Continue ( ) ;
146
+ }
147
+
148
+ protected virtual void ZoomRotateCharacter ( )
149
+ {
150
+ if ( character && character . State . portraitImage != null )
151
+ {
152
+ if ( cacheChar != null )
153
+ {
154
+ var holdersS = character . State . holder . GetComponent < RectTransform > ( ) ;
155
+ for ( int i = 0 ; i < cacheChar . Count ; i ++ )
156
+ {
157
+ var b = cacheChar [ i ] ;
158
+ var c = b . GetComponent < RectTransform > ( ) ;
159
+ if ( enableScale )
160
+ {
161
+ LeanTween . scale ( c , scaleCharacterUI , scaleMoveDuration ) . setRecursive ( true ) . setEase ( easeType ) . setOnComplete ( ( ) =>
162
+ {
163
+ isCompleted = true ;
164
+ Continues ( ) ;
165
+ } ) ;
166
+ }
167
+ if ( enableMove )
168
+ {
169
+ LeanTween . move ( c , moveCharacterUI , scaleMoveDuration ) . setEase ( easeType ) . setOnComplete ( ( ) =>
170
+ {
171
+ moveIsCompleted = true ;
172
+ moveIsCompleted1 = true ;
173
+ moveIsCompleted2 = true ;
174
+ moveIsCompleted3 = true ;
175
+ Continues ( ) ;
176
+
177
+ } ) ;
178
+ }
179
+ if ( flipCharacter )
180
+ {
181
+ LeanTween . scale ( c , new Vector3 ( - scaleCharacterUI . x , scaleCharacterUI . y , scaleCharacterUI . z ) , scaleMoveDuration ) . setEase ( easeType ) . setOnComplete ( ( ) =>
182
+ {
183
+ Continues ( ) ;
184
+ } ) ;
185
+ }
186
+ if ( enableRotate )
187
+ {
188
+ LeanTween . rotateAround ( c , Vector3 . forward , rotateAngle , scaleMoveDuration ) . setEase ( easeType ) . setOnComplete ( ( ) =>
189
+ {
190
+ Continues ( ) ;
191
+ } ) ;
192
+ }
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ #region Public members
199
+
200
+ public override void OnEnter ( )
201
+ {
202
+ if ( status == RotateZoomType . SetCharacterToDefault )
203
+ {
204
+ if ( character != null )
205
+ {
206
+ moveIsCompleted = true ;
207
+ moveIsCompleted1 = true ;
208
+ moveIsCompleted2 = true ;
209
+ moveIsCompleted3 = true ;
210
+ isCompleted = true ;
211
+ rotIsTru = true ;
212
+
213
+ SetDefaultCharPosition ( ) ;
214
+ SetDefaultCharScale ( ) ;
215
+ }
216
+
217
+ if ( ! waitUntilFinished )
218
+ {
219
+ Continue ( ) ;
220
+ }
221
+ }
222
+ else
223
+ {
224
+ //Create new list on first run
225
+ cacheChar = new List < GameObject > ( ) ;
226
+ //Cache the character to a list
227
+ if ( character != null )
228
+ {
229
+ for ( int i = character . State . holder . transform . childCount - 1 ; i >= 0 ; i -- )
230
+ {
231
+ var b = character . State . holder . transform . GetChild ( i ) . gameObject ;
232
+ cacheChar . Add ( b ) ;
233
+ }
234
+ }
235
+
236
+ if ( character != null )
237
+ {
238
+ ZoomRotateCharacter ( ) ;
239
+ }
240
+
241
+ if ( ! waitUntilFinished )
242
+ {
243
+ Continue ( ) ;
244
+ }
245
+ }
246
+ }
247
+ public override string GetSummary ( )
248
+ {
249
+ if ( character == null )
250
+ {
251
+ return "Error: No character selected" ;
252
+ }
253
+ else
254
+ {
255
+ return character . name ;
256
+ }
257
+ }
258
+
259
+ public override Color GetButtonColor ( )
260
+ {
261
+ return new Color32 ( 216 , 228 , 170 , 255 ) ;
262
+ }
263
+
264
+ #endregion
265
+ }
266
+ }
0 commit comments