Package com.prineside.tdi2.events
Class EventListeners<T extends Event>
java.lang.Object
com.prineside.tdi2.events.EventListeners<T>
- Type Parameters:
T
- type of listener class
Contains a list of listeners for a specific event type.
Thread safe.
Listeners may have up to 8 different (binary) flags set to control their behavior. Flags are
being set as a bit mask which takes 1 byte:
- 0b1 / 1 / 1 << 1 / FLAG_AUTO_PRIORITY - set by the EventListeners automatically, ignored if set
manually. Marks listeners to decide their auto-priority
- 0b10 / 2 / 1 << 2 / FLAG_AFFECTS_STATE - can be used to denote listener as state-affecting,
normally used only in a game session. By default, all listeners are not considered to be
state-affecting thus will not be serialized (will throw exception if you try), will not affect
the order and priorities of the state-affecting listeners and will not be compared during sync
checks. A simple rule: if your listener affects game's state (for example, gives score to the
player = state-affecting, forces some graphics / ui to update = not-state-affecting), then use
this flag on it
- 0b100 / 4 / 1 << 3 / FLAG_PERSISTENT - listeners marked with this flag will survive
EventDispatcher.reset(false). Note: Engine.reset() triggers EventDispatcher.reset(false)
- Other flags / bytes (0b11111000) may depend on custom implementation of EventDispatcher. By
default, you are free to use them as you wish with the default implementation of EventDispatcher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
EventListeners.Entry<T extends Event>
static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DeepClassComparator<EventListeners>
static final byte
static final byte
static final byte
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd listener without flags with auto priority Note: listener will only get FLAG_AUTO_PRIORITY (not state-affecting, not persistent etc)Throws exception if listener does not affect game state but has been added during iteration.addStateAffecting
(Listener<T> listener) Auto-priorityaddStateAffectingWithPriority
(Listener<T> listener, int priority) Add listener with STATE_AFFECTING flag with manual priorityaddWithFlags
(Listener<T> listener, int flags) Add listener and set its priority automatically (will be lower than priority of any of the existing listeners in stateful / not stateful groups) Throws exception if listener is already added.addWithPriority
(Listener<T> listener, int priority) Add listener without flags with manual priority Note: flags will be 0 (not state-affecting, not persistent etc)void
clear()
boolean
com.badlogic.gdx.utils.StringBuilder
describe()
Iterate from 0 to size(), may contain nulls for (int i = 0; i < listeners.size(); i++) { EventListeners.Entry<?> entry = listeners.getEntriesBackingArray()[i]; if (entry == null) continue; ...Class<?>
int
int
int
getLoops()
int
int
int
boolean
Remove (unsubscribe) the listener.int
size()
void
-
Field Details
-
FLAG_AUTO_PRIORITY
public static final byte FLAG_AUTO_PRIORITY- See Also:
-
FLAG_AFFECTS_STATE
public static final byte FLAG_AFFECTS_STATE- See Also:
-
FLAG_PERSISTENT
public static final byte FLAG_PERSISTENT- See Also:
-
PRIORITY_STEP
public static final int PRIORITY_STEP- See Also:
-
PRIORITY_HIGHEST
public static final int PRIORITY_HIGHEST- See Also:
-
PRIORITY_VERY_HIGH
public static final int PRIORITY_VERY_HIGH- See Also:
-
PRIORITY_HIGH
public static final int PRIORITY_HIGH- See Also:
-
PRIORITY_DEFAULT
public static final int PRIORITY_DEFAULT- See Also:
-
PRIORITY_LOW
public static final int PRIORITY_LOW- See Also:
-
PRIORITY_VERY_LOW
public static final int PRIORITY_VERY_LOW- See Also:
-
PRIORITY_LOWEST
public static final int PRIORITY_LOWEST- See Also:
-
CLASS_COMPARATOR
-
-
Constructor Details
-
EventListeners
-
-
Method Details
-
getEntriesBackingArray
Iterate from 0 to size(), may contain nulls for (int i = 0; i < listeners.size(); i++) { EventListeners.Entry<?> entry = listeners.getEntriesBackingArray()[i]; if (entry == null) continue; ... } -
trigger
-
getEventClass
- Returns:
- event type class passed to listeners
-
contains
-
getStateHash
public int getStateHash() -
getStateAffectingEntriesCount
public int getStateAffectingEntriesCount() -
getNonStateAffectingEntriesCount
public int getNonStateAffectingEntriesCount() -
getLoops
public int getLoops() -
add
Add listener without flags with auto priority Note: listener will only get FLAG_AUTO_PRIORITY (not state-affecting, not persistent etc) -
addWithPriority
Add listener without flags with manual priority Note: flags will be 0 (not state-affecting, not persistent etc) -
addStateAffectingWithPriority
Add listener with STATE_AFFECTING flag with manual priority -
addWithFlags
Add listener and set its priority automatically (will be lower than priority of any of the existing listeners in stateful / not stateful groups) Throws exception if listener is already added. Checks for AFFECTS_STATE flag.- Parameters:
flags
- will be set on the listener with FLAG_AUTO_PRIORITY set to true. Its AFFECTS_STATE bit will be checked to decide its priority among the listeners of the same state-affection Note: resulting flags will be exactly the same as in @param flags but with FLAG_AUTO_PRIORITY set to true
-
addStateAffecting
Auto-priority -
add
Throws exception if listener does not affect game state but has been added during iteration.- Returns:
- entry in the group
-
remove
Remove (unsubscribe) the listener. If event is being triggered, the listener will be removed at the end of the loop. Note: method references / lambdas are dynamic in Java, do not pass method references as listeners if you want to remove them later (or store them in some field first) as it is impossible to find a subscribed listener by method reference (new object is being created each time)- Returns:
- true if listener has been removed, false if it was not scheduled and was not in the list of listeners to be added
-
getEventsTriggered
public int getEventsTriggered()- Returns:
- number of begun events (begin() calls)
-
getEventsStopped
public int getEventsStopped() -
size
public int size() -
clear
public void clear() -
describe
public com.badlogic.gdx.utils.StringBuilder describe()
-