Skip to content

Commit f9c0e39

Browse files
committed
add a SignalTracker class based on jme3utilities.ui.Signals
1 parent 87338b3 commit f9c0e39

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
Copyright (c) 2013-2020, Stephen Gold
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* Neither the name of the copyright holder nor the names of its contributors
13+
may be used to endorse or promote products derived from this software without
14+
specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
package jme3utilities;
28+
29+
import java.util.Map;
30+
import java.util.Set;
31+
import java.util.TreeMap;
32+
import java.util.TreeSet;
33+
import java.util.logging.Level;
34+
import java.util.logging.Logger;
35+
36+
/**
37+
* Track the active/inactive status of named signals. A signal may originate
38+
* from multiple sources such as buttons or hotkeys. A signal is active as long
39+
* as any of its sources is active.
40+
*
41+
* @author Stephen Gold sgold@sonic.net
42+
*/
43+
public class SignalTracker {
44+
// *************************************************************************
45+
// constants and loggers
46+
47+
/**
48+
* message logger for this class
49+
*/
50+
final public static Logger logger
51+
= Logger.getLogger(SignalTracker.class.getName());
52+
// *************************************************************************
53+
// fields
54+
55+
/**
56+
* map signal names to statuses
57+
*/
58+
final private Map<String, Set<Integer>> statusMap = new TreeMap<>();
59+
// *************************************************************************
60+
// new methods exposed
61+
62+
/**
63+
* Add a new signal with all of its sources inactive. If the signal name is
64+
* already in use, this has no effect.
65+
*
66+
* @param name the name for the signal (not null)
67+
*/
68+
public void add(String name) {
69+
Validate.nonNull(name, "signal name");
70+
71+
Set<Integer> status = statusMap.get(name);
72+
if (status == null) {
73+
status = new TreeSet<>();
74+
statusMap.put(name, status);
75+
}
76+
}
77+
78+
/**
79+
* Update whether a named signal source is active.
80+
*
81+
* @param signalName the signal's name (not null)
82+
* @param sourceIndex the index of the signal source (key or button) which
83+
* is being updated
84+
* @param newState true if the source has gone active; false if the source
85+
* has gone inactive
86+
*/
87+
public void setActive(String signalName, int sourceIndex,
88+
boolean newState) {
89+
Validate.nonNull(signalName, "signal name");
90+
91+
Set<Integer> status = statusMap.get(signalName);
92+
if (status == null) {
93+
logger.log(Level.WARNING, "Unknown signal: {0}",
94+
MyString.quote(signalName));
95+
return;
96+
}
97+
logger.log(Level.INFO, "name = {0}, newState = {1}", new Object[]{
98+
MyString.quote(signalName), newState
99+
});
100+
101+
if (newState) {
102+
status.add(sourceIndex);
103+
} else {
104+
status.remove(sourceIndex);
105+
}
106+
}
107+
108+
/**
109+
* Test whether the named signal is active.
110+
*
111+
* @param signalName the name of the signal (not null)
112+
* @return true if any of the signal's sources is active, otherwise false
113+
*/
114+
public boolean test(String signalName) {
115+
Validate.nonNull(signalName, "signal name");
116+
117+
Set<Integer> status = statusMap.get(signalName);
118+
if (status == null) {
119+
logger.log(Level.WARNING,
120+
"Testing a signal which has not yet been added: {0}.",
121+
MyString.quote(signalName));
122+
status = new TreeSet<>();
123+
statusMap.put(signalName, status);
124+
}
125+
boolean result = !status.isEmpty();
126+
127+
return result;
128+
}
129+
}

0 commit comments

Comments
 (0)