Utility.cs 3.3 KB

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