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
|
"""These widgets are all grouped together because they are non-interactive widgets.
"""
import pygame
from .const import *
from . import widget
# Turns a descriptive string or a tuple into a pygame color
def parse_color(desc):
if (is_color(desc)):
# Already a color
return desc
elif (desc and desc[0] == "#"):
# Because of a bug in pygame 1.8.1 we need to explicitly define the
# alpha value otherwise it will default to transparent.
if (len(desc) == 7):
desc += "FF"
return pygame.Color(desc)
# Determines if the given object is a pygame-compatible color or not
def is_color(col):
# In every version of pygame (up to 1.8.1 so far) will interpret
# a tuple as a color.
if (type(col) == tuple):
return col
if (hasattr(pygame, "Color") and type(pygame.Color) == type):
# This is a recent version of pygame that uses a proper type
# instance for colors.
return (isinstance(col, pygame.Color))
# Otherwise, this version of pygame only supports tuple colors
return False
class Spacer(widget.Widget):
"""An invisible space widget."""
def __init__(self,width,height,**params):
params.setdefault('focusable',False)
widget.Widget.__init__(self,width=width,height=height,**params)
class Color(widget.Widget):
"""A widget that renders as a solid block of color.
Note the color can be changed by setting the 'value' field, and the
widget will automatically be repainted, eg:
c = Color()
c.value = (255,0,0)
c.value = (0,255,0)
"""
_value = None
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)
@property
def value(self):
return self._value
@value.setter
def value(self, val):
if (isinstance(val, basestring)):
# Parse the string as a color
val = parse_color(val)
oldval = self._value
self._value = val
if (oldval != val):
# Emit a change signal
self.send(CHANGE)
self.repaint()
class Label(widget.Widget):
"""A text label widget."""
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):
"""Renders the label onto the given surface in the upper-left corner."""
s.blit(self.font.render(self.value, 1, self.style.color),(0,0))
def set_text(self, txt):
"""Set the text of this label."""
self.value = txt
# Signal to the application that we need to resize this widget
self.chsize()
def set_font(self, font):
"""Set the font used to render this label."""
this.font = font
# Signal to the application that we need a resize
this.chsize()
def resize(self,width=None,height=None):
# Calculate the size of the rendered text
(self.style.width, self.style.height) = self.font.size(self.value)
return (self.style.width, self.style.height)
class Image(widget.Widget):
"""An image widget. The constructor takes a file name or a pygame surface."""
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))
|