foot_bar.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import os
  4. ##local import
  5. from constants import Width,Height,ICON_TYPES,ALIGN
  6. from util_funcs import FileExists,midRect,SkinMap
  7. from icon_item import IconItem
  8. from fonts import fonts
  9. from multi_icon_item import MultiIconItem
  10. from icon_pool import MyIconPool
  11. from libs.roundrects import aa_round_rect
  12. from lang_manager import MyLangManager
  13. from widget import Widget
  14. icon_base_path = SkinMap("gameshell/footbar_icons/")
  15. class FootBarIcon(MultiIconItem):
  16. def TotalWidth(self):
  17. return self._Width+self._Label._Width
  18. def Draw(self):
  19. if self._Align==ALIGN["VCenter"]: #default
  20. if self._Label != None:
  21. self._Label._PosX = self._PosX - self._Label._Width/2
  22. self._Label._PosY = self._PosY + self._Height/2 + 12
  23. elif self._Align ==ALIGN["HLeft"]:
  24. if self._Label != None:
  25. self._Label._PosX = self._PosX + self._Width/2 + 3
  26. self._Label._PosY = self._PosY - self._Label._Height/2
  27. if self._Label!=None:
  28. self._Label.Draw()
  29. if self._ImgSurf != None:
  30. self._Parent._CanvasHWND.blit(self._ImgSurf,midRect(self._PosX,
  31. self._PosY,
  32. self._Width,self._Height,Width,Height),
  33. (0,self._IconIndex*self._IconHeight,self._IconWidth,self._IconHeight))
  34. class FootBar(Widget):
  35. _Width = Width
  36. _Height = 20
  37. _BarHeight = 20.5
  38. _BorderWidth = 1
  39. _CanvasHWND = None
  40. _HWND = None
  41. _Icons = {}
  42. _IconWidth = 18
  43. _IconHeight = 18
  44. _LabelFont = MyLangManager.TrFont("veramono10")
  45. _State = "normal"
  46. _SkinManager = None
  47. def __init__(self):
  48. self._Icons = {}
  49. def ReadFootBarIcons(self,icondir):
  50. if FileExists(icondir) == False and os.path.isdir(icondir) == False:
  51. return
  52. keynames = ["nav","x","y","a","b","select"]
  53. share_surf = pygame.image.load(icon_base_path+"footbar.png").convert_alpha()
  54. files = os.listdir(icondir)
  55. for _i,i in enumerate( keynames):
  56. it = FootBarIcon()
  57. it._MyType = ICON_TYPES["NAV"]
  58. it._Parent = self
  59. it._ImgSurf= share_surf
  60. it._Align = ALIGN["HLeft"] # (x)text <=
  61. it.AddLabel("game",self._LabelFont)
  62. it.Adjust(self._IconWidth/2+_i*self._IconWidth, self._IconHeight/2+2, self._IconWidth, self._IconHeight,0)
  63. it._IconIndex = _i
  64. self._Icons[i] = it
  65. def Init(self,screen):
  66. self._HWND = screen
  67. self._CanvasHWND = pygame.Surface((Width,int(self._BarHeight)))
  68. self.ReadFootBarIcons(icon_base_path)
  69. round_corners = MultiIconItem()
  70. round_corners._IconWidth = 10
  71. round_corners._IconHeight = 10
  72. round_corners._MyType = ICON_TYPES["STAT"]
  73. round_corners._Parent = self
  74. round_corners._ImgSurf = MyIconPool._Icons["roundcorners"]
  75. round_corners.Adjust(0,0,10,10,0)
  76. self._Icons["round_corners"] = round_corners
  77. def ResetNavText(self):
  78. self._Icons["nav"]._Label.SetText(MyLangManager.Tr("Nav"))
  79. self._State = "normal"
  80. self.Draw()
  81. return False
  82. def UpdateNavText(self,texts):
  83. self._State = "tips"
  84. texts = MyLangManager.Tr(texts)
  85. my_text = self._LabelFont.render(texts,True,self._SkinManager.GiveColor("Text"))
  86. """
  87. _w = 0
  88. for i, x in enumerate(("b","a","y","x")):
  89. if self._Icons[x]._Label._Text!="":
  90. if i==0:
  91. _w += self._Icons[x].TotalWidth()
  92. else:
  93. _w += self._Icons[x].TotalWidth()+5
  94. """
  95. left_width = self._Width - 18
  96. final_piece = ""
  97. for i ,v in enumerate(texts):
  98. text_slice = texts[:i+1]
  99. my_text = self._LabelFont.render(text_slice,True, self._SkinManager.GiveColor("Text"))
  100. final_piece = text_slice
  101. if my_text.get_width() >= left_width:
  102. break
  103. print("finalpiece %s" %final_piece)
  104. self._Icons["nav"]._Label.SetText( final_piece )
  105. self.Draw()
  106. def SetLabelTexts(self,texts):
  107. barr = ["nav","y","x","b","a","select"]
  108. texts2 = texts + [""] if len(texts) == 5 else texts
  109. for idx,x in enumerate(barr):
  110. try:
  111. self._Icons[x]._Label.SetText(MyLangManager.Tr(texts2[idx]))
  112. except IndexError:
  113. print("Index "+x+" doesn't exist!")
  114. def ClearCanvas(self):
  115. self._CanvasHWND.fill( self._SkinManager.GiveColor("White") )
  116. self._Icons["round_corners"].NewCoord(5,self._Height -5 )
  117. self._Icons["round_corners"]._IconIndex = 2
  118. self._Icons["round_corners"].Draw()
  119. self._Icons["round_corners"].NewCoord(self._Width-5,self._Height-5)
  120. self._Icons["round_corners"]._IconIndex = 3
  121. self._Icons["round_corners"].Draw()
  122. """
  123. aa_round_rect(self._CanvasHWND,
  124. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  125. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,0,Width,self._BarHeight/2), 0 )
  126. """
  127. def Draw(self):
  128. self.ClearCanvas()
  129. self._Icons["nav"].NewCoord(self._IconWidth/2+3,self._IconHeight/2+2)
  130. self._Icons["nav"].Draw()
  131. if self._State == "normal":
  132. _w=0
  133. #for i,x in enumerate(("a","b","x","y")):
  134. for i, x in enumerate(("b","a","y","x","select")):
  135. if self._Icons[x]._Label._Text!="":
  136. if i==0:
  137. _w += self._Icons[x].TotalWidth()
  138. else:
  139. _w += self._Icons[x].TotalWidth()+5
  140. start_x = self._Width - _w
  141. start_y = self._IconHeight/2+2
  142. self._Icons[x].NewCoord(start_x,start_y)
  143. self._Icons[x].Draw()
  144. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,0),(Width,0),self._BorderWidth)
  145. if self._HWND != None:
  146. self._HWND.blit(self._CanvasHWND,(self._PosX,Height - self._Height,Width,self._BarHeight))