textarea.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from libs.roundrects import aa_round_rect
  4. ## local UI import
  5. from UI.page import Page,PageStack,PageSelector
  6. from UI.label import Label
  7. from UI.skin_manager import MySkinManager
  8. from UI.lang_manager import MyLangManager
  9. from UI.widget import Widget
  10. class Textarea(Widget):
  11. _BackgroundColor = MySkinManager.GiveColor('TitleBg')
  12. _CanvasHWND = None
  13. _MyWords = []
  14. _BlitWords = []
  15. _FontObj = None
  16. _LineNumber = 0
  17. _TextLimit = 63
  18. _TextFull = False
  19. _TextIndex = 0
  20. _BlitIndex = 0
  21. def __init__(self):
  22. pass
  23. def Init(self):
  24. self._FontObj = MyLangManager.TrFont("Eurostile15")
  25. #pygame.font.Font(fonts_path["Eurostile"],20)
  26. def SubTextIndex(self):
  27. self._TextIndex-=1
  28. if self._TextIndex < 0:
  29. self._TextIndex = 0
  30. def AddTextIndex(self):
  31. self._TextIndex +=1
  32. if self._TextIndex > len(self._MyWords):
  33. self._TextIndex = len(self._MyWords)
  34. def ResetMyWords(self):
  35. self._MyWords = []
  36. self._TextIndex = 0
  37. def RemoveFromLastText(self):
  38. if len(self._MyWords) > 0:
  39. self.SubTextIndex()
  40. del self._MyWords[self._TextIndex]
  41. return self._MyWords
  42. def AppendText(self,alphabet):
  43. self.AppendAndBlitText(alphabet)
  44. def AppendAndBlitText(self,alphabet):
  45. if self._TextFull != True:
  46. self._MyWords.insert(self._TextIndex,alphabet)
  47. self.BlitText()
  48. self.AddTextIndex()
  49. else:
  50. print("is Full %s" % "".join(str(self._MyWords)))
  51. def BuildBlitText(self):
  52. blit_rows = [[]]
  53. w = 0
  54. xmargin = 5
  55. endmargin = 15
  56. x = self._PosX+xmargin
  57. linenumber = 0
  58. cursor_row = 0
  59. for i, v in enumerate(self._MyWords):
  60. t = self._FontObj.render(v, True, (8, 135, 174))
  61. t_width = t.get_width()
  62. w += t_width
  63. del(t)
  64. blit_rows[linenumber].append(v)
  65. if i == self._TextIndex - 1:
  66. cursor_row = linenumber
  67. if w + t_width >= self._Width-endmargin:
  68. x = self._PosX+xmargin
  69. w = 0
  70. linenumber += 1
  71. blit_rows.append([])
  72. # only paint 2 rows
  73. if len(blit_rows) == 1:
  74. self._BlitWords = blit_rows[0]
  75. self._BlitIndex = self._TextIndex
  76. elif len(blit_rows) == 2 or cursor_row < 2:
  77. self._BlitWords = blit_rows[0] + blit_rows[1]
  78. self._BlitIndex = self._TextIndex
  79. else:
  80. self._BlitWords = blit_rows[cursor_row - 1] + blit_rows[cursor_row]
  81. self._BlitIndex = self._TextIndex
  82. for i,v in enumerate(blit_rows):
  83. if i == cursor_row - 1:
  84. break
  85. self._BlitIndex -= len(v)
  86. def BlitText(self):
  87. """
  88. blit every single word into surface and calc the width ,check multi line
  89. """
  90. # build up blitwords
  91. self.BuildBlitText()
  92. w = 0
  93. xmargin = 5
  94. endmargin = 15
  95. x = self._PosX+xmargin
  96. y = self._PosY
  97. linenumber = 0
  98. self._TextFull = len(self._MyWords) > self._TextLimit
  99. for i, v in enumerate(self._BlitWords):
  100. t = self._FontObj.render(v,True,(8,135,174))
  101. w += t.get_width()
  102. if w >= self._Width-endmargin and linenumber == 0:
  103. linenumber +=1
  104. x = self._PosX+xmargin
  105. y = self._PosY + t.get_height() * linenumber
  106. w = 0
  107. self._CanvasHWND.blit(t, (x,y))
  108. x += t.get_width()
  109. def Cursor(self):
  110. w = 0
  111. xmargin = 5
  112. endmargin = 15
  113. x = self._PosX+xmargin
  114. y = self._PosY
  115. linenumber = 0
  116. for i,v in enumerate(self._BlitWords[:self._BlitIndex]):
  117. t = self._FontObj.render(v,True,(8,135,174))
  118. w += t.get_width()
  119. if w >= self._Width-endmargin and linenumber == 0:
  120. x = self._PosX+xmargin
  121. y = self._PosY+ t.get_height()
  122. w = 0
  123. linenumber +=1
  124. if w >= self._Width-endmargin*3 and linenumber > 0:
  125. x += t.get_width()
  126. break
  127. x += t.get_width()
  128. self._CanvasHWND.blit(self._FontObj.render("_",True,(0,0,0)),(x+1,y-2))
  129. def Draw(self):
  130. #aa_round_rect(self._CanvasHWND, (4,24.5+6,312,60),self._BackgroundColor,4,0,self._BackgroundColor)
  131. aa_round_rect(self._CanvasHWND,
  132. (self._PosX,self._PosY,self._Width,self._Height),self._BackgroundColor,4,0,self._BackgroundColor)
  133. self.BlitText()
  134. self.Cursor()