SelectableObjectTrigger.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(MouseInputUIBlocker.BlockedByUI) return;
  33. isSelected = true;
  34. ShowOutline();
  35. }
  36. public void Deselect()
  37. {
  38. isSelected = false;
  39. HideOutline();
  40. }
  41. public void OnPointerEnter(PointerEventData eventData) => ShowOutline();
  42. public void OnPointerExit(PointerEventData eventData) => HideOutline();
  43. public void OnMouseEnter() => ShowOutline();
  44. public void OnMouseExit() => HideOutline();
  45. private void ShowOutline()
  46. {
  47. bool hasTool = toolsManager.currentTools == 0 || toolsManager.currentTools == 2;
  48. if(hasTool && !MouseInputUIBlocker.BlockedByUI)
  49. {
  50. outlinable.enabled = true;
  51. if(toolsManager.currentTools == 2)
  52. {
  53. outlinable.OutlineParameters.Color = Color.red;
  54. }
  55. else
  56. {
  57. outlinable.OutlineParameters.Color = Color.white;
  58. }
  59. }
  60. }
  61. private void HideOutline()
  62. {
  63. if(!isSelected) outlinable.enabled = false;
  64. }
  65. }
  66. }