<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , unicode_literals <EOL> __docformat__ = '<STR_LIT>' <EOL> import sys <EOL> from os import getenv <EOL> import warnings <EOL> import pyglet <EOL> from pyglet import window , event <EOL> from pyglet import gl <EOL> import pyglet . clock <EOL> import cocos <EOL> import cocos . audio <EOL> import cocos . custom_clocks <EOL> import cocos . fps <EOL> if hasattr ( sys , '<STR_LIT>' ) and sys . is_cocos_sphinx : <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class DefaultHandler ( object ) : <EOL> def __init__ ( self ) : <EOL> super ( DefaultHandler , self ) . __init__ ( ) <EOL> self . wired = False <EOL> def on_key_press ( self , symbol , modifiers ) : <EOL> if symbol == pyglet . window . key . F and ( modifiers & pyglet . window . key . MOD_ACCEL ) : <EOL> director . window . set_fullscreen ( not director . window . fullscreen ) <EOL> return True <EOL> elif symbol == pyglet . window . key . P and ( modifiers & pyglet . window . key . MOD_ACCEL ) : <EOL> import cocos . scenes . pause as pause <EOL> pause_sc = pause . get_pause_scene ( ) <EOL> if pause : <EOL> director . push ( pause_sc ) <EOL> return True <EOL> elif symbol == pyglet . window . key . W and ( modifiers & pyglet . window . key . MOD_ACCEL ) : <EOL> if not self . wired : <EOL> gl . glDisable ( gl . GL_TEXTURE_2D ) <EOL> gl . glPolygonMode ( gl . GL_FRONT , gl . GL_LINE ) <EOL> gl . glPolygonMode ( gl . GL_BACK , gl . GL_LINE ) <EOL> self . wired = True <EOL> else : <EOL> gl . glEnable ( gl . GL_TEXTURE_2D ) <EOL> gl . glPolygonMode ( gl . GL_FRONT , gl . GL_FILL ) <EOL> gl . glPolygonMode ( gl . GL_BACK , gl . GL_FILL ) <EOL> self . wired = False <EOL> return True <EOL> elif symbol == pyglet . window . key . X and ( modifiers & pyglet . window . key . MOD_ACCEL ) : <EOL> director . show_FPS = not director . show_FPS <EOL> return True <EOL> elif symbol == pyglet . window . key . I and ( modifiers & pyglet . window . key . MOD_ACCEL ) : <EOL> from . layer import PythonInterpreterLayer <EOL> if not director . show_interpreter : <EOL> if director . python_interpreter is None : <EOL> director . python_interpreter = cocos . scene . Scene ( PythonInterpreterLayer ( ) ) <EOL> director . python_interpreter . enable_handlers ( True ) <EOL> director . python_interpreter . on_enter ( ) <EOL> director . show_interpreter = True <EOL> else : <EOL> director . python_interpreter . on_exit ( ) <EOL> director . show_interpreter = False <EOL> return True <EOL> elif symbol == pyglet . window . key . S and ( modifiers & pyglet . window . key . MOD_ACCEL ) : <EOL> import time <EOL> pyglet . image . get_buffer_manager ( ) . get_color_buffer ( ) . save ( '<STR_LIT>' % ( int ( time . time ( ) ) ) ) <EOL> return True <EOL> if symbol == pyglet . window . key . ESCAPE : <EOL> director . pop ( ) <EOL> return True <EOL> class Director ( event . EventDispatcher ) : <EOL> """<STR_LIT>""" <EOL> interpreter_locals = { } <EOL> def init ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . fps_display_provider = cocos . fps . get_default_fpsdisplay <EOL> self . show_FPS = False <EOL> self . scene_stack = [ ] <EOL> self . scene = None <EOL> self . next_scene = None <EOL> self . python_interpreter = None <EOL> self . show_interpreter = False <EOL> self . terminate_app = False <EOL> if '<STR_LIT>' in kwargs : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in kwargs : <EOL> warnings . warn ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> self . autoscale = kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> self . autoscale = not kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> self . autoscale = kwargs . pop ( '<STR_LIT>' , True ) <EOL> def _get_do_not_scale_window ( ) : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> return not self . autoscale <EOL> def _set_do_not_scale_window ( v ) : <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> self . autoscale = not v <EOL> do_not_scale_window = property ( _get_do_not_scale_window , _set_do_not_scale_window ) <EOL> audio_backend = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> audio_settings = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> self . _window_virtual_width = kwargs . get ( '<STR_LIT:width>' , None ) <EOL> self . _window_virtual_height = kwargs . get ( '<STR_LIT>' , None ) <EOL> if pyglet . version . startswith ( '<STR_LIT>' ) and kwargs . get ( '<STR_LIT>' , False ) : <EOL> kwargs . pop ( '<STR_LIT:width>' , <NUM_LIT:0> ) <EOL> kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . window = window . Window ( * args , ** kwargs ) <EOL> if self . _window_virtual_width is None : <EOL> self . _window_virtual_width = self . window . width <EOL> if self . _window_virtual_height is None : <EOL> self . _window_virtual_height = self . window . height <EOL> self . _window_virtual_aspect = ( <EOL> self . _window_virtual_width / float ( self . _window_virtual_height ) ) <EOL> self . _offset_x = <NUM_LIT:0> <EOL> self . _offset_y = <NUM_LIT:0> <EOL> if self . autoscale : <EOL> resize_handler = self . scaled_resize_window <EOL> self . set_projection = self . set_projection3D <EOL> else : <EOL> resize_handler = self . unscaled_resize_window <EOL> self . set_projection = self . set_projection2D <EOL> self . _resize_no_events = True <EOL> resize_handler ( self . window . width , self . window . height ) <EOL> self . _resize_no_events = False <EOL> self . window . push_handlers ( on_resize = resize_handler ) <EOL> self . window . push_handlers ( self . on_draw ) <EOL> self . set_alpha_blending ( ) <EOL> self . window . push_handlers ( DefaultHandler ( ) ) <EOL> if getenv ( '<STR_LIT>' , None ) == '<STR_LIT:1>' or audio_backend == '<STR_LIT>' : <EOL> audio_settings = None <EOL> if not cocos . audio . _working and audio_settings is not None : <EOL> from cocos . audio . exceptions import NoAudioError <EOL> msg = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> raise NoAudioError ( msg ) <EOL> import os <EOL> if not os . environ . get ( '<STR_LIT>' , False ) : <EOL> cocos . audio . initialize ( audio_settings ) <EOL> return self . window <EOL> fps_display = None <EOL> def set_show_FPS ( self , value ) : <EOL> if value and self . fps_display is None : <EOL> self . fps_display = self . fps_display_provider ( ) <EOL> self . fps_display . init ( ) <EOL> elif not value and self . fps_display : <EOL> self . fps_display . terminate ( ) <EOL> self . fps_display = None <EOL> show_FPS = property ( lambda self : self . fps_display is not None , set_show_FPS ) <EOL> def run ( self , scene ) : <EOL> """<STR_LIT>""" <EOL> self . _set_scene ( scene ) <EOL> pyglet . clock . schedule_interval ( lambda dt : None , <NUM_LIT:0.1> ) <EOL> event_loop . run ( ) <EOL> def set_recorder ( self , framerate , template = "<STR_LIT>" , duration = None ) : <EOL> """<STR_LIT>""" <EOL> clock = cocos . custom_clocks . get_recorder_clock ( framerate , template , duration ) <EOL> cocos . custom_clocks . set_app_clock ( clock ) <EOL> def on_draw ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . window . width == <NUM_LIT:0> or self . window . height == <NUM_LIT:0> ) and not self . terminate_app : <EOL> return <EOL> if self . terminate_app : <EOL> self . next_scene = None <EOL> if self . next_scene is not None or self . terminate_app : <EOL> self . _set_scene ( self . next_scene ) <EOL> if self . terminate_app : <EOL> pyglet . app . exit ( ) <EOL> return <EOL> self . window . clear ( ) <EOL> gl . glPushMatrix ( ) <EOL> self . scene . visit ( ) <EOL> gl . glPopMatrix ( ) <EOL> if self . show_FPS : <EOL> self . fps_display . tick ( ) <EOL> self . fps_display . draw ( ) <EOL> if self . show_interpreter : <EOL> self . python_interpreter . visit ( ) <EOL> def push ( self , scene ) : <EOL> """<STR_LIT>""" <EOL> self . dispatch_event ( "<STR_LIT>" , scene ) <EOL> def on_push ( self , scene ) : <EOL> self . next_scene = scene <EOL> self . scene_stack . append ( self . scene ) <EOL> def pop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . dispatch_event ( "<STR_LIT>" ) <EOL> def on_pop ( self ) : <EOL> if len ( self . scene_stack ) == <NUM_LIT:0> : <EOL> self . terminate_app = True <EOL> else : <EOL> self . next_scene = self . scene_stack . pop ( ) <EOL> def replace ( self , scene ) : <EOL> """<STR_LIT>""" <EOL> self . next_scene = scene <EOL> def _set_scene ( self , scene ) : <EOL> """<STR_LIT>""" <EOL> self . next_scene = None <EOL> if self . scene is not None : <EOL> self . scene . on_exit ( ) <EOL> self . scene . enable_handlers ( False ) <EOL> old = self . scene <EOL> self . scene = scene <EOL> if self . scene is not None : <EOL> self . scene . parent = None <EOL> self . scene . enable_handlers ( True ) <EOL> scene . on_enter ( ) <EOL> return old <EOL> def get_window_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _window_virtual_width , self . _window_virtual_height <EOL> def get_virtual_coordinates ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> x_diff = self . _window_virtual_width / float ( self . window . width - self . _offset_x * <NUM_LIT:2> ) <EOL> y_diff = self . _window_virtual_height / float ( self . window . height - self . _offset_y * <NUM_LIT:2> ) <EOL> adjust_x = ( self . window . width * x_diff - self . _window_virtual_width ) / <NUM_LIT:2> <EOL> adjust_y = ( self . window . height * y_diff - self . _window_virtual_height ) / <NUM_LIT:2> <EOL> return int ( x_diff * x ) - adjust_x , int ( y_diff * y ) - adjust_y <EOL> def scaled_resize_window ( self , width , height ) : <EOL> """<STR_LIT>""" <EOL> pw , ph = width , height <EOL> vw , vh = self . get_window_size ( ) <EOL> v_ar = vw / float ( vh ) <EOL> uw = int ( min ( pw , ph * v_ar ) ) <EOL> uh = int ( min ( ph , pw / v_ar ) ) <EOL> ox = ( pw - uw ) // <NUM_LIT:2> <EOL> oy = ( ph - uh ) // <NUM_LIT:2> <EOL> self . _offset_x = ox <EOL> self . _offset_y = oy <EOL> self . _usable_width = uw <EOL> self . _usable_height = uh <EOL> self . set_projection ( ) <EOL> if self . _resize_no_events : <EOL> return <EOL> self . dispatch_event ( "<STR_LIT>" , width , height ) <EOL> self . dispatch_event ( "<STR_LIT>" , self . _usable_width , self . _usable_height ) <EOL> return pyglet . event . EVENT_HANDLED <EOL> def unscaled_resize_window ( self , width , height ) : <EOL> """<STR_LIT>""" <EOL> self . _usable_width = width <EOL> self . _usable_height = height <EOL> if self . _resize_no_events : <EOL> return <EOL> self . dispatch_event ( "<STR_LIT>" , width , height ) <EOL> self . dispatch_event ( "<STR_LIT>" , self . _usable_width , self . _usable_height ) <EOL> def set_projection ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_projection3D ( self ) : <EOL> """<STR_LIT>""" <EOL> vw , vh = self . get_window_size ( ) <EOL> gl . glViewport ( self . _offset_x , self . _offset_y , self . _usable_width , self . _usable_height ) <EOL> gl . glMatrixMode ( gl . GL_PROJECTION ) <EOL> gl . glLoadIdentity ( ) <EOL> gl . gluPerspective ( <NUM_LIT> , self . _usable_width / float ( self . _usable_height ) , <NUM_LIT:0.1> , <NUM_LIT> ) <EOL> gl . glMatrixMode ( gl . GL_MODELVIEW ) <EOL> gl . glLoadIdentity ( ) <EOL> gl . gluLookAt ( vw / <NUM_LIT> , vh / <NUM_LIT> , vh / <NUM_LIT> , <EOL> vw / <NUM_LIT> , vh / <NUM_LIT> , <NUM_LIT:0> , <EOL> <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> <EOL> ) <EOL> def set_projection2D ( self ) : <EOL> """<STR_LIT>""" <EOL> self . window . on_resize ( self . _usable_width , self . _usable_height ) <EOL> def set_alpha_blending ( self , on = True ) : <EOL> """<STR_LIT>""" <EOL> if on : <EOL> gl . glEnable ( gl . GL_BLEND ) <EOL> gl . glBlendFunc ( gl . GL_SRC_ALPHA , gl . GL_ONE_MINUS_SRC_ALPHA ) <EOL> else : <EOL> gl . glDisable ( gl . GL_BLEND ) <EOL> def set_depth_test ( self , on = True ) : <EOL> """<STR_LIT>""" <EOL> if on : <EOL> gl . glClearDepth ( <NUM_LIT:1.0> ) <EOL> gl . glEnable ( gl . GL_DEPTH_TEST ) <EOL> gl . glDepthFunc ( gl . GL_LEQUAL ) <EOL> gl . glHint ( gl . GL_PERSPECTIVE_CORRECTION_HINT , gl . GL_NICEST ) <EOL> else : <EOL> gl . glDisable ( gl . GL_DEPTH_TEST ) <EOL> event_loop = pyglet . app . event_loop <EOL> if not hasattr ( event_loop , "<STR_LIT>" ) : <EOL> event_loop = pyglet . app . EventLoop ( ) <EOL> director = Director ( ) <EOL> director . event = event_loop . event <EOL> """<STR_LIT>""" <EOL> director . interpreter_locals [ "<STR_LIT>" ] = director <EOL> director . interpreter_locals [ "<STR_LIT>" ] = cocos <EOL> Director . register_event_type ( '<STR_LIT>' ) <EOL> Director . register_event_type ( '<STR_LIT>' ) <EOL> Director . register_event_type ( '<STR_LIT>' ) <EOL> Director . register_event_type ( '<STR_LIT>' ) </s>
