#!/usr/bin/python
from pyglet.gl import glClear,GLfloat as GLF,glColor3fv,glVertex2f,glBegin,glEnd,glRectf
from pyglet.clock import set_fps_limit as a,tick
a(60)
from pyglet.window import Window as W
from os import listdir
LvL=iter(sorted(x for x in listdir(".") if x.endswith(".txt")))
W=W(fullscreen=1)
W.maximize()
GLF=(GLF*3)(1,1,1),(GLF*3)(.8,.8,.8),(GLF*3)(.7,.7,.8)
W.event("on_mouse_motion")(lambda x,y,z1,z2,g=globals().__setitem__:(g("Mx",x-Wi>>5),g("My",Hi-y>>5)))
W.event("on_mouse_press")(lambda*x:globals().__setitem__("Li",-len(Lv)))
def makeLv():
	global Lv,Wi,Hi,Wil,Hil
	Lv=[Lv.split("\n") for Lv in open(next(LvL)).read().rstrip("\n").split("\n\n")]
	Wil=max(max(len(Lv) for Lv in Lv) for Lv in Lv)
	Hil=max(len(Lv) for Lv in Lv)
	Lv=[[[b not in " ." for b in a]+[False]*(Wil-len(a)) for a in Lv]+[[True]*Wil]*(Hil-len(Lv)) for Lv in Lv]
	Wi=W.width-(Wil<<5)>>1
	Hi=W.height+(Hil+2<<5)>>1
	reLv()
def reLv():
	global Li,Lf
	Li=-1
	Lf=False
	glClear(16384)
	W.flip()
makeLv()
li=60
while not W.has_exit:
	li-=not W.dispatch_events()
	if not li:
		li=60
		glClear(16384)
		Li+=1
		if Li==len(Lv):
			if Lf:reLv()
			else:makeLv()
			continue
		glColor3fv(GLF[2 if Lf else Li>0])
		glBegin(2)
		glVertex2f(Wi,Hi)
		glVertex2f(Wi+(Wil<<5),Hi)
		glVertex2f(Wi+(Wil<<5),Hi-(Hil<<5))
		glVertex2f(Wi,Hi-(Hil<<5))
		glEnd()
		for b,x in enumerate(Lv[Li]):
			b<<=5
			for a,x in enumerate(x):
				if x:
					a<<=5
					glRectf(Wi+a,Hi-b-32,Wi+a+32,Hi-b)
		W.flip()
	tick()
	if not(0<=My<Hil and 0<=Mx<Wil):Lf=True
	elif Lf or Lv[Li][My][Mx]:reLv()