IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
DualSlider.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5 using UnityEngine.Events;
6 using UnityEngine.EventSystems;
7 using UnityEngine.Serialization;
8 
9 namespace UI
10 {
11  [System.Serializable]
12  public class OnDualSliderValuesChanged : UnityEvent<float, float>
13  {
14  }
15 
16 
24  public class DualSlider : MonoBehaviour, IPointerHoverHandler, IPointerDownHandler, IPointerUpHandler
25  {
26  public Sprite SliderSprite;
27  public Color SliderColor = new Color32 (101, 131, 161, 220);
28  public OnDualSliderValuesChanged OnValuesChanged;
29 
30  private GameObject leftSlider;
31  private GameObject rightSlider;
32 
33  private bool slidingLeft = false;
34  private bool slidingRight = false;
35 
36  float curLeft = 0f;
37  float curRight = 0f;
38 
39  // Use this for initialization
40  void Start () {
41  leftSlider = new GameObject ("LeftSlider");
42  Image leftSliderImg = leftSlider.AddComponent<Image> ();
43  leftSliderImg.sprite = SliderSprite;
44  leftSliderImg.material = GetComponent<Image> ().material;
45  leftSliderImg.color = SliderColor;
46  leftSlider.transform.SetParent (transform, false);
47  RectTransform lRect = leftSlider.GetComponent<RectTransform> ();
48  lRect.anchorMin = new Vector2 (0, 0);
49  lRect.anchorMax = new Vector2 (1, 1);
50  lRect.offsetMin = new Vector2( 0, 0 );
51  lRect.offsetMax = new Vector2( 0, 0 );
52 
53  rightSlider = new GameObject ("RightSlider");
54  Image rightSliderImg = rightSlider.AddComponent<Image> ();
55  rightSliderImg.sprite = SliderSprite;
56  rightSliderImg.material = GetComponent<Image> ().material;
57  rightSliderImg.color = SliderColor;
58  rightSlider.transform.SetParent (transform, false);
59  RectTransform rRight = rightSlider.GetComponent<RectTransform> ();
60  rRight.anchorMin = new Vector2 (0, 0);
61  rRight.anchorMax = new Vector2 (1, 1);
62  rRight.offsetMin = new Vector2( 0, 0 );
63  rRight.offsetMax = new Vector2( 0, 0 );
64 
65  setValues (0.01f, 0.99f);
66  }
67 
69  public void setValues( float left, float right )
70  {
71  left = Mathf.Clamp (left, 0f, 1f);
72  right = Mathf.Clamp (right, 0f, 1f);
73  RectTransform rect = GetComponent<RectTransform> ();
74  leftSlider.GetComponent<RectTransform>().offsetMax = new Vector2( -rect.rect.width*(1f-left), 0 );
75  rightSlider.GetComponent<RectTransform>().offsetMin = new Vector2( rect.rect.width*right, 0 );
76 
77  curLeft = left;
78  curRight = right;
79  // Activate the callback
80  if( OnValuesChanged != null )
81  OnValuesChanged.Invoke (curLeft, curRight);
82  }
83 
84  public void OnPointerDown( PointerEventData data )
85  {
86  slidingLeft = false;
87  slidingRight = false;
88 
89  Vector2 localMousePos;
90  RectTransform rectTf = GetComponent<RectTransform> ();
91  if (RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTf, data.position, data.enterEventCamera, out localMousePos)) {
92  Rect r = rectTf.rect;
93 
94  float amount = (localMousePos.x + r.size.x * 0.5f) / r.size.x;
95 
96  float distToLeftSlider = Mathf.Abs (amount - curLeft);
97  float distToRightSlider = Mathf.Abs (amount - curRight);
98  if (distToLeftSlider < distToRightSlider) {
99  slidingLeft = true;
100  } else {
101  slidingRight = true;
102  }
103  }
104  }
105 
106  public void OnPointerUp( PointerEventData data )
107  {
108  slidingLeft = false;
109  slidingRight = false;
110  }
111 
112  public void OnPointerHover( PointerEventData data )
113  {
114  if( InputDeviceManager.instance.currentInputDevice.isLeftButtonDown() && (slidingLeft || slidingRight )) {
115  Vector2 localMousePos;
116  RectTransform rectTf = GetComponent<RectTransform> ();
117  if (RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTf, data.position, data.enterEventCamera, out localMousePos)) {
118  Rect r = rectTf.rect;
119 
120  float amount = (localMousePos.x + r.size.x * 0.5f) / r.size.x;
121  if (slidingLeft) {
122  float right = curRight;
123  if (amount > right)
124  right = amount;
125  setValues (amount, right);
126  } else if (slidingRight) {
127  float left = curLeft;
128  if (amount < left)
129  left = amount;
130  setValues (left, amount);
131  }
132  }
133  }
134  }
135  }
136 }
void setValues(float left, float right)
Modifies the slider values.
Definition: DualSlider.cs:69