Tanoda
InteractionHandEditor.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 Leap.Unity.Query;
10using System.Collections;
11using System.Collections.Generic;
12using UnityEditor;
13using UnityEngine;
14
15namespace Leap.Unity.Interaction {
16
17 [CanEditMultipleObjects]
18 [CustomEditor(typeof(InteractionHand), editorForChildClasses: true)]
20
21 private Texture _handTex;
22 private Rect _handTexRect;
23
24 protected override void OnEnable() {
25 base.OnEnable();
26
27 _handTex = EditorResources.Load<Texture2D>("HandTex");
28
29 hideField("_leapProvider");
30 specifyCustomDecorator("manager", drawProvider);
31
32 specifyCustomDrawer("enabledPrimaryHoverFingertips", drawPrimaryHoverFingertipsEditor);
33 }
34
35 private void drawProvider(SerializedProperty p) {
36 EditorGUILayout.PropertyField(serializedObject.FindProperty("_leapProvider"));
37 }
38
39 private void drawPrimaryHoverFingertipsEditor(SerializedProperty property) {
40
41 EditorGUILayout.BeginHorizontal();
42
43 EditorGUILayout.PrefixLabel(
44 new GUIContent("Primary Hover Fingertips",
45 "Check which fingertips should be used "
46 + "as primary hover points for this interaction controller. Fewer "
47 + "points is cheaper. Proximity to one of these points determines "
48 + "which interaction object is chosen as the primary hover for this "
49 + "interaction controller at any given time. Generally speaking, "
50 + "choose the fingertips you'd like users to be able to use to "
51 + "choose and push a button."));
52
53 Rect controlRect = EditorGUILayout.GetControlRect(GUILayout.Height(102));
54 drawHandEditor(controlRect);
55
56 EditorGUILayout.EndHorizontal();
57 }
58
59 private void drawHandEditor(Rect controlRect) {
60 // Determine whether the target object is a prefab. AttachmentPoints cannot be edited on prefabs.
61 var isTargetPrefab = Utils.IsObjectPartOfPrefabAsset(target.gameObject);
62
63 // Image container.
64 Rect imageContainerRect = controlRect;
65 EditorGUI.DrawRect(imageContainerRect, new Color(0.2F, 0.2F, 0.2F));
66 imageContainerRect.x += 1; imageContainerRect.y += 1; imageContainerRect.width -= 2; imageContainerRect.height -= 2;
67 EditorGUI.DrawRect(imageContainerRect, new Color(0.6F, 0.6F, 0.6F));
68 imageContainerRect.x += 1; imageContainerRect.y += 1; imageContainerRect.width -= 2; imageContainerRect.height -= 2;
69 EditorGUI.DrawRect(imageContainerRect, new Color(0.2F, 0.2F, 0.2F));
70
71 // Hand image.
72 _handTexRect = new Rect(0F, 0F, (controlRect.height - 2) * (_handTex.width / (float)_handTex.height), controlRect.height - 2);
73 _handTexRect.center += imageContainerRect.center - _handTexRect.center;
74 EditorGUI.DrawTextureTransparent(_handTexRect, _handTex);
75
76 // Toggle boxes.
77 EditorGUI.BeginDisabledGroup(isTargetPrefab);
78
79 makeFingertipToggle(0, new Vector2(-0.390F, 0.110F));
80 makeFingertipToggle(1, new Vector2(-0.080F, -0.380F));
81 makeFingertipToggle(2, new Vector2(0.090F, -0.420F));
82 makeFingertipToggle(3, new Vector2(0.245F, -0.380F));
83 makeFingertipToggle(4, new Vector2(0.410F, -0.210F));
84 }
85
86 private void makeFingertipToggle(int fingerIndex, Vector2 offCenterPosImgSpace) {
87 InteractionHand targetHand = target.intHand;
88 InteractionHand[] targetHands = targets.Query().Cast<InteractionHand>().ToArray();
89
90 if (EditorGUI.Toggle(makeToggleRect(_handTexRect.center
91 + new Vector2(offCenterPosImgSpace.x * _handTexRect.width,
92 offCenterPosImgSpace.y * _handTexRect.height)),
93
94 targetHand.enabledPrimaryHoverFingertips[fingerIndex])) {
95 foreach (var singleTarget in targetHands) {
96 serializedObject.FindProperty("enabledPrimaryHoverFingertips").GetArrayElementAtIndex(fingerIndex).boolValue = true;
97 }
98 }
99 else {
100 foreach (var singleTarget in targetHands) {
101 serializedObject.FindProperty("enabledPrimaryHoverFingertips").GetArrayElementAtIndex(fingerIndex).boolValue = false;
102 }
103 }
104 }
105
106 private const float TOGGLE_SIZE = 15.0F;
107 private Rect makeToggleRect(Vector2 centerPos) {
108 return new Rect(centerPos.x - TOGGLE_SIZE / 2F, centerPos.y - TOGGLE_SIZE / 2F, TOGGLE_SIZE, TOGGLE_SIZE);
109 }
110
111 }
112
113}
UnityEngine.Color Color
Definition: TestScript.cs:32
void specifyCustomDrawer(string propertyName, Action< SerializedProperty > propertyDrawer)
Specify a callback to be used to draw a specific named property. Should be called in OnEnable.
void specifyCustomDecorator(string propertyName, Action< SerializedProperty > decoratorDrawer)
Specify a callback to be used to draw a decorator for a specific named property. Should be called in ...