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 149 150
|
#!/usr/bin/env python
"""
A Rectangle stimulus.
This module contains a class implementing a rectangle stimulus.
"""
__author__ = 'Florian Krause <florian@expyriment.org>, \
Oliver Lindemann <oliver@expyriment.org>'
__version__ = '0.7.0'
__revision__ = '55a4e7e'
__date__ = 'Wed Mar 26 14:33:37 2014 +0100'
import pygame
import defaults
from _visual import Visual
import expyriment
class Rectangle(Visual):
"""A class implementing a rectangle stimulus."""
def __init__(self, size, colour=None, line_width=None, position=None):
"""Create a rectangle.
Parameters
----------
size : (int, int)
size (width, height) of the rectangle
line_width : int, optional
line width in pixels; 0 will result in a filled rectangle,
as does a value < 0 or >= min(size)
position : (int, int), optional
position of the stimulus
colour : (int, int, int), optional
colour of the rectangle
"""
if position is None:
position = defaults.rectangle_position
Visual.__init__(self, position=position)
self._size = size
if colour is None:
colour = defaults.rectangle_colour
if colour is not None:
self._colour = colour
else:
self._colour = expyriment._active_exp.foreground_colour
if line_width is None:
line_width = defaults.rectangle_line_width
elif line_width < 0 or line_width >= min(self._size):
line_width = 0
self._line_width = line_width
_getter_exception_message = "Cannot set {0} if surface exists!"
@property
def size(self):
"""Getter for size."""
return self._size
@size.setter
def size(self, value):
"""Setter for size."""
if self.has_surface:
raise AttributeError(Rectangle._getter_exception_message.format(
"size"))
else:
self._size = value
@property
def colour(self):
"""Getter for colour."""
return self._colour
@colour.setter
def colour(self, value):
"""Setter for colour."""
if self.has_surface:
raise AttributeError(Rectangle._getter_exception_message.format(
"colour"))
else:
self._colour = value
@property
def line_width(self):
"""Getter for line_width."""
return self._line_width
@line_width.setter
def line_width(self, value):
"""Setter for line_width."""
if self.has_surface:
raise AttributeError(Rectangle._getter_exception_message.format(
"line_width"))
else:
self._line_width = value
def _create_surface(self):
"""Create the surface of the stimulus."""
if self._line_width == 0:
surface = pygame.surface.Surface(self._size,
pygame.SRCALPHA).convert_alpha()
surface.fill(self._colour)
else:
# Invert colours and use it as colourkey for a temporal surface,
# fill the surface and draw a smaller rectangle with colourkey
# colour
colour = [abs(self._colour[0] - 255),
abs(self._colour[1] - 255),
abs(self._colour[2] - 255)]
surface = pygame.surface.Surface(
[x + self._line_width for x in self._size],
pygame.SRCALPHA).convert_alpha()
tmp = pygame.surface.Surface(
[x + self._line_width for x in self._size]).convert()
tmp.set_colorkey(colour)
tmp.fill(colour)
pygame.draw.rect(tmp, self._colour, pygame.Rect(
(0, 0), [x + self._line_width for x in self._size]))
pygame.draw.rect(tmp, colour, pygame.Rect(
(self._line_width, self._line_width),
[x - self._line_width for x in self._size]))
surface.blit(tmp, (0, 0))
return surface
def is_point_inside(self, point_xy):
""""DEPRECATED METHOD: Please use 'overlapping_with_position'."""
return self.overlapping_with_position(point_xy)
if __name__ == "__main__":
from expyriment import control
control.set_develop_mode(True)
defaults.event_logging = 0
exp = control.initialize()
rect = Rectangle((20, 200), colour=(255, 0, 255))
rect.present()
exp.clock.wait(1000)
|