| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | namespace DCL.ExperiencesViewer |
| | 7 | | { |
| | 8 | | public interface IExperienceRowComponentView |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Event that will be triggered when the Show/Hide PEX UI button is clicked. |
| | 12 | | /// </summary> |
| | 13 | | event Action<string, bool> onShowPEXUI; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Event that will be triggered when the Start(Stop PEX button is clicked. |
| | 17 | | /// </summary> |
| | 18 | | event Action<string, bool> onStartPEX; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Set the PEX id. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="id">A string</param> |
| | 24 | | void SetId(string id); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Set an icon image from an uri. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="uri">Url of the icon image.</param> |
| | 30 | | void SetIcon(string uri); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Set the name label. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="name">A string.</param> |
| | 36 | | void SetName(string name); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Set the PEX UI as visible or not. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="isPlaying">True for set the PEX UI as visible.</param> |
| | 42 | | void SetUIVisibility(bool isVisible); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Set the PEX as playing or not. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="isPlaying">True for set it as playing.</param> |
| | 48 | | void SetAsPlaying(bool isPlaying); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Set the background color of the row. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="color">Color to apply.</param> |
| | 54 | | void SetRowColor(Color color); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Set the background color of the row when it is hovered. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="color">Color to apply.</param> |
| | 60 | | void SetOnHoverColor(Color color); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Set the ability of start/stop the experience (with a toggle) or only stop it (with a stop button). |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="isAllowed">True for allowing it.</param> |
| | 66 | | void SetAllowStartStop(bool isAllowed); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public class ExperienceRowComponentView : BaseComponentView, IExperienceRowComponentView, IComponentModelConfig |
| | 70 | | { |
| | 71 | | [Header("Prefab References")] |
| | 72 | | [SerializeField] internal ImageComponentView iconImage; |
| | 73 | | [SerializeField] internal ImageComponentView defaultIconImage; |
| | 74 | | [SerializeField] internal TMP_Text nameText; |
| | 75 | | [SerializeField] internal ShowHideAnimator showHideUIButtonsContainerAnimator; |
| | 76 | | [SerializeField] internal ButtonComponentView showPEXUIButton; |
| | 77 | | [SerializeField] internal ButtonComponentView hidePEXUIButton; |
| | 78 | | [SerializeField] internal Toggle startStopPEXToggle; |
| | 79 | | [SerializeField] internal ButtonComponentView stopPEXButton; |
| | 80 | | [SerializeField] internal Image backgroundImage; |
| | 81 | |
|
| | 82 | | [Header("Configuration")] |
| | 83 | | [SerializeField] internal ExperienceRowComponentModel model; |
| | 84 | |
|
| | 85 | | public event Action<string, bool> onShowPEXUI; |
| | 86 | | public event Action<string, bool> onStartPEX; |
| | 87 | |
|
| | 88 | | internal Color originalBackgroundColor; |
| | 89 | | internal Color onHoverColor; |
| | 90 | |
|
| | 91 | | public override void Awake() |
| | 92 | | { |
| 20 | 93 | | base.Awake(); |
| | 94 | |
|
| 20 | 95 | | originalBackgroundColor = backgroundImage.color; |
| 20 | 96 | | ConfigureRowButtons(); |
| 20 | 97 | | } |
| | 98 | |
|
| | 99 | | public override void OnEnable() |
| | 100 | | { |
| 28 | 101 | | base.OnEnable(); |
| | 102 | |
|
| 28 | 103 | | SetUIVisibility(model.isUIVisible); |
| 28 | 104 | | SetAsPlaying(model.isPlaying); |
| 28 | 105 | | } |
| | 106 | |
|
| | 107 | | public void Configure(BaseComponentModel newModel) |
| | 108 | | { |
| 10 | 109 | | model = (ExperienceRowComponentModel)newModel; |
| 10 | 110 | | RefreshControl(); |
| 10 | 111 | | } |
| | 112 | |
|
| | 113 | | public override void RefreshControl() |
| | 114 | | { |
| 10 | 115 | | if (model == null) |
| 0 | 116 | | return; |
| | 117 | |
|
| 10 | 118 | | SetId(model.id); |
| 10 | 119 | | SetIcon(model.iconUri); |
| 10 | 120 | | SetName(model.name); |
| 10 | 121 | | SetUIVisibility(model.isUIVisible); |
| 10 | 122 | | SetAsPlaying(model.isPlaying); |
| 10 | 123 | | SetRowColor(model.backgroundColor); |
| 10 | 124 | | SetOnHoverColor(model.onHoverColor); |
| 10 | 125 | | SetAllowStartStop(model.allowStartStop); |
| 10 | 126 | | } |
| | 127 | |
|
| | 128 | | public override void OnFocus() |
| | 129 | | { |
| 1 | 130 | | base.OnFocus(); |
| | 131 | |
|
| 1 | 132 | | backgroundImage.color = onHoverColor; |
| 1 | 133 | | } |
| | 134 | |
|
| | 135 | | public override void OnLoseFocus() |
| | 136 | | { |
| 29 | 137 | | base.OnLoseFocus(); |
| | 138 | |
|
| 29 | 139 | | backgroundImage.color = originalBackgroundColor; |
| 29 | 140 | | } |
| | 141 | |
|
| | 142 | | public void SetId(string id) |
| | 143 | | { |
| 0 | 144 | | model.id = id; |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public void SetIcon(string uri) |
| | 148 | | { |
| 11 | 149 | | model.iconUri = uri; |
| | 150 | |
|
| 11 | 151 | | if (iconImage == null) |
| 0 | 152 | | return; |
| | 153 | |
|
| 11 | 154 | | if (!String.IsNullOrEmpty(uri)) |
| | 155 | | { |
| 2 | 156 | | iconImage.gameObject.SetActive(true); |
| 2 | 157 | | iconImage.SetImage(uri); |
| 2 | 158 | | defaultIconImage.gameObject.SetActive(false); |
| 2 | 159 | | } |
| | 160 | | else |
| | 161 | | { |
| 9 | 162 | | iconImage.gameObject.SetActive(false); |
| 9 | 163 | | defaultIconImage.gameObject.SetActive(true); |
| | 164 | | } |
| 9 | 165 | | } |
| | 166 | |
|
| | 167 | | public void SetName(string name) |
| | 168 | | { |
| 11 | 169 | | model.name = name; |
| | 170 | |
|
| 11 | 171 | | if (nameText == null) |
| 0 | 172 | | return; |
| | 173 | |
|
| 11 | 174 | | nameText.text = name; |
| 11 | 175 | | } |
| | 176 | |
|
| | 177 | | public void SetUIVisibility(bool isVisible) |
| | 178 | | { |
| 42 | 179 | | model.isUIVisible = isVisible; |
| | 180 | |
|
| 42 | 181 | | if (showPEXUIButton != null) |
| | 182 | | { |
| 42 | 183 | | if (isVisible) |
| 32 | 184 | | showPEXUIButton.Hide(); |
| | 185 | | else |
| 10 | 186 | | showPEXUIButton.Show(); |
| | 187 | | } |
| | 188 | |
|
| 42 | 189 | | if (hidePEXUIButton != null) |
| | 190 | | { |
| 42 | 191 | | if (isVisible) |
| 32 | 192 | | hidePEXUIButton.Show(); |
| | 193 | | else |
| 10 | 194 | | hidePEXUIButton.Hide(); |
| | 195 | | } |
| 10 | 196 | | } |
| | 197 | |
|
| | 198 | | public void SetAsPlaying(bool isPlaying) |
| | 199 | | { |
| 46 | 200 | | model.isPlaying = isPlaying; |
| | 201 | |
|
| 46 | 202 | | if (startStopPEXToggle != null && startStopPEXToggle.gameObject.activeSelf) |
| 46 | 203 | | startStopPEXToggle.isOn = isPlaying; |
| | 204 | |
|
| 46 | 205 | | if (stopPEXButton != null && stopPEXButton.gameObject.activeSelf && !isPlaying) |
| 0 | 206 | | stopPEXButton.gameObject.SetActive(false); |
| | 207 | |
|
| 46 | 208 | | if (showHideUIButtonsContainerAnimator != null) |
| | 209 | | { |
| 46 | 210 | | if (isPlaying) |
| 34 | 211 | | showHideUIButtonsContainerAnimator.Show(); |
| | 212 | | else |
| 12 | 213 | | showHideUIButtonsContainerAnimator.Hide(); |
| | 214 | | } |
| 12 | 215 | | } |
| | 216 | |
|
| | 217 | | public void SetRowColor(Color color) |
| | 218 | | { |
| 11 | 219 | | model.backgroundColor = color; |
| | 220 | |
|
| 11 | 221 | | if (backgroundImage == null) |
| 0 | 222 | | return; |
| | 223 | |
|
| 11 | 224 | | backgroundImage.color = color; |
| 11 | 225 | | originalBackgroundColor = color; |
| 11 | 226 | | } |
| | 227 | |
|
| | 228 | | public void SetOnHoverColor(Color color) |
| | 229 | | { |
| 11 | 230 | | model.onHoverColor = color; |
| 11 | 231 | | onHoverColor = color; |
| 11 | 232 | | } |
| | 233 | |
|
| | 234 | | public void SetAllowStartStop(bool isAllowed) |
| | 235 | | { |
| 10 | 236 | | model.allowStartStop = isAllowed; |
| | 237 | |
|
| 10 | 238 | | if (startStopPEXToggle != null) |
| 10 | 239 | | startStopPEXToggle.gameObject.SetActive(isAllowed); |
| | 240 | |
|
| 10 | 241 | | if (stopPEXButton != null) |
| 10 | 242 | | stopPEXButton.gameObject.SetActive(!isAllowed); |
| 10 | 243 | | } |
| | 244 | |
|
| | 245 | | public override void Dispose() |
| | 246 | | { |
| 36 | 247 | | base.Dispose(); |
| | 248 | |
|
| 36 | 249 | | showPEXUIButton?.onClick.RemoveAllListeners(); |
| 36 | 250 | | hidePEXUIButton?.onClick.RemoveAllListeners(); |
| 36 | 251 | | startStopPEXToggle?.onValueChanged.RemoveAllListeners(); |
| 36 | 252 | | stopPEXButton?.onClick.RemoveAllListeners(); |
| 36 | 253 | | } |
| | 254 | |
|
| | 255 | | internal void ConfigureRowButtons() |
| | 256 | | { |
| 20 | 257 | | showPEXUIButton?.onClick.AddListener(() => |
| | 258 | | { |
| 1 | 259 | | SetUIVisibility(true); |
| 1 | 260 | | onShowPEXUI?.Invoke(model.id, true); |
| 1 | 261 | | }); |
| | 262 | |
|
| 20 | 263 | | hidePEXUIButton?.onClick.AddListener(() => |
| | 264 | | { |
| 1 | 265 | | SetUIVisibility(false); |
| 1 | 266 | | onShowPEXUI?.Invoke(model.id, false); |
| 1 | 267 | | }); |
| | 268 | |
|
| 20 | 269 | | startStopPEXToggle?.onValueChanged.AddListener((isOn) => |
| | 270 | | { |
| 6 | 271 | | SetAsPlaying(isOn); |
| 6 | 272 | | onStartPEX?.Invoke(model.id, isOn); |
| 4 | 273 | | }); |
| | 274 | |
|
| 20 | 275 | | stopPEXButton?.onClick.AddListener(() => |
| | 276 | | { |
| 0 | 277 | | SetAsPlaying(false); |
| 0 | 278 | | onStartPEX?.Invoke(model.id, false); |
| 0 | 279 | | }); |
| 20 | 280 | | } |
| | 281 | | } |
| | 282 | | } |