示例#1
0
    def __init__(self,
                 win,
                 border_thickness=.003,
                 button_text_sz=0.03,
                 buttonPos=(-.5, 0),
                 buttonText="text for button",
                 survey="",
                 thisExp="",
                 type="surveySubmit",
                 **kwargs):

        #local variables
        button_width = len(buttonText) * .025
        button_x_inner_margin = .02
        button_x_outer_margin = button_x_inner_margin + border_thickness
        button_y_inner_margin = button_text_sz
        button_y_outer_margin = button_text_sz + border_thickness
        button_x_range = (0 - button_width / 2 + buttonPos[0],
                          0 + button_width / 2 + buttonPos[0])

        self.thisExp = thisExp
        self._dragging = False
        self.survey = survey
        self.type = type  # this needs to be dynamic
        self.mouse = event.Mouse()
        self.button_selected = False
        self.buttonItems = []
        self.buttonColor = "blue"
        self.button_border = BaseShapeStim(
            win,
            fillColor="blue",
            vertices=((button_x_range[0] - button_x_outer_margin,
                       -button_y_outer_margin + buttonPos[1]),
                      (button_x_range[0] - button_x_outer_margin,
                       button_y_outer_margin + buttonPos[1]),
                      (button_x_range[1] + button_x_outer_margin,
                       button_y_outer_margin + buttonPos[1]),
                      (button_x_range[1] + button_x_outer_margin,
                       -button_y_outer_margin + buttonPos[1])))  #edges=4
        self.button_inner = BaseShapeStim(
            win,
            fillColor="white",
            vertices=((button_x_range[0] - button_x_inner_margin,
                       -button_y_inner_margin + buttonPos[1]),
                      (button_x_range[0] - button_x_inner_margin,
                       button_y_inner_margin + buttonPos[1]),
                      (button_x_range[1] + button_x_inner_margin,
                       button_y_inner_margin + buttonPos[1]),
                      (button_x_range[1] + button_x_inner_margin,
                       -button_y_inner_margin + buttonPos[1])))  #edges=4
        self.button_inner_text = visual.TextStim(win,
                                                 text=buttonText,
                                                 color="blue",
                                                 pos=buttonPos,
                                                 height=button_text_sz)

        self.buttonItems.append(self.button_border)
        self.buttonItems.append(self.button_inner)
        self.buttonItems.append(self.button_inner_text)
示例#2
0
    def __init__(self,
                 win,
                 borderThickness=.003,
                 labelSize=0.03,
                 pos=(0, 0),
                 labelText="text for button",
                 textColor='blue',
                 borderColor='blue',
                 buttonColor='white',
                 buttonEnabled=False,
                 ):

        # local variables
        super(ButtonStim, self).__init__(win)
        button_width = len(labelText) * .025
        button_x_inner_margin = .02
        button_x_outer_margin = button_x_inner_margin + borderThickness
        button_y_inner_margin = labelSize
        button_y_outer_margin = labelSize + borderThickness
        button_x_range = (0 - button_width / 2 + pos[0], 0 + button_width / 2 + pos[0])

        self.win = win
        self.borderThickness = borderThickness
        self.labelSize = labelSize
        self.pos = pos
        self.labelText = labelText
        self.textColor = textColor
        self.borderColor = borderColor
        self.buttonColor = buttonColor
        self.buttonEnabled = buttonEnabled

        self._dragging = False
        self.mouse = event.Mouse()
        self.buttonSelected = False
        self.buttonItems = []

        self.buttonBorder = BaseShapeStim(self.win, fillColor=self.borderColor, vertices=(
            (button_x_range[0] - button_x_outer_margin, -button_y_outer_margin + self.pos[1]),
            (button_x_range[0] - button_x_outer_margin, button_y_outer_margin + self.pos[1]),
            (button_x_range[1] + button_x_outer_margin, button_y_outer_margin + self.pos[1]),
            (button_x_range[1] + button_x_outer_margin, -button_y_outer_margin + self.pos[1])))
        self.buttonInner = BaseShapeStim(self.win, fillColor=self.buttonColor, vertices=(
            (button_x_range[0] - button_x_inner_margin, -button_y_inner_margin + self.pos[1]),
            (button_x_range[0] - button_x_inner_margin, button_y_inner_margin + self.pos[1]),
            (button_x_range[1] + button_x_inner_margin, button_y_inner_margin + self.pos[1]),
            (button_x_range[1] + button_x_inner_margin, -button_y_inner_margin + self.pos[1])))
        self.buttonInnerText = TextStim(self.win, text=self.labelText, color=self.textColor, pos=self.pos,
                                               height=self.labelSize)
        self.buttonItems.append(self.buttonBorder)
        self.buttonItems.append(self.buttonInner)
        self.buttonItems.append(self.buttonInnerText)
示例#3
0
    def __init__(self,
                 win,
                 size=1,
                 pos=(0, 0),
                 ori=0,
                 nVert=120,
                 shape='circle',
                 inverted=False,
                 units=None,
                 name=None,
                 autoLog=None):
        # what local vars are defined (these are the init params) for use by
        # __repr__
        self._initParams = dir()
        self._initParams.remove('self')
        super(Aperture, self).__init__(name=name, autoLog=False)

        # set self params
        self.autoLog = False  # change after attribs are set
        self.win = win
        if not win.allowStencil:
            logging.error('Aperture has no effect in a window created '
                          'without allowStencil=True')
            core.quit()
        self.__dict__['size'] = size
        self.__dict__['pos'] = pos
        self.__dict__['ori'] = ori
        self.__dict__['inverted'] = inverted
        self.__dict__['filename'] = False

        # unit conversions
        if units != None and len(units):
            self.units = units
        else:
            self.units = win.units

        # set vertices using shape, or default to a circle with nVerts edges
        if hasattr(shape, 'lower') and not os.path.isfile(shape):
            shape = shape.lower()
        if shape is None or shape == 'circle':
            # NB: pentagon etc point upwards by setting x,y to be y,x
            # (sin,cos):
            vertices = [(0.5 * sin(radians(theta)), 0.5 * cos(radians(theta)))
                        for theta in numpy.linspace(0, 360, nVert, False)]
        elif shape == 'square':
            vertices = [[0.5, -0.5], [-0.5, -0.5], [-0.5, 0.5], [0.5, 0.5]]
        elif shape == 'triangle':
            vertices = [[0.5, -0.5], [0, 0.5], [-0.5, -0.5]]
        elif type(shape) in [tuple, list, numpy.ndarray] and len(shape) > 2:
            vertices = shape
        elif isinstance(shape, basestring):
            # is a string - see if it points to a file
            if os.path.isfile(shape):
                self.__dict__['filename'] = shape
            else:
                msg = ("Unrecognized shape for aperture. Expected 'circle',"
                       " 'square', 'triangle', vertices, filename, or None;"
                       " got %s")
                logging.error(msg % repr(shape))

        if self.__dict__['filename']:
            self._shape = ImageStim(win=self.win,
                                    image=self.__dict__['filename'],
                                    pos=pos,
                                    size=size,
                                    autoLog=False,
                                    units=self.units)
        else:
            self._shape = BaseShapeStim(win=self.win,
                                        vertices=vertices,
                                        fillColor=1,
                                        lineColor=None,
                                        colorSpace='rgb',
                                        interpolate=False,
                                        pos=pos,
                                        size=size,
                                        autoLog=False,
                                        units=self.units)
            self.vertices = self._shape.vertices
            self._needVertexUpdate = True

        self._needReset = True  # Default when setting attributes
        # implicitly runs a self.enabled = True. Also sets
        # self._needReset = True on every call
        self._reset()

        # set autoLog now that params have been initialised
        wantLog = autoLog is None and self.win.autoLog
        self.__dict__['autoLog'] = autoLog or wantLog
        if self.autoLog:
            logging.exp("Created {} = {}".format(self.name, self))