multilabel.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from skin_manager import MySkinManager
  4. class MultiLabel: ##Multi Line Label
  5. _PosX=0
  6. _PosY=0
  7. _Width=135
  8. _Height=100
  9. _Text=""
  10. _FontObj=None
  11. _Color = MySkinManager.GiveColor('Text')
  12. _CanvasHWND = None
  13. _TextSurf = None
  14. _MaxWidth = 0
  15. def __init__(self):
  16. pass
  17. def Init(self,text,font_obj,color=MySkinManager.GiveColor('Text')):
  18. self._Color = color
  19. self._FontObj = font_obj
  20. self._Text = text
  21. self.blit_text(self._CanvasHWND,self._Text,(self._PosX,self._PosY),self._FontObj)
  22. def NewCoord(self,x,y):
  23. self._PosX = x
  24. self._PosY = y
  25. def SetColor(self,color):
  26. self._Color = color
  27. def GetText(self):
  28. return self._Text
  29. def SetText(self,text):
  30. self._Text = text
  31. self.blit_text(self._CanvasHWND,self._Text,(self._PosX,self._PosY),self._FontObj)
  32. def Width(self):
  33. return self._Width
  34. def SetCanvasHWND(self,_canvashwnd):
  35. self._CanvasHWND = _canvashwnd
  36. def blit_text(self, surface,text, pos, font):
  37. color = self._Color
  38. words = [word.split(' ') for word in text.splitlines()]
  39. space = font.size(' ')[0]
  40. max_width = self._Width
  41. x ,y = pos
  42. row_total_width = 0
  43. lines = 0
  44. for i,line in enumerate(words[:4]):
  45. for word in line[:12]:
  46. word_surface = font.render(word, True, color)
  47. word_width = word_surface.get_width()
  48. word_height = word_surface.get_height()
  49. row_total_width += word_width
  50. if row_total_width+space >= max_width:
  51. x = pos[0] # Reset the x.
  52. y += word_height # Start on new row.
  53. row_total_width = word_width
  54. if lines == 0:
  55. lines += word_height
  56. else:
  57. lines += word_height
  58. surface.blit(word_surface, (x, y))
  59. x += word_width + space
  60. x = pos[0] # Reset the x.
  61. y += word_height # Start on new row.
  62. lines += word_height
  63. self._Height = lines
  64. def Draw(self):
  65. #my_text = self._FontObj.render( self._Text,True,self._Color)
  66. self.blit_text(self._CanvasHWND,self._Text,(self._PosX,self._PosY),self._FontObj)
  67. #pygame.draw.rect(self._CanvasHWND,(83,83,83), (self._PosX,self._PosY,self._Width,self._Height) , 1 )
  68. #self._CanvasHWND.blit(my_text,(self._PosX,self._PosY,self._Width,self._Height))