EventHandler
Module: terminaltexteffects.engine.base_character
Register and handle events related to a character.
Events related to character state changes (e.g. scene complete) 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 caller ID (waypoint id/scene id). The value is a list of tuples of the action and the action target (path 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
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
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. |
RESET_APPEARANCE |
Action
|
Resets the appearance of the character to the input symbol and color. |
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.
The callback function will be called with the character and any additional arguments when the event is triggered. The character will be the first argument passed to the callback function followed by any additional arguments in the order they were passed to the Callback object.
Example
Create a callback to set the character's visibility to False. The following code would be used within an effect where 'self' is the EffectIterator instance:
cb = EventHandler.Callback(lambda c: self.terminal.set_character_visibility(c, is_visible=False))
Source code in terminaltexteffects/engine/base_character.py
__init__(callback, *args)
Initialize 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)
Initialize 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=None)
register_event(event: Event, caller: animation.Scene | motion.Waypoint | motion.Path, action: typing.Literal[Action.ACTIVATE_SCENE, Action.DEACTIVATE_SCENE], target: animation.Scene) -> None
register_event(event: Event, caller: animation.Scene | motion.Waypoint | motion.Path, action: typing.Literal[Action.ACTIVATE_PATH, Action.DEACTIVATE_PATH], target: motion.Path) -> None
register_event(event: Event, caller: animation.Scene | motion.Waypoint | motion.Path, action: typing.Literal[Action.SET_COORDINATE], target: Coord) -> None
register_event(event: Event, caller: animation.Scene | motion.Waypoint | motion.Path, action: typing.Literal[Action.SET_LAYER], target: int) -> None
Register 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 | Path | int | Coord | Callback
|
The target of the action. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the caller or target object is not the correct type for the event or action. |
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)