EventHandler
Module: terminaltexteffects.engine.base_character
Register and handle events related to a character.
Events related to character state changes (e.g. waypoint reached) can be registered with the EventHandler. When an event is triggered, the EventHandler will take the specified action (e.g. activate a Path). The EventHandler is used by the EffectCharacter class to handle events related to the character.
Attributes:
Name | Type | Description |
---|---|---|
character |
EffectCharacter
|
The character that the EventHandler is handling events for. |
registered_events |
dict[tuple[Event, str], list[tuple[Action, str]]]
|
A dictionary of registered events. The key is a tuple of the event and the subject_id (waypoint id/scene id). The value is a list of tuples of the action and the action target (waypoint id/scene id). |
layer |
int
|
The layer of the character. The layer determines the order in which characters are printed. |
Note
SEGMENT_ENTERED/EXITED events will trigger the first time the character enters or exits a segment. If looping, each loop will trigger the event, but not backwards motion as is possible with the bounce easing functions.
Source code in terminaltexteffects/engine/base_character.py
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
Action
Bases: Enum
Actions that can be taken when an event is triggered.
An Action is taken when an Event is triggered. Register Actions with the EventHandler using the register_event method of the EventHandler class.
Attributes:
Name | Type | Description |
---|---|---|
ACTIVATE_PATH |
Action
|
Activates a path. The action target is the path ID. |
ACTIVATE_SCENE |
Action
|
Activates an animation scene. The action target is the scene ID. |
DEACTIVATE_PATH |
Action
|
Deactivates a path. The action target is the path ID. |
DEACTIVATE_SCENE |
Action
|
Deactivates an animation scene. The action target is the scene ID. |
SET_LAYER |
Action
|
Sets the layer of the character. The action target is the layer number. |
SET_COORDINATE |
Action
|
Sets the coordinate of the character. The action target is the coordinate. |
CALLBACK |
Action
|
Calls a callback function. The action target is an EventHandler.Callback object. |
Source code in terminaltexteffects/engine/base_character.py
Callback
dataclass
A callback action target that can be taken when an event is triggered.
Register callback actions with the EventHandler using the register_event method of the EventHandler class.
Source code in terminaltexteffects/engine/base_character.py
__init__(callback, *args)
Initializes the instance with the callback function and arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
The callback function to call. |
required |
args |
tuple[Any, ...]
|
A tuple of arguments to pass to the callback function. The first argument will be the character, followed by any additional arguments. |
()
|
Source code in terminaltexteffects/engine/base_character.py
Event
Bases: Enum
An Event that can be registered with the EventHandler.
Register Events with the EventHandler using the register_event method of the EventHandler class.
Attributes:
Name | Type | Description |
---|---|---|
SEGMENT_ENTERED |
Event
|
A path segment has been entered. |
SEGMENT_EXITED |
Event
|
A path segment has been exited. |
PATH_ACTIVATED |
Event
|
A path has been activated. |
PATH_COMPLETE |
Event
|
A path has been completed. |
PATH_HOLDING |
Event
|
A path has entered the holding state. |
SCENE_ACTIVATED |
Event
|
An animation scene has been activated. |
SCENE_COMPLETE |
Event
|
An animation scene has completed. |
Source code in terminaltexteffects/engine/base_character.py
__init__(character)
Initializes the instance with the EffectCharacter object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
character |
EffectCharacter
|
The character for which the EventHandler is handling events. |
required |
Source code in terminaltexteffects/engine/base_character.py
register_event(event, caller, action, target)
Registers an event to be handled by the EventHandler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event
|
The event to register. |
required |
caller |
Scene | Waypoint | Path
|
The object that triggers the event. |
required |
action |
Action
|
The action to take when the event is triggered. |
required |
target |
Scene | Waypoint | Path | int | Coord | Callback
|
The target of the action. |
required |
Example
Register an event to activate a scene when a Path is complete:
event_handler.register_event(EventHandler.Event.PATH_COMPLETE, some_path, EventHandler.Action.ACTIVATE_SCENE, some_scene)