foot_bar.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. icon_base_path = SkinMap("gameshell/footbar_icons/")
  13. class FootBarIcon(MultiIconItem):
  14. def TotalWidth(self):
  15. return self._Width+self._Label._Width
  16. def Draw(self):
  17. if self._Align==ALIGN["VCenter"]: #default
  18. if self._Label != None:
  19. self._Label._PosX = self._PosX - self._Label._Width/2
  20. self._Label._PosY = self._PosY + self._Height/2 + 12
  21. elif self._Align ==ALIGN["HLeft"]:
  22. if self._Label != None:
  23. self._Label._PosX = self._PosX + self._Width/2 + 3
  24. self._Label._PosY = self._PosY - self._Label._Height/2
  25. if self._Label!=None:
  26. self._Label.Draw()
  27. if self._ImgSurf != None:
  28. self._Parent._CanvasHWND.blit(self._ImgSurf,midRect(self._PosX,
  29. self._PosY,
  30. self._Width,self._Height,Width,Height),
  31. (0,self._IconIndex*self._IconHeight,self._IconWidth,self._IconHeight))
  32. class FootBar:
  33. _PosX = 0
  34. _PosY = 0
  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 = fonts["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"]
  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("Nav.")
  79. self._State = "normal"
  80. self.Draw()
  81. return False
  82. def UpdateNavText(self,texts):
  83. self._State = "tips"
  84. my_text = self._LabelFont.render(texts,True,self._SkinManager.GiveColor("Text"))
  85. """
  86. _w = 0
  87. for i, x in enumerate(("b","a","y","x")):
  88. if self._Icons[x]._Label._Text!="":
  89. if i==0:
  90. _w += self._Icons[x].TotalWidth()
  91. else:
  92. _w += self._Icons[x].TotalWidth()+5
  93. """
  94. left_width = self._Width - 18
  95. final_piece = ""
  96. for i ,v in enumerate(texts):
  97. text_slice = texts[:i+1]
  98. my_text = self._LabelFont.render(text_slice,True, self._SkinManager.GiveColor("Text"))
  99. final_piece = text_slice
  100. if my_text.get_width() >= left_width:
  101. break
  102. print("finalpiece %s" %final_piece)
  103. self._Icons["nav"]._Label.SetText( final_piece )
  104. self.Draw()
  105. def GetButtonsLayoutMode(self):
  106. lm = "xbox"
  107. try:
  108. with open(".buttonslayout", "r") as f:
  109. lm = f.read()
  110. except:
  111. None
  112. if lm not in ["xbox","snes"]:
  113. lm = "xbox"
  114. return lm
  115. def SetLabelTexts(self,texts):
  116. barr = ["nav","x","y","a","b"]
  117. if self.GetButtonsLayoutMode() == "snes":
  118. barr = ["nav","y","x","b","a"]
  119. for idx,x in enumerate(barr):
  120. try:
  121. self._Icons[x]._Label.SetText(texts[idx])
  122. except IndexError:
  123. print("Index "+x+" doesn't exist!")
  124. def ClearCanvas(self):
  125. self._CanvasHWND.fill( self._SkinManager.GiveColor("White") )
  126. self._Icons["round_corners"].NewCoord(5,self._Height -5 )
  127. self._Icons["round_corners"]._IconIndex = 2
  128. self._Icons["round_corners"].Draw()
  129. self._Icons["round_corners"].NewCoord(self._Width-5,self._Height-5)
  130. self._Icons["round_corners"]._IconIndex = 3
  131. self._Icons["round_corners"].Draw()
  132. """
  133. aa_round_rect(self._CanvasHWND,
  134. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  135. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,0,Width,self._BarHeight/2), 0 )
  136. """
  137. def Draw(self):
  138. self.ClearCanvas()
  139. self._Icons["nav"].NewCoord(self._IconWidth/2+3,self._IconHeight/2+2)
  140. self._Icons["nav"].Draw()
  141. if self._State == "normal":
  142. _w=0
  143. #for i,x in enumerate(("a","b","x","y")):
  144. for i, x in enumerate(("b","a","y","x")):
  145. if self._Icons[x]._Label._Text!="":
  146. if i==0:
  147. _w += self._Icons[x].TotalWidth()
  148. else:
  149. _w += self._Icons[x].TotalWidth()+5
  150. start_x = self._Width - _w
  151. start_y = self._IconHeight/2+2
  152. self._Icons[x].NewCoord(start_x,start_y)
  153. self._Icons[x].Draw()
  154. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,0),(Width,0),self._BorderWidth)
  155. if self._HWND != None:
  156. self._HWND.blit(self._CanvasHWND,(self._PosX,Height - self._Height,Width,self._BarHeight))