text_bulletinboard.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 skin_manager import MySkinManager
  8. from lang_manager import MyLangManager
  9. from widget import Widget
  10. from textarea import Textarea
  11. class Word:
  12. _T = ""
  13. _Color = MySkinManager.GiveColor('Text') ## default text color
  14. _FontObj = MyLangManager.TrFont("Eurostile12") ##default font
  15. _Size = 12
  16. _Bold = False
  17. _UndLine = False
  18. def SetColor(self,color):
  19. self._Color = color
  20. def GetColor(self):
  21. return self._Color
  22. def SetFont(self,fnt):
  23. self._FontObj = fnt
  24. def SetBold(self,bd):
  25. self._Bold = bd
  26. def SetUnderLine(self,bd):
  27. self._UndLine = bd
  28. def __init__(self,v=""):
  29. self._T = v
  30. def __str__(self):
  31. return self._T
  32. def __unicode__(self):
  33. return self._T.encode("utf-8")
  34. def __add__(self,a):
  35. return self._T + a
  36. def __repr__(self):
  37. return self._T
  38. def __len__(self):
  39. return len(self._T)
  40. def __eq__(self, other):
  41. return self._T == other
  42. def FnHeight(self):
  43. return self._FontObj.get_height()
  44. def Render(self):
  45. self._FontObj.set_bold(self._Bold)
  46. self._FontObj.set_underline(self._UndLine)
  47. sur = self._FontObj.render(self._T,True,self._Color)
  48. self._FontObj.set_bold(False)
  49. self._FontObj.set_underline(False)
  50. return sur
  51. class Text:
  52. _Words = []
  53. def __init__(self,content="",color=None,fnt=None,Bold=False,Und=False):
  54. self._Words = [ Word(x) for x in list(content) ]
  55. if color != None:
  56. self.SetColor(color)
  57. if fnt != None:
  58. self.SetFont(fnt)
  59. if Bold == True:
  60. self.SetBold(True)
  61. if Und == True:
  62. self.SetUnderLine(True)
  63. def SetColor(self,color):
  64. if len(self._Words) > 0:
  65. for i,x in enumerate(self._Words):
  66. self._Words[i].SetColor(color)
  67. def SetBold(self,bd):
  68. if len(self._Words) > 0:
  69. for i,x in enumerate(self._Words):
  70. self._Words[i].SetBold(bd)
  71. def SetUnderLine(self,bd):
  72. if len(self._Words) > 0:
  73. for i,x in enumerate(self._Words):
  74. self._Words[i].SetUnderLine(bd)
  75. def SetFont(self,fnt):
  76. if len(self._Words) > 0:
  77. for i,x in enumerate(self._Words):
  78. self._Words[i].SetFont(fnt)
  79. def __add__(self,a):
  80. return self._Words+a.Words()
  81. def Words(self):
  82. return self._Words
  83. class Textbulletinboard(Textarea):
  84. _TextLimit = 200
  85. _BackgroundColor = MySkinManager.GiveColor("White")
  86. _Align = "Left" ## Left or Center
  87. _RowPitch = -1 ## for \n
  88. _BreakPitch = -1 ## for linebreak line wrapp
  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. self._BlitWords = blit_rows
  120. self._BlitIndex = self._TextIndex
  121. def BlitText(self):
  122. # build up blitwords
  123. self.BuildBlitText()
  124. xmargin = 5
  125. endmargin = 5
  126. start_x = self._PosX+xmargin ##start_point_x
  127. start_y = self._PosY ## start_point_y
  128. x = self._PosX+xmargin ##start_point_x
  129. y = self._PosY ## start_point_y
  130. self._TextFull = len(self._MyWords) > self._TextLimit
  131. last_height = 0
  132. for row_idx, row in enumerate(self._BlitWords):
  133. if len(row) == 0:
  134. if self._RowPitch > 0:
  135. y = y + self._RowPitch
  136. else:
  137. y = y + 16
  138. w = 0
  139. continue
  140. else:
  141. total_row_width = 0
  142. for i,v in enumerate(row):
  143. t = v.Render()
  144. total_row_width += t.get_width()
  145. if total_row_width > self._Width-endmargin:
  146. start_x = self._PosX + xmargin
  147. else:
  148. if self._Align == "Center":
  149. start_x = (self._Width - total_row_width)/2
  150. last_height = 0
  151. row_width = 0
  152. x = start_x
  153. y = y + last_height
  154. for i,v in enumerate(row):
  155. t = v.Render()
  156. row_width += t.get_width()
  157. if last_height < v.FnHeight():
  158. last_height = v.FnHeight()
  159. if row_width >= self._Width-endmargin:
  160. x = start_x
  161. if self._Align == "Center":
  162. whatisleft = total_row_width - row_width
  163. if whatisleft >= self._Width-endmargin:
  164. x = start_x
  165. else:
  166. x = (self._Width-whatisleft)/2-endmargin
  167. if self._BreakPitch > 0:
  168. y = y + self._BreakPitch
  169. else:
  170. y = y + last_height
  171. row_width = 0
  172. self._CanvasHWND.blit(t, (x,y))
  173. x += t.get_width()
  174. def Draw(self):
  175. #aa_round_rect(self._CanvasHWND, (4,24.5+6,312,60),self._BackgroundColor,4,0,self._BackgroundColor)
  176. aa_round_rect(self._CanvasHWND,
  177. (self._PosX,self._PosY,self._Width,self._Height),self._BackgroundColor,4,0,self._BackgroundColor)
  178. self.BlitText()