Skip to content

Commit 0360173

Browse files
committed
feat: Add delta and delta p extension
1 parent 628c536 commit 0360173

File tree

3 files changed

+129
-85
lines changed

3 files changed

+129
-85
lines changed

Assets/JCSUnity/Scripts/UI/Timer/JCS_SpriteTimer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* $Notice: See LICENSE.txt for modification and distribution information
77
* Copyright (c) 2016 by Shen, Jen-Chieh $
88
*/
9+
using System;
910
using UnityEngine;
1011
using MyBox;
1112

@@ -18,7 +19,7 @@ public class JCS_SpriteTimer : MonoBehaviour
1819
{
1920
/* Variables */
2021

21-
public TimeIsUpFunc timeIsUpCallback = null;
22+
public Action onTimeUp = null;
2223

2324
private const float MAX_HOUR_TIME = 23.0f;
2425
private const float MAX_MINUTE_TIME = 59.0f;
@@ -32,7 +33,7 @@ public class JCS_SpriteTimer : MonoBehaviour
3233

3334
[SerializeField]
3435
[ReadOnly]
35-
private bool mDoTimeIsUpCallback = false;
36+
private bool mDoTimeUpCallback = false;
3637

3738
[Separator("Runtime Variables (JCS_SpriteTimer)")]
3839

@@ -218,7 +219,7 @@ public void SetCurrentTime(float hour, float minute, float second)
218219
UpdateTimeUI();
219220

220221
// reset callback everytime we set to a new time.
221-
this.mDoTimeIsUpCallback = false;
222+
this.mDoTimeUpCallback = false;
222223
}
223224

224225
/// <summary>
@@ -544,14 +545,13 @@ private void DoTimer()
544545
/// </summary>
545546
private void DoTimeIsUpCallback()
546547
{
547-
if (!IsTimeUp() || mDoTimeIsUpCallback)
548+
if (!IsTimeUp() || mDoTimeUpCallback)
548549
return;
549550

550551
// make sure we only do one time the callback.
551-
mDoTimeIsUpCallback = true;
552+
mDoTimeUpCallback = true;
552553

553-
if (timeIsUpCallback != null)
554-
timeIsUpCallback.Invoke();
554+
onTimeUp?.Invoke();
555555
}
556556

557557
/// <summary>

Assets/JCSUnity/Scripts/UI/Timer/JCS_TextTimer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
#define TMP_PRO
1313

14+
using System;
1415
using UnityEngine;
1516
using MyBox;
1617

@@ -23,7 +24,7 @@ public class JCS_TextTimer : JCS_TextObject
2324
{
2425
/* Variables */
2526

26-
public TimeIsUpFunc timeIsUpCallback = null;
27+
public Action onTimeUp = null;
2728

2829
private const float MAX_HOUR_TIME = 23.0f;
2930
private const float MAX_MINUTE_TIME = 59.0f;
@@ -36,7 +37,7 @@ public class JCS_TextTimer : JCS_TextObject
3637
[Separator("Check Variables (JCS_TextTimer)")]
3738

3839
[SerializeField]
39-
private bool mDoTimeIsUpCallback = false;
40+
private bool mDoTimeUpCallback = false;
4041

4142
[SerializeField]
4243
private string mHoursText = "";
@@ -165,7 +166,7 @@ public void SetCurrentTime(float hour, float minute, float second)
165166
UpdateTimeUI();
166167

167168
// reset callback everytime we set to a new time.
168-
this.mDoTimeIsUpCallback = false;
169+
this.mDoTimeUpCallback = false;
169170
}
170171

171172
/// <summary>
@@ -367,14 +368,13 @@ private void DoTimer()
367368
/// </summary>
368369
private void DoTimeIsUpCallback()
369370
{
370-
if (!IsTimeUp() || mDoTimeIsUpCallback)
371+
if (!IsTimeUp() || mDoTimeUpCallback)
371372
return;
372373

373374
// make sure we only do one time the callback.
374-
mDoTimeIsUpCallback = true;
375+
mDoTimeUpCallback = true;
375376

376-
if (timeIsUpCallback != null)
377-
timeIsUpCallback.Invoke();
377+
onTimeUp?.Invoke();
378378
}
379379

380380
/// <summary>

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 115 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ namespace JCSUnity
2121
// Function pointer
2222
public delegate float TweenDelegate(float t, float b, float c, float d);
2323

24-
/// <summary>
25-
/// Callback when time is up.
26-
/// </summary>
27-
public delegate void TimeIsUpFunc();
28-
2924
public delegate int JCS_Range(int min, int max);
30-
public delegate void ReattachCallback(Transform parent);
3125

3226
/// <summary>
3327
/// All the utility function put here.
@@ -88,6 +82,117 @@ public static bool Parse(string str, bool defaultValue)
8882

8983
#endregion
9084

85+
#region Number
86+
87+
/// <summary>
88+
/// Delta the `num` with `val` and clamp the result with `min`
89+
/// and `max`.
90+
/// </summary>
91+
public static int Delta(this int data, int val, int max)
92+
{
93+
return data.Delta(val, 0, max);
94+
}
95+
public static int Delta(this int data, int val, int min, int max)
96+
{
97+
return Mathf.Clamp(data + val, min, max);
98+
}
99+
100+
public static float Delta(this float data, float val, float max)
101+
{
102+
return data.Delta(val, 0.0f, max);
103+
}
104+
public static float Delta(this float data, float val, float min, float max)
105+
{
106+
return Mathf.Clamp(data + val, min, max);
107+
}
108+
109+
/// <summary>
110+
/// Delta the `num` with `val` by percentage and clamp the
111+
/// result with `min` and `max`.
112+
/// </summary>
113+
public static int DeltaP(this int data, int p, int max)
114+
{
115+
return data.DeltaP(p, 0, max);
116+
}
117+
public static int DeltaP(this int data, int p, int min, int max)
118+
{
119+
int val = (int)(max * p / 100.0f);
120+
121+
return data.Delta(val, min, max);
122+
}
123+
124+
public static float DeltaP(this float data, float p, float max)
125+
{
126+
return data.DeltaP(p, 0.0f, max);
127+
}
128+
public static float DeltaP(this float data, float p, float min, float max)
129+
{
130+
float val = (max * p / 100.0f);
131+
132+
return data.Delta(val, min, max);
133+
}
134+
135+
#endregion
136+
137+
#region String
138+
139+
/// <summary>
140+
/// Convert byte array to string by charset type.
141+
/// </summary>
142+
/// <param name="data"> Byte array data to convert to string data. </param>
143+
/// <param name="charset"> Target charset type. </param>
144+
/// <returns> String data that had been converted. </returns>
145+
public static string BytesToString(byte[] data, JCS_CharsetType charset)
146+
{
147+
switch (charset)
148+
{
149+
case JCS_CharsetType.DEFAULT: return Encoding.Default.GetString(data);
150+
case JCS_CharsetType.ASCII: return Encoding.ASCII.GetString(data);
151+
case JCS_CharsetType.UTF7: return Encoding.UTF7.GetString(data);
152+
case JCS_CharsetType.UTF8: return Encoding.UTF8.GetString(data);
153+
case JCS_CharsetType.UTF32: return Encoding.UTF32.GetString(data);
154+
case JCS_CharsetType.Unicode: return Encoding.Unicode.GetString(data);
155+
case JCS_CharsetType.BigEndianUnicode: return Encoding.BigEndianUnicode.GetString(data);
156+
}
157+
JCS_Debug.LogError("This shouldn't happens, charset `bytes to string`");
158+
return null;
159+
}
160+
161+
/// <summary>
162+
/// Convert string to byte array by charset type.
163+
/// </summary>
164+
/// <param name="data"> String data to convert to byte array. </param>
165+
/// <param name="charset"> Target charset type. </param>
166+
/// <returns> Byte array that had been converted. </returns>
167+
public static byte[] StringToBytes(string data, JCS_CharsetType charset)
168+
{
169+
switch (charset)
170+
{
171+
case JCS_CharsetType.DEFAULT: return Encoding.Default.GetBytes(data);
172+
case JCS_CharsetType.ASCII: return Encoding.ASCII.GetBytes(data);
173+
case JCS_CharsetType.UTF7: return Encoding.UTF7.GetBytes(data);
174+
case JCS_CharsetType.UTF8: return Encoding.UTF8.GetBytes(data);
175+
case JCS_CharsetType.UTF32: return Encoding.UTF32.GetBytes(data);
176+
case JCS_CharsetType.Unicode: return Encoding.Unicode.GetBytes(data);
177+
case JCS_CharsetType.BigEndianUnicode: return Encoding.BigEndianUnicode.GetBytes(data);
178+
}
179+
JCS_Debug.LogError("This shouldn't happens, charset `string to bytes`");
180+
return null;
181+
}
182+
183+
/// <summary>
184+
/// Simple version of escape url.
185+
/// </summary>
186+
/// <param name="url"> Url you want to escape. </param>
187+
/// <returns> Return the escaped url. </returns>
188+
public static string EscapeURL(string url)
189+
{
190+
url = url.Replace(" ", "%20");
191+
return url;
192+
}
193+
194+
#endregion
195+
91196
#region Enum
92197

93198
/// <summary>
@@ -398,65 +503,6 @@ public static List<T> RemoveEmptySlotIncludeMissing<T>(List<T> inList)
398503

399504
#endregion
400505

401-
#region String
402-
403-
/// <summary>
404-
/// Convert byte array to string by charset type.
405-
/// </summary>
406-
/// <param name="data"> Byte array data to convert to string data. </param>
407-
/// <param name="charset"> Target charset type. </param>
408-
/// <returns> String data that had been converted. </returns>
409-
public static string BytesToString(byte[] data, JCS_CharsetType charset)
410-
{
411-
switch (charset)
412-
{
413-
case JCS_CharsetType.DEFAULT: return Encoding.Default.GetString(data);
414-
case JCS_CharsetType.ASCII: return Encoding.ASCII.GetString(data);
415-
case JCS_CharsetType.UTF7: return Encoding.UTF7.GetString(data);
416-
case JCS_CharsetType.UTF8: return Encoding.UTF8.GetString(data);
417-
case JCS_CharsetType.UTF32: return Encoding.UTF32.GetString(data);
418-
case JCS_CharsetType.Unicode: return Encoding.Unicode.GetString(data);
419-
case JCS_CharsetType.BigEndianUnicode: return Encoding.BigEndianUnicode.GetString(data);
420-
}
421-
JCS_Debug.LogError("This shouldn't happens, charset `bytes to string`");
422-
return null;
423-
}
424-
425-
/// <summary>
426-
/// Convert string to byte array by charset type.
427-
/// </summary>
428-
/// <param name="data"> String data to convert to byte array. </param>
429-
/// <param name="charset"> Target charset type. </param>
430-
/// <returns> Byte array that had been converted. </returns>
431-
public static byte[] StringToBytes(string data, JCS_CharsetType charset)
432-
{
433-
switch (charset)
434-
{
435-
case JCS_CharsetType.DEFAULT: return Encoding.Default.GetBytes(data);
436-
case JCS_CharsetType.ASCII: return Encoding.ASCII.GetBytes(data);
437-
case JCS_CharsetType.UTF7: return Encoding.UTF7.GetBytes(data);
438-
case JCS_CharsetType.UTF8: return Encoding.UTF8.GetBytes(data);
439-
case JCS_CharsetType.UTF32: return Encoding.UTF32.GetBytes(data);
440-
case JCS_CharsetType.Unicode: return Encoding.Unicode.GetBytes(data);
441-
case JCS_CharsetType.BigEndianUnicode: return Encoding.BigEndianUnicode.GetBytes(data);
442-
}
443-
JCS_Debug.LogError("This shouldn't happens, charset `string to bytes`");
444-
return null;
445-
}
446-
447-
/// <summary>
448-
/// Simple version of escape url.
449-
/// </summary>
450-
/// <param name="url"> Url you want to escape. </param>
451-
/// <returns> Return the escaped url. </returns>
452-
public static string EscapeURL(string url)
453-
{
454-
url = url.Replace(" ", "%20");
455-
return url;
456-
}
457-
458-
#endregion
459-
460506
#region JSON
461507

462508
/// <summary>
@@ -785,20 +831,19 @@ public static void AttachChildren(RectTransform trans, List<RectTransform> child
785831
/// </summary>
786832
/// <param name="trans"> Transform you want to detach and reattach after callback. </param>
787833
/// <param name="callback"> Callback after detach and before reattach. </param>
788-
public static void ReattachSelf(Transform trans, ReattachCallback callback)
834+
public static void ReattachSelf(Transform trans, System.Action<Transform> callback)
789835
{
790836
if (trans == null || callback == null)
791837
return;
792838

793839
var parent = trans.parent;
794840
trans.SetParent(null);
795841

796-
if (callback != null)
797-
callback.Invoke(parent);
842+
callback?.Invoke(parent);
798843

799844
trans.SetParent(parent);
800845
}
801-
public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
846+
public static void ReattachSelf(RectTransform trans, System.Action<Transform> callback)
802847
{
803848
if (trans == null || callback == null)
804849
return;
@@ -808,8 +853,7 @@ public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
808853
var parent = trans.parent;
809854
trans.SetParent(canvas.AppRect);
810855

811-
if (callback != null)
812-
callback.Invoke(parent);
856+
callback?.Invoke(parent);
813857

814858
trans.SetParent(parent);
815859
}

0 commit comments

Comments
 (0)