/
brush.py
179 lines (150 loc) · 4.97 KB
/
brush.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""A PsychoPy drawing tool
Inspired by rockNroll87q - https://github.com/rockNroll87q/pyDrawing
"""
# Part of the PsychoPy library
# Copyright (C) 2002-2018 Jonathan Peirce (C) 2019 Open Science Tools Ltd.
# Distributed under the terms of the GNU General Public License (GPL).
from __future__ import absolute_import, print_function
from psychopy import event, logging
from .shape import ShapeStim
from .basevisual import MinimalStim
__author__ = 'David Bridges'
class Brush(MinimalStim):
"""A class for creating a freehand drawing tool.
"""
def __init__(self,
win,
lineWidth=1.5,
lineColor=(1.0, 1.0, 1.0),
lineColorSpace='rgb',
opacity=1.0,
closeShape=False,
name=None,
depth=0,
autoLog=True,
autoDraw=False
):
super(Brush, self).__init__(name=name,
autoLog=False)
self.win = win
self.name = name
self.depth = depth
self.lineColor = lineColor
self.lineColorSpace = lineColorSpace
self.lineWidth = lineWidth
self.opacity = opacity
self.closeShape = closeShape
self.pointer = event.Mouse(win=self.win)
self.shapes = []
self.brushPos = []
self.strokeIndex = -1
self.atStartPoint = False
self.autoLog = autoLog
self.autoDraw = autoDraw
if self.autoLog:
logging.exp("Created {name} = {obj}".format(name=self.name,
obj=str(self)))
def _resetVertices(self):
"""
Resets list of vertices passed to ShapeStim.
"""
if self.autoLog:
logging.exp("Resetting {name} parameter: brushPos.".format(name=self.name))
self.brushPos = []
def _createStroke(self):
"""
Creates ShapeStim for each stroke.
"""
if self.autoLog:
logging.exp("Creating ShapeStim for {name}".format(name=self.name))
self.shapes.append(ShapeStim(self.win,
vertices=[[0, 0]],
closeShape=self.closeShape,
lineWidth=self.lineWidth,
lineColor=self.lineColor,
lineColorSpace=self.lineColorSpace,
opacity=self.opacity,
autoLog=False,
autoDraw=True))
@property
def currentShape(self):
"""The index of current shape to be drawn.
Returns
-------
Int
The index as length of shapes attribute - 1.
"""
return len(self.shapes) - 1
@property
def brushDown(self):
"""
Checks whether the mouse button has been clicked in order to start drawing.
Returns
-------
Bool
True if left mouse button is pressed, False otherwise.
"""
return self.pointer.getPressed()[0] == 1
def onBrushDown(self):
"""
On first brush stroke, empty pointer position list, and create a new ShapeStim.
"""
if self.brushDown and not self.atStartPoint:
self.atStartPoint = True
self._resetVertices()
self._createStroke()
def onBrushDrag(self):
"""
Check whether the brush is down. If brushDown is True, the brush path is drawn on screen.
"""
if self.brushDown:
self.brushPos.append(self.pointer.getPos())
self.shapes[self.currentShape].setVertices(self.brushPos)
else:
self.atStartPoint = False
def draw(self):
"""
Get starting stroke and begin painting on screen.
"""
self.onBrushDown()
self.onBrushDrag()
def reset(self):
"""
Clear ShapeStim objects from shapes attribute.
"""
if self.autoLog:
logging.exp("Resetting {name}".format(name=self.name))
if len(self.shapes):
for shape in self.shapes:
shape.setAutoDraw(False)
self.atStartPoint = False
self.shapes = []
def setLineColor(self, value):
"""
Sets the line color passed to ShapeStim.
Parameters
----------
value
Line color
"""
self.lineColor = value
def setLineWidth(self, value):
"""
Sets the line width passed to ShapeStim.
Parameters
----------
value
Line width in pixels
"""
self.lineWidth = value
def setOpacity(self, value):
"""
Sets the line opacity passed to ShapeStim.
Parameters
----------
value
Opacity range(0, 1)
"""
self.opacity = value