Utility.cs 3.3 KB

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