-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputhandler.py
78 lines (63 loc) · 2.47 KB
/
inputhandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from singleton import Singleton
from vector import Vector2D
from typing import Literal
from enum import Enum
import pygame
class Keyboard(Enum):
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
ESC = 4
class TheInputHandler(metaclass=Singleton):
mousePosition: Vector2D
mouseBtnStates: Literal[3]
keyDown: dict[Keyboard, bool]
def init(self):
self.reset()
def reset(self):
self.mousePosition = Vector2D(0, 0)
self.mouseBtnStates = (False, False, False)
self.keyDown = {}
# Init keyboard states
for key in Keyboard:
self.keyDown[key] = False
def getMouseBtnStates(self) -> Literal[3]:
return self.mouseBtnStates
def getMousePosition(self) -> Vector2D:
return self.mousePosition
def update(self):
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
x, y = pygame.mouse.get_pos()
self.mousePosition.set(x, y)
if event.type == pygame.QUIT:
import game
game.TheGame().gameStateMachine.popState()
game.TheGame().quit()
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouseBtnStates = pygame.mouse.get_pressed()
if event.type == pygame.MOUSEBUTTONUP:
self.mouseBtnStates = pygame.mouse.get_pressed()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.keyDown[Keyboard.UP] = True
if event.key == pygame.K_RIGHT:
self.keyDown[Keyboard.RIGHT] = True
if event.key == pygame.K_DOWN:
self.keyDown[Keyboard.DOWN] = True
if event.key == pygame.K_LEFT:
self.keyDown[Keyboard.LEFT] = True
if event.key == pygame.K_ESCAPE:
self.keyDown[Keyboard.ESC] = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.keyDown[Keyboard.UP] = False
if event.key == pygame.K_RIGHT:
self.keyDown[Keyboard.RIGHT] = False
if event.key == pygame.K_DOWN:
self.keyDown[Keyboard.DOWN] = False
if event.key == pygame.K_LEFT:
self.keyDown[Keyboard.LEFT] = False
if event.key == pygame.K_ESCAPE:
self.keyDown[Keyboard.ESC] = False