|
1 | | -""" |
2 | | -A light-weight implementation of event handling built on calls to SDL. |
| 1 | +"""A light-weight implementation of event handling built on calls to SDL. |
3 | 2 |
|
4 | 3 | Many event constants are derived directly from SDL. |
5 | 4 | For example: ``tcod.event.K_UP`` and ``tcod.event.SCANCODE_A`` refer to |
|
8 | 7 | <https://wiki.libsdl.org/SDL_Keycode>`_ |
9 | 8 |
|
10 | 9 | Printing any event will tell you its attributes in a human readable format. |
11 | | -An events type attribute if omitted is just the classes name with all letters |
12 | | -upper-case. |
| 10 | +An events type attribute if omitted is just the classes name with all letters upper-case. |
| 11 | +
|
| 12 | +As a general guideline, you should use :any:`KeyboardEvent.sym` for command inputs, |
| 13 | +and :any:`TextInput.text` for name entry fields. |
| 14 | +
|
| 15 | +Example:: |
| 16 | +
|
| 17 | + import tcod |
| 18 | +
|
| 19 | + KEY_COMMANDS = { |
| 20 | + tcod.event.KeySym.UP: "move N", |
| 21 | + tcod.event.KeySym.DOWN: "move S", |
| 22 | + tcod.event.KeySym.LEFT: "move W", |
| 23 | + tcod.event.KeySym.RIGHT: "move E", |
| 24 | + } |
| 25 | +
|
| 26 | + context = tcod.context.new() |
| 27 | + while True: |
| 28 | + console = context.new_console() |
| 29 | + context.present(console, integer_scaling=True) |
| 30 | + for event in tcod.event.wait(): |
| 31 | + context.convert_event(event) # Adds tile coordinates to mouse events. |
| 32 | + if isinstance(event, tcod.event.Quit): |
| 33 | + print(event) |
| 34 | + raise SystemExit() |
| 35 | + elif isinstance(event, tcod.event.KeyDown): |
| 36 | + print(event) # Prints the Scancode and KeySym enums for this event. |
| 37 | + if event.sym in KEY_COMMANDS: |
| 38 | + print(f"Command: {KEY_COMMANDS[event.sym]}") |
| 39 | + elif isinstance(event, tcod.event.MouseButtonDown): |
| 40 | + print(event) # Prints the mouse button constant names for this event. |
| 41 | + elif isinstance(event, tcod.event.MouseMotion): |
| 42 | + print(event) # Prints the mouse button mask bits in a readable format. |
| 43 | + else: |
| 44 | + print(event) # Print any unhandled events. |
| 45 | +
|
| 46 | +Python 3.10 introduced `match statements <https://docs.python.org/3/tutorial/controlflow.html#match-statements>`_ |
| 47 | +which can be used to dispatch events more gracefully: |
| 48 | +
|
| 49 | +Example:: |
13 | 50 |
|
14 | | -As a general guideline, you should use :any:`KeyboardEvent.sym` for command |
15 | | -inputs, and :any:`TextInput.text` for name entry fields. |
| 51 | + import tcod |
16 | 52 |
|
17 | | -Remember to add the line ``import tcod.event``, as importing this module is not |
18 | | -implied by ``import tcod``. |
| 53 | + KEY_COMMANDS = { |
| 54 | + tcod.event.KeySym.UP: "move N", |
| 55 | + tcod.event.KeySym.DOWN: "move S", |
| 56 | + tcod.event.KeySym.LEFT: "move W", |
| 57 | + tcod.event.KeySym.RIGHT: "move E", |
| 58 | + } |
| 59 | +
|
| 60 | + context = tcod.context.new() |
| 61 | + while True: |
| 62 | + console = context.new_console() |
| 63 | + context.present(console, integer_scaling=True) |
| 64 | + for event in tcod.event.wait(): |
| 65 | + context.convert_event(event) # Adds tile coordinates to mouse events. |
| 66 | + match event: |
| 67 | + case tcod.event.Quit(): |
| 68 | + raise SystemExit() |
| 69 | + case tcod.event.KeyDown(sym) if sym in KEY_COMMANDS: |
| 70 | + print(f"Command: {KEY_COMMANDS[sym]}") |
| 71 | + case tcod.event.KeyDown(sym, scancode, mod, repeat): |
| 72 | + print(f"KeyDown: {sym=}, {scancode=}, {mod=}, {repeat=}") |
| 73 | + case tcod.event.MouseButtonDown(button, pixel, tile): |
| 74 | + print(f"MouseButtonDown: {button=}, {pixel=}, {tile=}") |
| 75 | + case tcod.event.MouseMotion(pixel, pixel_motion, tile, tile_motion): |
| 76 | + print(f"MouseMotion: {pixel=}, {pixel_motion=}, {tile=}, {tile_motion=}") |
| 77 | + case tcod.event.Event() as event: |
| 78 | + print(event) # Show any unhandled events. |
19 | 79 |
|
20 | 80 | .. versionadded:: 8.4 |
21 | 81 | """ |
@@ -762,48 +822,10 @@ def _parse_event(sdl_event: Any) -> Event: |
762 | 822 | def get() -> Iterator[Any]: |
763 | 823 | """Return an iterator for all pending events. |
764 | 824 |
|
765 | | - Events are processed as the iterator is consumed. Breaking out of, or |
766 | | - discarding the iterator will leave the remaining events on the event queue. |
767 | | - It is also safe to call this function inside of a loop that is already |
768 | | - handling events (the event iterator is reentrant.) |
769 | | -
|
770 | | - Example:: |
771 | | -
|
772 | | - context: tcod.context.Context # Context object initialized earlier. |
773 | | - for event in tcod.event.get(): |
774 | | - context.convert_event(event) # Add tile coordinates to mouse events. |
775 | | - if isinstance(event, tcod.event.Quit): |
776 | | - print(event) |
777 | | - raise SystemExit() |
778 | | - elif isinstance(event, tcod.event.KeyDown): |
779 | | - print(event) # Prints the Scancode and KeySym enums for this event. |
780 | | - elif isinstance(event, tcod.event.MouseButtonDown): |
781 | | - print(event) # Prints the mouse button constant names for this event. |
782 | | - elif isinstance(event, tcod.event.MouseMotion): |
783 | | - print(event) # Prints the mouse button mask bits in a readable format. |
784 | | - else: |
785 | | - print(event) # Print any unhandled events. |
786 | | - # For loop exits after all current events are processed. |
787 | | -
|
788 | | - Python 3.10 introduced `match statements <https://docs.python.org/3/tutorial/controlflow.html#match-statements>`_ |
789 | | - which can be used to dispatch events more gracefully: |
790 | | -
|
791 | | - Example:: |
792 | | -
|
793 | | - context: tcod.context.Context # Context object initialized earlier. |
794 | | - for event in tcod.event.get(): |
795 | | - context.convert_event(event) # Add tile coordinates to mouse events. |
796 | | - match event: |
797 | | - case tcod.event.Quit(): |
798 | | - raise SystemExit() |
799 | | - case tcod.event.KeyDown(sym, scancode, mod, repeat): |
800 | | - print(f"KeyDown: {sym=}, {scancode=}, {mod=}, {repeat=}") |
801 | | - case tcod.event.MouseButtonDown(button, pixel, tile): |
802 | | - print(f"MouseButtonDown: {button=}, {pixel=}, {tile=}") |
803 | | - case tcod.event.MouseMotion(pixel, pixel_motion, tile, tile_motion): |
804 | | - print(f"MouseMotion: {pixel=}, {pixel_motion=}, {tile=}, {tile_motion=}") |
805 | | - case tcod.event.Event() as event: |
806 | | - print(event) # Show any unhandled events. |
| 825 | + Events are processed as the iterator is consumed. |
| 826 | + Breaking out of, or discarding the iterator will leave the remaining events on the event queue. |
| 827 | + It is also safe to call this function inside of a loop that is already handling events |
| 828 | + (the event iterator is reentrant.) |
807 | 829 | """ |
808 | 830 | sdl_event = ffi.new("SDL_Event*") |
809 | 831 | while lib.SDL_PollEvent(sdl_event): |
|
0 commit comments