Advertisement
Toude

GUI functions

Mar 2nd, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.52 KB | None | 0 0
  1. import pygame as pgm
  2. from settings import *
  3.  
  4. '''Imported the four first classes from my PixelBrawl project which can be found on Github'''
  5.  
  6.  
  7. class TextSurface(pgm.sprite.Sprite):
  8.     """docstring for Text, is there a way to delete a surface or remove
  9.     a subsurface for a parent surface ?
  10.     """
  11.     def __init__(self, x, y, text, fg_color=RED, bg_color=None):
  12.         pgm.sprite.Sprite.__init__(self)
  13.         self.text = text
  14.         self.bg_color = bg_color
  15.         self.fg_color = fg_color
  16.         if self.bg_color == None: self.bg_color=ALPHA
  17.         if self.bg_color!=ALPHA: self.on_hover_bg_color = (self.bg_color[0]-math.floor(0.3*self.bg_color[0]), self.bg_color[1]-math.floor(0.3*self.bg_color[1]), self.bg_color[2]-math.floor(0.3*self.bg_color[2]))
  18.         else: self.on_hover_bg_color = self.bg_color
  19.         self.txt_font = pgm_frtp.Font(None, 20)
  20.         self.txt_rect = self.txt_font.get_rect(self.text)
  21.         self.image = pgm.Surface((self.txt_rect.w+TEXT_CORR_W, self.txt_rect.h+TEXT_CORR_H)).convert_alpha()
  22.         self.image.fill(self.bg_color)
  23.         self.rect = self.image.get_rect()
  24.         self.rect.x = x - (self.txt_rect.w /2)
  25.         self.rect.y = y
  26.         self.txt_font.render_to(self.image, (TEXT_CORR_W/2,TEXT_CORR_H  /2), self.text, fgcolor=self.fg_color, bgcolor=None)
  27.  
  28.     def chg_pos(self, new_pos=(0,0)):
  29.         self.rect.x = new_pos[0]
  30.         self.rect.y = new_pos[1]
  31.  
  32.     def chg_color(self, state=0, bg_color=ALPHA):
  33.         '''state is 0 if the bgcolor is normal and 1 if highlighted'''
  34.         if state==1:
  35.             self.new_bg_color = self.on_hover_bg_color
  36.         else:
  37.             self.new_bg_color = self.bg_color
  38.         self.image.fill(bg_color)
  39.         self.txt_font.render_to(self.image, (TEXT_CORR_W/2,TEXT_CORR_H  /2), self.text, fgcolor=self.fg_color, bgcolor=self.new_bg_color)
  40.  
  41.     def chg_txt(self, new_txt, bg_color=ALPHA):
  42.         '''Improve the code in order to rebuild the image surface to fit the
  43.         whole new text perfectly -> really needed ?
  44.         '''
  45.         self.image.fill(bg_color)
  46.         self.text = new_txt
  47.         self.txt_rect = self.txt_font.get_rect(new_txt)
  48.         self.rect.w = self.txt_rect.w
  49.         self.txt_font.render_to(self.image, (TEXT_CORR_W/2,TEXT_CORR_H  /2), new_txt, fgcolor=self.fg_color, bgcolor=None)
  50.  
  51. class TextButton(pgm.sprite.Sprite):
  52.     """docstring for Button how could I make it inherit from TextSurface ?"""
  53.     def __init__(self, x, y, text, action=None, bg_color=BLACK, fg_color=GREEN):
  54.         pgm.sprite.Sprite.__init__(self)
  55.         self.text = text
  56.         self.action = action
  57.         self.bg_color = bg_color
  58.         self.fg_color = fg_color
  59.         self.txt_displayer = TextSurface(x, y, self.text, self.fg_color, self.bg_color)
  60.         #self.txt_displayer.image.fill(self.bg_color)
  61.         self.image = self.txt_displayer.image
  62.         self.rect = self.txt_displayer.rect
  63.         self.was_hovered = False
  64.         self.acting = False
  65.  
  66.     def update(self):
  67.         self.events = pgm.event.get()
  68.         self.mouse_pos = pgm.mouse.get_pos()
  69.         if (self.mouse_pos[0]>= self.rect.x and self.mouse_pos[0]<= self.rect.x + self.rect.w) and (self.mouse_pos[1]>= self.rect.y and self.mouse_pos[1] <=self.rect.y + self.rect.h) :
  70.             self.txt_displayer.chg_color(state=1, bg_color=self.txt_displayer.on_hover_bg_color)
  71.             self.was_hovered = True
  72.             self.mouse_clicks = pgm.mouse.get_pressed()
  73.             if self.mouse_clicks[0]:
  74.                 self.acting = True
  75.         elif self.was_hovered:
  76.             self.was_hovered = False
  77.             self.txt_displayer.chg_color(bg_color=self.bg_color)
  78.  
  79.  
  80. class ImageButton(pgm.sprite.Sprite):
  81.     """docstring for ImageButton
  82.     maybe add another surface under the image which would change colors when hovered ?
  83.     or just modify the color of the pixels of the image"""
  84.     def __init__(self, x, y, image, action=None):
  85.         pgm.sprite.Sprite.__init__(self)
  86.         self.x = x
  87.         self.y = y
  88.         self.image = pgm.image.load(str("./"+image)).convert_alpha()
  89.         self.rect = self.image.get_rect()
  90.         self.rect.x = x - (self.rect.w/2)
  91.         self.rect.y = y - (self.rect.h/2)
  92.         self.action = action
  93.         self.acting = False
  94.         self.was_hovered = False
  95.  
  96.     def chg_color(self, new_fg_color, new_bg_color):
  97.         self.txt_font.render_to(self.image, (0, 0), self.text, fgcolor=new_fg_color, bgcolor=new_bg_color)
  98.  
  99.     def update(self):
  100.         self.events = pgm.event.get()
  101.         self.mouse_pos = pgm.mouse.get_pos()
  102.         if (self.mouse_pos[0]>= self.rect.x and self.mouse_pos[0]<= self.rect.x + self.rect.w) and (self.mouse_pos[1]>= self.rect.y and self.mouse_pos[1] <=self.rect.y + self.rect.h) :
  103.             self.was_hovered = True
  104.             self.mouse_clicks = pgm.mouse.get_pressed()
  105.             if self.mouse_clicks[0]:
  106.                 self.acting = True
  107.         elif self.was_hovered:
  108.             self.was_hovered = False
  109.             self.chg_color(self.fg_color, self.bg_color)
  110.  
  111.  
  112. class InputField(pgm.sprite.Sprite):
  113.     """docstring for InputField, heavily depends on TextSurface. Just a prototype, not very well written
  114.     add a background image/surface to help the user determine the nature of the sprite and its rect"""
  115.     def __init__(self, x, y, hint_text, fg_color=BLUE, bg_color=None):
  116.         pgm.sprite.Sprite.__init__(self)
  117.         self.hint_text = self.crt_txt = hint_text
  118.         self.fg_color = fg_color
  119.         self.bg_color = bg_color
  120.         self.txt_displayer = TextSurface(x, y, str(self.hint_text), LIGHT_GREY, self.bg_color)
  121.         self.rect = self.txt_displayer.rect
  122.         self.image = self.txt_displayer.image
  123.         self.was_hovered = self.focused = False
  124.         self.returned = False
  125.  
  126.  
  127.     def update(self, key=None):
  128.         #if self.returned: self.crt_txt = ""
  129.         self.mouse_pos = pgm.mouse.get_pos()
  130.         self.key = key
  131.         if (self.mouse_pos[0]>= self.rect.x and self.mouse_pos[0]<= self.rect.x + self.rect.w) and (self.mouse_pos[1]>= self.rect.y and self.mouse_pos[1] <=self.rect.y + self.rect.h) :
  132.             self.was_hovered = True #replaces the on_hover function along with the next line
  133.             if self.crt_txt == self.hint_text or "":
  134.                 self.txt_displayer.chg_color(state=1)
  135.             self.mouse_clicks = pgm.mouse.get_pressed()
  136.             if self.mouse_clicks[0]:
  137.                 self.focused = True
  138.         elif self.was_hovered:
  139.             self.was_hovered = False
  140.             if self.crt_txt == "": self.txt_displayer.chg_txt(self.hint_text)
  141.             self.txt_displayer.chg_color()
  142.  
  143.         if self.focused and self.key!=None:
  144.             if self.crt_txt == self.hint_text:
  145.                 self.crt_txt = ""
  146.  
  147.             self.new_txt = self.crt_txt
  148.             self.str_to_append = ""
  149.             if self.key == "\b":
  150.                 if len(self.str_to_append) == 0:
  151.                     self.crt_txt = self.crt_txt[:-1]
  152.                 else:
  153.                     self.str_to_append = self.str_to_append[:-1]
  154.  
  155.             elif self.key == "\r":
  156.                 self.returned = True
  157.  
  158.             else:
  159.                 self.str_to_append = self.str_to_append + self.key
  160.             self.crt_txt = self.crt_txt + self.str_to_append
  161.  
  162.             #if self.txt_displayer.txt_rect.w >= INPUT_MAX_LENGTH:
  163.             drawn_txt = self.crt_txt[-INPUT_MAX_LENGTH:]
  164.             self.txt_displayer.chg_txt(drawn_txt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement