ToolElement.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * 2021.10 DESKTOP-PBQ45G2
  3. ****************************************************************************/
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using QFramework;
  9. namespace QFramework
  10. {
  11. public enum ToolStatus
  12. {
  13. Normal,
  14. Select,
  15. Hint
  16. }
  17. public delegate void ToolElementEventHandle(object sender, ToolElement e);
  18. public partial class ToolElement : UIElement
  19. {
  20. private ToolStatus mtoolStatus;
  21. public ToolStatus M_ToolStatus
  22. {
  23. get
  24. {
  25. return mtoolStatus;
  26. }
  27. set
  28. {
  29. if (mtoolStatus != value)
  30. {
  31. mtoolStatus = value;
  32. OnToolStatusChanged();
  33. }
  34. }
  35. }
  36. public ToolElementEventHandle ToolSelectStatusChanged;
  37. private Image toolBtnImg;
  38. public Sprite toolBtnNormailSprite;
  39. public Sprite toolBtnHighlightSprite;
  40. public Sprite toolBtnSelectSprite;
  41. public Sprite toolBtnHintSprite;
  42. private bool isHintTool = false;
  43. public bool IsHintTool
  44. {
  45. get { return isHintTool; }
  46. set
  47. {
  48. if (isHintTool != value)
  49. {
  50. isHintTool = value;
  51. if (isHintTool)
  52. {
  53. if (M_ToolStatus == ToolStatus.Select) return;
  54. M_ToolStatus = ToolStatus.Hint;
  55. }
  56. else if (M_ToolStatus == ToolStatus.Hint)
  57. {
  58. M_ToolStatus = ToolStatus.Normal;
  59. }
  60. }
  61. }
  62. }
  63. private ToolConfig toolConfig;
  64. private void Awake()
  65. {
  66. toolBtnImg = ToolBtn.GetComponent<Image>();
  67. ToolBtn.onClick.AddListener(() =>
  68. {
  69. ToolBtnClick();
  70. });
  71. }
  72. private void ToolBtnClick()
  73. {
  74. if (M_ToolStatus != ToolStatus.Select)
  75. {
  76. M_ToolStatus = ToolStatus.Select;
  77. }
  78. else if (isHintTool)
  79. {
  80. M_ToolStatus = ToolStatus.Hint;
  81. }
  82. else
  83. {
  84. M_ToolStatus = ToolStatus.Normal;
  85. }
  86. if (toolConfig.changedCursor)
  87. {
  88. Cursor.SetCursor(M_ToolStatus == ToolStatus.Select ? toolConfig.cursor : null, Vector2.zero, CursorMode.Auto);
  89. }
  90. }
  91. /// <summary>
  92. /// 놓迦뺏묏야토零
  93. /// </summary>
  94. /// <param name="config"></param>
  95. public void InitConfig(ToolConfig config)
  96. {
  97. toolConfig = config;
  98. ToolImg.sprite = toolConfig.toolImg;
  99. if (toolConfig.toolImg == null)
  100. {
  101. ToolName.text = toolConfig.toolName;
  102. }
  103. else
  104. {
  105. ToolName.text = toolConfig.toolImg.name;
  106. }
  107. }
  108. /// <summary>
  109. /// 헌뇜묏야토零
  110. /// </summary>
  111. public void ClearConfig()
  112. {
  113. ClearState();
  114. toolConfig = null;
  115. }
  116. /// <summary>
  117. /// 헌뇜객큐榴檄
  118. /// </summary>
  119. public void ClearState()
  120. {
  121. IsHintTool = false;
  122. M_ToolStatus = ToolStatus.Normal;
  123. if (toolConfig.changedCursor)
  124. {
  125. Cursor.SetCursor(M_ToolStatus == ToolStatus.Select ? toolConfig.cursor : null, Vector2.zero, CursorMode.Auto);
  126. }
  127. }
  128. private void OnToolStatusChanged()
  129. {
  130. switch (mtoolStatus)
  131. {
  132. case ToolStatus.Normal:
  133. toolBtnImg.sprite = toolBtnNormailSprite;
  134. break;
  135. case ToolStatus.Select:
  136. toolBtnImg.sprite = toolBtnSelectSprite;
  137. break;
  138. case ToolStatus.Hint:
  139. toolBtnImg.sprite = toolBtnHintSprite;
  140. break;
  141. }
  142. ToolSelectStatusChanged?.Invoke(this, this);
  143. }
  144. }
  145. }