foot_bar.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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
  7. from icon_item import IconItem
  8. from multi_icon_item import MultiIconItem
  9. from icon_pool import MyIconPool
  10. from libs.roundrects import aa_round_rect
  11. from lang_manager import MyLangManager
  12. from widget import Widget
  13. from skin_manager import MySkinManager
  14. import config
  15. icon_base_path = MySkinManager.GiveIcon("gameshell/footbar_icons/")
  16. class FootBarIcon(MultiIconItem):
  17. def TotalWidth(self):
  18. return self._Width+self._Label._Width
  19. def Draw(self):
  20. if self._Align==ALIGN["VCenter"]: #default
  21. if self._Label != None:
  22. self._Label._PosX = self._PosX - self._Label._Width/2
  23. self._Label._PosY = self._PosY + self._Height/2 + 12
  24. elif self._Align ==ALIGN["HLeft"]:
  25. if self._Label != None:
  26. self._Label._PosX = self._PosX + self._Width/2 + 3
  27. self._Label._PosY = self._PosY - self._Label._Height/2
  28. if self._Label!=None:
  29. self._Label.Draw()
  30. if self._ImgSurf != None:
  31. self._Parent._CanvasHWND.blit(self._ImgSurf,midRect(self._PosX,
  32. self._PosY,
  33. self._Width,self._Height,Width,Height),
  34. (0,self._IconIndex*self._IconHeight,self._IconWidth,self._IconHeight))
  35. class FootBar(Widget):
  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("Eurostile10")
  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","select"]
  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.GiveIconSurface("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 SetLabelTexts(self,texts):
  108. barr = ["nav","y","x","b","a","select"]
  109. texts2 = texts + [""] if len(texts) == 5 else texts
  110. for idx,x in enumerate(barr):
  111. try:
  112. self._Icons[x]._Label.SetText(MyLangManager.Tr(texts2[idx]))
  113. except IndexError:
  114. print("Index "+x+" doesn't exist!")
  115. def ClearCanvas(self):
  116. self._CanvasHWND.fill( self._SkinManager.GiveColor("White") )
  117. self._Icons["round_corners"].NewCoord(5,self._Height -5 )
  118. self._Icons["round_corners"]._IconIndex = 2
  119. self._Icons["round_corners"].Draw()
  120. self._Icons["round_corners"].NewCoord(self._Width-5,self._Height-5)
  121. self._Icons["round_corners"]._IconIndex = 3
  122. self._Icons["round_corners"].Draw()
  123. """
  124. aa_round_rect(self._CanvasHWND,
  125. (0,0,self._Width,self._Height),self._BgColor,8,0, self._BgColor)
  126. pygame.draw.rect(self._CanvasHWND,self._BgColor,(0,0,Width,self._BarHeight/2), 0 )
  127. """
  128. def Draw(self):
  129. self.ClearCanvas()
  130. self._Icons["nav"].NewCoord(self._IconWidth/2+3,self._IconHeight/2+2)
  131. self._Icons["nav"].Draw()
  132. if self._State == "normal":
  133. _w=0
  134. #for i,x in enumerate(("a","b","x","y")):
  135. for i, x in enumerate(("b","a","y","x","select")):
  136. if self._Icons[x]._Label._Text!="":
  137. if i==0:
  138. _w += self._Icons[x].TotalWidth()
  139. else:
  140. _w += self._Icons[x].TotalWidth()+5
  141. start_x = self._Width - _w
  142. start_y = self._IconHeight/2+2
  143. self._Icons[x].NewCoord(start_x,start_y)
  144. self._Icons[x].Draw()
  145. pygame.draw.line(self._CanvasHWND,self._SkinManager.GiveColor("Line"),(0,0),(Width,0),self._BorderWidth)
  146. if self._HWND != None:
  147. self._HWND.blit(self._CanvasHWND,(self._PosX,Height - self._Height,Width,self._BarHeight))