Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as pgm
- from settings import *
- '''Imported the four first classes from my PixelBrawl project which can be found on Github'''
- class TextSurface(pgm.sprite.Sprite):
- """docstring for Text, is there a way to delete a surface or remove
- a subsurface for a parent surface ?
- """
- def __init__(self, x, y, text, fg_color=RED, bg_color=None):
- pgm.sprite.Sprite.__init__(self)
- self.text = text
- self.bg_color = bg_color
- self.fg_color = fg_color
- if self.bg_color == None: self.bg_color=ALPHA
- 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]))
- else: self.on_hover_bg_color = self.bg_color
- self.txt_font = pgm_frtp.Font(None, 20)
- self.txt_rect = self.txt_font.get_rect(self.text)
- self.image = pgm.Surface((self.txt_rect.w+TEXT_CORR_W, self.txt_rect.h+TEXT_CORR_H)).convert_alpha()
- self.image.fill(self.bg_color)
- self.rect = self.image.get_rect()
- self.rect.x = x - (self.txt_rect.w /2)
- self.rect.y = y
- self.txt_font.render_to(self.image, (TEXT_CORR_W/2,TEXT_CORR_H /2), self.text, fgcolor=self.fg_color, bgcolor=None)
- def chg_pos(self, new_pos=(0,0)):
- self.rect.x = new_pos[0]
- self.rect.y = new_pos[1]
- def chg_color(self, state=0, bg_color=ALPHA):
- '''state is 0 if the bgcolor is normal and 1 if highlighted'''
- if state==1:
- self.new_bg_color = self.on_hover_bg_color
- else:
- self.new_bg_color = self.bg_color
- self.image.fill(bg_color)
- 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)
- def chg_txt(self, new_txt, bg_color=ALPHA):
- '''Improve the code in order to rebuild the image surface to fit the
- whole new text perfectly -> really needed ?
- '''
- self.image.fill(bg_color)
- self.text = new_txt
- self.txt_rect = self.txt_font.get_rect(new_txt)
- self.rect.w = self.txt_rect.w
- self.txt_font.render_to(self.image, (TEXT_CORR_W/2,TEXT_CORR_H /2), new_txt, fgcolor=self.fg_color, bgcolor=None)
- class TextButton(pgm.sprite.Sprite):
- """docstring for Button how could I make it inherit from TextSurface ?"""
- def __init__(self, x, y, text, action=None, bg_color=BLACK, fg_color=GREEN):
- pgm.sprite.Sprite.__init__(self)
- self.text = text
- self.action = action
- self.bg_color = bg_color
- self.fg_color = fg_color
- self.txt_displayer = TextSurface(x, y, self.text, self.fg_color, self.bg_color)
- #self.txt_displayer.image.fill(self.bg_color)
- self.image = self.txt_displayer.image
- self.rect = self.txt_displayer.rect
- self.was_hovered = False
- self.acting = False
- def update(self):
- self.events = pgm.event.get()
- self.mouse_pos = pgm.mouse.get_pos()
- 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) :
- self.txt_displayer.chg_color(state=1, bg_color=self.txt_displayer.on_hover_bg_color)
- self.was_hovered = True
- self.mouse_clicks = pgm.mouse.get_pressed()
- if self.mouse_clicks[0]:
- self.acting = True
- elif self.was_hovered:
- self.was_hovered = False
- self.txt_displayer.chg_color(bg_color=self.bg_color)
- class ImageButton(pgm.sprite.Sprite):
- """docstring for ImageButton
- maybe add another surface under the image which would change colors when hovered ?
- or just modify the color of the pixels of the image"""
- def __init__(self, x, y, image, action=None):
- pgm.sprite.Sprite.__init__(self)
- self.x = x
- self.y = y
- self.image = pgm.image.load(str("./"+image)).convert_alpha()
- self.rect = self.image.get_rect()
- self.rect.x = x - (self.rect.w/2)
- self.rect.y = y - (self.rect.h/2)
- self.action = action
- self.acting = False
- self.was_hovered = False
- def chg_color(self, new_fg_color, new_bg_color):
- self.txt_font.render_to(self.image, (0, 0), self.text, fgcolor=new_fg_color, bgcolor=new_bg_color)
- def update(self):
- self.events = pgm.event.get()
- self.mouse_pos = pgm.mouse.get_pos()
- 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) :
- self.was_hovered = True
- self.mouse_clicks = pgm.mouse.get_pressed()
- if self.mouse_clicks[0]:
- self.acting = True
- elif self.was_hovered:
- self.was_hovered = False
- self.chg_color(self.fg_color, self.bg_color)
- class InputField(pgm.sprite.Sprite):
- """docstring for InputField, heavily depends on TextSurface. Just a prototype, not very well written
- add a background image/surface to help the user determine the nature of the sprite and its rect"""
- def __init__(self, x, y, hint_text, fg_color=BLUE, bg_color=None):
- pgm.sprite.Sprite.__init__(self)
- self.hint_text = self.crt_txt = hint_text
- self.fg_color = fg_color
- self.bg_color = bg_color
- self.txt_displayer = TextSurface(x, y, str(self.hint_text), LIGHT_GREY, self.bg_color)
- self.rect = self.txt_displayer.rect
- self.image = self.txt_displayer.image
- self.was_hovered = self.focused = False
- self.returned = False
- def update(self, key=None):
- #if self.returned: self.crt_txt = ""
- self.mouse_pos = pgm.mouse.get_pos()
- self.key = key
- 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) :
- self.was_hovered = True #replaces the on_hover function along with the next line
- if self.crt_txt == self.hint_text or "":
- self.txt_displayer.chg_color(state=1)
- self.mouse_clicks = pgm.mouse.get_pressed()
- if self.mouse_clicks[0]:
- self.focused = True
- elif self.was_hovered:
- self.was_hovered = False
- if self.crt_txt == "": self.txt_displayer.chg_txt(self.hint_text)
- self.txt_displayer.chg_color()
- if self.focused and self.key!=None:
- if self.crt_txt == self.hint_text:
- self.crt_txt = ""
- self.new_txt = self.crt_txt
- self.str_to_append = ""
- if self.key == "\b":
- if len(self.str_to_append) == 0:
- self.crt_txt = self.crt_txt[:-1]
- else:
- self.str_to_append = self.str_to_append[:-1]
- elif self.key == "\r":
- self.returned = True
- else:
- self.str_to_append = self.str_to_append + self.key
- self.crt_txt = self.crt_txt + self.str_to_append
- #if self.txt_displayer.txt_rect.w >= INPUT_MAX_LENGTH:
- drawn_txt = self.crt_txt[-INPUT_MAX_LENGTH:]
- self.txt_displayer.chg_txt(drawn_txt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement