<s> import pygame <EOL> import view <EOL> import label <EOL> import callback <EOL> class TextField ( view . View ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , frame , text = '<STR_LIT>' , placeholder = '<STR_LIT>' ) : <EOL> view . View . __init__ ( self , frame ) <EOL> self . text = text or '<STR_LIT>' <EOL> self . placeholder = placeholder <EOL> self . label = label . Label ( pygame . Rect ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , frame . size ) , <EOL> text or placeholder ) <EOL> self . label . halign = label . LEFT <EOL> self . add_child ( self . label ) <EOL> self . enabled = True <EOL> self . max_len = None <EOL> self . secure = False <EOL> self . on_return = callback . Signal ( ) <EOL> self . on_text_change = callback . Signal ( ) <EOL> def layout ( self ) : <EOL> self . label . topleft = self . padding <EOL> r_before = self . label . frame . right <EOL> self . label . frame . w = self . frame . w - self . padding [ <NUM_LIT:0> ] * <NUM_LIT:2> <EOL> self . label . frame . h = self . frame . h - self . padding [ <NUM_LIT:1> ] * <NUM_LIT:2> <EOL> self . label . frame . right = r_before <EOL> self . _update_text ( ) <EOL> view . View . layout ( self ) <EOL> def key_down ( self , key , code ) : <EOL> if key == pygame . K_BACKSPACE : <EOL> self . text = self . text [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> elif key == pygame . K_RETURN : <EOL> can_submit = True <EOL> if self . placeholder and self . text == self . placeholder : <EOL> can_submit = False <EOL> if can_submit : <EOL> self . on_return ( self , self . text ) <EOL> else : <EOL> try : <EOL> self . text = '<STR_LIT>' % ( self . text , str ( code ) ) <EOL> except : <EOL> pass <EOL> self . on_text_change ( self , self . text ) <EOL> if self . max_len : <EOL> self . text = self . text [ <NUM_LIT:0> : self . max_len ] <EOL> self . _update_text ( ) <EOL> self . label . shrink_wrap ( ) <EOL> self . label . layout ( ) <EOL> if self . label . frame . right > self . frame . w - self . padding [ <NUM_LIT:0> ] * <NUM_LIT:2> : <EOL> self . label . frame . right = self . frame . w - self . padding [ <NUM_LIT:0> ] * <NUM_LIT:2> <EOL> else : <EOL> self . label . frame . left = self . padding [ <NUM_LIT:0> ] <EOL> def _update_text ( self ) : <EOL> if ( len ( self . text ) == <NUM_LIT:0> and <EOL> self . placeholder is not None and <EOL> not self . has_focus ( ) ) : <EOL> self . label . text_color = self . placeholder_text_color <EOL> self . label . text = self . placeholder <EOL> elif len ( self . text ) >= <NUM_LIT:0> : <EOL> self . label . text_color = self . text_color <EOL> self . label . text = self . text <EOL> elif self . secure : <EOL> self . label . text = '<STR_LIT:*>' * len ( self . text ) <EOL> def draw ( self ) : <EOL> if not view . View . draw ( self ) or not self . has_focus ( ) : <EOL> return False <EOL> if ( not self . blink_cursor or <EOL> pygame . time . get_ticks ( ) / self . cursor_blink_duration % <NUM_LIT:2> == <NUM_LIT:0> ) : <EOL> size = self . label . font . size ( self . text ) <EOL> rect = pygame . Rect ( <EOL> self . label . frame . left + self . label . padding [ <NUM_LIT:0> ] + size [ <NUM_LIT:0> ] , <EOL> self . label . frame . bottom - self . label . padding [ <NUM_LIT:1> ] , <EOL> <NUM_LIT:10> , <NUM_LIT:2> ) <EOL> pygame . draw . rect ( self . surface , self . text_color , rect ) <EOL> return True <EOL> def __repr__ ( self ) : <EOL> return self . text </s>
