icon_item.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. ## local import
  4. from constants import icon_width,icon_height,ICON_TYPES,ALIGN,icon_ext,Width,Height
  5. from util_funcs import color_surface,midRect
  6. from label import Label
  7. from lang_manager import MyLangManager
  8. class IconItem:
  9. _PosX=0
  10. _PosY=0
  11. _Width=0
  12. _Height=0
  13. _ImageName=""
  14. _ImgSurf = None
  15. _Parent = None
  16. _Index = 0
  17. _MyType = ICON_TYPES["EXE"]
  18. _CmdPath = ""
  19. _LinkPage = None
  20. _Label = None
  21. _Align = ALIGN["VCenter"] # set for the Icon Image and Text Label
  22. def __init__(self):
  23. self._ImgSurf=None
  24. def Init(self,x,y,w,h,at): # the Surface is assigned in Screen
  25. self._PosX = x
  26. self._PosY = y
  27. self._Width = w
  28. self._Height = h
  29. self._AnimationTime = at
  30. def SetLableColor(self,color):
  31. self._Label.SetColor(color)
  32. def NewCoord(self,x,y):
  33. self._PosX = x
  34. self._PosY = y
  35. def AddLabel(self,text,fontobj):
  36. if self._Label == None:
  37. self._Label = Label()
  38. self._Label.Init(text,fontobj)
  39. else:
  40. #just replace the text
  41. self._Label._Init(text,fontobj)
  42. def Adjust(self,x,y,w,h,at): # the Surface is assigned in Screen
  43. self.Init(x,y,w,h,at)
  44. if self._Label != None:
  45. self._Label.SetCanvasHWND( self._Parent._CanvasHWND)
  46. self.CreateImageSurf()
  47. self.AdjustLinkPage()
  48. def AdjustLinkPage(self):
  49. if self._MyType==ICON_TYPES["DIR"] and self._LinkPage != None:
  50. self._LinkPage._Index = 0
  51. self._LinkPage._Align = ALIGN["SLeft"]
  52. self._LinkPage._IconNumbers = len(self._LinkPage._Icons)
  53. self._LinkPage._Screen = self._Parent._Screen
  54. self._LinkPage._CanvasHWND = self._Parent._Screen._CanvasHWND
  55. self._LinkPage._FootMsg = ["Nav","","","Back","Enter"] ## Default Page Foot info
  56. if self._LinkPage._Align == ALIGN["HLeft"]:
  57. self._LinkPage.AdjustHLeftAlign()
  58. elif self._LinkPage._Align == ALIGN["SLeft"]:
  59. self._LinkPage.AdjustSAutoLeftAlign()
  60. if self._LinkPage._IconNumbers > 1:
  61. self._LinkPage._PsIndex = 1
  62. self._LinkPage._IconIndex = self._LinkPage._PsIndex
  63. def CreateImageSurf(self):
  64. if self._ImgSurf == None and self._ImageName != "":
  65. # print(self._ImageName)
  66. self._ImgSurf = pygame.image.load( self._ImageName ).convert_alpha()
  67. if self._ImgSurf.get_width() > icon_width or self._ImgSurf.get_height() > icon_height:
  68. self._ImgSurf = pygame.transform.scale(self._ImgSurf,(icon_width,icon_height))
  69. def ChangeImgSurfColor(self,color):
  70. color_surface(self._ImgSurf,color)
  71. def Clear(self):
  72. pass
  73. def Draw(self):
  74. if self._Align==ALIGN["VCenter"]: #default
  75. if self._Label != None:
  76. self._Label._PosX = self._PosX - self._Label._Width/2 + self._Parent._PosX
  77. self._Label._PosY = self._PosY + self._Height/2 +6 + self._Parent._PosY
  78. elif self._Align ==ALIGN["HLeft"]:
  79. if self._Label != None:
  80. self._Label._PosX = self._PosX + self._Width/2 + 3 + self._Parent._PosX
  81. self._Label._PosY = self._PosY - self._Label._Height/2 + self._Parent._PosY
  82. if self._Label!=None:
  83. self._Label.Draw()
  84. if self._ImgSurf != None:
  85. self._Parent._CanvasHWND.blit(self._ImgSurf,midRect(self._PosX+self._Parent._PosX,
  86. self._PosY+self._Parent._PosY,
  87. self._Width,self._Height,Width,Height))