Utility.cs 3.9 KB

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