Tanoda
DetectorLogicGate.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;
11using System.Collections.Generic;
12
13namespace Leap.Unity {
14
28 public class DetectorLogicGate : Detector {
29 #pragma warning disable 0649
30 [SerializeField]
31 [Tooltip("The list of observed detectors.")]
32 private List<Detector> Detectors;
33 #pragma warning restore 0649
43 [Tooltip("Add all detectors on this object automatically.")]
45
50 [Tooltip("The type of logic used to combine detector state.")]
51 public LogicType GateType = LogicType.AndGate;
52
57 [Tooltip("Whether to negate the gate output.")]
58 public bool Negate = false;
59
67 public void AddDetector(Detector detector){
68 if(!Detectors.Contains(detector)){
69 Detectors.Add(detector);
70 activateDetector(detector);
71 }
72 }
73
80 public void RemoveDetector(Detector detector){
81 detector.OnActivate.RemoveListener(CheckDetectors);
82 detector.OnDeactivate.RemoveListener(CheckDetectors);
83 Detectors.Remove(detector);
84 }
85
94 Detector[] detectors = GetComponents<Detector>();
95 for(int g = 0; g < detectors.Length; g++){
96 if ( detectors[g] != this && detectors[g].enabled) {
97 AddDetector(detectors[g]);
98 }
99 }
100 }
101
102 private void Awake(){
103 for (int d = 0; d < Detectors.Count; d++) {
104 activateDetector(Detectors[d]);
105 }
108 }
109 }
110
111 private void activateDetector(Detector detector){
112 detector.OnActivate.RemoveListener(CheckDetectors); //avoid double subscription
113 detector.OnDeactivate.RemoveListener(CheckDetectors);
114 detector.OnActivate.AddListener(CheckDetectors);
115 detector.OnDeactivate.AddListener(CheckDetectors);
116 }
117
118 private void OnEnable() {
120 }
121
122 private void OnDisable () {
123 Deactivate();
124 }
125
131 protected void CheckDetectors(){
132 if (Detectors.Count < 1)
133 return;
134 bool state = Detectors[0].IsActive;
135 for(int a = 1; a < Detectors.Count; a++){
136 if(GateType == LogicType.AndGate){
137 state = state && Detectors[a].IsActive;
138 } else {
139 state = state || Detectors[a].IsActive;
140 }
141 }
142
143 if(Negate){
144 state = !state;
145 }
146
147 if(state){
148 Activate();
149 } else {
150 Deactivate();
151 }
152 }
153 }
154
156 public enum LogicType{ AndGate, OrGate }
157}
UnityEvent OnActivate
Definition: Detector.cs:42
virtual void Deactivate()
Definition: Detector.cs:66
UnityEvent OnDeactivate
Definition: Detector.cs:47
virtual void Activate()
Definition: Detector.cs:54
void RemoveDetector(Detector detector)
void AddDetector(Detector detector)