multilabel.py 3.0 KB

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