File: display.py

package info (click to toggle)
pyepl 1.1.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,120 kB
  • sloc: cpp: 7,986; python: 6,026; makefile: 360; ansic: 132
file content (2139 lines) | stat: -rw-r--r-- 71,942 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
# PyEPL: display.py
#
# Copyright (C) 2003-2005 Michael J. Kahana
# Authors: Ian Schleifer, Per Sederberg, Aaron Geller, Josh Jacobs
# URL: http://memory.psych.upenn.edu/programming/pyepl
#
# Distributed under the terms of the GNU Lesser General Public License
# (LGPL). See the license.txt that came with this file.

"""
This module provides graphics abstractions.
"""

import textlog
from base import Carvable, Track, Registered, MediaFile, UniquelyConstructed
import hardware
import timing
import threading
#import PIL.Image  # catch error here!
import os
import weakref
import exputils
from stimulus import Stimulus

import pygame

# Get the PyEPL directory (used below to find the default font)
pyepldir = os.path.abspath(os.path.dirname(__file__))

# Use the Color class from the hardware.graphics.image module
Color = hardware.graphics.image.Color

# Define positional relations:
NORTH = 0
NORTHEAST = 1
EAST = 2
SOUTHEAST = 3
SOUTH = 4
SOUTHWEST = 5
WEST = 6
NORTHWEST = 7
CENTER = 8

BELOW = 9
ABOVE = 10
LEFT = 11
RIGHT = 12
OVER = 13


class Showable:
    """
    An object which may be displayed in the basic video output layer.
    """
    def __init__(self):
        self.loadRequireCount = 0
    def requireLoaded(self):
        self.loadRequireCount += 1
        if not self.isLoaded():
            self.load()
    def unrequireLoaded(self):
        self.loadRequireCount -= 1
        if self.loadRequireCount < 0:
            self.loadRequireCount = 0
        if self.loadRequireCount == 0 and \
                self.isLoaded() and \
                (hasattr(self,'filename') and not self.filename is None):
            self.unload()
    def show(self, x, y):  # to be overridden
        """
        Draw this object.
        """
        pass
    def getSize(self):  # to be overridden
        """
        Return a 2-tuple representing the X and Y dimensions of this
        showable in pixels.
        """
        return (0, 0)
    def logLine(self):  # to be overridden
        """
        Log the object.
        """
        return "SHOWABLE"
    def isLoaded(self):
        """
        Returns true if the showable is loaded in memory, otherwise returns false.
        """
        return True
    def load(self):
        """
        Loads the object into memory.
        """
        pass
    def unload(self):
        """
        Unloads the object from memory.
        """
        pass

class Shown:
    """
    Instances of this class are meant to act as unique identifiers for
    an instance of a showable on the screen.  They also provide
    information for the size and placement of the showable, as well as
    a dictionary of anchor points (anchor[NORTH], anchor[SOUTHWEST],
    ...) for use in placing other showables.
    """
    def __init__(self, x, y, width, height):
        """
        Create a shown object.
        """
        # set up the values
        self.x = x
        self.y = y
        self.width = width
        self.height = height

        # calculate the anchors
        self.anchor = {}
        self.anchor[CENTER] = (x + (width / 2), y + (height / 2))
        self.anchor[NORTH] = (x + (width / 2), y)
        self.anchor[NORTHEAST] = (x + width, y)
        self.anchor[EAST] = (x + width, y + (height / 2))
        self.anchor[SOUTHEAST] = (x + width, y + height)
        self.anchor[SOUTH] = (x + (width / 2), y + height)
        self.anchor[SOUTHWEST] = (x, y + height)
        self.anchor[WEST] = (x, y + (height / 2))
        self.anchor[NORTHWEST] = (x, y)

class ActiveShowable(Showable):
    """
    Showable that must be continually refreshed.
    """
    def logLine(self):  # to be overridden
        """
        Log the object.
        """
        return "ACTIVESHOWABLE"

class Image(UniquelyConstructed, Carvable, Showable, Stimulus):
    """
    Represents an image.
    """
#    def __uinit__(self, x, propxsize = 0.5, propysize = 0.5):
    def __uinit__(self, x, propxsize = None, propysize = None):
        """
        Creates a new Image object.

        INPUT ARGS:
          x- Data used to construct Image. Can be the filename of an image file, or a LowImage object.
          propxsize- (optional) size of the X dimension of the Image.  specified as a fraction of the Y dimension of the screen.
          propysize- (optional) size of the y dimension of the Image.  specified as a fraction of the Y dimension of the screen.          
        ...
        """
        Carvable.__init__(self)
        Showable.__init__(self)
        
        # save the proportional dimensions...
        self.propxsize = propxsize
        self.propysize = propysize
        
        if isinstance(x, hardware.graphics.LowImage):
            # if the parameter is a LowImage object, just wrap it
            self.img = x
            self.img_unscaled = x
            self.filename = None
        else:
            # otherwise, assume it's a filename
            self.filename = x
            self.img = None
            self.img_unscaled = None

        # set empty logline
        self.logLineStr = 'IMAGE'

    def getLow(self):
        """
        Returns the LowImage object associated with this Image.

        OUTPUT ARGS:
          r- a reference to the new LowImage object.
        """
        if self.img:
            # if it's loaded, just return it
            return self.img
        
        # otherwise, load it, unload it (keeping a reference), and return it
        self.load()
        r = self.img
        self.unload()
        return r
    def getLowUnscaled(self):
        """
        """
        if self.img:
            return self.img_unscaled
        self.load()
        r = self.img_unscaled
        self.unload()
        return r
    def load(self):
        """
        After calling this method, the image data is gauranteed to be
        loaded into primary memory.

        If keepLoadedAfterSmartShow is set to True, the image will not
        be unloaded after it is shown with SmartShow.
        """
        if not self.img:
            # if it's not already loaded, load it by constructing a LowImage object...

            # if there are no proportional dimensions...
            if self.propxsize is None or self.propysize is None:
                # construct the LowImage without scaling
                self.img = hardware.graphics.LowImage(self.filename)

                # set the unscaled image to be the normal one
                self.img_unscaled = self.img

                # and we're done
                return

            # try to get the VideoTrack
            v = VideoTrack.lastInstance()

            # if there is a VideoTrack...
            if v:
                # get the screen's vertical resolution
                yres = v.getResolution()[1]

                # calculate the pixel dimensions for the image based on the vertical resolution and the proportional dimensions
                xs = int(yres * self.propxsize)
                ys = int(yres * self.propysize)
                
                # construct the LowImage
                self.img_unscaled = hardware.graphics.LowImage(self.filename)

                # scale it
                self.img = self.img_unscaled.scale(xs, ys)
            else:
                # no VideoTrack...

                # construct the LowImage without scaling
                self.img = hardware.graphics.LowImage(self.filename)

                # set the unscaled image to be the normal one
                self.img_unscaled = self.img

                # set the proportional dimensions to None...
                self.propxsize = None
                self.propysize = None
    def unload(self):
        """
        After calling this method, the Image's data is unloaded from memory.
        """
        # unload by removing our internal reference to the LowImage object
        self.img = None
        self.img_unscaled = None
    def isLoaded(self):
        """
        Checks if this object is actually loaded into memory.

        OUTPUT ARGS:
          b- True if the object is loaded in memory, False if not.
        """
        # check whether or not it's loaded by seeing if we have an internal reference to a LowImage object
        return not self.img is None
    def __getstate__(self):
        """
        Don't give the image data to pickle.
        """
        # remove the LowImage object before pickle looks at the data (because LowImages are not picklable)
        d = self.__dict__.copy()
        del d["img"]
        try:
            del d["img_unscaled"]
        except KeyError:
            pass
        return d
    def __setstate__(self, state):
        """
        After unpickling, the image is NOT loaded.
        """
        # unpickle with None as self.image (since we left it out when we pickled)
        self.__dict__.update(state)
        self.img = None
    def export(self, archive, namehint):
        """
        Export image.
        """
        pass #...
    def setLogLine(self):
        """
        Set the ID for the video log.

        XXX IMPORTANT XXX
        We will eventually have to call this every time the image is changed.
        XXX IMPORTANT XXX
        """
        # get the image's pixel size
        xs, ys = self.getSize()

        # return the log line
        self.logLineStr = "IMAGE\t%s\t%d\t%d" % (self.filename, xs, ys)
    def logLine(self):
        """
        Identify this image for the video log.
        """
        return self.logLineStr
    def __getitem__(self, index):
        """
        Two dimensional indexing and slicing.
        """
        # make sure it's loaded first
        self.load()
            
        # index or slice the LowImage
        r = self.img[index]

        # if we get a LowImage back, wrap it with a new Image object and return that
        if isinstance(r, hardware.graphics.LowImage):
            return Image(r)

        # otherwise, return the color value we got from it
        return r

    def __setitem__(self, index, value):
        """
        Index and slice assignment.
        """
        # make sure the image is not "carved in stone"
        self.aboutToChange()

        # make sure it's loaded already
        self.load()
        
        # Set the value or slice in the LowImage
        self.img[index] = value
    def __mul__(self, x):
        """
        Color multiplication.
        """
        # make sure the image is loaded
        self.load()
        
        # multiply the LowImages and return the result wrapped in an Image object
        return Image(self.img * x)
    def __div__(self, x):
        """
        Color division.
        """
        # make sure the image is loaded
        self.load()

        # divide the LowImages and return the result wrapped in an Image object
        return Image(self.img / x)
    def __add__(self, x):
        """
        Color addition.
        """
        # make sure the image is loaded
        self.load()
        
        # add the LowImages and return the result wrapped in an Image object
        return Image(self.img + x)
    def __sub__(self, x):
        """
        Color subtraction.
        """
        # make sure the image is loaded
        self.load()

        # subtract the LowImages and return the result wrapped in an Image object
        return Image(self.img - x)
    def __neg__(self):
        """
        Color inversion.
        """
        # make sure the image is loaded
        self.load()
        
        # negate the LowImages and return the result wrapped in an Image object
        return Image(-self.img)
    def scale(self, x, y):
        """
        Rescales the image to the specified x, y proportions of the screen.
        """
        # make sure the image is loaded
        self.load()

        # try to get the VideoTrack
        v = VideoTrack.lastInstance()

        # if there is a VideoTrack...
        if v:
            # get the screen's vertical resolution
            yres = v.getResolution()[1]
            
            # calculate the pixel dimensions for the image based on the vertical resolution and the proportional dimensions
            xs = int(yres * x)
            ys = int(yres * y)

        # scale the LowImages and return the result wrapped in an Image object
        return Image(self.img_unscaled.scale(int(xs), int(ys)), x, y)
    def apply(self, f):
        """
        Apply function f to all pixels of image for result.  f can
        accept just a Color or a Color and and x, y tuple for
        position.
        """
        # make sure the image is loaded
        self.load()

        # apply the LowImage and function and return the result wrapped in an Image object
        return Image(self.img.apply(x))
    def getSize(self):
        """
        Return an x, y tuple for image dimensions.
        """
        # make sure the image is loaded
        self.load()
        
        # return the size of the LowImage
        return self.img.getSize()
    def show(self, x, y):
        """
        Put this image on the screen at x, y.
        """
        # make sure the image is loaded
        self.load()
        
        # have the LowImage draw itself at the specified coordinates
        self.img.show(x, y)

        # set the logLine
        self.setLogLine()

    def present(self, clk = None, duration = None, jitter = None, bc = None, minDuration = None):
        """
        Present an image on the screen.  If a ButtonChooser is
        provided, present ignores the jitter and clears the stimulus
        once a button is pressed or the duration waiting for a
        response is exceeded.

        INPUT ARGS:
          clk- Optional PresentationClock to update.
          duration/jitter- Duration to display the stimulus.
          bc- Optional ButtonChooser object.
          minDuration- Used in combination with bc to specify a minimum
                       time that a stimulus must be presented before a
                       keypress to the bc is accepted.
          
        OUTPUT ARGS:
          timestamp- time and latency of when the image came on the screen.
          button- Button pressed if we passed in bc.
          bc_time- Time and latency of when the button was pressed (if provided)
        """

        v = VideoTrack.lastInstance()
        
        # get the clock if needed
        if clk is None:
            clk = exputils.PresentationClock()

        # show the image
        t = v.showCentered(self)
        timestamp = v.updateScreen(clk)

        if bc:
            # wait for button press
            button,bc_time = bc.waitWithTime(minDuration,duration,clk)
        else:
            clk.delay(duration,jitter)

        # unshow that image
        v.unshow(t)
        v.updateScreen(clk)

        if bc:
            return timestamp,button,bc_time
        else:
            return timestamp


# New Movie Class
class Movie(UniquelyConstructed, Showable, Stimulus):
    """
    Represents a movie.  In PyEPL, movies are rendered onto an image
    surface, which you must first show (via one of the many show
    methods in the VideoTrack).

    Once a movie surface is shown, you can start and stop playback
    with the playMovie and stopMovie methods of the VideoTrack.

    When you are done playing a movie, you can then unshow the image
    where the movie was projected.
    """
    def __uinit__(self, x):
        """
        Creates a new Movie object.  It uses pygame to play the movie,
        so it only supports mpg files.

        INPUT ARGS:
          x- Data used to construct Movie.
             Can be the filename of an movie file,
             or a pygame.movie.Movie object.
        ...
        """
        Showable.__init__(self)

        if isinstance(x, pygame.movie.MovieType):
            # if the parameter is a Movie object, just wrap it
            self.movie = x
            self.filename = None
            self.img = None
        else:
            # otherwise, assume it's a filename
            self.filename = x
            self.movie = None
            self.img = None
        
        # set empty logline
        self.logLineStr = 'MOVIE'

    def load(self):
        """
        After calling this method, the image data is gauranteed to be
        loaded into primary memory.

        If keepLoadedAfterSmartShow is set to True, the image will not
        be unloaded after it is shown with SmartShow.
        """
        if self.movie is None:
            # create the movie instance
            self.movie = pygame.movie.Movie(self.filename)

        if self.img is None:
            # Create a surface where pygame will render the movie
            self._render_surf = pygame.Surface(self.movie.get_size())

            # Tell the movie to render frames to that new surface
            self.movie.set_display(self._render_surf)

            # Create a LowImage for performing the OpenGL blitting of the
            # movie frames to the actual OpenGL surface that we use
            self.img = hardware.graphics.LowImage(self._render_surf)
    def unload(self):
        """
        After calling this method, the Movie's data is unloaded from memory.
        """
        # stop playing if we are
        self.stop()
        
        # unload by removing our internal reference to the LowImage object
        self._render_surf = None
        self.img = None
        self.movie = None
        
    def isLoaded(self):
        """
        Checks if this object is actually loaded into memory.

        OUTPUT ARGS:
          b- True if the object is loaded in memory, False if not.
        """
        # check whether or not it's loaded by seeing if we have an
        # internal reference to a LowImage object
        return not self.img is None
    
    def __getstate__(self):
        """
        Don't give the image data to pickle.
        """
        # remove the Movie and LowImage objects before pickle looks at
        # the data (because LowImages are not picklable)
        d = self.__dict__.copy()
        for todel in ['img','movie','_render_surf']:
            try:
                del d[todel]
            except KeyError:
                pass
        return d
    def __setstate__(self, state):
        """
        After unpickling, the movie is NOT loaded.
        """
        # unpickle with None as self.image (since we left it out when
        # we pickled)
        self.__dict__.update(state)
        self.img = None
        self.movie = None
        self._render_surf = None

    def export(self, archive, namehint):
        """
        Export image.
        """
        raise NotImplementedError #...
    def setLogLine(self):
        """
        Set the ID for the video log.

        XXX IMPORTANT XXX
        We will eventually have to call this every time the movie is changed.
        XXX IMPORTANT XXX
        """
        # get the movie's pixel size
        xs, ys = self.getSize()

        # return the log line
        self.logLineStr = "MOVIE\t%s\t%d\t%d\t%d" % \
                          (self.filename, xs, ys, self.getTotalTime())
    def logLine(self):
        """
        Identify this movie for the video log.
        """
        return self.logLineStr
    def getSize(self):
        """
        Return an x, y tuple for movie dimensions.
        """
        # make sure the image is loaded
        self.load()
        
        # return the size of the LowImage
        return self.img.getSize()
    def show(self, x, y):
        """
        Put this image on the screen at x, y.
        """
        # make sure the image is loaded
        self.load()

        # see if we have to update the frame
        if self.hasNewFrame():
            self._updateFrame()
        
        # have the LowImage draw itself at the specified coordinates
        self.img.show(x, y)

        # set the logLine
        self.setLogLine()

    def hasNewFrame(self):
        """
        See if the movie has a new frame ready.
        """
        if not self.isLoaded():
            self.load()
            
        return self.movie.get_frame() != self._prevFrame
            
    def _updateFrame(self):
        """
        Update the surface with the current movie frame.
        """
        # set the gl_texture to be dirty so it can be updated
        self.img.gl_texture_dirty = True

        # clean the image to which the movie will render
        # this will render the new frame to the surface
        self.img.cleanGLTexture()

        # save the current frame
        self._prevFrame = self.movie.get_frame()
        

    def play(self,loops=0):
        
        # make sure we are loaded
        self.load()

        # reset the previous frame
        self._prevFrame = None
        self._latest_timestamp = None
        
        # start playing
        self.movie.play(loops)
                
    def pause(self):
        # only pause if was running
        if self.movie and self.movie.get_busy():
            # pause it
            self.movie.pause()
#             # remove the playing callback
#             removePollCallback(self._playMovieCallback)

    def stop(self):
        # stop the movie if playing
        if self.movie and self.movie.get_busy():
            self.movie.stop()

#         # remove the poll callback
#         removePollCallback(self._playMovieCallback)

        # reset the previous frame
        self._prevFrame = None
        
    def getCurrentFrame(self):
        if self.movie:
            return self.movie.get_frame()
        
    def getElapsedTime(self):
        if self.movie:
            return long(self.movie.get_time()*1000)
        
    def getTotalTime(self):
        if self.movie:
            return long(self.movie.get_length()*1000)

    def getRemainingTime(self):
        if self.movie:
            return long((self.movie.get_length() -
                         self.movie.get_time())*1000)

    def present(self, clk = None, duration = None, jitter = None, bc = None, minDuration = None):
        """
        Present an movie on the screen.  If a ButtonChooser is
        provided, present ignores the jitter and clears the stimulus
        once a button is pressed or the duration waiting for a
        response is exceeded.

        INPUT ARGS:
          clk- Optional PresentationClock to update.
          duration/jitter- Duration to display the stimulus.
          bc- Optional ButtonChooser object.
          minDuration- Used in combination with bc to specify a minimum
                       time that a stimulus must be presented before a
                       keypress to the bc is accepted.
          
        OUTPUT ARGS:
          timestamp- time and latency of when the movie came on the screen.
          button- Button pressed if we passed in bc.
          bc_time- Time and latency of when the button was pressed (if provided)
        """

        v = VideoTrack.lastInstance()
        
        # get the clock if needed
        if clk is None:
            clk = exputils.PresentationClock()

        # do the auto delay if no duration is provided
        if not bc and not duration:
            doDelay = True
        else:
            doDelay = False
        # show the image
        shown_movie = v.showCentered(self)
        # show the movie
        timestamp = v.playMovie(self,t=clk,doDelay=doDelay)

        if bc:
            # wait for button press
            button,bc_time = bc.waitWithTime(minDuration,duration,clk)
        elif duration:
            clk.delay(duration,jitter)

        # stop the movie
        v.stopMovie(self,t=clk)
        
        # unshow that image
        v.unshow(shown_movie)
        v.updateScreen(clk)

        if bc:
            return timestamp,button,bc_time
        else:
            return timestamp



class Font(MediaFile):
    """
    Represents a graphical font.
    """
    def __init__(self, filename):
        """
        Create font object from TrueType font file.
        """
        # save the filename
        self.filename = filename

        # while the font is not loaded, self.font is None
        self.font = None
    def load(self):
        """
        After this call, the font is guaranteed to be loaded into
        primary memory.
        """
        if not self.font:
            # if it's not loaded already, set self.font to a LowFont object from the saved filename
            self.font = hardware.graphics.LowFont(self.filename)
    def unload(self):
        """
        """
        # remove the internal reference to the LowFont object
        self.font = None
    def isLoaded(self):
        """
        """
        # do we have a LowFont object?
        return bool(self.font)
    def __getstate__(self):
        """
        Don't give the internal font object to pickle.
        """
        # LowFont objects are not picklable, so we remove them before pickle does its thang
        d = self.__dict__.copy()
        del d["font"]
        return d
    def __setstate__(self, state):
        """
        After unpickling, the font is NOT loaded.
        """
        # since we excluded self.font from the pickle, we need to set self.font to None when we unpickle
        self.__dict__.update(state)
        self.font = None
    def export(self, archive, namehint):
        """
        Export font.
        """
        pass #...
    def write(self, text, size, color):
        """
        Return an object representing a rendition of the string text
        with the specified size and color in this font.
        """
        # return a text object
        return Text(text, self, size, color)
    def wordWrap(self, text, size, color, width):
        """
        Like write, but instead return a list of text objects where
        each item represents a line with text word wrapped to width.
        """
        # make sure we can access the global variables dealing with font defaults
        global defaultFont
        global defualtFontSize
        global defaultFontColor
        
        # use font defaults where needed
        if color:
            color = Color(color)
        else:
            color = defaultFontColor
        if not size:
            size = defaultFontSize

        # calculate the font size in pixels (from its proportional size)
        truesize = int(VideoTrack.lastInstance().getResolution()[1] * size)

        # make sure the font is loaded
        self.load()

        # split up the text by paragraph breaks
        para = text.split("\n\n")

        # start a list of paragraphs
        paragraphs = []

        # for each paragraph (between paragraph breaks)...
        for p in para:
            # remove leading and trailing white space and replace newlines with spaces
            p = p.lstrip().rstrip().replace("\n", " ")

            # if there anything left after that...
            if len(p):
                # start a list of words in the paragraph
                words = []

                # split up the paragraph at spaces
                for word in p.split(" "):
                    # for each resulting string that's not empty...
                    if len(word):
                        # add the word to the word list
                        words.append(word)

                # add the word list to the paragraph list
                paragraphs.append(words)

        # start a list to hold the text objects
        result = []

        # iterate over the paragraphs (each one is a list of words)...
        for words in paragraphs:
            # start with the first word of the paragraph
            wordbase = 0

            # set the limit to the length of the paragraph (in words)
            wordlimit = len(words)

            # as long as we haven't placed all the words yet...
            while wordbase != wordlimit:
                # construct a string - a potential line
                line = " ".join(words[wordbase : wordlimit])

                # find out how long it would be if we rendered it
                lwidth, lheight = self.font.getSize(line, truesize)
                if lwidth > width:
                    # if it's to big for our width, take off one of the words and try again
                    wordlimit -= 1
                else:
                    # otherwise, append the appropriate Text object to the result
                    result.append(Text(line, self, size, color))

                    # reset the word base so only the remaining words of the paragraph will be placed
                    wordbase = wordlimit

                    # reset the word limit so the rest of the paragraph will be placed
                    wordlimit = len(words)

            # add a blank line after each paragraph
            result.append(Text("", self, size, color))

        # return the lines
        return result

# create the initial font defaults...
defaultFont = Font("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf")
defaultFontSize = 0.0625
defaultFontColor = Color("white")

def setDefaultFont(font = None, size = None, color = None):
    global defaultFont
    global defaultFontSize
    global defaultFontColor

    # set the defaults given...
    if font:
        defaultFont = font
    if size:
        defaultFontSize = size
    if color:
        defaultFontColor = color

class Text(Showable, Stimulus):
    """
    Represents a string of text rendered with a certain font, color,
    and size.
    """
    def __init__(self, text, font = None, size = None, color = None):
        """
        Create text object.
        """
        # make sure we can access the global variables dealing with font defaults
        global defaultFont
        global defualtFontSize
        global defaultFontColor
		
        Showable.__init__(self)
        
        # split the text into lines
        self.text = text.split("\n")

        # use the font defaults where needed...
        if color:
            self.color = Color(color)
        else:
            self.color = defaultFontColor
        if not size:
            self.propsize = defaultFontSize
        else:
            self.propsize = size
        if font:
            self.font = font
        else:
            self.font = defaultFont
            
        # set the rendered text to None (not rendered)
        self.rendered = None
    def __getstate__(self):
        """
        Don't give the rendered form to pickle.
        """
        # remove the LowImage object before pickle looks at the data (because LowImages are not picklable)
        d = self.__dict__.copy()
        del d["rendered"]
        return d
    def __setstate__(self, state):
        """
        After unpickling, the text should NOT be rendered.
        """
        # unpickle with None as self.image (since we left it out when we pickled)
        self.__dict__.update(state)
        self.rendered = None
    def render(self):
        # make sure the font is loaded
        self.font.load()

        # calculate the pixel size of the text (from the given proportional size)
        self.size = int(VideoTrack.lastInstance().getResolution()[1] * self.propsize)
        
        # start rendered as an empty list
        self.rendered = []
        
        # for each line of text...
        for t in self.text:
            # ...append the rendered line
            self.rendered.append(self.font.font.write(t, self.size, self.color.getTuple()))
    def unrender(self):
        """
        Delete all the rendered text.
        """
        self.rendered = []
    def load(self):
        """
        Wrapper to render.
        """
        self.render()

    def unload(self):
        """
        Wrapper to unrender.
        """
        self.unrender()

    def isLoaded(self):
        """
        Tell if the text is rendered.
        """
        return bool(self.rendered)
        
    def show(self, x, y):
        """
        Draw the image on the screen.
        """
        # if the text isn't yet rendered...
        if not self.rendered:
            self.render()
            
        # get the size of the rendered text
        xsize, ysize = self.getSize()

        # calculate the horizontal center of the text area
        xplushalfxsize = x + (xsize / 2)

        # for each rendered line of text...
        for i in self.rendered:
            # get the size of the line
            ixsize, iysize = i.getSize()

            # show the line in a position such that it is horizontally centered in the text area
            i.show(xplushalfxsize - (ixsize / 2), y)

            # move the y position down the correct amount for the next line
            y += iysize

    def setXYSize(self):
        if self.rendered:
            # if it's been rendered, we'll get the size from the LowImage objects...
            self.xsize = 0
            self.ysize = 0
            for i in self.rendered:
                # iterate through the LowImage objects, getting the sum of y sizes and the maximum of x sizes
                x, y = i.getSize()
                self.xsize = max(self.xsize, x)
                self.ysize += y
        else:
            # in it hasn't been rendered...
            # make sure the font is loaded
            self.font.load()
            self.xsize = 0
            self.ysize = 0
            for t in self.text:
                # iterate through the lines of text, getting the sum of y sizes and the maximum of x sizes
                # we query the font object for how large the text would be if rendered
                x, y = self.font.font.getSize(t, self.size)
                self.xsize = max(self.xsize, x)
                self.ysize += y
        return self.xsize, self.ysize
    
    def getSize(self):
        """
        Return x, y dimensions of text.
        """
        try:
            # if we've already calculated the size of the text, just return it
            return self.xsize, self.ysize
        except AttributeError:
            # othersize...
            self.render()
            return self.setXYSize()
        
    def setColor(self, color):
        #set the color
        self.color = Color(color)
        #re-render
        self.render()

    def setSize(self, size):
        self.propsize = size
        self.render()
        self.setXYSize()

    def setFont(self, font):
        self.font = font
        self.render()
        self.setXYSize()
            
    def logLine(self):
        """
        Identify this text for the video log.
        """
        return "TEXT\t%s\t%d\t%s\t%r" % (self.font.filename, self.size, self.color, "\n".join(self.text))


    def present(self, clk = None, duration = None, jitter = None, bc = None, minDuration = None):
        """
        Present an text on the screen.  If a ButtonChooser is
        provided, present ignores the jitter and clears the stimulus
        once a button is pressed or the duration is exceeded.

        INPUT ARGS:
          clk- Optional PresentationClock to update.
          duration/jitter- Duration to display the stimulus.
          bc- Optional ButtonChooser object
          
        OUTPUT ARGS:
          timestamp- time and latency of when the text came on the screen.
          button- Button pressed if we passed in bc.
          bc_time- Time and latency of when the button was pressed (if provided)

        """

        v = VideoTrack.lastInstance()
        
        # get the clock if needed
        if clk is None:
            clk = exputils.PresentationClock()

        # show the image
        t = v.showCentered(self)
        timestamp = v.updateScreen(clk)

        if bc:
            # wait for button press
            button,bc_time = bc.waitWithTime(minDuration,duration,clk)
        else:
            clk.delay(duration,jitter)

        v.unshow(t)
        v.updateScreen(clk)

        if bc:
            return timestamp,button,bc_time
        else:
            return timestamp


class CompoundStimulus(Stimulus):
    """ 
    Comibine multiple stimuli into a single compound stimulus that you
    can present at once.  
    """
    def __init__(self,stimuli):
	"""
	stimuli = [(Name,[Text,Image],[ABS,PROP,REL],[LOC,(Relation,Name)]), ...]

	Create a CompoundStimulus object, which groups a set of showable objects.

	The constructor taks a single argument, which is a list of 4-tuples.

	The 4-tuples are of the form: 
	string-label, stimulus-object, location-specification-mode, location.

	The string-label can be any string.

	The stimulus-object can be an instance of Text or Image.

	Location-specification-mode can be one of: 'ABS', 'PROP', and 'REL'.

	The form of the location field will depend on the value of
	location-specification-mode.  

	If location-specification-mode is 'ABS', then the fourth field
	must be a 2-tuple containing the x- and y-coordinates (in
	pixels) for displaying the the showable.

	If location-specification-mode is 'PROP', then the fourth field
	must be a 2-tuple containing the x- and y screen fractions for
	displaying the the showable (e.g., (.6, .1) puts the showable
	3/5ths of the way from the left edge, and 1/10th down from the
	top edge).  

	If location-specification-mode is 'REL', the fourth field must
	be a 2- (or 3-) tuple whose first dimension contains the one
	of the relations: BELOW, ABOVE, LEFT, RIGHT, OVER.  The second
	dimension of must be the text-label of another showable in the
	list.  The (optional) 3rd dimension is used as the offset to
	adjust the relational location.  The first stimulus in the
	list can not be displayed using relative
	location-specification.
	
	If location-specification-mode is 'ANCHOR', the fourth field
	must be a 2- (up to 4-) tuple specifying first, the anchor on
	the stim to show with CENTER, NORTH, NORTHWEST, ..., second,
	the relative starting position as a 2-tuple of an (x,y)
	location or the text-label and anchor point of a shown
	('jubba',NORTHEAST).  You can optionally provide an offset in
	proportional units or pixels as the third part of the tuple,
	but the fourth part must be set to False if you want to provide
	the offset in pixels and not proportional units.

	('cool',stim,'ANCHOR',(CENTER, propToPixel(.25,.75), (.1,.15), True))
	
	Example:
	"""
	self.stimuli = stimuli

    def show(self):
	# loop and show the stimuli, keeping track of the showns in a
	# dictionary referenced by Name
	self.showns = {}
	v = VideoTrack.lastInstance()
	for label, stim, specMode, location in self.stimuli:
	    if specMode=='ABS':
		self.showns[label] = v.show(stim, location[0], location[1])
	    elif specMode=='PROP':
		self.showns[label] = v.showProportional(stim, location[0], location[1])
	    elif specMode=='REL':
		# check that relatum-label exists
		relatum = location[1]
		if relatum not in self.showns.keys():
		    raise ValueError, "Invalid target for relative location: " % relatum
		if len(location)==2:
		    self.showns[label] = v.showRelative(stim, location[0], self.showns[relatum])
		elif len(location)==3:
		    self.showns[label] = v.showRelative(stim, location[0], self.showns[relatum], location[2])
	    elif specMode=='ANCHOR':
		# get the anchor of the stim		
		anchor = location[0]

		# determine the relative position
		relPos = location[1]
		if type(relPos[0]) is str:
		    # must determine based on a shown and 
		    relPos = self.showns[relPos[0]].anchor[relPos[1]]

		# see about offset and isProportional
		offset = (0,0)
		isProp = True
		if len(location) > 2:
		    offset = location[2]
		    if len(location) > 3:
			isProp = location[3]

		# show the showable with all the anchor info
		self.showns[label] = v.showAnchored(stim, anchor, relPos, offset, isProp)
	    else:
		raise ValueError, "Invalid location specification mode %s" % specMode

    def unshow(self):
	v = VideoTrack.lastInstance()
	# loop and unshow the showns
	keyslist = self.showns.keys()
	for key in keyslist:
	    v.unshow(self.showns[key])

    def present(self, clk = None, duration = None, jitter = None, bc = None, minDuration = None):
        """
        Present a CompoundStimulus on the screen.  If a ButtonChooser is
        provided, present ignores the jitter and clears the stimulus
        once a button is pressed or the duration is exceeded.

        INPUT ARGS:
          clk- Optional PresentationClock to update.
          duration/jitter- Duration to display the stimulus.
          bc- Optional ButtonChooser object
          
        OUTPUT ARGS:
          timestamp- time and latency of when the text came on the screen.
          button- Button pressed if we passed in bc.
          bc_time- Time and latency of when the button was pressed (if provided)

        """

        v = VideoTrack.lastInstance()
        
        # get the clock if needed
        if clk is None:
            clk = exputils.PresentationClock()

	self.show()
        timestamp = v.updateScreen(clk)

        if bc:
            # wait for button press
            button,bc_time = bc.waitWithTime(minDuration,duration,clk)
        else:
            clk.delay(duration,jitter)

	self.unshow()
        v.updateScreen(clk)

        if bc:
            return timestamp,button,bc_time
        else:
            return timestamp

    
class SolidBackground(Showable):  # change this to handle any rectangular region
    """
    Draw the same color to every pixel on the screen regardless of
    showing position.
    """
    def __init__(self, color):
        """
        Create SolidBackground object.
        """
        Showable.__init__(self)
        
        # save the color
        self.color = color
 
    def show(self, x, y):
        """
        Clear the screen to the correct color.
        """
        # paint the whole screen with the saved color
        hardware.clearScreen(self.color)
    def logLine(self):
        """
        Log the background color.
        """
        return "BG\t%s" % (self.color,)

class VideoTrack(textlog.LogTrack):
    """
    A track for video output.
    """
    # set the track's registry info...
    logExtension = ".vidlog"
    trackTypeName = "VideoTrack"
    def __init__(self, basename, archive = None, autoStart = True):
        """
        Create the VideoTrack.
        """
        # call the LogTrack constructor
        textlog.LogTrack.__init__(self, basename, archive, autoStart)

        # list of things that should be displayed when the screen is next updated
        self.pending = []

        # list of things on the screen now
        self.onscreen = []

        # number of active showables represented in the onscreen list
        self.activepending = 0

        # default to no minimum frame duration (no maximum framerate)
        self.minframeduration = 0L

        # initialize the last_updated timestamp
        self.last_updated = (0L, 0L)

        # start with no update callbacks
        self.update_callbacks = []

        # list of calls to make after the screen is updated
        self.do_after_update = []

        # list of currently playing movies
        self._playing_movies = []

    def startLogging(self):
        """
        Begin logging all basic video output.
        """
        # call the LogTrack's startLogging method
        textlog.LogTrack.startLogging(self)

        # log the current resolution
        self.logResolution()
    def startService(self):
        """
        Initialize video output.
        """
        # create the window
        hardware.startVideo()

        # save the resolution
        self.screensize = hardware.getResolution()
        
        self.showFPS = hardware.getShowFPS()
    def stopService(self):
        """
        Finalize video output.
        """
        # close the window
        hardware.stopVideo()
        
    def logResolution(self):
        """
        Note the resolution and windowedness in the video log.
        """
        # figure out if we're full screen or in a window and set fs to be an appropriate string in a tuple to note the mode in the log message
        if hardware.getFullscreen():
            fs = ("Fullscreen",)
        else:
            fs = ("Window",)

        # log the resolution and mode
        self.logMessage("R\t%d\t%d\t%s" % (self.screensize + fs))
    def toggleFullscreen(self):
        """
        Toggle the fullscreen status of the display.
        """
        # toggle
        hardware.toggleFullscreen()

        # log the resolution again
        self.logResolution()
    def setCaption(self, caption):
        """
        Set window caption.
        """
        # set the caption
        hardware.setCaption(caption)
    def setMaximumFramerate(self, fps = None):
        """
        Set a framerate limit.  None means no limit.

        This can be used to enforce a particular framerate.
        """
        if fps:
            # if fps is provided, calculate and set the minimum frame duration
            self.minframeduration = 1000L / fps
        else:
            # otherwise, set it to zero
            self.minframeduration = 0L
    def getResolution(self):
        """
        Return an x, y tuple of the screen's resolution.
        """
        # return the saved resolution
        return self.screensize
    def clear(self, color = None, keep_background=True):
        """
        Clear basic video.  Make a color background or no background
        in unspecified.
        """
        # clear by unshowing all the pending items
        # get list of pending items
        if color != None or keep_background==False:
            # remove them all
            toclear = [p[0] for p in self.pending]
        else:
            # keep any background color they have set
            toclear = []
            for p in self.pending:
                if not isinstance(p[1], SolidBackground):
                    toclear.append(p[0])
        # unshow what we've selected
        self.unshow(*toclear)

        # if a color is provided we want to show that after we've removed everything else...
        if color != None:
            # create a SolidBackground object
            bg = SolidBackground(Color(color))

            # show the background, returning the shown object
            return self.show(bg, 0, 0)
    def show(self, showable, x, y):
        """
        Draw a basic video element.
        """
        # persistently load the showable
        showable.requireLoaded()
        
        # create a new Shown object to represent this instance of the showable on the screen
        shown = Shown(x,y,*showable.getSize())

        # add the shown, showable, and position to the pending list
        self.pending.append((shown, showable, x, y))

        if isinstance(showable, ActiveShowable):
            # if it's an active showable, increment the activingpending count
            self.activepending += 1
        
        # return the shown
        return shown
    def showProportional(self, showable, x, y, constrain=True):
        """
        showProportional displays a showable, with its position
        specified as a proportion of the width and height of the whole
        screen, such that (0.5,0.5) is the center.

        The original (default) implementation of this was designed so
        that objects would be placed at the edge (but not spilling
        out) of the screen when the proportion was set to 0 or
        1. Leave constrain=True to keep this behavior.

        However, in retrospect, it makes more sense for these
        proportion values to refer to the center of the object, such
        that the object is 50% off the screen when the proportions are
        0 or 1. For this behavior, set constrain=False. See
        http://sourceforge.net/forum/forum.php?thread_id=1871462&forum_id=548620
        for a clear demonstration of these two behaviors.
        
        """
        # get the size of the showable
        objectsize = showable.getSize()

        if constrain:
            # calculate the correct pixel position (for the upper left corner) based on the showable size and the screen size
            position = (int((self.screensize[0] - objectsize[0]) * x), int((self.screensize[1] - objectsize[1]) * y))
        else:
            # center the object at the specified position without the
            # constraint that the object must be fully in the screen
            position = (int((self.screensize[0] * x) - (objectsize[0] / 2)), int((self.screensize[1] * y) - (objectsize[1] / 2))) 
            
        # show the showable at the calculated position, returning the Shown object
        return self.show(showable, *position)
    def showCentered(self, showable):
        """
        """
        # do a showProportional with coordinates (0.5, 0.5)
        return self.showProportional(showable, 0.5, 0.5)
    def showRelative(self, showable, relation, shown, offset = 0):
        """
	Show a showable relative to another shown object.  You can
	optionally specify an offset in the relative direction.
	Relations can be one of BELOW, ABOVE, LEFT, RIGHT, OVER.  The
	offset does not apply to the OVER relation.
        """
        # iterate through the pending list looking for the shown specified...
        for xshown, xshowable, x, y in self.pending:
            # when we find it...
            if xshown is shown:
                # get the size of the associated showable
                xs, ys = xshowable.getSize()

                # get the size of the new showable
                xs2, ys2 = showable.getSize()

                # calculate the pixel position based on the sizes and positions of the showables and which relation is used...
                if relation is BELOW:
                    position = (x + (xs / 2) - (xs2 / 2), y + ys + offset)
                elif relation is ABOVE:
                    position = (x + (xs / 2) - (xs2 / 2), y - ys2 - offset)
                elif relation is LEFT:
                    position = (x - xs2 - offset, y + (ys / 2) - (ys2 / 2))
                elif relation is RIGHT:
                    position = (x + xs + offset, y + (ys / 2) - (ys2 / 2))
                elif relation is OVER:
                    position = (x + (xs / 2) - (xs2 / 2), y + (ys / 2) - (ys2 / 2))
                else:
                    # raise an exception if the relation provided is not recognized
                    raise ValueError, "Invalid positional relation: %r" % relation
                # show the new showable at the calculated position, returning the Shown object
                return self.show(showable, *position)
        # if we never found a matching Shown, raise an exception
        raise ValueError, "Shown not present."
    
    def propToPixel(self, x, y):
	"""
	Convert proportional coordinates to actual pixel values.

	Returns a tuple for the actal position in pixels.
	"""
	return (int(self.screensize[0]*x),int(self.screensize[1]*y))
	
    def showAnchored(self, showable, anchor, relPosition, offset = (0,0), isProp = True):
	"""	
	Place a showable relative to any point on the screen or
	another showable, using anchor points on the showable.  

	FUNCTION:
	  shown = showAnchored(showable,anchor,relPosition, offset=(0,0), isProp=True)

	INPUT ARGS:
	  showable - Showable to place.
	  anchor - Anchor point on the showable (NORTH, SOUTH, EAST, WEST, NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST, CENTER) 
	  relPosition - Screen position in pixels.  You can use propToPixel or a shown object (shown.NorthWest, shown.Center) to specify the pixel position.
	  offset - Offset in pixels or proportional units (see isProp arg) from the relPosition to place the showable by its anchor.
	  isProp - Whether the offset is in pixels or proportional units.

        OUTPUT ARGS:
	  shown - The object that was shown.

	"""
	
	# get the width and height of the showable
	width, height = showable.getSize()

	# see if must convert offset from proportional units
	if isProp:
	    offset = (int(self.screensize[0] * offset[0]), int(self.screensize[1] * offset[1]))

	# adjust the position based on the desired anchor
	if anchor is SOUTHWEST:
	    adjust = (0, -height)
	elif anchor is WEST:
	    adjust = (0, -(height/2))
	elif anchor is NORTHWEST:
	    adjust = (0, 0)
	elif anchor is NORTH:
	    adjust = (-(width/2), 0)
	elif anchor is NORTHEAST:
	    adjust = (-width, 0)
	elif anchor is EAST:
	    adjust = (-width, -(height/2))
	elif anchor is SOUTHEAST:
	    adjust = (-width, -height)
	elif anchor is SOUTH:
	    adjust = (-(width/2), -height)
	elif anchor is CENTER:
	    adjust = (-(width/2), -(height/2))
	else:
	    # raise an exception if the relation provided is not recognized
	    raise ValueError, "Invalid positional relation: %r" % relation

	# calculate the position to add the object based on the offset and adjustment
	position = (relPosition[0]+offset[0]+adjust[0],relPosition[1]+offset[1]+adjust[1])

	# show the showable and return the shown
	return self.show(showable, *position)

    def unshow(self, *showns):
        """
        Remove showables from display.
        """
        # keep a list of the indices of the entries in the pending list to be deleted
        # we don't delete as we find them because changing the size of the list would corrupt the iterater
        deletes = []

        # iterate through the pending list searching for the given Shown...
        for n, (shown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if shown in showns:
                if isinstance(showable, ActiveShowable):
                    # decrement the active pending count, if the showable is active
                    self.activepending -= 1
                
                # allow showable to be unloaded
                showable.unrequireLoaded()
                
                # append the index to the deletion list
                deletes.append(n)

        # reverse the deletion list so that we delete from later in the list first
        # this way, the indices for the remaining items to be deleted are still correct
        deletes.reverse()

        # iterate through the deletion list...
        for n in deletes:
            # ...deleting as we go
            del self.pending[n]
    def getPosition(self, shown):
        """
        Return the position of the Shown on the screen.
        """
        # iterate through the pending list searching for the shown provided...
        for xshown, showable, x, y in self.pending:
            # when we find it...
            if xshown is shown:
                # ...return its position
                return x, y

        # if we never found it, raise an exception
        raise ValueError, "Shown not present."
    def replace(self, shown, showable):
        """
        Replace a shown object with a new object.
        """
        # iterate through the pending list searching for the shown provided...
        for xshown, xshowable, x, y in self.pending:
            # when we find it...
            if xshown is shown:
                # ...unshow it
                self.unshow(shown)

                # ...and show the new showable at the same coordinates
                return self.show(showable, x, y)

        # if we never found it, raise an exception
        raise ValueError, "Shown not present."
    def updateScreen(self, t = None, force = False):
        """
        Change the screen output to refect all basic video elements
        drawn.  

	If t is a PresentationClock, updateScreen will tare the time to
        represent the time the update occured, which is critical for
        timing events based on actual screen refreshes as opposed to
        times those refreshes were requested.  
	"""
        # update the onscreen list to match the pending list
        self.onscreen = self.pending[:]

        if self.activepending and not force:
            # if we're in activing mode (and not being forced to ignore it), then we're done
            return
	# handle timing
	clock = None
        if t is None:
            # if t is None, use the current time
            t = timing.now()
        elif isinstance(t, exputils.PresentationClock):
            # if t is a PresentationClock, use the time from that clock
            clock = t
            t = t.get()
            
        # calculate the earliest allowable update time, based on the minimum frame duration
        mintime = self.last_updated[0] + self.minframeduration
        
        if t < mintime:
            # if t is before the the earliest allowable time, set t to the earliest allowable time
            t = mintime

        # iterate through the onscreen list...
        for shown, showable, x, y in self.onscreen:
            # ...showing everything in order
            showable.show(x, y)

        # update the screen at the calculated time
	# now passes in self as a video track instance
        #r = hardware.makeVideoChanges(t,self)  
        r = hardware.makeVideoChanges(t)  
        
        for fnc, targs, dargs in self.do_after_update:
            # perform all waiting do_after_update calls
            fnc(*targs, **dargs)

        # ...and clear the do_after_update list
        self.do_after_update = []

        for cb in self.update_callbacks:
            # call all update callbacks
            cb()(r)

        # log the contents of the display
        self.logDisplay(r)

        # save the update timestamp as the time of last update
        self.last_updated = r

	# see if we should tare the time 
	if clock is not None:
	    # we have a clock
            # accumulate errors
            clock.accumulatedTimingError += r[0]-t
            #so tare the clock to the presentation time
	    clock.tare(r)

        # return the timestamp
        return r
    def renderLoop(self, f, *pargs, **kwargs):
        """
        There has to be a render loop when active drawables are
        displayed.
        """
        # mark the start of the render loop in the log
        self.logMessage("ENTERLOOP")
        
        prev_last_updated = self.last_updated
        fpsFrameCount = 1
        fpsMinimumFrames = 50
        fpsText = None
        t_delta = 0.0

        # keep calling f until it returns a false value...
        while f(self.last_updated, *pargs, **kwargs):
            # after each call to f:
            # poll for events
            hardware.pollEvents()

            # calculate the earliest allowable update time, based on the minimum frame duration
            mintime = self.last_updated[0] + self.minframeduration

            # get the current time
            t = timing.now()

            if t < mintime:
                # if the current time is earlier than the earliest allowable time, use the earliest allowable time instead
                t = mintime

            # iterate through the onscreen list...
            for shown, showable, x, y in self.onscreen:
                # ...showing everything in order
                showable.show(x, y)
            
            # Show FPS
            if self.showFPS:
                t1 = prev_last_updated[0]
                t2 = self.last_updated[0]
                t_delta += t2 - t1
                if t_delta and fpsFrameCount >= fpsMinimumFrames:
                    fps = fpsFrameCount * 1000.0 / t_delta
                    fpsText = Text("%.1fFPS"  % fps)
                    fpsFrameCount = 1
                    t_delta = 0.0
                else:
                    fpsFrameCount += 1
                if fpsText:
                    fpsText.show(0, 0)

            # update the screen at the calculated time
            prev_last_updated = self.last_updated
            #self.last_updated = hardware.makeVideoChanges(t, self)
            self.last_updated = hardware.makeVideoChanges(t)

            # log the contents of the display
            self.logDisplay(self.last_updated)

            for cb in self.update_callbacks:
                # call all update callbacks
                cb()(self.last_updated)
            for fnc, targs, dargs in self.do_after_update:
                # perform any waiting do_after_update calls
                fnc(*targs, **dargs)

            # ...and clear the do_after_update list
            self.do_after_update = []

        # mark the end of the render loop in the log
        self.logMessage("EXITLOOP")
    def doAfterUpdate(self, f, *targs, **dargs):
        """
        """
        # append the call to the do_after_update list
        self.do_after_update.append((f, targs, dargs))
    def logDisplay(self, timestamp = None):
        """
        Log the basic video components currently displayed.
        """
        # if we are currently logging...
        if self.logall:
            if timestamp == None:
                # if the timestamp is not given, use the current time
                timestamp = (timing.now(), 0L)

            # get the number of showables on screen
            t = len(self.onscreen)

            # log a header message for this update
            self.logMessage("D\t0/%d\t0\t0\tUPDATE" % t, timestamp)

            for n, (showable, showable, x, y) in enumerate(self.onscreen):
                # log each showable on the screen
                self.logMessage("D\t%d/%d\t%d\t%d\t%s" % (n + 1, t, x, y, showable.logLine()), timestamp)
    def toFront(self, shown):
        """
        """
        # iterate through the pending list searching for the given shown...
        for n, (xshown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if xshown is shown:
                # remove it from the pending list
                del self.pending[n]

                # ...and append it to the end of the list
                self.pending.append((xshown, showable, x, y))

                # then return
                return
            
        # if we never found a matching Shown, raise an exception
        raise ValueError, "Shown not present."
    def toBack(self, shown):
        """
        """
        # iterate through the pending list searching for the given shown...
        for n, (xshown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if xshown is shown:
                # remove it from the pending list
                del self.pending[n]

                # ...and insert it at the beginning of the list
                self.pending.insert(0, (xshown, showable, x, y))

                # then return
                return
            
        # if we never found a matching Shown, raise an exception
        raise ValueError, "Shown not present."
    def putBehind(self, shown1, shown2):
        """
        """
        # iterate through the pending list searching for shown2...
        for n2, (shown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if shown is shown2:
                # go on to the next loop
                break
        else:
            # if we never found a matching Shown, raise an exception
            raise ValueError, "Shown (#2) not present."

        # iterate through the pending list searching for shown1...
        for n1, (shown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if shown is shown1:
                # remove it from the pending list
                del self.pending[n1]

                # ...and insert it before the position of shown2
                self.pending.insert(n2, (shown1, showable, x, y))

                # then return
                return
        # if we never found a matching Shown, raise an exception
        raise ValueError, "Shown (#1) not present."
    def putInFrontOf(self, shown1, shown2):
        """
        """
        # iterate through the pending list searching for shown2...
        for n2, (shown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if shown is shown2:
                # go on to the next loop
                break
        else:
            # if we never found a matching Shown, raise an exception
            raise ValueError, "Shown (#2) not present."

        # iterate through the pending list searching for shown1...
        for n1, (shown, showable, x, y) in enumerate(self.pending):
            # when we find it...
            if shown is shown1:
                # remove it from the pending list
                del self.pending[n1]

                # ...and insert it after the position of shown2
                self.pending.insert(n2 + 1, (shown1, showable, x, y))

                # then return
                return
        # if we never found a matching Shown, raise an exception
        raise ValueError, "Shown (#1) not present."
    def addUpdateCallback(self, cb):
        """
        Call cb whenever the screen is updated.
        """
        # append an update callback using a weak reference, so that it cleans itself up
        self.update_callbacks.append(weakref.ref(cb, self.removeUpdateCallback))
    def removeUpdateCallback(self, cbref):
        """
        This method is used to clean up callbacks that no longer
        exist.
        """
        try:
            # try to remove an update callback
            self.update_callbacks.remove(cbref)
        except ValueError:
            # if it fails, it must have cleaned itself up automatically, and that's fine
            pass

    def setGammaRamp(self,red,green,blue):
	"""
	Sets the gamma ramp to correct monitor distortions.  Returns True on success.
	"""
	# Call the set_gamma_ramp function
	return hardware.setGammaRamp(red,green,blue)
    def loadGammaRampFile(self,filename):
	"""
	Load gamma ramp information from a file.  Returns
	red,green,blue lists.  If the file only contains a single
	column, it will be replicated into the other two columns.
	"""
	# Function inspired by VisionEgg function
	fid = open(filename,'r')
	gvals = []
	for line in fid.readlines():
	    # clean up the line
	    line = line.strip()
	    
	    # convert strings to numbers and append
	    gvals.append(map(int,line.split()))
	    
	    # make sure we got three values
	    if len(gvals[-1]) != 3:
		raise ValueError("Expected 3 values per row")

	# make sure we got 256 lines
	if len(gvals) != 256:
	    raise ValueError("Expected 256 rows")
	
	# split out to three rgb
	red, green, blue = zip(*gvals)

	return red,green,blue

    def playMovie(self,movie,t=None,doDelay=True,loops=0):
        """
        Start playback of a movie object.

        INPUT ARGS:
           movie- Instance of a Movie object.
           t- Time or presentation clock instance to control onset time.
           doDelay- Whether to advance the clock the length of the movie.
                    Defaults to True
           loops- Number of times the movie will be repeated. A value of
                  -1 will repeat forever.

        OUTPUT ARGS:
           timestamp- When the first frame played
        """
        # make sure it's not in the playing movies
        if movie in self._playing_movies:
            raise RuntimeError('You can only play a movie instance once.')
        
        # handle timing
	clk = None
        if t is None:
            # if t is None, use the current time
            t = timing.now()
        elif isinstance(t, exputils.PresentationClock):
            # if t is a PresentationClock, use the time from that clock
            clk = t
            t = t.get()

        # are there any movies already playing
        numPlaying = len(self._playing_movies)
        
        (timeInterval, start_timestamp) = timing.timedCall(t,
                                                           self._startMovie,
                                                           movie,loops)
        
        # start the callback if it's not already started
        if numPlaying == 0:
            hardware.addPollCallback(self._playingMovieCallback)

        # log it
        self.logMessage("%s\t%s\t%s" % \
                        ("MOVIE_PLAY",movie.filename,
                         movie.getRemainingTime()), timeInterval)

        # tare the clock to the start time if we have a clock
        if doDelay and clk:
            # accumulate the error
            clk.accumulatedTimingError += start_timestamp[0]-t
            # tare the clock and delay the proper amount.
            # I'm basing this on when the movie.play began, not when
            # the first frame was rendered.
            clk.tare(timeInterval[0])
            clk.delay(movie.getRemainingTime())

        # return the start time
        return start_timestamp

    def _startMovie(self,movie,loops=0):
        # start playing the movie
        movie.play(loops)

        # add it to the playing movies
        self._playing_movies.append(movie)
        
        # call the callback once to get the starting time
        self._playingMovieCallback()
        return self.last_frame_timestamp

    def _playingMovieCallback(self):
        # loop over playing movies to see if we have a new frame
        hasNewFrame = False
        for mov in self._playing_movies:
            if mov.movie.get_busy():
                if mov.hasNewFrame():
                    hasNewFrame = True
            else:
                # we are done with this movie, so remove it from the
                # list and tell it to stop
                self.stopMovie(mov)

        # see if we need to refresh the screen
        if hasNewFrame:
            # see if restart logging
            if self.logall:
                self.logall = False
                restartLogging = True
            else:
                restartLogging = False
            self.last_frame_timestamp = self.updateScreen()
            if restartLogging:
                self.logall = True

    def stopMovie(self,movie,t=None):
        # make sure it was playing
        if movie in self._playing_movies:
            # stop the movie at the specified time
            (timeInterval,res) = timing.timedCall(t,
                                              movie.stop)

            # remove it from the playlist
            # not sure why it's sometimes not there to remove
            try:
                self._playing_movies.remove(movie)
            except:
                print self._playing_movies
                pass
            
            # see if remove callback
            if len(self._playing_movies) == 0:
                # none left to play, so stop callback
                hardware.removePollCallback(self._playingMovieCallback)

            self.logMessage("%s\t%s\t%s" % \
                            ("MOVIE_STOP",movie.filename,
                             movie.getRemainingTime()), timeInterval)

    def stopAllMovies(self):
        for movie in self._playing_movies:
            # stop the movie
            (timeInterval,res) = timing.timedCall(t,
                                              movie.stop)

            # remove it from the playlist
            self._playing_movies.remove(movie)

            # see if remove callback
            if len(self._playing_movies) == 0:
                # none left to play, so stop callback
                hardware.removePollCallback(self._playingMovieCallback)

            self.logMessage("%s\t%s\t%s" % \
                            ("MOVIE_STOP",movie.filename,
                             movie.getRemainingTime()), timeInterval)

        
    def pauseMovie(self,movie,t=None):
        # make sure it was playing
        if movie in self._playing_movies:
            # pause the movie
            (timeInterval,res) = timing.timedCall(t,
                                              movie.pause)

            # remove it from the playlist
            self._playing_movies.remove(movie)

            # see if remove callback
            if len(self._playing_movies) == 0:
                # none left to play, so stop callback
                hardware.removePollCallback(self._playingMovieCallback)

            self.logMessage("%s\t%s\t%s" % \
                            ("MOVIE_PAUSE",movie.filename,
                             movie.getRemainingTime()), timeInterval)