~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychDemos/MouseTraceDemo4.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
% MouseTraceDemo4
 
2
%
 
3
% ___________________________________________________________________
 
4
 
5
% Draw a curve with the mouse. Same as MouseTraceDemo, but asks
 
6
% Screen('Flip') to not clear the framebuffer after flip. This way,
 
7
% we don't need to redraw the whole mouse trace in each frame.
 
8
% Uses imaging pipeline via PsychImaging for optimal performance
 
9
% on modern hardware, as opposed to old style method shown in
 
10
% MouseTraceDemo2, which was appropriate before the year 2007.
 
11
% ___________________________________________________________________
 
12
%
 
13
% See also: PsychDemos, MouseTraceDemo, GetMouse.
 
14
%
 
15
% HISTORY
 
16
%                       
 
17
% 4/1/2013  mk       Derived from MouseTraceDemo2.
 
18
 
 
19
try
 
20
    % Open up a window on the screen and clear it.
 
21
    whichScreen = max(Screen('Screens'));
 
22
    PsychImaging('PrepareConfiguration');
 
23
    PsychImaging('AddTask', 'General', 'UseVirtualFramebuffer');
 
24
    [theWindow,theRect] = PsychImaging('OpenWindow', whichScreen, 0);
 
25
 
 
26
    % Move the cursor to the center of the screen
 
27
    theX = theRect(RectRight)/2;
 
28
    theY = theRect(RectBottom)/2;
 
29
    SetMouse(theX,theY);
 
30
 
 
31
    % Wait for a click and hide the cursor
 
32
    Screen(theWindow,'TextSize',24);
 
33
    Screen(theWindow,'DrawText','Drag mouse (i.e. hold button down) to draw',50,50,255);
 
34
    Screen('Flip', theWindow);
 
35
    while (1)
 
36
        [x,y,buttons] = GetMouse(theWindow);
 
37
        if buttons(1)
 
38
          break;
 
39
        end
 
40
    end
 
41
    Screen(theWindow,'DrawText','Release button to finish',50,50,255);
 
42
 
 
43
    HideCursor;
 
44
 
 
45
    % Loop and track the mouse, drawing the contour
 
46
    [theX,theY] = GetMouse(theWindow);
 
47
    thePoints = [theX theY];
 
48
    Screen(theWindow,'DrawLine',255,theX,theY,theX,theY);
 
49
    % Set the 'dontclear' flag of Flip to 1 to prevent erasing the
 
50
    % frame-buffer:
 
51
    Screen('Flip', theWindow, 0, 1);
 
52
    while (1)
 
53
        [x,y,buttons] = GetMouse(theWindow);    
 
54
        if ~buttons(1)
 
55
            break;
 
56
        end
 
57
        if (x ~= theX || y ~= theY)
 
58
            thePoints = [thePoints ; x y]; %#ok<AGROW>
 
59
            [numPoints, two]=size(thePoints);
 
60
            % Only draw the most recent line segment: This is possible,
 
61
            % because...
 
62
            Screen(theWindow,'DrawLine',128,thePoints(numPoints-1,1),thePoints(numPoints-1,2),thePoints(numPoints,1),thePoints(numPoints,2));
 
63
            % ...we ask Flip to not clear the framebuffer after flipping:
 
64
            Screen('Flip', theWindow, 0, 1);
 
65
            theX = x; theY = y;
 
66
        end
 
67
    end
 
68
 
 
69
    % Close up
 
70
    Screen(theWindow,'DrawText','Click mouse to finish',50,50,255);
 
71
    ShowCursor;
 
72
    Screen(theWindow,'Close');
 
73
 
 
74
    % Plot the contour in a Matlab figure
 
75
    plot(thePoints(:,1),theRect(RectBottom)-thePoints(:,2));
 
76
catch
 
77
    Screen('CloseAll')
 
78
    ShowCursor;
 
79
    psychrethrow(psychlasterror);
 
80
end %try..catch..