foot_bar.py 6.7 KB

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