forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchManagerEditor.cs
More file actions
133 lines (115 loc) · 6.07 KB
/
TouchManagerEditor.cs
File metadata and controls
133 lines (115 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System.Collections.Generic;
using System.Linq;
using TouchScript.Devices.Display;
using TouchScript.Layers;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Object = UnityEngine.Object;
namespace TouchScript.Editor
{
[CustomEditor(typeof(TouchManager))]
internal sealed class TouchManagerEditor : UnityEditor.Editor
{
private static readonly GUIContent DISPLAY_DEVICE = new GUIContent("Display Device", "Display device properties where such parameters as target DPI are stored.");
private static readonly GUIContent CREATE_CAMERA_LAYER = new GUIContent("Create Camera Layer", "Indicates if TouchScript should create a CameraLayer for you if no layers present in a scene. This is usually a desired behavior but sometimes you would want to turn this off if you are using TouchScript only to get touch input from some device.");
private static readonly GUIContent USE_SEND_MESSAGE = new GUIContent("Use SendMessage", "If you use UnityScript or prefer using Unity Messages you can turn them on with this option.");
private static readonly GUIContent SEND_MESSAGE_TARGET = new GUIContent("SendMessage Target", "The GameObject target of Unity Messages. If null, host GameObject is used.");
private static readonly GUIContent SEND_MESSAGE_EVENTS = new GUIContent("SendMessage Events", "Which events should be sent as Unity Messages.");
private static readonly GUIContent LAYERS_HEADER = new GUIContent("Touch Layers", "Sorted array of Touch Layers in the scene.");
private TouchManager instance;
private ReorderableList layersList;
private SerializedProperty layers, displayDevice, shouldCreateCameraLayer, useSendMessage, sendMessageTarget, sendMessageEvents;
private void OnEnable()
{
instance = target as TouchManager;
layers = serializedObject.FindProperty("layers");
displayDevice = serializedObject.FindProperty("displayDevice");
shouldCreateCameraLayer = serializedObject.FindProperty("shouldCreateCameraLayer");
useSendMessage = serializedObject.FindProperty("useSendMessage");
sendMessageTarget = serializedObject.FindProperty("sendMessageTarget");
sendMessageEvents = serializedObject.FindProperty("sendMessageEvents");
refresh();
layersList = new ReorderableList(serializedObject, layers, true, true, false, false);
layersList.drawHeaderCallback += rect => GUI.Label(rect, LAYERS_HEADER);
layersList.drawElementCallback += (rect, index, active, focused) =>
{
rect.height = 16;
rect.y += 2;
if (index >= layers.arraySize) return;
var layer = layers.GetArrayElementAtIndex(index).objectReferenceValue as TouchLayer;
if (layer == null)
{
EditorGUI.LabelField(rect, "null");
return;
}
EditorGUI.LabelField(rect, layer.Name);
};
}
public override void OnInspectorGUI()
{
serializedObject.Update();
var r = EditorGUILayout.GetControlRect(true, 16f, EditorStyles.objectField);
var label = EditorGUI.BeginProperty(r, DISPLAY_DEVICE, displayDevice);
EditorGUI.BeginChangeCheck();
r = EditorGUI.PrefixLabel(r, label);
var newDevice = EditorGUI.ObjectField(r, instance.DisplayDevice as Object, typeof(IDisplayDevice), true) as IDisplayDevice;
if (EditorGUI.EndChangeCheck())
{
instance.DisplayDevice = newDevice;
EditorUtility.SetDirty(instance);
}
EditorGUI.EndProperty();
if (Application.isPlaying) GUI.enabled = false;
EditorGUILayout.PropertyField(shouldCreateCameraLayer, CREATE_CAMERA_LAYER);
GUI.enabled = true;
EditorGUIUtility.labelWidth = 160;
EditorGUILayout.PropertyField(useSendMessage, USE_SEND_MESSAGE);
if (useSendMessage.boolValue)
{
EditorGUILayout.PropertyField(sendMessageTarget, SEND_MESSAGE_TARGET);
r = EditorGUILayout.GetControlRect(true, 16f, EditorStyles.layerMaskField);
label = EditorGUI.BeginProperty(r, SEND_MESSAGE_EVENTS, sendMessageEvents);
EditorGUI.BeginChangeCheck();
r = EditorGUI.PrefixLabel(r, label);
var sMask = (TouchManager.MessageType)EditorGUI.EnumMaskField(r, instance.SendMessageEvents);
if (EditorGUI.EndChangeCheck())
{
instance.SendMessageEvents = sMask;
EditorUtility.SetDirty(instance);
}
EditorGUI.EndProperty();
}
if (Application.isPlaying) GUI.enabled = false;
layersList.DoLayoutList();
GUI.enabled = true;
serializedObject.ApplyModifiedProperties();
}
private void refresh()
{
var allLayers = FindObjectsOfType(typeof(TouchLayer)).Cast<TouchLayer>().ToList();
var toRemove = new List<int>();
for (var i = 0; i < layers.arraySize; i++)
{
var layer = layers.GetArrayElementAtIndex(i).objectReferenceValue as TouchLayer;
if (layer == null || allLayers.IndexOf(layer) == -1) toRemove.Add(i);
else allLayers.Remove(layer);
}
for (var i = toRemove.Count - 1; i >= 0; i--)
{
var index = toRemove[i];
layers.GetArrayElementAtIndex(index).objectReferenceValue = null;
layers.DeleteArrayElementAtIndex(index);
}
for (var i = 0; i < allLayers.Count; i++)
{
layers.arraySize++;
layers.GetArrayElementAtIndex(layers.arraySize - 1).objectReferenceValue = allLayers[i];
}
serializedObject.ApplyModifiedProperties();
}
}
}