You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
8.0 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace ProGrids
  5. {
  6. public class pg_Preferences
  7. {
  8. static Color _gridColorX;
  9. static Color _gridColorY;
  10. static Color _gridColorZ;
  11. static float _alphaBump;
  12. static bool _scaleSnapEnabled;
  13. static int _snapMethod;
  14. static float _BracketIncreaseValue;
  15. static SnapUnit _GridUnits;
  16. static bool _syncUnitySnap;
  17. static KeyCode _IncreaseGridSize = KeyCode.Equals;
  18. static KeyCode _DecreaseGridSize = KeyCode.Minus;
  19. static KeyCode _NudgePerspectiveBackward = KeyCode.LeftBracket;
  20. static KeyCode _NudgePerspectiveForward = KeyCode.RightBracket;
  21. static KeyCode _NudgePerspectiveReset = KeyCode.Alpha0;
  22. static KeyCode _CyclePerspective = KeyCode.Backslash;
  23. /** Defaults **/
  24. public static Color GRID_COLOR_X = new Color(.9f, .46f, .46f, .15f);
  25. public static Color GRID_COLOR_Y = new Color(.46f, .9f, .46f, .15f);
  26. public static Color GRID_COLOR_Z = new Color(.46f, .46f, .9f, .15f);
  27. public static float ALPHA_BUMP = .25f;
  28. public static bool USE_AXIS_CONSTRAINTS = false;
  29. public static bool SHOW_GRID = true;
  30. static string[] SnapMethod = new string[]
  31. {
  32. "Snap on Selected Axis",
  33. "Snap on All Axes"
  34. };
  35. static int[] SnapVals = new int[] { 1, 0 };
  36. static bool prefsLoaded = false;
  37. [PreferenceItem("ProGrids")]
  38. public static void PreferencesGUI()
  39. {
  40. if (!prefsLoaded)
  41. prefsLoaded = LoadPreferences();
  42. // EditorGUILayout.HelpBox("Changes will take effect on the next ProGrids open.", MessageType.Info);
  43. GUILayout.Label("Grid Colors per Axis", EditorStyles.boldLabel);
  44. _gridColorX = EditorGUILayout.ColorField("X Axis", _gridColorX);
  45. _gridColorY = EditorGUILayout.ColorField("Y Axis", _gridColorY);
  46. _gridColorZ = EditorGUILayout.ColorField("Z Axis", _gridColorZ);
  47. _alphaBump = EditorGUILayout.Slider(new GUIContent("Tenth Line Alpha", "Every 10th line will have it's alpha value bumped by this amount."), _alphaBump, 0f, 1f);
  48. // not used
  49. // _BracketIncreaseValue = EditorGUILayout.FloatField(new GUIContent("Grid Increment Value", "Affects the amount by which the bracket keys will increment or decrement that snap value."), _BracketIncreaseValue);
  50. _GridUnits = (SnapUnit)EditorGUILayout.EnumPopup("Grid Units", _GridUnits);
  51. _scaleSnapEnabled = EditorGUILayout.Toggle("Snap On Scale", _scaleSnapEnabled);
  52. // GUILayout.BeginHorizontal();
  53. // EditorGUILayout.PrefixLabel(new GUIContent("Axis Constraints", "If toggled, objects will be automatically grid aligned on all axes when moving."));
  54. _snapMethod = EditorGUILayout.IntPopup("Snap Method", _snapMethod, SnapMethod, SnapVals);
  55. _syncUnitySnap = EditorGUILayout.Toggle("Sync w/ Unity Snap", _syncUnitySnap);
  56. // GUILayout.EndHorizontal();
  57. GUILayout.Label("Shortcuts", EditorStyles.boldLabel);
  58. _IncreaseGridSize = (KeyCode)EditorGUILayout.EnumPopup("Increase Grid Size", _IncreaseGridSize);
  59. _DecreaseGridSize = (KeyCode)EditorGUILayout.EnumPopup("Decrease Grid Size", _DecreaseGridSize);
  60. _NudgePerspectiveBackward = (KeyCode)EditorGUILayout.EnumPopup("Nudge Perspective Backward", _NudgePerspectiveBackward);
  61. _NudgePerspectiveForward = (KeyCode)EditorGUILayout.EnumPopup("Nudge Perspective Forward", _NudgePerspectiveForward);
  62. _NudgePerspectiveReset = (KeyCode)EditorGUILayout.EnumPopup("Nudge Perspective Reset", _NudgePerspectiveReset);
  63. _CyclePerspective = (KeyCode)EditorGUILayout.EnumPopup("Cycle Perspective", _CyclePerspective);
  64. if (GUILayout.Button("Reset"))
  65. {
  66. if (EditorUtility.DisplayDialog("Delete ProGrids editor preferences?", "Are you sure you want to delete these?, this action cannot be undone.", "Yes", "No"))
  67. ResetPrefs();
  68. }
  69. if (GUI.changed)
  70. SetPreferences();
  71. }
  72. public static bool LoadPreferences()
  73. {
  74. _scaleSnapEnabled = EditorPrefs.HasKey("scaleSnapEnabled") ? EditorPrefs.GetBool("scaleSnapEnabled") : false;
  75. _gridColorX = (EditorPrefs.HasKey("gridColorX")) ? pg_Util.ColorWithString(EditorPrefs.GetString("gridColorX")) : GRID_COLOR_X;
  76. _gridColorY = (EditorPrefs.HasKey("gridColorY")) ? pg_Util.ColorWithString(EditorPrefs.GetString("gridColorY")) : GRID_COLOR_Y;
  77. _gridColorZ = (EditorPrefs.HasKey("gridColorZ")) ? pg_Util.ColorWithString(EditorPrefs.GetString("gridColorZ")) : GRID_COLOR_Z;
  78. _alphaBump = (EditorPrefs.HasKey("pg_alphaBump")) ? EditorPrefs.GetFloat("pg_alphaBump") : ALPHA_BUMP;
  79. _snapMethod = System.Convert.ToInt32(
  80. (EditorPrefs.HasKey(pg_Constant.UseAxisConstraints)) ? EditorPrefs.GetBool(pg_Constant.UseAxisConstraints) : USE_AXIS_CONSTRAINTS
  81. );
  82. _BracketIncreaseValue = EditorPrefs.HasKey(pg_Constant.BracketIncreaseValue) ? EditorPrefs.GetFloat(pg_Constant.BracketIncreaseValue) : .25f;
  83. _GridUnits = (SnapUnit)(EditorPrefs.HasKey(pg_Constant.GridUnit) ? EditorPrefs.GetInt(pg_Constant.GridUnit) : 0);
  84. _syncUnitySnap = EditorPrefs.GetBool(pg_Constant.SyncUnitySnap, true);
  85. _IncreaseGridSize = EditorPrefs.HasKey("pg_Editor::IncreaseGridSize")
  86. ? (KeyCode)EditorPrefs.GetInt("pg_Editor::IncreaseGridSize")
  87. : KeyCode.Equals;
  88. _DecreaseGridSize = EditorPrefs.HasKey("pg_Editor::DecreaseGridSize")
  89. ? (KeyCode)EditorPrefs.GetInt("pg_Editor::DecreaseGridSize")
  90. : KeyCode.Minus;
  91. _NudgePerspectiveBackward = EditorPrefs.HasKey("pg_Editor::NudgePerspectiveBackward")
  92. ? (KeyCode)EditorPrefs.GetInt("pg_Editor::NudgePerspectiveBackward")
  93. : KeyCode.LeftBracket;
  94. _NudgePerspectiveForward = EditorPrefs.HasKey("pg_Editor::NudgePerspectiveForward")
  95. ? (KeyCode)EditorPrefs.GetInt("pg_Editor::NudgePerspectiveForward")
  96. : KeyCode.RightBracket;
  97. _NudgePerspectiveReset = EditorPrefs.HasKey("pg_Editor::NudgePerspectiveReset")
  98. ? (KeyCode)EditorPrefs.GetInt("pg_Editor::NudgePerspectiveReset")
  99. : KeyCode.Alpha0;
  100. _CyclePerspective = EditorPrefs.HasKey("pg_Editor::CyclePerspective")
  101. ? (KeyCode)EditorPrefs.GetInt("pg_Editor::CyclePerspective")
  102. : KeyCode.Backslash;
  103. return true;
  104. }
  105. public static void SetPreferences()
  106. {
  107. EditorPrefs.SetBool("scaleSnapEnabled", _scaleSnapEnabled);
  108. EditorPrefs.SetString("gridColorX", _gridColorX.ToString("f3"));
  109. EditorPrefs.SetString("gridColorY", _gridColorY.ToString("f3"));
  110. EditorPrefs.SetString("gridColorZ", _gridColorZ.ToString("f3"));
  111. EditorPrefs.SetFloat("pg_alphaBump", _alphaBump);
  112. EditorPrefs.SetBool(pg_Constant.UseAxisConstraints, System.Convert.ToBoolean(_snapMethod));
  113. EditorPrefs.SetFloat(pg_Constant.BracketIncreaseValue, _BracketIncreaseValue);
  114. EditorPrefs.SetInt(pg_Constant.GridUnit, (int)_GridUnits);
  115. EditorPrefs.SetBool(pg_Constant.SyncUnitySnap, _syncUnitySnap);
  116. EditorPrefs.SetInt("pg_Editor::IncreaseGridSize", (int)_IncreaseGridSize);
  117. EditorPrefs.SetInt("pg_Editor::DecreaseGridSize", (int)_DecreaseGridSize);
  118. EditorPrefs.SetInt("pg_Editor::NudgePerspectiveBackward", (int)_NudgePerspectiveBackward);
  119. EditorPrefs.SetInt("pg_Editor::NudgePerspectiveForward", (int)_NudgePerspectiveForward);
  120. EditorPrefs.SetInt("pg_Editor::NudgePerspectiveReset", (int)_NudgePerspectiveReset);
  121. EditorPrefs.SetInt("pg_Editor::CyclePerspective", (int)_CyclePerspective);
  122. if (pg_Editor.instance != null)
  123. {
  124. pg_Editor.instance.LoadPreferences();
  125. }
  126. }
  127. public static void ResetPrefs()
  128. {
  129. EditorPrefs.DeleteKey("scaleSnapEnabled");
  130. EditorPrefs.DeleteKey("gridColorX");
  131. EditorPrefs.DeleteKey("gridColorY");
  132. EditorPrefs.DeleteKey("gridColorZ");
  133. EditorPrefs.DeleteKey("pg_alphaBump");
  134. EditorPrefs.DeleteKey(pg_Constant.UseAxisConstraints);
  135. EditorPrefs.DeleteKey(pg_Constant.BracketIncreaseValue);
  136. EditorPrefs.DeleteKey(pg_Constant.GridUnit);
  137. EditorPrefs.DeleteKey("showgrid");
  138. EditorPrefs.DeleteKey(pg_Constant.SnapMultiplier);
  139. EditorPrefs.DeleteKey(pg_Constant.SyncUnitySnap);
  140. EditorPrefs.DeleteKey("pg_Editor::IncreaseGridSize");
  141. EditorPrefs.DeleteKey("pg_Editor::DecreaseGridSize");
  142. EditorPrefs.DeleteKey("pg_Editor::NudgePerspectiveBackward");
  143. EditorPrefs.DeleteKey("pg_Editor::NudgePerspectiveForward");
  144. EditorPrefs.DeleteKey("pg_Editor::NudgePerspectiveReset");
  145. EditorPrefs.DeleteKey("pg_Editor::CyclePerspective");
  146. LoadPreferences();
  147. }
  148. }
  149. }