IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
OpacitySlider.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using UnityEngine.EventSystems;
4 using System.Collections;
5 using UI;
6 
7 // TODO Update this class to work like DualSlider?
8 public class OpacitySlider : MonoBehaviour, IPointerHoverHandler, IPointerDownHandler, IPointerUpHandler
9 {
10 
11  public GameObject gameObjectToChangeOpacity;
12  private GameObject sliderFill;
13  private float dampeningArea = 0.05f;
14 
15  private bool sliding = false;
16 
17  // Use this for initialization
18  void Start()
19  {
20  sliderFill = transform.Find ("Fill").gameObject;
21  updateSlider();
22  }
23 
24  void OnEnable()
25  {
26  // Register event callbacks:
27  PatientEventSystem.startListening(PatientEventSystem.Event.MESH_Opacity_Changed, updateSlider);
28  }
29 
30  void OnDisable()
31  {
32  // Unregister myself:
33  PatientEventSystem.stopListening(PatientEventSystem.Event.MESH_Opacity_Changed, updateSlider);
34  }
35 
37  public void changeOpacity(float f)
38  {
39  MeshMaterialControl moc = gameObjectToChangeOpacity.GetComponent<MeshMaterialControl> ();
40  if (moc != null) {
41  moc.changeOpactiyOfChildren (f);
42  }
43  }
44 
46  public void updateSlider(object obj = null){
47  if (gameObjectToChangeOpacity != null) {
48  float newOpacity = 0f;
49  if (gameObjectToChangeOpacity.activeSelf) {
50  MeshRenderer mr = gameObjectToChangeOpacity.GetComponentInChildren<MeshRenderer> ();
51  if( mr != null )
52  newOpacity = mr.material.color.a;
53  } else {
54  newOpacity = 0f;
55  }
56 
57 
58  //GetComponent<Slider> ().value = currentOpacity;
59 
60  float sliderAmount = newOpacity * (1f - 2f * dampeningArea) + dampeningArea;
61  if (newOpacity >= 1)
62  sliderAmount = 1;
63  else if (newOpacity <= 0)
64  sliderAmount = 0;
65 
66  //Rect r = transform.GetComponent<RectTransform> ().rect;
67  //Debug.Log("sliderFill: " + sliderFill);
68  if (sliderFill != null) {
69  RectTransform fillRT = sliderFill.GetComponent<RectTransform> ();
70 
71  RectTransform rectTF = transform.GetComponent<RectTransform> ();
72  Rect r = rectTF.rect;
73  fillRT.offsetMax = new Vector2 (-(r.size.x - r.size.x * sliderAmount), fillRT.offsetMax.y);
74  }
75 
76  //float resultingAmount = Mathf.Clamp ((currentOpacity - dampeningArea)/(1f-2f*dampeningArea), 0f, 1f);
77  }
78  }
79 
80 
81  public void OnPointerDown( PointerEventData data )
82  {
83  sliding = true;
84  }
85 
86  public void OnPointerUp( PointerEventData data )
87  {
88  sliding = false;
89  }
90 
91  public void OnPointerHover( PointerEventData data )
92  {
93  if (sliding && InputDeviceManager.instance.currentInputDevice.isLeftButtonDown() ) {
94  Vector2 localMousePos;
95  RectTransform rectTF = transform.GetComponent<RectTransform> ();
96  if (RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTF, data.position, data.enterEventCamera, out localMousePos)) {
97  Rect r = rectTF.rect;
98 
99  float amount = (localMousePos.x + r.size.x * 0.5f) / r.size.x;
100  float scaledAmount = amount;
101  if (amount > 1f - dampeningArea)
102  scaledAmount = 1f;
103  else if (amount < dampeningArea)
104  scaledAmount = 0f;
105 
106  RectTransform fillRT = sliderFill.GetComponent<RectTransform> ();
107  fillRT.offsetMax = new Vector2 ( -(r.size.x - r.size.x*scaledAmount), fillRT.offsetMax.y);
108 
109  float resultingAmount = Mathf.Clamp ((amount - dampeningArea)/(1f-2f*dampeningArea), 0f, 1f);
110 
111  changeOpacity (resultingAmount);
112  }
113  }
114  }
115 }
void updateSlider(object obj=null)
Called if someone else changed the opacity!
void changeOpacity(float f)
Set the slider to the value of f.