Skip to content

Commit

Permalink
BF: fixes to differences between PyOpenGL.GL and pyglet.gl
Browse files Browse the repository at this point in the history
Also updated the demo to be more clear about some of TextBox2 features
  • Loading branch information
peircej committed Nov 30, 2020
1 parent 9589e48 commit 0fcb89c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
69 changes: 35 additions & 34 deletions psychopy/demos/coder/stimuli/textbox_editable.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import psychopy
from psychopy import visual, core, event, logging
import numpy as np

logging.console.setLevel(logging.DEBUG)
logging.console.setLevel(logging.EXP)
c = core.Clock()

from psychopy.visual.textbox2 import TextBox2, allFonts

win = visual.Window([800, 800], monitor='testMonitor', backend='glfw')
win = visual.Window([800, 800], monitor='testMonitor')
logging.exp("{:.3f}: created window".format(c.getTime()))

text = u"<i>The quick</i> brown <b>fox</b> jumped"
loremIpsum = u"PsychoPy is an open-source Python application allowing you to run a supercali-fragilisticexpeilidocious wide range of neuroscience, psychology and psychophysics experiments. It’s a free, powerful alternative to Presentation™ or e-Prime™, written in Python (a free alternative to Matlab™ g)."
psychopyInfo = u"<b>PsychoPy</b> is an <i>open-source</i> Python application allowing you to run a supercali-fragilisticexpeilidocious wide range of neuroscience, psychology and psychophysics experiments. It’s a free, powerful alternative to Presentation™ or e-Prime™, written in Python (a free alternative to Matlab™ g)."

fontSize = 16
# preload some chars into a font to see how long it takes
nChars = 256
fontSize = 16
arial = allFonts.getFont("Arial", fontSize)
logging.exp("{:.3f}: created font".format(c.getTime()))
arial.preload(nChars)
nChars = 256
arial.preload(nChars) # or set to preload specific string of chars
logging.exp("{:.3f}: preloaded {} chars".format(c.getTime(), nChars))
# arial.saveToCache() # can't yet retrieve the font but it's interesting to see!


txt1 = TextBox2(win, color='black', colorSpace='named', text='Toptastic', font='Times',
pos=(0, 0.0), letterHeight=0.1, units='height',
size=[1, 1],
anchor='right-bottom',
borderColor='red',
txt1 = TextBox2(win, text="Type here, it's toptastic", font='Times',
color='black', colorSpace='named',
pos=(0, 0.4), letterHeight=0.05, units='height',
size=[0.8, 0.2],
anchor='center-top',
borderColor='lightgrey',
fillColor='slategrey',
editable=True)
txt1.draw()

x, y = 0, -5

txt2 = TextBox2(win, color='blue', text=loremIpsum, font='Arial',
pos=(x, y), anchor='bottom', size=(20, None), units='cm',
txt2 = TextBox2(win, text=psychopyInfo, font='Arial',
pos=(0, -5), anchor='middle', size=(20, None), units='cm',
lineSpacing=1.1,
letterHeight=1.,
borderColor='white',
fillColor=None,
color='LightGrey', borderColor='Moccasin', fillColor=None,
editable=True)
txt2.draw()

logging.exp("{:.3f}: drew altered Arial text".format(c.getTime()))

win.flip()
logging.exp("{:.3f}: drew TextBox Times (no preload)".format(c.getTime()))

stims = [txt1, txt2]
win.flip()
for frame in range(1000):
txt2.pos += 0.01
for stim in stims:
stim.draw()
txt3 = TextBox2(win, text='Good for non-editable text (Esc to quit)',
font='Arial',
borderColor=None, fillColor=None,
pos=(-0.5,-0.5), units='height', anchor='bottom-left',
letterHeight=0.02,
editable=False)

clock = core.Clock()
t=0
while t<20:
t= clock.getTime()

txt1.draw()

txt2.pos = (0.2*np.sin(t), 0.2*np.cos(t))
txt2.draw()

txt3.draw()
if 'escape' in event.getKeys():
core.quit()

Expand Down
16 changes: 8 additions & 8 deletions psychopy/visual/textbox2/textbox2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""
import numpy as np
from pyglet import gl # import OpenGL.GL not compatible with Big Sur (2020)
from pyglet import gl

from ..basevisual import BaseVisualStim, ColorMixin, ContainerMixin
from psychopy.tools.attributetools import attributeSetter, setAttribute
Expand Down Expand Up @@ -311,8 +311,8 @@ def getLineWidthFromPix(pixVal):
# then they are converted back during rendering using standard BaseStim
vertices = np.zeros((len(text) * 4, 2), dtype=np.float32)
self._charIndices = np.zeros((len(text)), dtype=int)
self._colors = np.zeros((len(text) * 4, 4), dtype=np.float32)
self._texcoords = np.zeros((len(text) * 4, 2), dtype=np.float32)
self._colors = np.zeros((len(text) * 4, 4), dtype=np.double)
self._texcoords = np.zeros((len(text) * 4, 2), dtype=np.double)
self._glIndices = np.zeros((len(text) * 4), dtype=int)

# the following are used internally for layout
Expand Down Expand Up @@ -510,16 +510,16 @@ def draw(self):
gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)

gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.verticesPix.ctypes)
gl.glColorPointer(4, gl.GL_FLOAT, 0, self._colors.ctypes)
gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, self._texcoords.ctypes)
gl.glVertexPointer(2, gl.GL_DOUBLE, 0, self.verticesPix.ctypes)
gl.glColorPointer(4, gl.GL_DOUBLE, 0, self._colors.ctypes)
gl.glTexCoordPointer(2, gl.GL_DOUBLE, 0, self._texcoords.ctypes)

self.shader.bind()
self.shader.setInt('texture', 0)
self.shader.setFloat('pixel', [1.0 / 512, 1.0 / 512])
nVerts = len(self.text)*4
gl.glDrawElements(gl.GL_QUADS, nVerts,
gl.GL_UNSIGNED_INT, np.arange(nVerts, dtype=int).ctypes)

gl.glDrawArrays(gl.GL_QUADS, 0, nVerts)
self.shader.unbind()

# removed the colors and font texture
Expand Down

0 comments on commit 0fcb89c

Please sign in to comment.