IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
RiftToolRing.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections;
4 using System.Collections.Generic;
5 
9 public class RiftToolRing : MonoBehaviour {
10 
11  public static RiftToolRing instance { private set; get; }
12 
13  public void OnEnable()
14  {
15  if (instance != null) {
16  throw(new System.Exception ("Error: Cannot create more than one instance of RiftToolRing!"));
17  }
18  instance = this;
19 
20  setAvailableTools (null);
21  }
22 
23  public void OnDisable()
24  {
25  if( this == instance )
26  {
27  instance = null;
28  }
29  }
30 
31  public void setAvailableTools (List<ToolWidget> tools)
32  {
33  // Disable the toolbar if there's no tools to display:
34  Transform toolBar = transform.Find ("ToolBar");
35  if (tools == null || tools.Count == 0) {
36  toolBar.gameObject.SetActive (false);
37  return;
38  } else {
39  toolBar.gameObject.SetActive (true);
40  }
41 
42  // Get the default tool button:
43  GameObject toolButton = toolBar.Find ("ToolButton").gameObject;
44  float toolButtonWidth = toolButton.GetComponent<RectTransform> ().rect.width;
45  // Resize the tool bar:
46  RectTransform r = toolBar.GetComponent<RectTransform> ();
47  r.sizeDelta = new Vector2 ((tools.Count + 1)* toolButtonWidth + 2f, r.sizeDelta.y);
48  // Add an entry for each tool:
49  int i = 0;
50  foreach (ToolWidget tool in tools) {
51  GameObject b = Instantiate (toolButton);
52  b.SetActive (true);
53  b.transform.SetParent (toolButton.transform.parent, false);
54  RectTransform rb = b.GetComponent<RectTransform> ();
55  rb.anchoredPosition = new Vector2 (1f + i * toolButtonWidth, 0f);
56 
57  Image im = b.transform.Find ("Image").GetComponent<Image> ();
58  im.sprite = tool.ToolIcon;
59 
60  Button button = b.GetComponent<Button> ();
61  ToolWidget captured = tool;
62  button.onClick.AddListener (() => ToolControl.instance.chooseTool (captured));
63  i++;
64  }
65 
66  GameObject closeButton = toolBar.Find ("CloseButton").gameObject;
67  RectTransform rc = closeButton.GetComponent<RectTransform> ();
68  rc.anchoredPosition = new Vector2 (1f + i * toolButtonWidth, 0f);
69  Button cButton = closeButton.GetComponent<Button> ();
70  cButton.onClick.RemoveAllListeners ();
71  cButton.onClick.AddListener (() => ToolControl.instance.closeActiveTool ());
72  closeButton.SetActive (true);
73  }
74 }