/
sketchpad.py
150 lines (111 loc) · 3.91 KB
/
sketchpad.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
#-*- coding:utf-8 -*-
"""
This file is part of OpenSesame.
OpenSesame 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.
OpenSesame is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSesame. If not, see <http://www.gnu.org/licenses/>.
"""
from libopensesame.py3compat import *
from libopensesame import sketchpad_elements
from libopensesame.exceptions import osexception
from libopensesame.item import item
from libopensesame.base_response_item import base_response_item
from libopensesame.keyboard_response import keyboard_response_mixin
from libopensesame.mouse_response import mouse_response_mixin
from openexp.canvas import canvas
class sketchpad(base_response_item, keyboard_response_mixin,
mouse_response_mixin):
"""
desc:
The runtime part of the sketchpad item.
"""
description = u'Displays stimuli'
is_oneshot_coroutine = True
def reset(self):
"""See item."""
self.var.duration = u'keypress'
self.elements = []
def element_module(self):
"""
desc:
Determines the module to be used for the element classes. The
runtime and GUI use different modules.
returns:
desc: A module containing sketchpad-element classes
type: module
"""
return sketchpad_elements
def from_string(self, string):
"""See item."""
self.var.clear()
self.comments = []
self.reset()
if string is None:
return
for line in string.split(u'\n'):
if self.parse_variable(line):
continue
cmd, arglist, kwdict = self.syntax.parse_cmd(line)
if cmd != u'draw':
continue
if len(arglist) == 0:
raise osexception(u'Incomplete draw command: \'%s\'' % line)
element_type = arglist[0]
if not hasattr(self.element_module(), element_type):
raise osexception(
u'Unknown sketchpad element: \'%s\'' % element_type)
element_class = getattr(self.element_module(), element_type)
element = element_class(self, line)
self.elements.append(element)
self.elements.sort(key=lambda element: -element.z_index)
def process_response(self, response_args):
"""See base_response_item."""
if self.var.duration == u'mouseclick':
mouse_response_mixin.process_response(self, response_args)
return
base_response_item.process_response(self, response_args)
def prepare_response_func(self):
"""See base_response_item."""
if isinstance(self.var.duration, (int, float)):
return self._prepare_sleep_func(self.var.duration)
if self.var.duration == u'keypress':
return keyboard_response_mixin.prepare_response_func(self)
if self.var.duration == u'mouseclick':
return mouse_response_mixin.prepare_response_func(self)
raise osexception(u'Invalid duration: %s' % self.var.duration)
def prepare(self):
"""See item."""
base_response_item.prepare(self)
self.canvas = canvas(self.experiment, color=self.var.foreground,
background_color=self.var.background, auto_prepare=False)
for element in self.elements:
if element.is_shown():
element.draw()
self.canvas.prepare()
def run(self):
"""See item."""
self._t0 = self.set_item_onset(self.canvas.show())
base_response_item.run(self)
def coroutine(self):
"""See coroutines plug-in."""
yield
self.set_item_onset(self.canvas.show())
def to_string(self):
"""See item."""
s = base_response_item.to_string(self)
for element in self.elements:
s += u'\t%s\n' % element.to_string()
return s
def var_info(self):
"""See item."""
if self.var.get(u'duration', _eval=False, default=u'') in \
[u'keypress', u'mouseclick']:
return base_response_item.var_info(self)
return item.var_info(self)