SelectableObjectTrigger.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using EPOOutline;
  6. using KairoEngine.GameTools.InteractionTools;
  7. using KairoEngine.UI;
  8. namespace KairoEngine.GameTools.Selectables
  9. {
  10. /// <summary>
  11. /// Trigger for an ISelectableOnject. This component needs to be placed with a collider component.
  12. /// </summary>
  13. public class SelectableObjectTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  14. {
  15. private InteractionToolsManager toolsManager;
  16. public Outlinable outlinable;
  17. public ISelectableObject selectableObject;
  18. public bool isSelected = false;
  19. private void Start()
  20. {
  21. toolsManager = InteractionToolsManager.instance;
  22. if(outlinable == null) outlinable = GetComponent<Outlinable>();
  23. if(outlinable == null) outlinable = GetComponentInParent<Outlinable>();
  24. if(outlinable == null) outlinable = GetComponentInChildren<Outlinable>();
  25. if(outlinable == null) Debug.LogError($"Could not find Outlinable component in {gameObject.name} hierarchy", this.gameObject);
  26. if(outlinable != null) outlinable.enabled = false;
  27. if(selectableObject == null) gameObject.transform.parent.GetComponent<ISelectableObject>();
  28. if(selectableObject == null) gameObject.transform.parent.GetComponentInChildren<ISelectableObject>();
  29. }
  30. public void Select()
  31. {
  32. if(isSelected) return;
  33. if(MouseInputUIBlocker.BlockedByUI) return;
  34. isSelected = true;
  35. ShowOutline();
  36. }
  37. public void Deselect()
  38. {
  39. if(!isSelected) return;
  40. isSelected = false;
  41. HideOutline();
  42. }
  43. public void OnPointerEnter(PointerEventData eventData) => ShowOutline();
  44. public void OnPointerExit(PointerEventData eventData) => HideOutline();
  45. public void OnMouseEnter() => ShowOutline();
  46. public void OnMouseExit() => HideOutline();
  47. private void ShowOutline()
  48. {
  49. var highlightInterface = toolsManager.toolInterfaces[toolsManager.currentTools] as IHighlightableObject;
  50. bool hasTool = highlightInterface != null;
  51. if(hasTool && !MouseInputUIBlocker.BlockedByUI && highlightInterface.ShowOutline())
  52. {
  53. outlinable.enabled = true;
  54. outlinable.OutlineParameters.Color = highlightInterface.GetHighlightColor();
  55. }
  56. }
  57. private void HideOutline()
  58. {
  59. if(!isSelected) outlinable.enabled = false;
  60. }
  61. }
  62. }