<s> import pygame <EOL> ALIGN_LEFT = <NUM_LIT:0.0> <EOL> ALIGN_TOP = <NUM_LIT:0.0> <EOL> ALIGN_CENTER = <NUM_LIT:0.5> <EOL> ALIGN_RIGHT = <NUM_LIT:1.0> <EOL> ALIGN_BOTTOM = <NUM_LIT:1.0> <EOL> def align ( child , parent , horizontal = ALIGN_CENTER , vertical = ALIGN_CENTER , <EOL> hpad = <NUM_LIT:0> , vpad = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> cx , cy , cwidth , cheight = child <EOL> px , py , pwidth , pheight = parent <EOL> return ( px + ( horizontal * pwidth - horizontal * cwidth ) + hpad , <EOL> py + ( vertical * pheight - vertical * cheight ) + vpad ) <EOL> font_cache = { } <EOL> def get_font ( size ) : <EOL> """<STR_LIT>""" <EOL> if size not in font_cache : <EOL> font_cache [ size ] = pygame . font . Font ( None , size ) <EOL> return font_cache [ size ] <EOL> def render_text ( text , size = <NUM_LIT> , fg = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , bg = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> if bg is not None : <EOL> return get_font ( size ) . render ( text , True , fg , bg ) <EOL> else : <EOL> return get_font ( size ) . render ( text , True , fg ) <EOL> class Button ( object ) : <EOL> fg_color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> bg_color = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> border_color = ( <NUM_LIT:200> , <NUM_LIT:200> , <NUM_LIT:200> ) <EOL> padding_px = <NUM_LIT:2> <EOL> border_px = <NUM_LIT:2> <EOL> font_size = <NUM_LIT> <EOL> def __init__ ( self , rect , text , click = None , font_size = None , bg_color = None ) : <EOL> """<STR_LIT>""" <EOL> self . text = text <EOL> self . bg_color = bg_color if bg_color is not None else self . bg_color <EOL> self . font_size = font_size if font_size is not None else self . font_size <EOL> self . click_func = click <EOL> x , y , width , height = rect <EOL> x += self . padding_px <EOL> y += self . padding_px <EOL> width -= <NUM_LIT:2> * self . padding_px <EOL> height -= <NUM_LIT:2> * self . padding_px <EOL> self . rect = ( x , y , width , height ) <EOL> self . label = render_text ( text , size = self . font_size , fg = self . fg_color , <EOL> bg = self . bg_color ) <EOL> self . label_pos = align ( self . label . get_rect ( ) , self . rect ) <EOL> def render ( self , screen ) : <EOL> """<STR_LIT>""" <EOL> screen . fill ( self . bg_color , self . rect ) <EOL> pygame . draw . rect ( screen , self . border_color , self . rect , self . border_px ) <EOL> screen . blit ( self . label , self . label_pos ) <EOL> def click ( self , location ) : <EOL> """<STR_LIT>""" <EOL> x , y , width , height = self . rect <EOL> mx , my = location <EOL> if mx >= x and mx <= ( x + width ) and my >= y and my <= ( y + height ) and self . click_func is not None : <EOL> self . click_func ( self ) <EOL> class ButtonGrid ( object ) : <EOL> def __init__ ( self , width , height , cols , rows ) : <EOL> """<STR_LIT>""" <EOL> self . col_size = width / cols <EOL> self . row_size = height / rows <EOL> self . buttons = [ ] <EOL> def add ( self , col , row , text , rowspan = <NUM_LIT:1> , colspan = <NUM_LIT:1> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> x = col * self . col_size <EOL> y = row * self . row_size <EOL> width = colspan * self . col_size <EOL> height = rowspan * self . row_size <EOL> self . buttons . append ( Button ( ( x , y , width , height ) , text , ** kwargs ) ) <EOL> def render ( self , screen ) : <EOL> """<STR_LIT>""" <EOL> for button in self . buttons : <EOL> button . render ( screen ) <EOL> def click ( self , location ) : <EOL> """<STR_LIT>""" <EOL> for button in self . buttons : <EOL> button . click ( location ) </s>
