Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import pygame
- except ImportError as e:
- import sys
- sys.exit("Error -> {}".format(e))
- class TextWidget(object):
- def __init__(self, x, y, message, fontName, size, AA, Fore_color, Back_color = None):
- self.x = x
- self.y = y
- self.message = message
- self.fontName = fontName
- self.size = size
- self.AA = AA
- self.FC = Fore_color
- self.BC = Back_color if Back_color is not None else None
- # Make sure we the color arg is right.
- self.CheckColor(self.FC)
- self.CheckColor(self.BC) if self.BC is not None else None
- # Make a dict out of all the args.
- self.dictArgs = dict(message = self.message, fontName = self.fontName,
- size = self.size, AA = self.AA,
- Fore = self.FC, Back = self.BC)
- self.text = self.font(self.message, self.fontName, self.size, self.AA, self.FC, self.BC)
- self.rect = self.text.get_rect()
- self.rect.x = self.x
- self.rect.y = self.y
- def draw(self, surface):
- surface.blit(self.text, (self.x, self.y))
- pygame.display.flip()
- @staticmethod
- def CheckColor(color):
- """
- Function that checks an argument.
- """
- if len(color) != 3:
- raise TypeError("Lenght of color is too small.")
- elif type(color) != tuple and type(color) != list:
- raise TypeError("The color must be a tuple or a list of 3 ints.")
- for i in color:
- if type(i) != int:
- raise TypeError("The color must be a int.")
- if i not in list(range(0, 256)):
- raise TypeError("Color must be in range 0-255")
- @staticmethod
- def font(text, font_name, size, AA=1, F_color=(), B_color=()):
- # INIT pygame's font module
- pygame.font.init()
- if font_name is None:
- font_name = pygame.font.get_default_font()
- font = pygame.font.SysFont(font_name, size)
- font = font.render(text, AA, F_color, B_color)
- return font
- def MouseOverText(self):
- if self.rect.collidepoint(pygame.mouse.get_pos()):
- return True
- return False
- def CallFuncOnMouseOver(self, callFunc, *args, **kwargs):
- """Call a function when the text is hovered."""
- if self.MouseOverText():
- callFunc(*args, **kwargs)
- def HighLight(self, amount):
- hl = (x + amount for x in self.FC if x <= 255 and x + amount <= 255)
- del self.text
- self.text = self.font(self.message, self.fontName, self.size, self.AA, tuple(hl), self.BC)
- def LowLight(self, amount):
- hl = (x - amount for x in self.FC if x <= 255 and x - amount <= 255)
- del self.text
- self.text = self.font(self.message, self.fontName, self.size, self.AA, tuple(hl), self.BC)
- def IncreaseSize(self, amount):
- self.size += amount
- del self.text
- self.text = self.font(self.message, self.fontName, self.size, self.AA, self.FC, self.BC)
- def DecreaseSize(self, amount):
- self.size += amount
- del self.text
- self.text = self.font(self.message, self.fontName, self.size, self.AA, self.FC, self.BC)
- def config(self, **kwargs):
- """
- Like tkinter's config.
- Pass the key you want the value to be changed.
- """
- for k, v in kwargs.items():
- self.dictArgs[k] = v
- self.text = self.font(self.dictArgs["message"], self.dictArgs["fontName"], self.dictArgs["size"], self.dictArgs["AA"], self.dictArgs["Fore"], self.dictArgs["Back"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement