Skip to content

Commit e05d063

Browse files
committed
chore: Add enum module
1 parent 994e226 commit e05d063

File tree

8 files changed

+91
-31
lines changed

8 files changed

+91
-31
lines changed

Assets/JCSUnity/Scripts/Input/JCS_Input.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ public static bool GetButtonUp(params string[] names)
762762
public static KeyCode GetAnyKeyByAction(JCS_KeyActionType type)
763763
{
764764
// loop through the key code list
765-
foreach (KeyCode val in JCS_Util.GetValues<KeyCode>())
765+
foreach (KeyCode val in JCS_Enum.GetValues<KeyCode>())
766766
{
767767
// if the key is pressed, return it.
768768
if (GetKeyByAction(type, val))

Assets/JCSUnity/Scripts/Input/JCS_InputController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class JCS_InputController : MonoBehaviour
2121
{
2222
public static int GAMEPAD_COUNT = 0; // How many gamepad in this game?
2323

24-
public static int SELECT_GAMEPAD_TYPE= 0;
24+
public static int SELECT_GAMEPAD_TYPE = 0;
2525

2626
public static string[] GAMEPAD_PLATFORM = {
2727
"Select Platform",
@@ -185,7 +185,7 @@ public static void SetupInputManager()
185185

186186
/* Microsoft XBox */
187187
case 5: /* ==> XBox <== */
188-
188+
189189
break;
190190
case 6: /* ==> XBox 360 <== */
191191
SetupXBox360Joystick();
@@ -561,7 +561,7 @@ public static void SetupPS4Joystick()
561561

562562
for (int joystickNum = 0; joystickNum < GAMEPAD_COUNT; ++joystickNum)
563563
{
564-
foreach (JCS_JoystickButton val in JCS_Util.GetValues<JCS_JoystickButton>())
564+
foreach (JCS_JoystickButton val in JCS_Enum.GetValues<JCS_JoystickButton>())
565565
{
566566
if (val == JCS_JoystickButton.NONE)
567567
continue;
@@ -594,7 +594,7 @@ public static void SetupXBox360Joystick()
594594

595595
for (int joystickNum = 0; joystickNum < GAMEPAD_COUNT; ++joystickNum)
596596
{
597-
foreach (JCS_JoystickButton val in JCS_Util.GetValues<JCS_JoystickButton>())
597+
foreach (JCS_JoystickButton val in JCS_Enum.GetValues<JCS_JoystickButton>())
598598
{
599599
if (val == JCS_JoystickButton.NONE)
600600
continue;

Assets/JCSUnity/Scripts/Network/Interface/JCS_PacketProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected void InitPacketHandlersArray<K>()
4141
}
4242

4343
int maxRecvOp = 0;
44-
foreach (K op in JCS_Util.GetValues<K>())
44+
foreach (K op in JCS_Enum.GetValues<K>())
4545
{
4646
int opId = Convert.ToInt32(op);
4747

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* $File: JCS_Enum.cs $
3+
* $Date: $
4+
* $Revision: $
5+
* $Creator: Jen-Chieh Shen $
6+
* $Notice: See LICENSE.txt for modification and distribution information
7+
* Copyright (c) 2025 by Shen, Jen-Chieh $
8+
*/
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
13+
namespace JCSUnity
14+
{
15+
/// <summary>
16+
/// Enumerator utilities.
17+
/// </summary>
18+
public static class JCS_Enum
19+
{
20+
/* Variables */
21+
22+
/* Setter & Getter */
23+
24+
/* Functions */
25+
26+
/// <summary>
27+
/// Enum typed version casting.
28+
/// Source: http://stackoverflow.com/questions/972307/can-you-loop-through-all-enum-values
29+
/// </summary>
30+
public static IEnumerable<T> GetValues<T>()
31+
{
32+
return Enum.GetValues(typeof(T)).Cast<T>();
33+
}
34+
35+
/// <summary>
36+
/// Convert enum values to array.
37+
/// </summary>
38+
public static ICollection<T> ToArray<T>()
39+
{
40+
return GetValues<T>().ToArray();
41+
}
42+
43+
/// <summary>
44+
/// Convert enum values to list.
45+
/// </summary>
46+
public static ICollection<T> ToList<T>()
47+
{
48+
return GetValues<T>().ToList();
49+
}
50+
51+
/// <summary>
52+
/// Retrieves an array of the names of the constants
53+
/// in a specified enumeration.
54+
/// </summary>
55+
public static string[] GetNames<T>()
56+
{
57+
return Enum.GetNames(typeof(T));
58+
}
59+
60+
/// <summary>
61+
/// Return the length of an enumerator.
62+
/// </summary>
63+
/// <typeparam name="T"> Enum type. </typeparam>
64+
/// <returns> Size of the enum listed. </returns>
65+
public static int Size<T>()
66+
{
67+
return GetNames<T>().Length;
68+
}
69+
}
70+
}

Assets/JCSUnity/Scripts/Util/JCS_Enum.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,29 +160,6 @@ public static string EscapeURL(string url)
160160

161161
#endregion
162162

163-
#region Enum
164-
165-
/// <summary>
166-
/// Enum typed version casting.
167-
/// Source: http://stackoverflow.com/questions/972307/can-you-loop-through-all-enum-values
168-
/// </summary>
169-
public static IEnumerable<T> GetValues<T>()
170-
{
171-
return System.Enum.GetValues(typeof(T)).Cast<T>();
172-
}
173-
174-
/// <summary>
175-
/// Return the length of an enumerator.
176-
/// </summary>
177-
/// <typeparam name="T"> Enum type. </typeparam>
178-
/// <returns> Size of the enum listed. </returns>
179-
public static int EnumSize<T>()
180-
{
181-
return System.Enum.GetNames(typeof(T)).Length;
182-
}
183-
184-
#endregion
185-
186163
#region Range
187164

188165
/// <summary>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# JCS_Enum
2+
3+
Enum utilities.
4+
5+
## Functions
6+
7+
| Name | Description |
8+
|:----------|:-----------------------------------------------------------------------------|
9+
| GetValues | Get the value for each enum, use to loop through the enum. |
10+
| ToArray | Convert enum values to array. |
11+
| ToList | Convert enum values to list. |
12+
| GetNames | Retrieves an array of the names of the constants in a specified enumeration. |
13+
| Size | Returns the length of an enumerator. |

docs/ScriptReference/Util/JCS_Util.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ All code utility is stored here.
1111
| BytesToString | Convert byte array to string by charset type. |
1212
| StringToBytes | Convert string to byte array by charset type. |
1313
| EscapeURL | Simple version of escape url. |
14-
| GetValues | Get the value for each enum, use to loop through the enum. |
15-
| EnumSize | Returns the length of an enumerator. |
1614
| WithInRange | Check if the value is within the range. |
1715
| WithInRange | Check if the index is valid within the collection's size. |
1816
| CopyBtyeArray | Copy byte array to another byte array memory space. |

0 commit comments

Comments
 (0)