Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RF: Fix indentation and logic on Button boilerplate #5220

Merged
merged 1 commit into from Dec 8, 2022

Conversation

TEParsons
Copy link
Contributor

Because of Button using setIndentLevel unnecessarily, it was possible to have a hanging indent. Also, as it was inheriting the full each frame code from BaseVisual, it was writing its active test code multiple times.

The boilerplate produced here is slightly different, but has the same effect (all the logic evaluates the same).

Example of boilerplate produced from this branch:

    # if button is starting this frame...
    if button.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
        # keep track of start time/frame for later
        button.frameNStart = frameN  # exact frame index
        button.tStart = t  # local t and not account for scr refresh
        button.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(button, 'tStartRefresh')  # time at next scr refresh
        # add timestamp to datafile
        thisExp.timestampOnFlip(win, 'button.started')
        # update status
        button.status = STARTED
        button.setAutoDraw(True)
    
    # if button is active this frame...
    if button.status == STARTED:
        # update params
        pass
        # check whether button has been pressed
        if button.isClicked:
            if not button.wasClicked:
                # if this is a new click, store time of first click and clicked until
                button.timesOn.append(button.buttonClock.getTime())
                button.timesOff.append(button.buttonClock.getTime())
            elif len(button.timesOff):
                # if click is continuing from last frame, update time of clicked until
                button.timesOff[-1] = button.buttonClock.getTime()
            if not button.wasClicked:
                # end routine when button is clicked
                continueRoutine = False
            if not button.wasClicked:
                # run callback code when button is clicked
                pass
    # take note of whether button was clicked, so that next frame we know if clicks are new
    button.wasClicked = button.isClicked and button.status == STARTED
    
    # if button is stopping this frame...
    if button.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > button.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            button.tStop = t  # not accounting for scr refresh
            button.frameNStop = frameN  # exact frame index
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'button.stopped')
            # update status
            button.status = FINISHED
            button.setAutoDraw(False)

vs current dev:

    # *button* updates
    
    # if button is starting this frame...
    if button.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
        # keep track of start time/frame for later
        button.frameNStart = frameN  # exact frame index
        button.tStart = t  # local t and not account for scr refresh
        button.tStartRefresh = tThisFlipGlobal  # on global time
        win.timeOnFlip(button, 'tStartRefresh')  # time at next scr refresh
        # add timestamp to datafile
        thisExp.timestampOnFlip(win, 'button.started')
        # update status
        button.status = STARTED
        button.setAutoDraw(True)
    
    # if button is active this frame...
    if button.status == STARTED:
        # update params
        pass
    
    # if button is stopping this frame...
    if button.status == STARTED:
        # is it time to stop? (based on global clock, using actual start)
        if tThisFlipGlobal > button.tStartRefresh + 10-frameTolerance:
            # keep track of stop time/frame for later
            button.tStop = t  # not accounting for scr refresh
            button.frameNStop = frameN  # exact frame index
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'button.stopped')
            # update status
            button.status = FINISHED
            button.setAutoDraw(False)
    if button.status == STARTED:
        # check whether button has been pressed
        if button.isClicked:
            if not button.wasClicked:
                button.timesOn.append(button.buttonClock.getTime()) # store time of first click
                button.timesOff.append(button.buttonClock.getTime()) # store time clicked until
            elif len(button.timesOff):
                button.timesOff[-1] = button.buttonClock.getTime() # update time clicked until
            if not button.wasClicked:
                continueRoutine = False  # end routine when button is clicked
                None
            button.wasClicked = True  # if button is still clicked next frame, it is not a new click
        else:
            button.wasClicked = False  # if button is clicked next frame, it is a new click
    else:
        button.wasClicked = False  # if button is clicked next frame, it is a new click

@codecov
Copy link

codecov bot commented Nov 21, 2022

Codecov Report

Merging #5220 (4a9b3b8) into dev (879376a) will decrease coverage by 0.03%.
The diff coverage is 80.00%.

@@            Coverage Diff             @@
##              dev    #5220      +/-   ##
==========================================
- Coverage   39.02%   38.99%   -0.04%     
==========================================
  Files         313      313              
  Lines       63821    63808      -13     
  Branches    10557    10559       +2     
==========================================
- Hits        24908    24883      -25     
- Misses      36790    36803      +13     
+ Partials     2123     2122       -1     
Impacted Files Coverage Δ
psychopy/experiment/components/button/__init__.py 87.56% <80.00%> (+0.67%) ⬆️
psychopy/visual/textbox2/textbox2.py 78.54% <0.00%> (-0.85%) ⬇️
psychopy/visual/window.py 65.73% <0.00%> (-0.51%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@peircej peircej merged commit 2772e1a into psychopy:dev Dec 8, 2022
@TEParsons TEParsons deleted the dev-button-indent branch February 10, 2023 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants