multilabel.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from skin_manager import MySkinManager
  4. from lang_manager import MyLangManager
  5. from widget import Widget
  6. class MultiLabel(Widget): ##Multi Line Label
  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 SetColor(self,color):
  23. self._Color = color
  24. def GetText(self):
  25. return self._Text
  26. def SetText(self,text):
  27. self._Text = text
  28. self.blit_text(self._CanvasHWND,self._Text,(self._PosX,self._PosY),self._FontObj)
  29. def SetCanvasHWND(self,_canvashwnd):
  30. self._CanvasHWND = _canvashwnd
  31. def blit_text(self, surface,text, pos, font):
  32. iscjk = MyLangManager.IsCJKMode()
  33. color = self._Color
  34. words = [word.split(' ') for word in text.splitlines()]
  35. space = font.size(' ')[0]
  36. if iscjk:
  37. space = 0
  38. words = [list(word.decode("utf8")) for word in text.splitlines()]
  39. max_width = self._Width
  40. x ,y = pos
  41. row_total_width = 0
  42. lines = 0
  43. line_max = 12
  44. row_max = 4
  45. if iscjk:
  46. line_max = line_max*2
  47. for i,line in enumerate(words[:row_max]):
  48. for word in line[:line_max]:
  49. word_surface = font.render(word, True, color)
  50. word_width = word_surface.get_width()
  51. word_height = word_surface.get_height()
  52. row_total_width += word_width
  53. if row_total_width+space >= max_width:
  54. x = pos[0] # Reset the x.
  55. y += word_height # Start on new row.
  56. row_total_width = word_width
  57. if lines == 0:
  58. lines += word_height
  59. else:
  60. lines += word_height
  61. surface.blit(word_surface, (x, y))
  62. x += word_width + space
  63. x = pos[0] # Reset the x.
  64. y += word_height # Start on new row.
  65. lines += word_height
  66. self._Height = lines
  67. def Draw(self):
  68. #my_text = self._FontObj.render( self._Text,True,self._Color)
  69. self.blit_text(self._CanvasHWND,self._Text,(self._PosX,self._PosY),self._FontObj)
  70. #pygame.draw.rect(self._CanvasHWND,(83,83,83), (self._PosX,self._PosY,self._Width,self._Height) , 1 )
  71. #self._CanvasHWND.blit(my_text,(self._PosX,self._PosY,self._Width,self._Height))