Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/buttons.py
blob: 9542796a08b9c876df70af030e458268a0e5f218 (plain)
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
# buttons.py
import g,utils,pygame

class Button:
    _instances=[]
    
    def __init__(self,name,(x1,y1),centre=True): # eg ('plus',(30,40))
        self._instances.append(self)
        up=utils.load_image(name+"_up.png",True)
        down=utils.load_image(name+"_down.png",True)
        w=up.get_width();h=up.get_height();x=x1;y=y1
        if centre:
            self.cx=x; self.cy=y
            x=x-w/2; y=y-h/2
        else:
            self.cx=x+w/2; self.cy=y+h/2
        self.rect=pygame.Rect(x,y,w,h)
        self.name=name; self.x=x; self.y=y; self.active=True
        self.up=up; self.down=down; self.stay_down=False

    def mouse_on(self):
        mx,my=g.pos
        return self.rect.collidepoint(mx,my)

    def draw_up(self):
        g.screen.blit(self.up,(self.x,self.y))

    def draw_down(self):
        g.screen.blit(self.down,(self.x,self.y))

    def on(self):
        self.active=True

    def off(self):
        self.active=False

def draw():
    for b in Button._instances:
        if b.active:
            if b.stay_down:
                b.draw_down()
            else:
                b.draw_up()

def check():
    clear()
    for b in Button._instances:
        if b.active:
            if b.mouse_on():
                if b.name in ('xyz1','xyz2'):
                    b.stay_down=True
                else:
                    b.draw_down()
                    g.screen.blit(g.pointer,g.pos)
                    pygame.display.flip()
                    pygame.time.wait(300)
                return b.name #****
    return '' # no button pressed

def clear():
    for b in Button._instances: b.stay_down=False
    
def active(name):
    for b in Button._instances:
        if b.name==name: return b.active #****
    return False # not found

def stay_down(name):
    for b in Button._instances:
        if b.name==name: b.stay_down=True; return #****
    return

def mouse_on(name):
    mx,my=g.pos
    for b in Button._instances:
        if b.name==name:
            return b.rect.collidepoint(mx,my)
    return False

def set_mouse(name):
    p2=g.pointer.get_height()/2
    for b in Button._instances:
        if b.name==name:
            x=b.cx; y=b.cy+p2
            pygame.mouse.set_pos(x,y); g.pos=(x,y)

# eg1 buttons.on('plus')
# eg2 buttons.on(['plus','times'])
def on(name):
    if type(name)==type('a'):
        list1=[]; list1.append(name)
    else:
        list1=name
    for b in Button._instances:
        if b.name in list1: b.active=True

# eg1 buttons.off('plus')
# eg2 buttons.off(['plus','times'])
def off(name):
    if type(name)==type('a'):
        list1=[]; list1.append(name)
    else:
        list1=name
    for b in Button._instances:
        if b.name in list1: b.active=False