/GUI/ButtonManager.cs

https://bitbucket.org/ozgurtaskin/mobgedomain · C# · 239 lines · 182 code · 13 blank · 44 comment · 26 complexity · d44939857ae5c6b123e365495e5029a8 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. [ExecuteInEditMode]
  6. public class ButtonManager : MonoBehaviour
  7. {
  8. private static ButtonManager singelton;
  9. private const int mouseId = 1000;
  10. private List<Button4> buttons;
  11. private Dictionary<int, ButtonGroup> pressedButtons;
  12. private Transform camtr;
  13. public bool Dirty { get; set; }
  14. public static ButtonManager Singelton
  15. {
  16. get
  17. {
  18. if (singelton == null)
  19. {
  20. singelton = new GameObject("button manager").AddComponent<ButtonManager>();
  21. GameObject.DontDestroyOnLoad(Singelton.gameObject);
  22. singelton.levelStarted();
  23. singelton.buttons = new List<Button4>();
  24. singelton.pressedButtons = new Dictionary<int, ButtonGroup>();
  25. //singelton.buttons.OrderBy
  26. }
  27. return singelton;
  28. }
  29. }
  30. void OnLevelWasLoaded (int level)
  31. {
  32. levelStarted();
  33. }
  34. void levelStarted ()
  35. {
  36. if (singelton && Camera.mainCamera)
  37. {
  38. singelton.camtr = Camera.mainCamera.transform;
  39. }
  40. }
  41. public int ButtonCount { get { return buttons.Count; } }
  42. void Update()
  43. {
  44. if (!Application.isPlaying)
  45. {
  46. Debug.Log("destroying singelton");
  47. GameObject.DestroyImmediate(gameObject);
  48. }
  49. if (Dirty)
  50. {
  51. buttons.Sort();
  52. Dirty = false;
  53. }
  54. //Camera cam = Camera.mainCamera;
  55. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  56. {
  57. foreach (var t in Input.touches)
  58. {
  59. switch (t.phase)
  60. {
  61. case TouchPhase.Began:
  62. handlePress(t.fingerId, screenPointToRay(t.position));
  63. break;
  64. case TouchPhase.Ended:
  65. handleRelease(t.fingerId, screenPointToRay(t.position));
  66. break;
  67. case TouchPhase.Moved:
  68. handleMove(t.fingerId, screenPointToRay(t.position));
  69. break;
  70. case TouchPhase.Stationary:
  71. handleMove(t.fingerId, screenPointToRay(t.position));
  72. break;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. if (Input.GetMouseButtonDown(0))
  79. {
  80. handlePress(mouseId, screenPointToRay(Input.mousePosition));
  81. }
  82. if (Input.GetMouseButtonUp(0))
  83. {
  84. handleRelease(mouseId, screenPointToRay(Input.mousePosition));
  85. }
  86. if (Input.GetMouseButton(0))
  87. {
  88. handleMove(mouseId, screenPointToRay(Input.mousePosition));
  89. }
  90. }
  91. }
  92. private Ray screenPointToRay(Vector3 pos)
  93. {
  94. Camera cam = Camera.mainCamera;
  95. Ray r = cam.ScreenPointToRay(Input.mousePosition);
  96. r.origin = camtr.position;
  97. return r;
  98. }
  99. private bool tryGetRay(int id, out Ray ray)
  100. {
  101. ButtonGroup group;
  102. bool b = pressedButtons.TryGetValue(id, out group);
  103. ray = group.args.ray;
  104. return b;
  105. }
  106. private void handlePress(int id, Ray r, Button4 ignoreButton = null)
  107. {
  108. ButtonEventArgs args = new ButtonEventArgs(r, id);
  109. //Debug.Log("handle press");
  110. var list = new List<Button4>();
  111. pressedButtons[id] = new ButtonGroup(list, new ButtonEventArgs(r, id));
  112. foreach (var b in buttons)
  113. {
  114. if (b != ignoreButton && b.enabled && b.gameObject.active && b.cursorDown(args))
  115. {
  116. list.Add(b);
  117. if (b.holdInput)
  118. break;
  119. }
  120. }
  121. }
  122. private void handleMove(int id, Ray r)
  123. {
  124. ButtonGroup list;
  125. if (pressedButtons.TryGetValue(id, out list))
  126. {
  127. list.args.ray = r;
  128. foreach (var b in list.buttons)
  129. {
  130. var move = b.cursorMoved(list.args);
  131. if (!move)
  132. {
  133. list.args.touchCancel = true;
  134. handlePress(id, r, b);
  135. b.cursorUp(list.args);
  136. list.args.touchCancel = false;
  137. }
  138. }
  139. }
  140. }
  141. private void handleRelease(int id, Ray r)
  142. {
  143. //Debug.Log("handle release");
  144. ButtonGroup list;
  145. if (pressedButtons.TryGetValue(id, out list))
  146. {
  147. pressedButtons.Remove(id);
  148. list.args.ray = r;
  149. foreach (var b in list.buttons)
  150. {
  151. b.cursorUp(list.args);
  152. }
  153. }
  154. }
  155. /*public void priorityChanged(Button4 button, int oldProprity)
  156. {
  157. if(oldProprity<button.Priority)
  158. {
  159. }
  160. else
  161. {
  162. }
  163. }*/
  164. public void addButton(Button4 button)
  165. {
  166. buttons.Add(button);
  167. Dirty = true;
  168. /*int i = buttons.Length-2;
  169. while( i >= 0 )
  170. {
  171. if(buttons[i].CompareTo(button) < 0)
  172. {
  173. buttons[i+1] = buttons[i];
  174. i--;
  175. }
  176. else
  177. {
  178. break;
  179. }
  180. }
  181. buttons[i+1] = button;*/
  182. }
  183. public void removeButton(Button4 button)
  184. {
  185. if (Dirty)
  186. {
  187. buttons.Sort();
  188. if (this)
  189. {
  190. Dirty = false;
  191. }
  192. }
  193. int i = buttons.BinarySearch(button);
  194. if (i >= 0)
  195. {
  196. buttons.RemoveAt(i);
  197. }
  198. //Dirty = true;
  199. /*int i = 0;
  200. for(;i<buttons.Count; i++)
  201. {
  202. if(buttons[i] == button)
  203. {
  204. for(; i < buttons.Count-1; i++)
  205. {
  206. buttons[i] = buttons[i+1];
  207. }
  208. buttons.RemoveAt(i);
  209. break;
  210. }
  211. }*/
  212. }
  213. private class ButtonGroup
  214. {
  215. public List<Button4> buttons;
  216. public ButtonEventArgs args;
  217. public ButtonGroup(List<Button4> buttons, ButtonEventArgs args)
  218. {
  219. this.args = args;
  220. this.buttons = buttons;
  221. }
  222. }
  223. }