Source

fileadelphia / src / fileadelphia / states.py

Full commit
from OpenGL.GL import *
from OpenGL.GLU import *

import pygame
from pygame.locals import *
import time
import os
import sys
import subprocess

from fileadelphia.const import VIEW_WIDTH, VIEW_HEIGHT
from fileadelphia.text import Text

class State(object):
    def __init__(self):
        pass

    def display(self):
        pass

    def update(self, world, delta):
        pass

    def handle_keypress(self, event, x, y, world):
        pass

    def handle_mouse(self, button, state, x, y):
        pass

    def __str__(self):
        return type(self)

class StartState(State):
    
    density = 0.1
    fogColor = (1.0, 1.0, 1.0, 0.2)

    def __init__(self):
        State.__init__(self)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_FOG)
        glFogi(GL_FOG_MODE, GL_EXP2)
        glFogfv(GL_FOG_COLOR, self.fogColor)
        glFogf(GL_FOG_DENSITY, self.density)
        glHint(GL_FOG_HINT, GL_NICEST) #NOTE: may cause slowdown
        self.title = Text('Fileadelphia', 20, (255, 0, 0), font_name=pygame.font.match_font('couriernew', True))
        self.text = Text('A metaphysical microcosm\nNikhil Marathe and Shilpan Bhagat', 15, font_name=pygame.font.match_font('georgia'))

    def display(self):
        glClearColor(1.0, 1, 1, 1.0)
        glPushMatrix()
        glLoadIdentity()
        glColor3f(1, 0, 0)
        glBegin(GL_LINES)
        glVertex3f(0, 0, -10)
        glVertex3f(0, 0, 10)
        glVertex3f(0, 1, -10)
        glVertex3f(0, 1, 10)
        glVertex3f(1, 1, -10)
        glVertex3f(1, 1, 10)
        #glVertex3f(0, 3, -10)
        #glVertex3f(0, 3, 10)
        #glVertex3f(1, 4, -10)
        #glVertex3f(1, 4, 10)
        #glVertex3f(-1, 4, -10)
        #glVertex3f(-1, 4, 10)
        #glVertex3f(-2, 4, -10)
        #glVertex3f(-2, 4, 10)
        glEnd()
        glPopMatrix()

        glViewport(0, 0, VIEW_WIDTH, VIEW_HEIGHT)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluOrtho2D(0, VIEW_WIDTH, 0, VIEW_HEIGHT)
        glScalef(1, -1, 1)
        glTranslatef(0, -VIEW_HEIGHT, 0)

        
        glMatrixMode(GL_MODELVIEW)
        glClear(GL_COLOR_BUFFER_BIT)

        self.title.render(140, 140)
        self.text.render(140, 170)
        glFlush()

        glViewport(0, 0, VIEW_WIDTH, VIEW_HEIGHT)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(90, VIEW_WIDTH*1.0/VIEW_HEIGHT, 1.0, 20.0)
        glTranslatef(4, -VIEW_HEIGHT, 5)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

class FileInfoState(State):
    def __init__(self, path):
        self.file = path
        self.info = os.stat(path)
        self.text = Text("""
        Name: %s
        Last Modified: %s
        Size: %s
        """%(self.file,time.ctime(self.info.st_mtime),self.byte_measure(self.info.st_size)), 20, color=(255, 0, 0))

        self.btn_text = Text("Launch!", 20, color =(255,255,255), bg=(255,0,0))
        self.button = pygame.rect.Rect(100,150,100,50)

       
    def handle_mouse(self,button,state,x, y):
        if self.button.collidepoint(x, y):
            if sys.platform == 'win32':
                os.startfile(self.file)
            else:
                # try xdg-open
                subprocess.Popen(["xdg-open", self.file])
            return True
        return False

    def handle_keypress(self, event, x, y, world):
        if event.key == K_RETURN or event.key == K_SPACE:
            world.pop_state()
        if event.key == K_UP and pygame.key.get_mods() & KMOD_ALT:
            world.pop_state()
        return True # always return True to disable camera motion
                    
    def display(self):

        glClearColor(1, 1, 1, 1)
        glDisable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        gluOrtho2D(0, VIEW_WIDTH, VIEW_HEIGHT, 0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.text.render(40, 40)
        glColor3f(1, 0, 0)
        glBegin(GL_QUADS)
        glVertex2f(self.button.left, self.button.top)
        glVertex2f(self.button.right, self.button.top)
        glVertex2f(self.button.right, self.button.bottom)
        glVertex2f(self.button.left, self.button.bottom)
        glEnd()
        self.btn_text.render(self.button.left+10, self.button.top+35)

        glEnable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)


        """
        if sys.platform == 'win32' :
            os.startfile(self.file)
        else :
        """    

    def byte_measure(self, byte_size):
        if byte_size < 102400:
            return "%d bytes"%byte_size
        else :
            return "%f MB"%(byte_size/(1024.0*1024))
    
class DirectoryInfoState(State):
    
    def __init__(self, path):
        self.directory = path
        self.info = os.stat(path)
        self.text = Text("""
        Folder: %s
        Last Modified: %s
        """%(
        path,
        time.ctime(self.info.st_mtime)
        ), 20, color=(255, 0, 0))

    def display(self):
        glClearColor(1, 1, 1, 1)
        glDisable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        gluOrtho2D(0, VIEW_WIDTH, VIEW_HEIGHT, 0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.text.render(40, 40)

        glEnable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)

    def handle_keypress(self, event, x, y, world):
        return True # always return True to disable camera motion

class PermissionDeniedState(State):

    def __init__(self, path):
        self.directory = path
        self.text = Text("""
        Permission Denied!
        %s
        """%(
        path
        ), 20, color=(255, 0, 0), bg=(0, 0, 0))

    def display(self):
        glClearColor(0, 0, 0, 0)
        glDisable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        gluOrtho2D(0, VIEW_WIDTH, VIEW_HEIGHT, 0)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        self.text.render(40, 40)

        glEnable(GL_DEPTH_TEST)
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)

    def handle_keypress(self, event, x, y, world):
        if event.key == K_RETURN or event.key == K_SPACE:
            world.pop_state()
        return True

class TransitionState(State):
    
    def __init__(self, src, dst, time=1000, steps=10, after=200):
        self.blend_alpha = 0.0
        self.src = src
        self.dst = dst
        self.transition_time = time
        self.elapsed_time = 0
        self.interval = time/steps
        self.after = 200
        self.transition_done = False
        print 'self.interval', self.interval
        self.accumulator = 0
        glEnable(GL_BLEND)

    def display(self):
        if not self.transition_done:
            glClearColor(1, 1, 1, 1)
            glBlendColor(self.blend_alpha, self.blend_alpha, self.blend_alpha, self.blend_alpha)
            glBlendFunc(GL_ONE, GL_CONSTANT_ALPHA)
            self.src.display()
            glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA)
        self.dst.display()
        glFlush()

    def update(self, world, delta):
        if not self.transition_done:
            self.src.update(world, delta)
            self.dst.update(world, delta)
            self.accumulator += delta
            self.elapsed_time += delta
            if self.accumulator > self.interval:
                self.accumulator = 0
                self.blend_alpha += 0.1
                #print 'incr alpha', self.blend_alpha
                if self.blend_alpha > 1.0:
                    self.blend_alpha = 1.0
            if self.elapsed_time > self.transition_time:
                self.transition_done = True
        else:
            self.accumulator += delta
            if self.accumulator > self.after:
                glBlendColor(1, 1, 1, 1)
                glDisable(GL_BLEND)
                glDisable(GL_FOG)
                world.switch_state(self.dst)
                return
            self.dst.update(world, delta)

    def handle_keypress(self, event, x, y, world):
        # don't allow any interaction while transitioning
        return True
        self.src.handle_keypress(event, x, y, world)
        self.dst.handle_keypress(event, x, y, world)

    def handle_mouse(self, button, state, x, y):
        # don't allow any interaction while transitioning
        return True
        self.src.handle_mouse(button, state, x, y)
        self.dst.handle_mouse(button, state, x, y)

    def __str__(self):
        return 'TransitionState: %s -> %s'%(self.src, self.dst)