Tanoda
MinMax.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10#if UNITY_EDITOR
11using UnityEditor;
12#endif
13using System.Collections.Generic;
14
15namespace Leap.Unity.Attributes {
16
18 public const float PERCENT_NUM = 0.2f;
19 public const float SPACING = 3;
20
21 public readonly float min, max;
22 public readonly bool isInt;
23
24 public MinMax(float min, float max) {
25 this.min = min;
26 this.max = max;
27 isInt = false;
28 }
29
30 public MinMax(int min, int max) {
31 this.min = min;
32 this.max = max;
33 isInt = true;
34 }
35
36#if UNITY_EDITOR
37 public void DrawProperty(Rect rect, SerializedProperty property, GUIContent label) {
38 EditorGUI.BeginProperty(rect, label, property);
39
40 Vector2 value = property.vector2Value;
41
42 rect = EditorGUI.PrefixLabel(rect, GUIUtility.GetControlID(FocusType.Passive), label);
43
44 int prevIndent = EditorGUI.indentLevel;
45 EditorGUI.indentLevel = 0;
46
47 float w = rect.width * PERCENT_NUM;
48
49 Rect leftNum = new Rect(rect.x, rect.y, w, rect.height);
50 Rect slider = new Rect(rect.x + w + SPACING, rect.y, rect.width - 2 * w - SPACING * 2, rect.height);
51 Rect rightNum = new Rect(rect.x + rect.width - w, rect.y, w, rect.height);
52
53 float newMin = EditorGUI.FloatField(leftNum, value.x);
54 float newMax = EditorGUI.FloatField(rightNum, value.y);
55
56 value.x = Mathf.Clamp(newMin, min, value.y);
57 value.y = Mathf.Clamp(newMax, value.x, max);
58
59 EditorGUI.MinMaxSlider(slider, ref value.x, ref value.y, min, max);
60
61 property.vector2Value = value;
62
63 EditorGUI.EndProperty();
64
65 EditorGUI.indentLevel = prevIndent;
66 }
67
68 public override IEnumerable<SerializedPropertyType> SupportedTypes {
69 get {
70 yield return SerializedPropertyType.Vector2;
71 }
72 }
73#endif
74 }
75}
readonly bool isInt
Definition: MinMax.cs:22
readonly float min
Definition: MinMax.cs:21
const float PERCENT_NUM
Definition: MinMax.cs:18
MinMax(int min, int max)
Definition: MinMax.cs:30
readonly float max
Definition: MinMax.cs:21
MinMax(float min, float max)
Definition: MinMax.cs:24