Skip to content

Commit 0fcb89c

Browse files
committed
BF: fixes to differences between PyOpenGL.GL and pyglet.gl
Also updated the demo to be more clear about some of TextBox2 features
1 parent 9589e48 commit 0fcb89c

2 files changed

Lines changed: 43 additions & 42 deletions

File tree

psychopy/demos/coder/stimuli/textbox_editable.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,62 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
4+
import psychopy
55
from psychopy import visual, core, event, logging
6+
import numpy as np
67

7-
logging.console.setLevel(logging.DEBUG)
8+
logging.console.setLevel(logging.EXP)
89
c = core.Clock()
910

1011
from psychopy.visual.textbox2 import TextBox2, allFonts
1112

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

15-
text = u"<i>The quick</i> brown <b>fox</b> jumped"
16-
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)."
16+
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)."
1717

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

27-
28-
txt1 = TextBox2(win, color='black', colorSpace='named', text='Toptastic', font='Times',
29-
pos=(0, 0.0), letterHeight=0.1, units='height',
30-
size=[1, 1],
31-
anchor='right-bottom',
32-
borderColor='red',
26+
txt1 = TextBox2(win, text="Type here, it's toptastic", font='Times',
27+
color='black', colorSpace='named',
28+
pos=(0, 0.4), letterHeight=0.05, units='height',
29+
size=[0.8, 0.2],
30+
anchor='center-top',
31+
borderColor='lightgrey',
3332
fillColor='slategrey',
3433
editable=True)
35-
txt1.draw()
36-
37-
x, y = 0, -5
3834

39-
txt2 = TextBox2(win, color='blue', text=loremIpsum, font='Arial',
40-
pos=(x, y), anchor='bottom', size=(20, None), units='cm',
35+
txt2 = TextBox2(win, text=psychopyInfo, font='Arial',
36+
pos=(0, -5), anchor='middle', size=(20, None), units='cm',
4137
lineSpacing=1.1,
4238
letterHeight=1.,
43-
borderColor='white',
44-
fillColor=None,
39+
color='LightGrey', borderColor='Moccasin', fillColor=None,
4540
editable=True)
46-
txt2.draw()
47-
48-
logging.exp("{:.3f}: drew altered Arial text".format(c.getTime()))
49-
50-
win.flip()
51-
logging.exp("{:.3f}: drew TextBox Times (no preload)".format(c.getTime()))
5241

53-
stims = [txt1, txt2]
54-
win.flip()
55-
for frame in range(1000):
56-
txt2.pos += 0.01
57-
for stim in stims:
58-
stim.draw()
42+
txt3 = TextBox2(win, text='Good for non-editable text (Esc to quit)',
43+
font='Arial',
44+
borderColor=None, fillColor=None,
45+
pos=(-0.5,-0.5), units='height', anchor='bottom-left',
46+
letterHeight=0.02,
47+
editable=False)
48+
49+
clock = core.Clock()
50+
t=0
51+
while t<20:
52+
t= clock.getTime()
53+
54+
txt1.draw()
55+
56+
txt2.pos = (0.2*np.sin(t), 0.2*np.cos(t))
57+
txt2.draw()
58+
59+
txt3.draw()
5960
if 'escape' in event.getKeys():
6061
core.quit()
6162

psychopy/visual/textbox2/textbox2.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
"""
1919
import numpy as np
20-
from pyglet import gl # import OpenGL.GL not compatible with Big Sur (2020)
20+
from pyglet import gl
2121

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

318318
# the following are used internally for layout
@@ -510,16 +510,16 @@ def draw(self):
510510
gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
511511
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
512512

513-
gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.verticesPix.ctypes)
514-
gl.glColorPointer(4, gl.GL_FLOAT, 0, self._colors.ctypes)
515-
gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, self._texcoords.ctypes)
513+
gl.glVertexPointer(2, gl.GL_DOUBLE, 0, self.verticesPix.ctypes)
514+
gl.glColorPointer(4, gl.GL_DOUBLE, 0, self._colors.ctypes)
515+
gl.glTexCoordPointer(2, gl.GL_DOUBLE, 0, self._texcoords.ctypes)
516516

517517
self.shader.bind()
518518
self.shader.setInt('texture', 0)
519519
self.shader.setFloat('pixel', [1.0 / 512, 1.0 / 512])
520520
nVerts = len(self.text)*4
521-
gl.glDrawElements(gl.GL_QUADS, nVerts,
522-
gl.GL_UNSIGNED_INT, np.arange(nVerts, dtype=int).ctypes)
521+
522+
gl.glDrawArrays(gl.GL_QUADS, 0, nVerts)
523523
self.shader.unbind()
524524

525525
# removed the colors and font texture

0 commit comments

Comments
 (0)