foot_bar.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. def UpdateNavText(self,texts):
  82. self._State = "tips"
  83. my_text = self._LabelFont.render(texts,True,self._SkinManager.GiveColor("Text"))
  84. """
  85. _w = 0
  86. for i, x in enumerate(("b","a","y","x")):
  87. if self._Icons[x]._Label._Text!="":
  88. if i==0:
  89. _w += self._Icons[x].TotalWidth()
  90. else:
  91. _w += self._Icons[x].TotalWidth()+5
  92. """
  93. left_width = self._Width - 18
  94. final_piece = ""
  95. for i ,v in enumerate(texts):
  96. text_slice = texts[:i+1]
  97. my_text = self._LabelFont.render(text_slice,True, self._SkinManager.GiveColor("Text"))
  98. final_piece = text_slice
  99. if my_text.get_width() >= left_width:
  100. break
  101. print("finalpiece %s" %final_piece)
  102. self._Icons["nav"]._Label.SetText( final_piece )
  103. self.Draw()
  104. def GetButtonsLayoutMode(self):
  105. lm = "xbox"
  106. try:
  107. with open(".buttonslayout", "r") as f:
  108. lm = f.read()
  109. except:
  110. None
  111. if lm not in ["xbox","snes"]:
  112. lm = "xbox"
  113. return lm
  114. def SetLabelTexts(self,texts):
  115. barr = ["nav","x","y","a","b"]
  116. if self.GetButtonsLayoutMode() == "snes":
  117. barr = ["nav","y","x","b","a"]
  118. for idx,x in enumerate(barr):
  119. try:
  120. self._Icons[x]._Label.SetText(texts[idx])
  121. except IndexError:
  122. print("Index "+x+" doesn't exist!")
  123. def ClearCanvas(self):
  124. self._CanvasHWND.fill( self._SkinManager.GiveColor("White") )
  125. self._Icons["round_corners"].NewCoord(5,self._Height -5 )
  126. self._Icons["round_corners"]._IconIndex = 2
  127. self._Icons["round_corners"].Draw()
  128. self._Icons["round_corners"].NewCoord(self._Width-5,self._Height-5)
  129. self._Icons["round_corners"]._IconIndex = 3
  130. self._Icons["round_corners"].Draw()
  131. """
  132. aa_round_rect(self._CanvasHWND,
  133. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  134. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,0,Width,self._BarHeight/2), 0 )
  135. """
  136. def Draw(self):
  137. self.ClearCanvas()
  138. self._Icons["nav"].NewCoord(self._IconWidth/2+3,self._IconHeight/2+2)
  139. self._Icons["nav"].Draw()
  140. if self._State == "normal":
  141. _w=0
  142. #for i,x in enumerate(("a","b","x","y")):
  143. for i, x in enumerate(("b","a","y","x")):
  144. if self._Icons[x]._Label._Text!="":
  145. if i==0:
  146. _w += self._Icons[x].TotalWidth()
  147. else:
  148. _w += self._Icons[x].TotalWidth()+5
  149. start_x = self._Width - _w
  150. start_y = self._IconHeight/2+2
  151. self._Icons[x].NewCoord(start_x,start_y)
  152. self._Icons[x].Draw()
  153. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,0),(Width,0),self._BorderWidth)
  154. if self._HWND != None:
  155. self._HWND.blit(self._CanvasHWND,(self._PosX,Height - self._Height,Width,self._BarHeight))