diff --git a/src/plugins/typing.py b/src/plugins/typing.py new file mode 100644 index 0000000..ff905ed --- /dev/null +++ b/src/plugins/typing.py @@ -0,0 +1,43 @@ +""" +Definit un plugin qui crée un texte avec les touches frappées +""" + +from engine import Scene, World +from plugins.inputs import Pressed +from plugins.text import Text + + +class Typing: + """ + Marque une entité comme un texte qui s'ecrit en fonction du clavier + """ + + def __init__(self, accepted_chars: str, max_chars: int = 10) -> None: + self.accepted_chars = accepted_chars + self.max_chars = max_chars + + +def __update(world: World): + """ + Met a jour les entitées contenant le composant Typing + """ + pressed = world[Pressed] + for entity in world.query(Typing, Text): + text = entity[Text] + for key in pressed: + if key == "backspace": + text.text = text.text[:-1] + if key.startswith("["): # pavé numerique + key = key[1] + if ( + key in entity[Typing].accepted_chars + and len(text.text) < entity[Typing].max_chars + ): + text.text += key + + +PLUGIN = Scene( + [], + [__update], + [], +)