<s> '''<STR_LIT>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import sys <EOL> from pyglet . gl import * <EOL> import pyglet <EOL> from pyglet . window import key <EOL> def draw_rect ( x , y , width , height ) : <EOL> glBegin ( GL_LINE_LOOP ) <EOL> glVertex2f ( x , y ) <EOL> glVertex2f ( x + width , y ) <EOL> glVertex2f ( x + width , y + height ) <EOL> glVertex2f ( x , y + height ) <EOL> glEnd ( ) <EOL> class Control ( pyglet . event . EventDispatcher ) : <EOL> x = y = <NUM_LIT:0> <EOL> width = height = <NUM_LIT:10> <EOL> def __init__ ( self , parent ) : <EOL> super ( Control , self ) . __init__ ( ) <EOL> self . parent = parent <EOL> def hit_test ( self , x , y ) : <EOL> return ( self . x < x < self . x + self . width and <EOL> self . y < y < self . y + self . height ) <EOL> def capture_events ( self ) : <EOL> self . parent . push_handlers ( self ) <EOL> def release_events ( self ) : <EOL> self . parent . remove_handlers ( self ) <EOL> class Button ( Control ) : <EOL> charged = False <EOL> def draw ( self ) : <EOL> if self . charged : <EOL> glColor3f ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> draw_rect ( self . x , self . y , self . width , self . height ) <EOL> glColor3f ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> self . draw_label ( ) <EOL> def on_mouse_press ( self , x , y , button , modifiers ) : <EOL> self . capture_events ( ) <EOL> self . charged = True <EOL> def on_mouse_drag ( self , x , y , dx , dy , buttons , modifiers ) : <EOL> self . charged = self . hit_test ( x , y ) <EOL> def on_mouse_release ( self , x , y , button , modifiers ) : <EOL> self . release_events ( ) <EOL> if self . hit_test ( x , y ) : <EOL> self . dispatch_event ( '<STR_LIT>' ) <EOL> self . charged = False <EOL> Button . register_event_type ( '<STR_LIT>' ) <EOL> class TextButton ( Button ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( TextButton , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _text = pyglet . text . Label ( '<STR_LIT>' , anchor_x = '<STR_LIT>' , anchor_y = '<STR_LIT>' ) <EOL> def draw_label ( self ) : <EOL> self . _text . x = self . x + self . width / <NUM_LIT:2> <EOL> self . _text . y = self . y + self . height / <NUM_LIT:2> <EOL> self . _text . draw ( ) <EOL> def set_text ( self , text ) : <EOL> self . _text . text = text <EOL> text = property ( lambda self : self . _text . text , <EOL> set_text ) <EOL> class Slider ( Control ) : <EOL> THUMB_WIDTH = <NUM_LIT:6> <EOL> THUMB_HEIGHT = <NUM_LIT:10> <EOL> GROOVE_HEIGHT = <NUM_LIT:2> <EOL> def draw ( self ) : <EOL> center_y = self . y + self . height / <NUM_LIT:2> <EOL> draw_rect ( self . x , center_y - self . GROOVE_HEIGHT / <NUM_LIT:2> , <EOL> self . width , self . GROOVE_HEIGHT ) <EOL> pos = self . x + self . value * self . width / ( self . max - self . min ) <EOL> draw_rect ( pos - self . THUMB_WIDTH / <NUM_LIT:2> , center_y - self . THUMB_HEIGHT / <NUM_LIT:2> , <EOL> self . THUMB_WIDTH , self . THUMB_HEIGHT ) <EOL> def coordinate_to_value ( self , x ) : <EOL> return float ( x - self . x ) / self . width * ( self . max - self . min ) + self . min <EOL> def on_mouse_press ( self , x , y , button , modifiers ) : <EOL> value = self . coordinate_to_value ( x ) <EOL> self . capture_events ( ) <EOL> self . dispatch_event ( '<STR_LIT>' ) <EOL> self . dispatch_event ( '<STR_LIT>' , value ) <EOL> def on_mouse_drag ( self , x , y , dx , dy , buttons , modifiers ) : <EOL> value = min ( max ( self . coordinate_to_value ( x ) , self . min ) , self . max ) <EOL> self . dispatch_event ( '<STR_LIT>' , value ) <EOL> def on_mouse_release ( self , x , y , button , modifiers ) : <EOL> self . release_events ( ) <EOL> self . dispatch_event ( '<STR_LIT>' ) <EOL> Slider . register_event_type ( '<STR_LIT>' ) <EOL> Slider . register_event_type ( '<STR_LIT>' ) <EOL> Slider . register_event_type ( '<STR_LIT>' ) <EOL> class PlayerWindow ( pyglet . window . Window ) : <EOL> GUI_WIDTH = <NUM_LIT> <EOL> GUI_HEIGHT = <NUM_LIT> <EOL> GUI_PADDING = <NUM_LIT:4> <EOL> GUI_BUTTON_HEIGHT = <NUM_LIT:16> <EOL> def __init__ ( self , player ) : <EOL> super ( PlayerWindow , self ) . __init__ ( caption = '<STR_LIT>' , <EOL> visible = False , <EOL> resizable = True ) <EOL> self . player = player <EOL> self . player . push_handlers ( self ) <EOL> self . slider = Slider ( self ) <EOL> self . slider . x = self . GUI_PADDING <EOL> self . slider . y = self . GUI_PADDING * <NUM_LIT:2> + self . GUI_BUTTON_HEIGHT <EOL> self . slider . on_begin_scroll = lambda : player . pause ( ) <EOL> self . slider . on_end_scroll = lambda : player . play ( ) <EOL> self . slider . on_change = lambda value : player . seek ( value ) <EOL> self . play_pause_button = TextButton ( self ) <EOL> self . play_pause_button . x = self . GUI_PADDING <EOL> self . play_pause_button . y = self . GUI_PADDING <EOL> self . play_pause_button . height = self . GUI_BUTTON_HEIGHT <EOL> self . play_pause_button . width = <NUM_LIT> <EOL> self . play_pause_button . on_press = self . on_play_pause <EOL> win = self <EOL> self . window_button = TextButton ( self ) <EOL> self . window_button . x = self . play_pause_button . x + self . play_pause_button . width + self . GUI_PADDING <EOL> self . window_button . y = self . GUI_PADDING <EOL> self . window_button . height = self . GUI_BUTTON_HEIGHT <EOL> self . window_button . width = <NUM_LIT> <EOL> self . window_button . text = '<STR_LIT>' <EOL> self . window_button . on_press = lambda : win . set_fullscreen ( False ) <EOL> self . controls = [ <EOL> self . slider , <EOL> self . play_pause_button , <EOL> self . window_button , <EOL> ] <EOL> x = self . window_button . x + self . window_button . width + self . GUI_PADDING <EOL> i = <NUM_LIT:0> <EOL> for screen in self . display . get_screens ( ) : <EOL> screen_button = TextButton ( self ) <EOL> screen_button . x = x <EOL> screen_button . y = self . GUI_PADDING <EOL> screen_button . height = self . GUI_BUTTON_HEIGHT <EOL> screen_button . width = <NUM_LIT> <EOL> screen_button . text = '<STR_LIT>' % ( i + <NUM_LIT:1> ) <EOL> screen_button . on_press = ( lambda s : lambda : win . set_fullscreen ( True , screen = s ) ) ( screen ) <EOL> self . controls . append ( screen_button ) <EOL> i += <NUM_LIT:1> <EOL> x += screen_button . width + self . GUI_PADDING <EOL> def on_eos ( self ) : <EOL> self . gui_update_state ( ) <EOL> def gui_update_source ( self ) : <EOL> if self . player . source : <EOL> source = self . player . source <EOL> self . slider . min = <NUM_LIT:0.> <EOL> self . slider . max = source . duration <EOL> self . gui_update_state ( ) <EOL> def gui_update_state ( self ) : <EOL> if self . player . playing : <EOL> self . play_pause_button . text = '<STR_LIT>' <EOL> else : <EOL> self . play_pause_button . text = '<STR_LIT>' <EOL> def get_video_size ( self ) : <EOL> if not self . player . source or not self . player . source . video_format : <EOL> return <NUM_LIT:0> , <NUM_LIT:0> <EOL> video_format = self . player . source . video_format <EOL> width = video_format . width <EOL> height = video_format . height <EOL> if video_format . sample_aspect > <NUM_LIT:1> : <EOL> width *= video_format . sample_aspect <EOL> elif video_format . sample_aspect < <NUM_LIT:1> : <EOL> height /= video_format . sample_aspect <EOL> return width , height <EOL> def set_default_video_size ( self ) : <EOL> '''<STR_LIT>''' <EOL> width = self . GUI_WIDTH <EOL> height = self . GUI_HEIGHT <EOL> video_width , video_height = self . get_video_size ( ) <EOL> width = max ( width , video_width ) <EOL> height += video_height <EOL> self . set_size ( int ( width ) , int ( height ) ) <EOL> def on_resize ( self , width , height ) : <EOL> '''<STR_LIT>''' <EOL> super ( PlayerWindow , self ) . on_resize ( width , height ) <EOL> self . slider . width = width - self . GUI_PADDING * <NUM_LIT:2> <EOL> height -= self . GUI_HEIGHT <EOL> if height <= <NUM_LIT:0> : <EOL> return <EOL> video_width , video_height = self . get_video_size ( ) <EOL> if video_width == <NUM_LIT:0> or video_height == <NUM_LIT:0> : <EOL> return <EOL> display_aspect = width / float ( height ) <EOL> video_aspect = video_width / float ( video_height ) <EOL> if video_aspect > display_aspect : <EOL> self . video_width = width <EOL> self . video_height = width / video_aspect <EOL> else : <EOL> self . video_height = height <EOL> self . video_width = height * video_aspect <EOL> self . video_x = ( width - self . video_width ) / <NUM_LIT:2> <EOL> self . video_y = ( height - self . video_height ) / <NUM_LIT:2> + self . GUI_HEIGHT <EOL> def on_mouse_press ( self , x , y , button , modifiers ) : <EOL> for control in self . controls : <EOL> if control . hit_test ( x , y ) : <EOL> control . on_mouse_press ( x , y , button , modifiers ) <EOL> def on_key_press ( self , symbol , modifiers ) : <EOL> if symbol == key . SPACE : <EOL> self . on_play_pause ( ) <EOL> elif symbol == key . ESCAPE : <EOL> self . dispatch_event ( '<STR_LIT>' ) <EOL> def on_close ( self ) : <EOL> self . player . pause ( ) <EOL> self . close ( ) <EOL> def on_play_pause ( self ) : <EOL> if self . player . playing : <EOL> self . player . pause ( ) <EOL> else : <EOL> if self . player . time >= self . player . source . duration : <EOL> self . player . seek ( <NUM_LIT:0> ) <EOL> self . player . play ( ) <EOL> self . gui_update_state ( ) <EOL> def on_draw ( self ) : <EOL> self . clear ( ) <EOL> if self . player . source and self . player . source . video_format : <EOL> self . player . get_texture ( ) . blit ( self . video_x , <EOL> self . video_y , <EOL> width = self . video_width , <EOL> height = self . video_height ) <EOL> self . slider . value = self . player . time <EOL> for control in self . controls : <EOL> control . draw ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> have_video = False <EOL> for filename in sys . argv [ <NUM_LIT:1> : ] : <EOL> player = pyglet . media . Player ( ) <EOL> window = PlayerWindow ( player ) <EOL> source = pyglet . media . load ( filename ) <EOL> player . queue ( source ) <EOL> have_video = have_video or bool ( source . video_format ) <EOL> window . gui_update_source ( ) <EOL> window . set_default_video_size ( ) <EOL> window . set_visible ( True ) <EOL> player . play ( ) <EOL> window . gui_update_state ( ) <EOL> if not have_video : <EOL> pyglet . clock . schedule_interval ( lambda dt : None , <NUM_LIT> ) <EOL> pyglet . app . run ( ) </s>
