DragPointProp.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. public class DragPointProp : DragPointGeneral
  9. {
  10. private List<Renderer> renderers;
  11. public AttachPointInfo AttachPointInfo { get; private set; } = AttachPointInfo.Empty;
  12. public string Name => MyGameObject.name;
  13. public string assetName = string.Empty;
  14. public PropInfo Info { get; set; }
  15. public bool ShadowCasting
  16. {
  17. get => renderers.Count != 0 && renderers.Any(r => r.shadowCastingMode == ShadowCastingMode.On);
  18. set
  19. {
  20. foreach (var renderer in renderers)
  21. renderer.shadowCastingMode = value ? ShadowCastingMode.On : ShadowCastingMode.Off;
  22. }
  23. }
  24. public override void Set(Transform myObject)
  25. {
  26. base.Set(myObject);
  27. DefaultRotation = MyObject.rotation;
  28. DefaultPosition = MyObject.position;
  29. DefaultScale = MyObject.localScale;
  30. renderers = new List<Renderer>(MyObject.GetComponentsInChildren<Renderer>());
  31. }
  32. public void AttachTo(Meido meido, AttachPoint point, bool keepWorldPosition = true)
  33. {
  34. var attachPoint = meido?.IKManager.GetAttachPointTransform(point);
  35. AttachPointInfo = meido == null ? AttachPointInfo.Empty : new AttachPointInfo(point, meido);
  36. var position = MyObject.position;
  37. var rotation = MyObject.rotation;
  38. var scale = MyObject.localScale;
  39. MyObject.transform.SetParent(attachPoint, keepWorldPosition);
  40. if (keepWorldPosition)
  41. {
  42. MyObject.position = position;
  43. MyObject.rotation = rotation;
  44. }
  45. else
  46. {
  47. MyObject.localPosition = Vector3.zero;
  48. MyObject.rotation = Quaternion.identity;
  49. }
  50. MyObject.localScale = scale;
  51. if (attachPoint == null) Utility.FixGameObjectScale(MyGameObject);
  52. }
  53. public void DetachFrom(bool keepWorldPosition = true) => AttachTo(null, AttachPoint.None, keepWorldPosition);
  54. public void DetachTemporary()
  55. {
  56. MyObject.transform.SetParent(null, true);
  57. Utility.FixGameObjectScale(MyGameObject);
  58. }
  59. protected override void ApplyDragType()
  60. {
  61. var active = DragPointEnabled && Transforming || Special;
  62. ApplyProperties(active, active, GizmoEnabled && Rotating);
  63. ApplyColours();
  64. }
  65. protected override void OnDestroy()
  66. {
  67. Destroy(MyGameObject);
  68. base.OnDestroy();
  69. }
  70. }
  71. public class PropInfo
  72. {
  73. public enum PropType { Mod, MyRoom, Bg, Odogu }
  74. public PropType Type { get; }
  75. public string IconFile { get; init; }
  76. public string Filename { get; init; }
  77. public string SubFilename { get; init; }
  78. public int MyRoomID { get; init; }
  79. public PropInfo(PropType type) => Type = type;
  80. public static PropInfo FromModItem(ModItem modItem) => new(PropType.Mod)
  81. {
  82. Filename = modItem.IsOfficialMod ? Path.GetFileName(modItem.MenuFile) : modItem.MenuFile,
  83. SubFilename = modItem.BaseMenuFile
  84. };
  85. public static PropInfo FromMyRoom(MyRoomItem myRoomItem) => new(PropType.MyRoom)
  86. {
  87. MyRoomID = myRoomItem.ID, Filename = myRoomItem.PrefabName
  88. };
  89. public static PropInfo FromBg(string name) => new(PropType.Bg) { Filename = name };
  90. public static PropInfo FromGameProp(string name) => new(PropType.Odogu) { Filename = name };
  91. }
  92. }