<s> import collections <EOL> import pyglet <EOL> import pyglet . gl as gl <EOL> import pyglet . window . key as pkey <EOL> import re , sys , time <EOL> from itertools import chain <EOL> import victor . mode as vmode <EOL> from victor . command_area import CommandArea <EOL> from victor . cursor import Cursor <EOL> from victor . keystroke import Keystrokes <EOL> from victor . movement_grid import MovementGrid ; <EOL> from victor . command import CommandException , register_ex_command , run_ex_command <EOL> from victor . path_group import PathGroup ; <EOL> from victor . path import Path ; <EOL> import victor . normal_dispatcher as vnd ; <EOL> class VIctorApp ( pyglet . window . Window ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( VIctorApp , self ) . __init__ ( <NUM_LIT> , <NUM_LIT> , caption = "<STR_LIT>" ) <EOL> self . set_default_options ( ) <EOL> self . mode = vmode . NORMAL <EOL> self . down_action = None <EOL> self . text_event = None <EOL> self . batch = pyglet . graphics . Batch ( ) <EOL> self . setup_cursor ( ) <EOL> self . marks = dict ( ) <EOL> self . command_area = CommandArea ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , self . batch ) <EOL> self . keystrokes = Keystrokes ( <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> , self . batch ) <EOL> self . current_multiplier = None <EOL> self . normal_dispatcher = vnd . construct_dispatcher ( self ) ; <EOL> self . set_ex_commands ( ) <EOL> self . groups = self . current_group = PathGroup ( ) ; <EOL> self . current_path = None ; <EOL> self . time = time . time ( ) <EOL> pyglet . clock . schedule_interval ( self . on_timer_fire , <NUM_LIT> ) <EOL> gl . glBlendFunc ( gl . GL_SRC_ALPHA , gl . GL_ONE_MINUS_SRC_ALPHA ) <EOL> gl . glEnable ( gl . GL_BLEND ) <EOL> def setup_cursor ( self ) : <EOL> self . cursor = Cursor ( <NUM_LIT> , <NUM_LIT:200> , self . batch ) <EOL> self . grid = MovementGrid ( <NUM_LIT> , <NUM_LIT> , self . options [ '<STR_LIT>' ] ) ; <EOL> self . grid . reset_batch ( ) ; <EOL> def set_ex_commands ( self ) : <EOL> register_ex_command ( '<STR_LIT>' , self . draw_line ) <EOL> register_ex_command ( '<STR_LIT>' , self . show_marks ) <EOL> register_ex_command ( '<STR_LIT>' , self . set_option ) <EOL> def set_default_options ( self ) : <EOL> self . options = { } <EOL> self . options [ "<STR_LIT>" ] = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) <EOL> self . options [ "<STR_LIT>" ] = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:50> ) <EOL> def set_mode ( self , mode ) : <EOL> if self . is_ex_mode ( ) : <EOL> self . command_area . unfocus ( ) <EOL> self . mode = mode <EOL> if mode == vmode . EX : <EOL> self . command_area . focus ( ) <EOL> def switch_to_ex_mode ( self ) : <EOL> self . set_mode ( vmode . EX ) <EOL> def run_command ( self ) : <EOL> try : run_ex_command ( self . command_area . text ) <EOL> except CommandException as e : sys . stderr . write ( '<STR_LIT>' % str ( e ) ) <EOL> self . set_mode ( vmode . NORMAL ) <EOL> def on_timer_fire ( self , dt ) : <EOL> self . time = time . time ( ) <EOL> self . normal_dispatcher . send ( vnd . NormalEvent ( vnd . TIMER_FIRE ) ) ; <EOL> def dispatch_both ( self ) : <EOL> if self . down_action is None : return <EOL> if self . text_event is None : return <EOL> self . down_action ( ) <EOL> self . down_action = self . text_event = None <EOL> def on_key_press ( self , symbol , modifiers ) : <EOL> is_mod_key = lambda key , mod : symbol == key and modifiers & mod <EOL> if self . is_ex_mode ( ) and symbol == pkey . ENTER : <EOL> self . run_command ( ) <EOL> elif symbol == pkey . ESCAPE or is_mod_key ( pkey . BRACKETLEFT , pkey . MOD_CTRL ) : <EOL> if not self . is_normal_mode ( ) : self . set_mode ( vmode . NORMAL ) <EOL> self . keystrokes . push_text ( "<STR_LIT>" ) <EOL> self . normal_dispatcher . send ( vnd . NormalEvent ( vnd . ESCAPE ) ) <EOL> return pyglet . event . EVENT_HANDLED <EOL> elif self . is_normal_mode ( ) : <EOL> self . normal_dispatcher . send ( vnd . NormalEvent ( vnd . ON_KEY_PRESS , symbol , modifiers ) ) ; <EOL> def on_key_release ( self , symbol , modifiers ) : <EOL> if self . is_normal_mode ( ) : <EOL> self . normal_dispatcher . send ( vnd . NormalEvent ( vnd . ON_KEY_RELEASE , symbol , modifiers ) ) ; <EOL> def on_text ( self , text ) : <EOL> if self . is_ex_mode ( ) : <EOL> self . command_area . on_text ( text ) <EOL> if not self . command_area . text : <EOL> self . set_mode ( vmode . NORMAL ) <EOL> elif self . is_normal_mode ( ) : <EOL> if text == '<STR_LIT::>' : <EOL> self . text_event = text <EOL> self . dispatch_both ( ) <EOL> self . keystrokes . push_text ( text ) <EOL> def on_text_motion ( self , motion ) : <EOL> if self . is_ex_mode ( ) : <EOL> self . command_area . on_text_motion ( motion ) <EOL> if not self . command_area . has_focus : <EOL> self . set_mode ( vmode . NORMAL ) <EOL> def current_position ( self ) : <EOL> return ( self . cursor . x , self . cursor . y ) <EOL> def start_path ( self ) : <EOL> self . current_path = Path ( self . cursor . position ) ; <EOL> self . paths . append ( self . current_path ) ; <EOL> def append_path ( self ) : <EOL> if self . current_path : <EOL> self . current_path . append ( self . cursor . position ) ; <EOL> def draw_line ( self , * args ) : <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> self . error ( "<STR_LIT>" , args ) <EOL> else : <EOL> start = self . marks [ args [ <NUM_LIT:0> ] ] <EOL> end = self . marks [ args [ <NUM_LIT:1> ] ] <EOL> self . batch . add ( <NUM_LIT:2> , pyglet . gl . GL_LINES , None , <EOL> ( '<STR_LIT>' , ( start [ <NUM_LIT:0> ] , start [ <NUM_LIT:1> ] , end [ <NUM_LIT:0> ] , end [ <NUM_LIT:1> ] ) ) , <EOL> ( '<STR_LIT>' , tuple ( chain ( self . options [ "<STR_LIT>" ] , self . options [ "<STR_LIT>" ] ) ) ) ) <EOL> def show_marks ( self , * args ) : <EOL> for key , value in self . marks . iteritems ( ) : <EOL> print key , value <EOL> def set_option ( self , * args ) : <EOL> if len ( args ) < <NUM_LIT:2> : raise CommandException ( "<STR_LIT>" ) <EOL> option = args [ <NUM_LIT:0> ] <EOL> if option == "<STR_LIT>" : <EOL> if len ( args ) != <NUM_LIT:5> : raise CommandException ( "<STR_LIT>" ) <EOL> self . options [ "<STR_LIT>" ] = tuple ( map ( int , args [ <NUM_LIT:1> : ] ) ) <EOL> elif option == "<STR_LIT>" : <EOL> pass <EOL> def error ( self , * args ) : <EOL> print args ; <EOL> def on_draw ( self ) : <EOL> pyglet . gl . glClearColor ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> self . clear ( ) <EOL> self . grid . draw ( ) ; <EOL> self . batch . draw ( ) <EOL> self . groups . draw ( ) ; <EOL> def is_normal_mode ( self ) : <EOL> return self . mode == vmode . NORMAL <EOL> def is_ex_mode ( self ) : <EOL> return self . mode == vmode . EX </s>
