| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | internal class SceneCardViewContextMenu : MonoBehaviour |
| | 6 | | { |
| | 7 | | [Header("Containers")] |
| | 8 | | [SerializeField] internal GameObject headerContainer; |
| | 9 | | [SerializeField] internal GameObject headerSeparator; |
| | 10 | |
|
| | 11 | | [Header("Buttons")] |
| | 12 | | [SerializeField] internal Button settingsButton; |
| | 13 | | [SerializeField] internal Button duplicateAsProjectButton; |
| | 14 | | [SerializeField] internal Button duplicateButton; |
| | 15 | | [SerializeField] internal Button downloadButton; |
| | 16 | | [SerializeField] internal Button shareButton; |
| | 17 | | [SerializeField] internal Button unpublishButton; |
| | 18 | | [SerializeField] internal Button deleteButton; |
| | 19 | | [SerializeField] internal Button quitContributorButton; |
| | 20 | |
|
| | 21 | | public event Action<string> OnSettingsPressed; |
| | 22 | | public event Action<string> OnDuplicatePressed; |
| | 23 | | public event Action<string> OnDownloadPressed; |
| | 24 | | public event Action<string> OnSharePressed; |
| | 25 | | public event Action<string> OnUnpublishPressed; |
| | 26 | | public event Action<string> OnDeletePressed; |
| | 27 | | public event Action<string> OnQuitContributorPressed; |
| | 28 | |
|
| | 29 | | [System.Flags] |
| | 30 | | public enum ConfigFlags |
| | 31 | | { |
| | 32 | | Settings = 1, |
| | 33 | | DuplicateAsProject = 2, |
| | 34 | | Duplicate = 4, |
| | 35 | | Download = 8, |
| | 36 | | Share = 16, |
| | 37 | | Unpublish = 32, |
| | 38 | | Delete = 64, |
| | 39 | | QuitContributor = 128 |
| | 40 | | } |
| | 41 | |
|
| | 42 | | private const ConfigFlags headerFlags = ConfigFlags.Settings; |
| | 43 | |
|
| | 44 | | private string sceneId; |
| | 45 | | private RectTransform rectTransform; |
| | 46 | |
|
| | 47 | | private void Awake() |
| | 48 | | { |
| 3 | 49 | | rectTransform = GetComponent<RectTransform>(); |
| | 50 | |
|
| 3 | 51 | | settingsButton.onClick.AddListener(() => |
| | 52 | | { |
| 0 | 53 | | OnSettingsPressed?.Invoke(sceneId); |
| 0 | 54 | | Hide(); |
| 0 | 55 | | }); |
| 3 | 56 | | duplicateAsProjectButton.onClick.AddListener(() => |
| | 57 | | { |
| 0 | 58 | | OnDuplicatePressed?.Invoke(sceneId); |
| 0 | 59 | | Hide(); |
| 0 | 60 | | }); |
| 3 | 61 | | duplicateButton.onClick.AddListener(() => |
| | 62 | | { |
| 0 | 63 | | OnDuplicatePressed?.Invoke(sceneId); |
| 0 | 64 | | Hide(); |
| 0 | 65 | | }); |
| 3 | 66 | | downloadButton.onClick.AddListener(() => |
| | 67 | | { |
| 0 | 68 | | OnDownloadPressed?.Invoke(sceneId); |
| 0 | 69 | | Hide(); |
| 0 | 70 | | }); |
| 3 | 71 | | shareButton.onClick.AddListener(() => |
| | 72 | | { |
| 0 | 73 | | OnSharePressed?.Invoke(sceneId); |
| 0 | 74 | | Hide(); |
| 0 | 75 | | }); |
| 3 | 76 | | unpublishButton.onClick.AddListener(() => |
| | 77 | | { |
| 0 | 78 | | OnUnpublishPressed?.Invoke(sceneId); |
| 0 | 79 | | Hide(); |
| 0 | 80 | | }); |
| 3 | 81 | | deleteButton.onClick.AddListener(() => |
| | 82 | | { |
| 0 | 83 | | OnDeletePressed?.Invoke(sceneId); |
| 0 | 84 | | Hide(); |
| 0 | 85 | | }); |
| 3 | 86 | | quitContributorButton.onClick.AddListener(() => |
| | 87 | | { |
| 0 | 88 | | OnQuitContributorPressed?.Invoke(sceneId); |
| 0 | 89 | | Hide(); |
| 0 | 90 | | }); |
| 3 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | private void Update() { HideIfClickedOutside(); } |
| | 94 | |
|
| | 95 | | public void Show(string sceneId, bool isSceneDeployed, bool isOwnerOrOperator, bool isContributor) |
| | 96 | | { |
| 3 | 97 | | ConfigFlags config = isSceneDeployed ? GetDeployedSceneConfig(isOwnerOrOperator, isContributor) : GetProjectScen |
| | 98 | |
|
| 3 | 99 | | Show(sceneId, config); |
| 3 | 100 | | } |
| | 101 | |
|
| | 102 | | public void Show(string sceneId, ConfigFlags configFlags) |
| | 103 | | { |
| 3 | 104 | | this.sceneId = sceneId; |
| 3 | 105 | | Build(configFlags); |
| 3 | 106 | | gameObject.SetActive(true); |
| 3 | 107 | | } |
| | 108 | |
|
| 36 | 109 | | public void Hide() { gameObject.SetActive(false); } |
| | 110 | |
|
| | 111 | | private void Build(ConfigFlags flags) |
| | 112 | | { |
| 3 | 113 | | headerContainer.SetActive((flags & headerFlags) != 0); |
| 3 | 114 | | headerSeparator.SetActive((flags & headerFlags) != 0); |
| 3 | 115 | | settingsButton.gameObject.SetActive((flags & ConfigFlags.Settings) != 0); |
| 3 | 116 | | duplicateAsProjectButton.gameObject.SetActive((flags & ConfigFlags.DuplicateAsProject) != 0); |
| 3 | 117 | | duplicateButton.gameObject.SetActive((flags & ConfigFlags.Duplicate) != 0); |
| 3 | 118 | | downloadButton.gameObject.SetActive((flags & ConfigFlags.Download) != 0); |
| 3 | 119 | | shareButton.gameObject.SetActive((flags & ConfigFlags.Share) != 0); |
| 3 | 120 | | unpublishButton.gameObject.SetActive((flags & ConfigFlags.Unpublish) != 0); |
| 3 | 121 | | deleteButton.gameObject.SetActive((flags & ConfigFlags.Delete) != 0); |
| 3 | 122 | | quitContributorButton.gameObject.SetActive((flags & ConfigFlags.QuitContributor) != 0); |
| 3 | 123 | | } |
| | 124 | |
|
| | 125 | | private ConfigFlags GetDeployedSceneConfig(bool isOwnerOrOperator, bool isContributor) |
| | 126 | | { |
| 1 | 127 | | ConfigFlags config = 0; |
| 1 | 128 | | if (isOwnerOrOperator) |
| | 129 | | { |
| 1 | 130 | | config |= ConfigFlags.Unpublish; |
| 1 | 131 | | } |
| 0 | 132 | | else if (isContributor) |
| | 133 | | { |
| 0 | 134 | | config |= ConfigFlags.QuitContributor; |
| | 135 | | } |
| | 136 | |
|
| 1 | 137 | | return config; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | private ConfigFlags GetProjectSceneConfig(bool isOwnerOrOperator, bool isContributor) |
| | 141 | | { |
| 2 | 142 | | ConfigFlags config = ConfigFlags.Duplicate | ConfigFlags.Download | ConfigFlags.Share; |
| 2 | 143 | | if (isOwnerOrOperator) |
| | 144 | | { |
| 1 | 145 | | config |= ConfigFlags.Settings | ConfigFlags.Delete; |
| 1 | 146 | | } |
| 1 | 147 | | else if (isContributor) |
| | 148 | | { |
| 1 | 149 | | config |= ConfigFlags.QuitContributor; |
| | 150 | | } |
| | 151 | |
|
| 2 | 152 | | return config; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | private void HideIfClickedOutside() |
| | 156 | | { |
| 0 | 157 | | if (Input.GetMouseButtonDown(0) && |
| | 158 | | !RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition)) |
| | 159 | | { |
| 0 | 160 | | Hide(); |
| | 161 | | } |
| 0 | 162 | | } |
| | 163 | | } |