|
2 | 2 | Effect Managers |
3 | 3 | =============== |
4 | 4 |
|
5 | | -Write stuff |
| 5 | +An effect manager is responsible of: |
| 6 | + |
| 7 | +- Instantiating effects |
| 8 | +- Knowing what effect should be drawn based in some internal state |
| 9 | +- Reading keyboard events if this is needed (optional) |
| 10 | + |
| 11 | +This is an example of the default ``SingleEffectManager``. |
| 12 | + |
| 13 | +.. code-block:: python |
| 14 | +
|
| 15 | + class SingleEffectManager(BaseEffectManger): |
| 16 | + """Run a single effect""" |
| 17 | + def __init__(self, effect_module=None): |
| 18 | + """ |
| 19 | + Initalize the manager telling it what effect should run. |
| 20 | +
|
| 21 | + :param effect_module: The effect module to run |
| 22 | + """ |
| 23 | + self.active_effect = None |
| 24 | + self.effect_module = effect_module |
| 25 | +
|
| 26 | + def pre_load(self): |
| 27 | + """ |
| 28 | + Initialize the effect that should run. |
| 29 | + """ |
| 30 | + # Instantiate all registered effects |
| 31 | + effect_list = [cfg.cls() for name, cfg in effects.effects.items()] |
| 32 | + # Find the single effect we are supposed to draw |
| 33 | + for effect in effect_list: |
| 34 | + if effect.name == self.effect_module: |
| 35 | + self.active_effect = effect |
| 36 | +
|
| 37 | + # Show some modest anger when we have been lied to |
| 38 | + if not self.active_effect: |
| 39 | + print("Cannot find effect '{}'".format(self.active_effect)) |
| 40 | + print("Available effects:") |
| 41 | + print("\n".join(e.name for e in effect_list)) |
| 42 | + return False |
| 43 | + return True |
| 44 | +
|
| 45 | + def post_load(self): |
| 46 | + return True |
| 47 | +
|
| 48 | + def draw(self, time, frametime, target): |
| 49 | + """This is called every frame by the framework""" |
| 50 | + self.active_effect.draw(time, frametime, target) |
| 51 | +
|
| 52 | + def key_event(self, key, scancode, action, mods): |
| 53 | + """Called on most key presses""" |
| 54 | + print("SingleEffectManager:key_event", key, scancode, action, mods) |
| 55 | +
|
| 56 | +It's important to understand that ``pre_load`` is called before resources are loaded |
| 57 | +and this is the correct place to instantiate effects. ``post_load`` is called right |
| 58 | +after loading is done. |
| 59 | + |
| 60 | +The ``draw`` method is called every frame and you will have to send this to the effect |
| 61 | +you want to draw. |
| 62 | + |
| 63 | +The ``key_events`` method will trigger on key presses. |
| 64 | + |
| 65 | +BaseEffectManger |
| 66 | +^^^^^^^^^^^^^^^^ |
| 67 | + |
| 68 | +.. autoclass:: demosys.effects.managers.BaseEffectManger |
| 69 | + :members: |
0 commit comments