# Copyright (c) 2008-2009 Mikael Lind
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import pyglet, sys
from pyglet.gl import *
from void.title_screen import TitleScreen

class VoidWindow(pyglet.window.Window):
    def __init__(self):
        pyglet.window.Window.__init__(self, fullscreen=True, caption="Void")
        self.set_mouse_visible(False)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        self.screens = [TitleScreen(self)]
        pyglet.clock.schedule_interval(self.step, 1.0 / 60.0)

    def push_screen(self, screen):
        self.screens.append(screen)

    def pop_screen(self):
        self.screens.pop()
        if not self.screens:
            self.on_close()

    def step(self, dt):
        if self.screens:
            self.screens[-1].step(dt)
        
    def on_draw(self):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        self.clear()
        if self.screens:
            self.screens[-1].on_draw()

    def on_close(self):
        pyglet.clock.unschedule(self.step)
        self.close()

    def on_key_press(self, symbol, modifiers):
        if self.screens:
            self.screens[-1].on_key_press(symbol, modifiers)

    def on_key_release(self, symbol, modifiers):
        if self.screens:
            self.screens[-1].on_key_release(symbol, modifiers)
