ecs #58
43
src/plugins/typing.py
Normal file
43
src/plugins/typing.py
Normal file
|
@ -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],
|
||||||
|
[],
|
||||||
|
)
|
Loading…
Reference in a new issue