# This demo was written by Max Sargent and Peter Scarfe

# Import the various libraries that we will be using
import pyglet
from psychopy import visual, event, core
import numpy as np

# Define our colours
black = 0
white = 1
red = [white, black, black]

# Get a list of the screens avaliable
display = pyglet.canvas.get_display()
screens = display.get_screens()

# Get the length of the list of screens
numScreens = len(screens)

# We want to present on the secondary monitor (assuming two monitors connected). Note screens are counted from 0.
screenNum = numScreens - 1;

# Get the size of the screen in pixels
myScreen = screens[screenNum]
widthPix = myScreen.width
heightPix = myScreen.height

# Create a full screen window on our secondary monitor. We use pyglet as our screen backend. Note the rbg1 colour profile.
# We color our screen black and will use pixel units.
mywin = visual.Window(fullscr = True, units = 'pix', color = [black, black, black], winType = 'pyglet', colorSpace = 'rgb1', screen = screenNum)

# Make a grid of dot coordinates, the dimensions will be dim * 2 + 1
dim = 10
posNegLine = np.linspace(-dim, dim, 2 * dim + 1)

# Create a meshgrid for dot positions
x, y = np.meshgrid(posNegLine, posNegLine)

# Scale the coordinates to the screen and make into vectors
pixelScale = heightPix / (dim * 2 + 2);
x = x.flatten() * pixelScale;
y = y.flatten() * pixelScale;

# Get the total number of dots
numDots = len(x)

# Combine the x and y coordinates for drawing
dotCoords = np.column_stack((x, y))

# A set of random RGB colors (between 0 and 1)
dotColors = np.random.rand(numDots, 3)

# Set the size of the dots randomly between 10 and 30 pixels
dotSizes = np.random.rand(numDots) * 20 + 10

# We create an ElementArrayStim which combined all of the attributes for all of our dots
allDots = visual.ElementArrayStim(
    win = mywin,
    units = 'pix',
    nElements = numDots,
    elementTex = None,
    elementMask = 'circle',
    xys = dotCoords,
    sizes = dotSizes,
    colors = dotColors,
    colorSpace = 'rgb1'
)

# Draw the dots
allDots.draw()

# Flip to the screen
mywin.flip()

# Wait for key press before continuing. When pressed close the screen and exit PsychoPy.
event.waitKeys()
mywin.close()
core.quit()