text_bulletinboard.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from libs.roundrects import aa_round_rect
  4. ## local UI import
  5. from page import Page,PageStack,PageSelector
  6. from label import Label
  7. from fonts import fonts
  8. from skin_manager import MySkinManager
  9. from lang_manager import MyLangManager
  10. from widget import Widget
  11. from textarea import Textarea
  12. class Word:
  13. _T = ""
  14. _Color = MySkinManager.GiveColor('Text') ## default text color
  15. _FontObj = MyLangManager.TrFont("varela12") ##default font
  16. _Size = 12
  17. _Bold = False
  18. _UndLine = False
  19. def SetColor(self,color):
  20. self._Color = color
  21. def GetColor(self):
  22. return self._Color
  23. def SetFont(self,fnt):
  24. self._FontObj = fnt
  25. def SetBold(self,bd):
  26. self._Bold = bd
  27. def SetUnderLine(self,bd):
  28. self._UndLine = bd
  29. def __init__(self,v=""):
  30. self._T = v
  31. def __str__(self):
  32. return self._T
  33. def __unicode__(self):
  34. return self._T.encode("utf-8")
  35. def __add__(self,a):
  36. return self._T + a
  37. def __repr__(self):
  38. return self._T
  39. def __len__(self):
  40. return len(self._T)
  41. def __eq__(self, other):
  42. return self._T == other
  43. def FnHeight(self):
  44. return self._FontObj.get_height()
  45. def Render(self):
  46. self._FontObj.set_bold(self._Bold)
  47. self._FontObj.set_underline(self._UndLine)
  48. sur = self._FontObj.render(self._T,True,self._Color)
  49. self._FontObj.set_bold(False)
  50. self._FontObj.set_underline(False)
  51. return sur
  52. class Text:
  53. _Words = []
  54. def __init__(self,content="",color=None,fnt=None,Bold=False,Und=False):
  55. self._Words = [ Word(x) for x in list(content) ]
  56. if color != None:
  57. self.SetColor(color)
  58. if fnt != None:
  59. self.SetFont(fnt)
  60. if Bold == True:
  61. self.SetBold(True)
  62. if Und == True:
  63. self.SetUnderLine(True)
  64. def SetColor(self,color):
  65. if len(self._Words) > 0:
  66. for i,x in enumerate(self._Words):
  67. self._Words[i].SetColor(color)
  68. def SetBold(self,bd):
  69. if len(self._Words) > 0:
  70. for i,x in enumerate(self._Words):
  71. self._Words[i].SetBold(bd)
  72. def SetUnderLine(self,bd):
  73. if len(self._Words) > 0:
  74. for i,x in enumerate(self._Words):
  75. self._Words[i].SetUnderLine(bd)
  76. def SetFont(self,fnt):
  77. if len(self._Words) > 0:
  78. for i,x in enumerate(self._Words):
  79. self._Words[i].SetFont(fnt)
  80. def __add__(self,a):
  81. return self._Words+a.Words()
  82. def Words(self):
  83. return self._Words
  84. class Textbulletinboard(Textarea):
  85. _TextLimit = 200
  86. _BackgroundColor = MySkinManager.GiveColor("White")
  87. _Align = "Left" ## Left or Center
  88. _RowPitch = -1
  89. def SetAndBlitText(self,words):# words => []
  90. if self._TextFull != True:
  91. self._MyWords = words
  92. self._TextIndex = len(self._MyWords)
  93. else:
  94. print("is Full %s" % "".join(str(self._MyWords)))
  95. def BuildBlitText(self):
  96. blit_rows = [[]]
  97. w = 0
  98. xmargin = 5
  99. endmargin = 15
  100. x = self._PosX+xmargin
  101. linenumber = 0
  102. cursor_row = 0
  103. #print(self._MyWords)
  104. for i, v in enumerate(self._MyWords):
  105. if str(v) == "\n":
  106. w = 0
  107. x = self._PosX+xmargin
  108. linenumber+=2
  109. blit_rows.append([])
  110. blit_rows.append([])
  111. else:
  112. t = v.Render()
  113. t_width = t.get_width()
  114. w += t_width
  115. del(t)
  116. blit_rows[linenumber].append(v)
  117. if i == self._TextIndex - 1:
  118. cursor_row = linenumber
  119. if w + t_width >= self._Width-endmargin:
  120. x = self._PosX+xmargin
  121. w = 0
  122. linenumber += 1
  123. blit_rows.append([])
  124. self._BlitWords = blit_rows
  125. self._BlitIndex = self._TextIndex
  126. def BlitText(self):
  127. # build up blitwords
  128. self.BuildBlitText()
  129. xmargin = 5
  130. endmargin = 5
  131. start_x = self._PosX+xmargin ##start_point_x
  132. start_y = self._PosY ## start_point_y
  133. x = self._PosX+xmargin ##start_point_x
  134. y = self._PosY ## start_point_y
  135. self._TextFull = len(self._MyWords) > self._TextLimit
  136. last_height = 0
  137. for row_idx, row in enumerate(self._BlitWords):
  138. if len(row) == 0:
  139. if self._RowPitch > 0:
  140. y = y + self._RowPitch
  141. else:
  142. y = y + 16
  143. w = 0
  144. continue
  145. else:
  146. total_row_width = 0
  147. for i,v in enumerate(row):
  148. t = v.Render()
  149. total_row_width += t.get_width()
  150. if total_row_width > self._Width-endmargin:
  151. total_row_width = self._Width
  152. start_x = self._PosX + xmargin
  153. break
  154. else:
  155. if self._Align == "Center":
  156. start_x = (self._Width - total_row_width)/2
  157. last_height = 0
  158. total_row_width = 0
  159. x = start_x
  160. y = y + last_height
  161. for i,v in enumerate(row):
  162. t = v.Render()
  163. total_row_width += t.get_width()
  164. if last_height < v.FnHeight():
  165. last_height = v.FnHeight()
  166. if total_row_width > self._Width-endmargin:
  167. x = start_x
  168. y = y + last_height
  169. total_row_width = 0
  170. self._CanvasHWND.blit(t, (x,y))
  171. x += t.get_width()
  172. def Draw(self):
  173. #aa_round_rect(self._CanvasHWND, (4,24.5+6,312,60),self._BackgroundColor,4,0,self._BackgroundColor)
  174. aa_round_rect(self._CanvasHWND,
  175. (self._PosX,self._PosY,self._Width,self._Height),self._BackgroundColor,4,0,self._BackgroundColor)
  176. self.BlitText()