Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/buttons.py
blob: dfb1a46b94d5e81a3418667d718078a377de5259 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# buttons.py
"""
    Copyright (C) 2010  Peter Hewitt

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

"""
import pygame

import g
import utils


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 isinstance(name, str):
        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 isinstance(name, str):
        list1 = []
        list1.append(name)
    else:
        list1 = name
    for b in Button._instances:
        if b.name in list1:
            b.active = False