foot_bar.py 5.3 KB

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