<s> import Tkinter <EOL> from color import ColorGenerator <EOL> from itertools import izip <EOL> from tilemap import * <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class TileMapGUI ( object ) : <EOL> def __init__ ( self , tile_map , tile_size , tile_colors , rooms = None , walk_graph = None , tk = None ) : <EOL> self . tk = tk or root_tk <EOL> self . tk . title ( "<STR_LIT>" ) <EOL> self . tile_size_x = tile_size <EOL> self . tile_size_y = tile_size <EOL> self . tile_colors = dict ( tile_colors ) <EOL> self . width = self . tile_size_x * tile_map . width <EOL> self . height = self . tile_size_y * tile_map . height <EOL> self . view_width = <NUM_LIT> <EOL> self . view_height = <NUM_LIT> <EOL> self . canvas = Tkinter . Canvas ( self . tk , <EOL> width = self . view_width , <EOL> height = self . view_height , <EOL> bg = '<STR_LIT>' , <EOL> scrollregion = ( <NUM_LIT:0> , <NUM_LIT:0> , self . width , self . height ) , <EOL> xscrollincrement = <NUM_LIT:5> , <EOL> yscrollincrement = <NUM_LIT:5> ) <EOL> self . canvas . pack ( ) <EOL> self . canvas . bind ( '<STR_LIT>' , self . click ) <EOL> self . canvas . bind ( '<STR_LIT>' , self . drag ) <EOL> self . canvas . bind ( '<STR_LIT>' , self . scroll ) <EOL> self . canvas . bind ( '<STR_LIT>' , self . keypress ) <EOL> self . create_tile_map ( tile_map ) <EOL> if rooms : <EOL> self . create_rooms ( rooms ) <EOL> if walk_graph : <EOL> self . create_walk_graph ( walk_graph ) <EOL> self . create_grid ( self . tile_size_x , self . tile_size_y ) <EOL> self . canvas . focus_set ( ) <EOL> def create_tile_map ( self , tile_map ) : <EOL> self . tile_map = tile_map . copy ( ) <EOL> self . tile_objects = [ ] <EOL> for y in range ( self . tile_map . height ) : <EOL> row = [ ] <EOL> for x in range ( self . tile_map . width ) : <EOL> tile = self . canvas . create_rectangle ( <EOL> x * self . tile_size_x , <EOL> y * self . tile_size_y , <EOL> ( x + <NUM_LIT:1> ) * self . tile_size_x , <EOL> ( y + <NUM_LIT:1> ) * self . tile_size_y , <EOL> fill = '<STR_LIT>' , <EOL> outline = '<STR_LIT>' , <EOL> tags = '<STR_LIT>' ) <EOL> row . append ( tile ) <EOL> self . tile_objects . append ( row ) <EOL> self . update_tile_map ( ) <EOL> def update_tile_map ( self ) : <EOL> default_color = self . tile_colors [ None ] <EOL> for y in range ( self . tile_map . height ) : <EOL> for x in range ( self . tile_map . width ) : <EOL> value = self . tile_map [ x , y ] <EOL> tile = self . tile_objects [ y ] [ x ] <EOL> color = self . tile_colors . get ( value , default_color ) <EOL> self . canvas . itemconfig ( tile , fill = color ) <EOL> def create_rooms ( self , rooms ) : <EOL> self . rooms = list ( rooms ) <EOL> self . room_objects = [ ] <EOL> color = '<STR_LIT>' <EOL> outline_width = <NUM_LIT:1> <EOL> for room in self . rooms : <EOL> rect = self . canvas . create_rectangle ( <EOL> room . tl . x * self . tile_size_x , <EOL> room . tl . y * self . tile_size_y , <EOL> room . br . x * self . tile_size_x - outline_width , <EOL> room . br . y * self . tile_size_y - outline_width , <EOL> fill = '<STR_LIT>' , <EOL> outline = color , <EOL> width = outline_width , <EOL> tags = '<STR_LIT>' ) <EOL> self . room_objects . append ( rect ) <EOL> def create_walk_graph ( self , walk_graph ) : <EOL> self . walk_graph = walk_graph . copy ( ) <EOL> two_way_line_options = dict ( <EOL> fill = '<STR_LIT>' , <EOL> width = <NUM_LIT:2> , <EOL> tags = '<STR_LIT>' ) <EOL> one_way_line_options = dict ( <EOL> fill = '<STR_LIT>' , <EOL> width = <NUM_LIT:2> , <EOL> tags = '<STR_LIT>' ) <EOL> def tile_center ( coord ) : <EOL> return [ ( coord . x + <NUM_LIT:0.5> ) * self . tile_size_x , ( coord . y + <NUM_LIT:0.5> ) * self . tile_size_x ] <EOL> for y in range ( self . tile_map . height ) : <EOL> for x in range ( self . tile_map . width ) : <EOL> coord = Coord ( x , y ) <EOL> can_reach = walk_graph . get ( coord ) <EOL> if not can_reach : continue <EOL> for other_coord in can_reach : <EOL> two_way = ( coord in walk_graph . get ( other_coord , [ ] ) ) <EOL> if two_way : <EOL> self . canvas . create_line ( * ( tile_center ( coord ) + tile_center ( other_coord ) ) , <EOL> ** two_way_line_options ) <EOL> else : <EOL> self . canvas . create_line ( * ( tile_center ( coord ) + tile_center ( other_coord ) ) , <EOL> ** one_way_line_options ) <EOL> def create_grid ( self , grid_size_x , grid_size_y ) : <EOL> grid_coords = [ ] <EOL> even = True <EOL> for x in range ( grid_size_x , self . width , grid_size_x ) : <EOL> line_start = [ x , - <NUM_LIT:1> ] <EOL> line_end = [ x , self . height + <NUM_LIT:1> ] <EOL> if even : <EOL> grid_coords += line_start <EOL> grid_coords += line_end <EOL> else : <EOL> grid_coords += line_end <EOL> grid_coords += line_start <EOL> even = not even <EOL> if even : <EOL> grid_coords += [ - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> else : <EOL> grid_coords += [ self . width + <NUM_LIT:1> , self . height + <NUM_LIT:1> ] <EOL> for y in range ( grid_size_y , self . height , grid_size_y ) : <EOL> line_start = [ - <NUM_LIT:1> , y ] <EOL> line_end = [ self . width + <NUM_LIT:1> , y ] <EOL> if even : <EOL> grid_coords += line_start <EOL> grid_coords += line_end <EOL> else : <EOL> grid_coords += line_end <EOL> grid_coords += line_start <EOL> even = not even <EOL> if even : <EOL> grid_coords += [ - <NUM_LIT:1> , - <NUM_LIT:1> ] <EOL> else : <EOL> grid_coords += [ self . width + <NUM_LIT:1> , self . height + <NUM_LIT:1> ] <EOL> grid_options = dict ( outline = '<STR_LIT>' , fill = '<STR_LIT>' , tags = '<STR_LIT>' , state = Tkinter . DISABLED ) <EOL> self . canvas . create_polygon ( * grid_coords , ** grid_options ) <EOL> self . bring_to_front ( ) <EOL> def click ( self , event ) : <EOL> self . canvas . scan_mark ( event . x , event . y ) <EOL> def drag ( self , event ) : <EOL> self . canvas . scan_dragto ( event . x , event . y , gain = <NUM_LIT:1> ) <EOL> def scroll ( self , event ) : <EOL> if event . state == <NUM_LIT:0> : <EOL> self . canvas . yview ( Tkinter . SCROLL , - event . delta , Tkinter . UNITS ) <EOL> elif event . state == <NUM_LIT:1> : <EOL> self . canvas . xview ( Tkinter . SCROLL , - event . delta , Tkinter . UNITS ) <EOL> def keypress ( self , event ) : <EOL> if event . keysym == '<STR_LIT>' : <EOL> self . tk . destroy ( ) <EOL> def run ( self ) : <EOL> self . tk . mainloop ( ) <EOL> @ staticmethod <EOL> def process_ids ( ) : <EOL> import subprocess <EOL> script = '''<STR_LIT>''' <EOL> output = subprocess . check_output ( [ '<STR_LIT>' , '<STR_LIT>' , script ] ) <EOL> return set ( s . strip ( ) for s in output . split ( '<STR_LIT:U+002C>' ) ) <EOL> def bring_to_front ( self ) : <EOL> import subprocess , textwrap <EOL> script = textwrap . dedent ( '''<STR_LIT>''' ) <EOL> script %= { <EOL> '<STR_LIT>' : root_process_id , <EOL> } <EOL> subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' , script ] ) <EOL> _process_ids = TileMapGUI . process_ids ( ) <EOL> root_tk = Tkinter . Tk ( ) <EOL> root_process_id = next ( iter ( TileMapGUI . process_ids ( ) - _process_ids ) ) <EOL> del _process_ids </s>
