Utility.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public static class Utility
  6. {
  7. public enum ModKey
  8. {
  9. Control, Shift, Alt
  10. }
  11. internal static int Wrap(int value, int min, int max)
  12. {
  13. max -= 1;
  14. return value < min ? max : value > max ? min : value;
  15. }
  16. internal static int GetPix(int num)
  17. {
  18. return (int)((1f + (Screen.width / 1280f - 1f) * 0.6f) * num);
  19. }
  20. internal static float Bound(float value, float left, float right)
  21. {
  22. if ((double)left > (double)right) return Mathf.Clamp(value, right, left);
  23. else return Mathf.Clamp(value, left, right);
  24. }
  25. internal static Texture2D MakeTex(int width, int height, Color color)
  26. {
  27. Color[] colors = new Color[width * height];
  28. for (int i = 0; i < colors.Length; i++)
  29. {
  30. colors[i] = color;
  31. }
  32. Texture2D texture2D = new Texture2D(width, height);
  33. texture2D.SetPixels(colors);
  34. texture2D.Apply();
  35. return texture2D;
  36. }
  37. internal static FieldInfo GetFieldInfo<T>(string field)
  38. {
  39. BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
  40. return typeof(T).GetField(field, bindingFlags);
  41. }
  42. internal static TValue GetFieldValue<TType, TValue>(TType instance, string field)
  43. {
  44. FieldInfo fieldInfo = GetFieldInfo<TType>(field);
  45. if (fieldInfo == null || !fieldInfo.IsStatic && instance == null) return default(TValue);
  46. return (TValue)fieldInfo.GetValue(instance);
  47. }
  48. internal static void SetFieldValue<TType, TValue>(TType instance, string name, TValue value)
  49. {
  50. FieldInfo fieldInfo = GetFieldInfo<TType>(name);
  51. fieldInfo.SetValue(instance, value);
  52. }
  53. internal static bool GetModKey(ModKey key)
  54. {
  55. switch (key)
  56. {
  57. case ModKey.Control: return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
  58. case ModKey.Alt: return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  59. case ModKey.Shift: return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  60. default: return false;
  61. }
  62. }
  63. internal static bool AnyMouseDown()
  64. {
  65. return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
  66. }
  67. }
  68. }