Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pgu/gui/basic.py
blob: 4d9ccffe3af41db8d6012c4cc155499baacbe45a (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
"""These widgets are all grouped together because they are non-interactive widgets.
"""

import pygame

from const import *
import widget

class Spacer(widget.Widget):
    """A invisible space.
    
    <pre>Spacer(width,height)</pre>
    
    """
    def __init__(self,width,height,**params):
        params.setdefault('focusable',False)
        widget.Widget.__init__(self,width=width,height=height,**params)
        

class Color(widget.Widget):
    """A block of color.
    
    <p>The color can be changed at run-time.</p>
    
    <pre>Color(value=None)</pre>
    
    <strong>Example</strong>
    <code>
    c = Color()
    c.value = (255,0,0)
    c.value = (0,255,0)
    </code>
    """
    
    
    def __init__(self,value=None,**params):
        params.setdefault('focusable',False)
        if value != None: params['value']=value
        widget.Widget.__init__(self,**params)
    
    def paint(self,s):
        if hasattr(self,'value'): s.fill(self.value)
    
    def __setattr__(self,k,v):
        if k == 'value' and type(v) == str: v = pygame.Color(v)
        _v = self.__dict__.get(k,NOATTR)
        self.__dict__[k]=v
        if k == 'value' and _v != NOATTR and _v != v: 
            self.send(CHANGE)
            self.repaint()

class Label(widget.Widget):
    """A text label.
    
    <pre>Label(value)</pre>
    
    <dl>
    <dt>value<dd>text to be displayed
    </dl>
    
    <strong>Example</strong>
    <code>
    w = Label(value="I own a rubber chicken!")
    
    w = Label("3 rubber chickens")
    </code>
    """
    def __init__(self,value,**params):
        params.setdefault('focusable',False)
        params.setdefault('cls','label')
        widget.Widget.__init__(self,**params)
        self.value = value
        self.font = self.style.font
        self.style.width, self.style.height = self.font.size(self.value)
    
    def paint(self,s):
        s.blit(self.font.render(self.value, 1, self.style.color),(0,0))

class Image(widget.Widget):
    """An image.
    
    <pre>Image(value)</pre>
    
    <dl>
    <dt>value<dd>a file name or a pygame.Surface
    </dl>
    
    """
    def __init__(self,value,**params):
        params.setdefault('focusable',False)
        widget.Widget.__init__(self,**params)
        if type(value) == str: value = pygame.image.load(value)
        
        ow,oh = iw,ih = value.get_width(),value.get_height()
        sw,sh = self.style.width,self.style.height
        
        if sw and not sh:
            iw,ih = sw,ih*sw/iw
        elif sh and not sw:
            iw,ih = iw*sh/ih,sh
        elif sw and sh:
            iw,ih = sw,sh
        
        if (ow,oh) != (iw,ih):
            value = pygame.transform.scale(value,(iw,ih))
        self.style.width,self.style.height = iw,ih
        self.value = value
    
    def paint(self,s):
        s.blit(self.value,(0,0))