# Display and run all patterns in Golly's Patterns folder.
# Author: Andrew Trevorrow (andrew@trevorrow.com), March 2006;
# v 1.1   modifications by Dave Greene (dvgrn@juno.com), 20 May 2006.

from glife import *
import golly as g

import os
from os.path import join
from time import time

global display_step
display_step=1
# remember initial hashing state so we can restore it if changed by a pattern file
inithash = g.getoption("hashing")

# ------------------------------------------------------------------------------

def readkey ():
   while True:
      ch = g.getkey()
      if len(ch) > 0: return ch

# ------------------------------------------------------------------------------

def handlekeys ():
  global display_step
  ch = g.getkey()
  chret="<reset>"
  if len(ch) > 0:
    if ch=="<":
      x, y = g.getpos()
      g.setpos(str(int(x.replace(",",""))-2**(4-g.getmag())),y)
    elif ch==">":
      x, y = g.getpos()
      g.setpos(str(int(x.replace(",",""))+2**(4-g.getmag())),y)
    elif ch=="=":
      x, y = g.getpos()
      g.setpos(x, str(int(y.replace(",",""))-2**(4-g.getmag())))
    elif ch=="?":
      x, y = g.getpos()
      g.setpos(x, str(int(y.replace(",",""))+2**(4-g.getmag())))
    elif ch=="f":
      g.fit()
    elif ch=="+":
      display_step *= 2
    elif ch=="-":
      if display_step != 1:
        display_step /= 2
    elif ch=="*":
      g.setmag( g.getmag() + 1 )
    elif ch=="/":
      g.setmag( g.getmag() - 1 )
    else:
    	chret=ch
    return chret

# ------------------------------------------------------------------------------

def slideshowmov ():
   secs_to_display=2
   secs_to_run=30
   for root, dirs, files in os.walk(g.appdir() + "Patterns"):
      for name in files:
         # ignore hidden files (like .DS_Store on Mac)
         if not name.startswith("."):
            fullname = join(root, name)
            g.open(fullname, False)       # don't add file to Open Recent submenu
            
            # dramatic pause before simulation begins
	    g.update()
	    g.show("space bar continues, arrow keys and / * - + f control pattern, q quits.")
            ticks=0
            time_to_switch = secs_to_run + time()
	    oldsecs = time()
	    while True:
	      newsecs = time()
              ch=handlekeys()
              if ch=="q" : return
              # if ch==" " :
                # time_to_switch-=secs_to_run # skip to next pattern immediately
              if newsecs - oldsecs >= secs_to_display or ch==" ":
                break
              # g.show(str(int(newsecs-oldsecs)))

            while True:
              g.run(1) # TODO:  convert this to run(display_step) if no other events possible
              ticks+=1
              ch=handlekeys()
              if ch=="q": return
              if ch=="<reset>" or ticks==display_step :
                g.update() # only redraw the display every display_step ticks
                g.show("space bar continues, arrow keys and / * - + f control pattern, q quits. " + str(int(time_to_switch - time())) + " seconds left.")
                ticks=0
              if ch=="<reset>":
                time_to_switch=secs_to_run + time()
              if ch==" " or time()>time_to_switch:
                g.new("")
                if inithash != g.getoption("hashing"):
                  if inithash:
                    # turn on hashing (B0-not-S8 rule turned it off)
                    g.setrule("b3/s23")
                    g.setoption("hashing", True)
                  else:
                    # turn off hashing (.mc file turned it on)
                    g.setoption("hashing", False)
                break # out of while loop, go on to next pattern
      
      if "CVS" in dirs:
         dirs.remove("CVS")  # don't visit CVS directories

# ------------------------------------------------------------------------------

# show status bar so user sees messages
initstatus = g.setoption("showstatusbar", True)

# hide other stuff to maximize the viewport
# (don't switch to fullscreen because user won't see pattern name in title)
inittoolbar = g.setoption("showtoolbar", False)
initscripts = g.setoption("showscripts", False)
initpatterns = g.setoption("showpatterns", False)

slideshowmov()
g.show("")

# restore original state
g.setoption("showstatusbar", initstatus)
g.setoption("showtoolbar", inittoolbar)
g.setoption("showscripts", initscripts)
g.setoption("showpatterns", initpatterns)


