From fdfb66fc594ded9b5fbc4ff75e374d704940221a Mon Sep 17 00:00:00 2001 From: Tipragot Date: Thu, 2 Nov 2023 12:54:37 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20plugin=20de=20d'=C3=A9criture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/typing.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/plugins/typing.py 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], + [], +)