Source code for peng3d.gui

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  __init__.py
#  
#  Copyright 2016 notna <notna@apparat.org>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

__all__ = ["GUIMenu","SubMenu","GUILayer"]

try:
    import pyglet
    from pyglet.gl import *
except ImportError:
    pass # Headless mode

from ..menu import Menu
from ..layer import Layer,Layer2D
from .widgets import *
from .button import *
from .slider import *
from .text import *
from .container import *

[docs]class GUIMenu(Menu): """ :py:class:`peng3d.menu.Menu` subclass adding 2D GUI Support. Note that widgets are not managed directly by this class, but rather by each :py:class:`SubMenu`\ . """ def __init__(self,name,window,peng): super(GUIMenu,self).__init__(name,window,peng) pyglet.clock.schedule_interval(lambda dt: None,1./30) self.submenus = {} self.activeSubMenu = None
[docs] def addSubMenu(self,submenu): """ Adds a :py:class:`SubMenu` to this Menu. Note that nothing will be displayed unless a submenu is activated. """ self.submenus[submenu.name] = submenu
[docs] def changeSubMenu(self,submenu): """ Changes the submenu that is displayed. :raises ValueError: if the name was not previously registered """ if submenu not in self.submenus: raise ValueError("Submenu %s does not exist!"%submenu) elif submenu == self.activeSubMenu: return # Ignore double submenu activation to prevent bugs in submenu initializer old = self.activeSubMenu self.activeSubMenu = submenu if old is not None: self.submenus[old].on_exit(submenu) self.submenu.on_enter(old)
[docs] def draw(self): """ Draws each menu layer and the active submenu. Note that the layers are drawn first and may be overridden by the submenu and widgets. """ super(GUIMenu,self).draw() self.submenu.draw()
@property def submenu(self): """ Property containing the :py:class:`SubMenu` instance that is currently active. """ return self.submenus[self.activeSubMenu]
[docs]class GUILayer(GUIMenu,Layer2D): """ Hybrid of :py:class:`GUIMenu` and :py:class:`peng3d.layer.Layer2D`\ . This class allows you to create Head-Up Displays and other overlays easily. """ # TODO: add safety recursion breaker if this is added as a layer to itself def __init__(self,name,menu,window,peng): Layer2D.__init__(self,menu,window,peng) GUIMenu.__init__(self,menu.name,window,peng)
[docs] def draw(self): """ Draws the Menu. """ self.predraw() GUIMenu.draw(self) self.postdraw()