close all;
clear;
PsychDefaultSetup(2);
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
grey = GrayIndex(screenNumber);
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black,...
[], [], [], [], [], kPsychNeedRetinaResolution);
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
Screen('TextSize', window, 90);
HideCursor;
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);
ifi = Screen('GetFlipInterval', window);
[xCenter, yCenter] = RectCenter(windowRect);
sliderLengthPix = screenYpixels / 1.5;
sliderHLengthPix = sliderLengthPix / 2;
leftEnd = [xCenter - sliderHLengthPix yCenter];
rightEnd = [xCenter + sliderHLengthPix yCenter];
sliderLineCoords = [leftEnd' rightEnd'];
sliderLineWidth = 10;
red = [white 0 0];
green = [0 white 0];
blue = [0 0 white];
yellowTrans = [white white 0 0.5];
SetMouse(xCenter, yCenter, window);
dim = screenYpixels / 18;
hDim = dim / 2;
baseRect = [0 0 dim dim];
sx = xCenter + (rand * 2 - 1) * sliderHLengthPix;
centeredRect = CenterRectOnPointd(baseRect, sx, yCenter);
sliderLabels = {'Not Cool', 'Cool'};
textBoundsAll = nan(2, 4);
for i = 1:2
[~, ~, textBoundsAll(i, :)] = DrawFormattedText(window, sliderLabels{i}, 0, 0, white);
end
textWidths = textBoundsAll(:, 3)';
halfTextWidths = textWidths / 2;
textHeights = range([textBoundsAll(:, 2) textBoundsAll(:, 4)], 2)';
halfTextHeights = textHeights / 2;
textPixGap = 10;
leftTextPosX = xCenter - sliderHLengthPix - hDim - textWidths(1) - textPixGap;
rightTextPosX = xCenter + sliderHLengthPix + hDim + textPixGap;
leftTextPosY = yCenter + halfTextHeights(1);
rightTextPosY = yCenter + halfTextHeights(2);
offsetSet = 0;
Screen('FillRect', window, black)
vbl = Screen('Flip', window);
waitframes = 1;
while ~KbCheck(-1)
[mx, my, buttons] = GetMouse(window);
[cx, cy] = RectCenter(centeredRect);
inside = IsInRect(mx, my, centeredRect);
if inside == 1 && sum(buttons) > 0 && offsetSet == 0
dx = mx - cx;
offsetSet = 1;
end
if offsetSet
sx = mx - dx;
end
if sx > xCenter + sliderHLengthPix
sx = xCenter + sliderHLengthPix;
elseif sx < xCenter - sliderHLengthPix
sx = xCenter - sliderHLengthPix;
end
centeredRect = CenterRectOnPointd(baseRect, sx, yCenter);
Screen('DrawLines', window, sliderLineCoords, sliderLineWidth, grey);
Screen('FillRect', window, yellowTrans, centeredRect);
Screen('FrameRect', window, green, centeredRect);
DrawFormattedText(window, sliderLabels{1}, leftTextPosX, leftTextPosY, blue);
DrawFormattedText(window, sliderLabels{2}, rightTextPosX, rightTextPosY, red);
DrawFormattedText(window, 'Coolness Slider', 'center', screenYpixels * 0.25, white);
currentCoolness = (sx - (xCenter - sliderHLengthPix)) / sliderLengthPix;
DrawFormattedText(window, ['Coolness Level: ' num2str(round(currentCoolness * 100)) '%'],...
'center', screenYpixels * 0.75, [currentCoolness 0 1 - currentCoolness]);
Screen('DrawDots', window, [mx my], 10, white, [], 2);
vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
if sum(buttons) <= 0
offsetSet = 0;
end
end
sca;