-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
121 lines (107 loc) · 3.83 KB
/
Copy pathbutton.py
File metadata and controls
121 lines (107 loc) · 3.83 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import pyxel
import time
KEY_PAD = (
pyxel.KEY_KP_0,
pyxel.KEY_KP_1,
pyxel.KEY_KP_2,
pyxel.KEY_KP_3,
pyxel.KEY_KP_4,
pyxel.KEY_KP_5,
pyxel.KEY_KP_6,
pyxel.KEY_KP_7,
pyxel.KEY_KP_8,
pyxel.KEY_KP_9,
)
class Button:
def __init__(self, buttonType, value, x, y, w, h, key=None, altKey=None):
self.type = buttonType
self.value = value
self.selected = False
self.x = x
self.y = y
self.w = w
self.h = h
# Key code
if key == None:
self.key = self.__getKey()
else:
self.key = key
self.altKey=altKey
# Must be above the threshold (seconds) to count as a keypress
self.keyCooldownThreshold = 0.02
self.lastPressed = time.time() - self.keyCooldownThreshold
def draw(self):
if self.value == "⌫":
# We don't display that button
return
col = 6
if self.selected:
col = 7
pyxel.rectb(self.x - 1, self.y - 1, self.w + 2, self.h + 2, 0)
pyxel.rect(self.x, self.y, self.w, self.h, col)
# Drawing the text on the middle
txtOff = 4 * len(str(self.value)) / 2
value = str(self.value)
pyxel.text(self.x + self.w / 2 - txtOff, self.y + self.h / 2 - 3, value, 0)
def keyPressed(self, key):
# If it is going to return True, it first needs to check if the cooldown time has passed
if isinstance(key, int):
if self.value == 8 or self.value == 9:
print(
self.value,
pyxel.btnp(key),
not pyxel.btn(pyxel.KEY_SHIFT),
self.__checkCooldown()
)
return pyxel.btnp(key) and not pyxel.btn(pyxel.KEY_SHIFT) and self.__checkCooldown()
elif isinstance(key, tuple):
# All keys must be pressed
for k in key:
if k == pyxel.KEY_SHIFT:
# Must be pressed (btn)
if not pyxel.btn(k):
return False
else:
# Just a click (btnp)
if not pyxel.btnp(k):
return False
# True, if there's enough cooldown
return self.__checkCooldown()
elif isinstance(key, list):
# Just one of them can be pressed
for k in key:
if pyxel.KEY_SHIFT not in key:
# Just a click
if pyxel.btnp(k):
return self.__checkCooldown() and not pyxel.btn(pyxel.KEY_SHIFT)
return False
def mouseInside(self):
x, y = pyxel.mouse_x, pyxel.mouse_y
return (x > self.x and x < self.x + self.w) and (y > self.y and y < self.y + self.h)
def __checkCooldown(self):
# REFACTOR/DEBUG: Cooldown not needed?
# return True
deltaT = time.time() - self.lastPressed
if deltaT >= self.keyCooldownThreshold:
self.lastPressed = time.time()
return True
return False
def __getKey(self):
if self.type == "num":
# Values go from 48 to 57
return [48 + self.value, KEY_PAD[self.value]]
elif self.type == "special":
if self.value == "(":
return (pyxel.KEY_SHIFT, pyxel.KEY_8)
elif self.value == ")":
return (pyxel.KEY_SHIFT, pyxel.KEY_9)
elif self.value == "CE":
return pyxel.KEY_DELETE
elif self.value == "-":
return pyxel.KEY_MINUS
elif self.value == ".":
return [pyxel.KEY_PERIOD, pyxel.KEY_KP_PERIOD]
elif self.value == "⌫":
return pyxel.KEY_BACKSPACE
def __str__(self):
return f"Button: {self.value} ({self.type})"