Advertisement
Guest User

Untitled

a guest
May 25th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. try:
  2. import pygame
  3.  
  4. except ImportError as e:
  5. import sys
  6. sys.exit("Error -> {}".format(e))
  7.  
  8. class TextWidget(object):
  9.  
  10. def __init__(self, x, y, message, fontName, size, AA, Fore_color, Back_color = None):
  11. self.x = x
  12. self.y = y
  13.  
  14. self.message = message
  15. self.fontName = fontName
  16. self.size = size
  17. self.AA = AA
  18.  
  19. self.FC = Fore_color
  20. self.BC = Back_color if Back_color is not None else None
  21.  
  22. # Make sure we the color arg is right.
  23. self.CheckColor(self.FC)
  24. self.CheckColor(self.BC) if self.BC is not None else None
  25.  
  26. # Make a dict out of all the args.
  27. self.dictArgs = dict(message = self.message, fontName = self.fontName,
  28. size = self.size, AA = self.AA,
  29. Fore = self.FC, Back = self.BC)
  30.  
  31. self.text = self.font(self.message, self.fontName, self.size, self.AA, self.FC, self.BC)
  32. self.rect = self.text.get_rect()
  33. self.rect.x = self.x
  34. self.rect.y = self.y
  35.  
  36. def draw(self, surface):
  37. surface.blit(self.text, (self.x, self.y))
  38. pygame.display.flip()
  39.  
  40. @staticmethod
  41. def CheckColor(color):
  42. """
  43. Function that checks an argument.
  44. """
  45.  
  46. if len(color) != 3:
  47. raise TypeError("Lenght of color is too small.")
  48.  
  49. elif type(color) != tuple and type(color) != list:
  50. raise TypeError("The color must be a tuple or a list of 3 ints.")
  51.  
  52. for i in color:
  53. if type(i) != int:
  54. raise TypeError("The color must be a int.")
  55.  
  56. if i not in list(range(0, 256)):
  57. raise TypeError("Color must be in range 0-255")
  58.  
  59. @staticmethod
  60. def font(text, font_name, size, AA=1, F_color=(), B_color=()):
  61. # INIT pygame's font module
  62. pygame.font.init()
  63.  
  64. if font_name is None:
  65. font_name = pygame.font.get_default_font()
  66.  
  67. font = pygame.font.SysFont(font_name, size)
  68. font = font.render(text, AA, F_color, B_color)
  69. return font
  70.  
  71. def MouseOverText(self):
  72. if self.rect.collidepoint(pygame.mouse.get_pos()):
  73. return True
  74.  
  75. return False
  76.  
  77. def CallFuncOnMouseOver(self, callFunc, *args, **kwargs):
  78. """Call a function when the text is hovered."""
  79.  
  80. if self.MouseOverText():
  81. callFunc(*args, **kwargs)
  82.  
  83. def HighLight(self, amount):
  84. hl = (x + amount for x in self.FC if x <= 255 and x + amount <= 255)
  85. del self.text
  86. self.text = self.font(self.message, self.fontName, self.size, self.AA, tuple(hl), self.BC)
  87.  
  88. def LowLight(self, amount):
  89. hl = (x - amount for x in self.FC if x <= 255 and x - amount <= 255)
  90. del self.text
  91. self.text = self.font(self.message, self.fontName, self.size, self.AA, tuple(hl), self.BC)
  92.  
  93. def IncreaseSize(self, amount):
  94. self.size += amount
  95. del self.text
  96. self.text = self.font(self.message, self.fontName, self.size, self.AA, self.FC, self.BC)
  97.  
  98. def DecreaseSize(self, amount):
  99. self.size += amount
  100. del self.text
  101. self.text = self.font(self.message, self.fontName, self.size, self.AA, self.FC, self.BC)
  102.  
  103. def config(self, **kwargs):
  104. """
  105. Like tkinter's config.
  106. Pass the key you want the value to be changed.
  107. """
  108.  
  109. for k, v in kwargs.items():
  110. self.dictArgs[k] = v
  111.  
  112. 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