-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttons.py
More file actions
83 lines (70 loc) · 3.18 KB
/
Copy pathbuttons.py
File metadata and controls
83 lines (70 loc) · 3.18 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
# classes are WEIRD man...
class button:
def __init__(self, attribute:str=None, screen=None, pos:tuple[int,int]=None, **kwargs):
self.screen = screen
self.pos = pos
self.type = 'button'
self.attribute = attribute
def was_clicked(self, mouse_data:dict, bounds:tuple[int,int]=None) -> bool:
'''
Detect if button object was clicked by checking if the mouse_data was
within a certain bounds.
For a simple button, these bounds are defined as the begining of the
buttons attribute, up to the end of the button's attribute.
# Parameters
1. **mouse_data**: the (line, column) coordinate of the mouse
2. **bounds**: lower and upper bound to check
'''
if bounds == None : bounds = (self.pos[1],self.pos[1]+len(self.attribute))
mouse_check=(
mouse_data['ln'] == self.pos[0] and
mouse_data['col'] in range(*bounds)
)
return(mouse_check)
def show(self, **kwargs):
'''
Displays the button object on the screen at a given position. Position
can be provided either in the object's deffinition or here, by passing
pos parameter
# Parameters
1. **pos**: the (line, column) position to display at
'''
try:
if self.screen==None: self.screen=kwargs['screen']
if self.pos==None: self.pos=kwargs['pos']
except: raise Exception(f"No screen or position set for {self}")
match self.type:
case 'button':
self.screen.addstr(*self.pos, self.attribute)
case 'toggle':
self.screen.addstr(*self.pos, self.attribute + self.flag)
case 'counter':
self.screen.addstr(*self.pos, ''.join(self.flag) + self.attribute)
class toggle(button):
def __init__(self, attribute = None, screen=None, pos = None, **kwargs):
super().__init__(attribute, screen, pos, **kwargs)
self.type='toggle'
self.state=False
self.flag='[ ]'
def was_clicked(self, mouse_data, bounds = None) -> bool:
if bounds == None: bounds = (self.pos[1], self.pos[1]+len(self.flag)+len(self.attribute))
mouse_check = super().was_clicked(mouse_data, bounds)
if mouse_check: self.swap_state()
return mouse_check
def swap_state(self) -> None:
state_update = not(self.state)
if state_update: self.flag = '[x]'
else: self.flag = '[ ]'
self.state = state_update
class counter(button):
def __init__(self, attribute = None, screen=None, pos = None, default=0, **kwargs):
super().__init__(attribute, screen, pos, **kwargs)
self.value = default
self.type = 'counter'
self.flag = ['<<', f'{self.value}', '>>']
def was_clicked(self, mouse_data):
low = super().was_clicked(mouse_data, bounds = (self.pos[1], self.pos[1]+len(self.flag[0])))
high = super().was_clicked(mouse_data, bounds = (self.pos[1]+len(''.join(self.flag[:-1])), self.pos[1]+len(''.join(self.flag))))
if low: self.value = self.value - 1
if high: self.value = self.value + 1
self.flag = ['<<',f'{self.value}','>>']