skin_manager.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import config
  4. import ConfigParser
  5. from util_funcs import FileExists
  6. class CaseConfigParser(ConfigParser.SafeConfigParser):
  7. def optionxform(self, optionstr):
  8. return optionstr
  9. class SkinManager(object):
  10. """
  11. _HighColor = pygame.Color(51,166,255) # #33a6ff
  12. _TextColor = pygame.Color(83,83,83) # #535353
  13. _FrontColor = pygame.Color(131,199,219) ## light blue,#83c7db
  14. _URLColor = pygame.Color(51,166,255) ## blue more #33a6ff
  15. _LineColor = pygame.Color(169,169,169) # #a9a9a9
  16. _TitleBgColor = pygame.Color(228,228,228) # #e4e4e4
  17. _ActiveColor = pygame.Color(175,90,0) ## light brown #af5a00
  18. """
  19. _Colors = {}
  20. _Config = None
  21. _Fonts = {}
  22. #backup skin
  23. DefaultSkin = "../skin/default"
  24. def __init__(self):
  25. self.Init()
  26. def ConvertToRGB(self,hexstr):
  27. h = hexstr.lstrip('#')
  28. return tuple(int(h[i:i+2], 16) for i in (0, 2 ,4))
  29. def Init(self):
  30. if not SkinManager._Colors:
  31. self.SetColors()
  32. if not SkinManager._Fonts:
  33. self.SetFonts()
  34. def SetFonts(self):
  35. if not pygame.font.get_init():
  36. pygame.font.init()
  37. skinpath = config.SKIN+"/truetype"
  38. fonts_path = {}
  39. fonts_path["varela"] = "%s/VarelaRound-Regular.ttf" % skinpath
  40. fonts_path["veramono"] = "%s/VeraMono.ttf" % skinpath
  41. fonts_path["noto"] = "%s/NotoSansMono-Regular.ttf" % skinpath
  42. fonts_path["notocjk"] = "%s/NotoSansCJK-Regular.ttf" % skinpath
  43. fonts_path["Eurostile"] = "%s/EurostileMN-Medium.pfb.ttf" %skinpath
  44. #print(fonts_path["Eurostile"])
  45. fonts_path["EurostileBold"] = "%s/EurostileMN-ExtendedBold.pfb.ttf" % skinpath
  46. for i in range(10,29):
  47. self._Fonts["varela%d"%i] = pygame.font.Font(fonts_path["varela"],i)
  48. for i in range(10,26):
  49. self._Fonts["veramono%d"%i] = pygame.font.Font(fonts_path["veramono"],i)
  50. for i in range(10,28):
  51. self._Fonts["notosansmono%d"%i] = pygame.font.Font(fonts_path["noto"],i)
  52. for i in range(10,28):
  53. self._Fonts["notosanscjk%d"%i] = pygame.font.Font(fonts_path["notocjk"],i)
  54. for i in range(10,29):
  55. self._Fonts["Eurostile%d"%i] = pygame.font.Font(fonts_path["Eurostile"],i)
  56. self._Fonts["Eurostile11"] = pygame.font.Font(fonts_path["Eurostile"],11)
  57. self._Fonts["Eurostile12"] = pygame.font.Font(fonts_path["Eurostile"],12)
  58. self._Fonts["Eurostile15"] = pygame.font.Font(fonts_path["Eurostile"],15)
  59. self._Fonts["Eurostile34"] = pygame.font.Font(fonts_path["Eurostile"],34)
  60. self._Fonts["Eurostile40"] = pygame.font.Font(fonts_path["Eurostile"],40)
  61. self._Fonts["Eurostile120"] = pygame.font.Font(fonts_path["Eurostile"],120)
  62. for i in range(10,30):
  63. self._Fonts["EurostileBold%d"%i] = pygame.font.Font(fonts_path["EurostileBold"],i)
  64. self._Fonts["EurostileBold13"] = pygame.font.Font(fonts_path["EurostileBold"],13)
  65. self._Fonts["EurostileBold30"] = pygame.font.Font(fonts_path["EurostileBold"],30)
  66. self._Fonts["arial"] = pygame.font.SysFont("arial",16)
  67. def SetColors(self):
  68. Colors = {}
  69. Colors["High"] = pygame.Color(51, 166, 255)
  70. Colors["Text"] = pygame.Color(83, 83, 83)
  71. Colors["ReadOnlyText"] = pygame.Color(130,130,130)
  72. Colors["Front"] = pygame.Color(131, 199, 219)
  73. Colors["URL"] = pygame.Color(51, 166, 255)
  74. Colors["Line"] = pygame.Color(169, 169, 169)
  75. Colors["TitleBg"] = pygame.Color(228, 228, 228)
  76. Colors["Active"] = pygame.Color(175, 90, 0)
  77. Colors["Inactive"] = pygame.Color(120, 160, 140)
  78. Colors["Disabled"] = pygame.Color(204, 204, 204)
  79. Colors["White"] = pygame.Color(255, 255, 255)
  80. Colors["Black"] = pygame.Color(0, 0, 0)
  81. SkinManager._Colors = Colors
  82. self._Config = CaseConfigParser()
  83. fname = config.SKIN+"/config.ini"
  84. try:
  85. self._Config.read(fname)
  86. except Exception, e:
  87. print("read skin config.cfg error %s" % str(e))
  88. return
  89. else:
  90. if "Colors" in self._Config.sections():
  91. colour_opts = self._Config.options("Colors")
  92. # print(colour_opts)
  93. for i in SkinManager._Colors:
  94. if i in colour_opts:
  95. try:
  96. SkinManager._Colors[i] = self.ConvertToRGB(
  97. self._Config.get("Colors", i))
  98. except Exception, e:
  99. print("error in ConvertToRGB %s" % str(e))
  100. continue
  101. def GiveFont(self,name):
  102. return SkinManager._Fonts[name]
  103. def GiveColor(self,name):
  104. if name in SkinManager._Colors:
  105. return SkinManager._Colors[name]
  106. else:
  107. return pygame.Color(255,0,0)
  108. def GiveIcon(self,orig_file_or_dir): ## return is string,not Surface
  109. #doing a wrapper for items under /home/cpi/apps/Menu/*, to be like Menu/GameShell/*
  110. if orig_file_or_dir.startswith("/home/cpi/apps/Menu"):
  111. orig_file_or_dir = orig_file_or_dir.replace("/home/cpi/apps/Menu/","../Menu/GameShell/")
  112. if orig_file_or_dir.startswith(".."):
  113. ret = orig_file_or_dir.replace("..",config.SKIN)
  114. if FileExists(ret) == False:
  115. ret = orig_file_or_dir.replace("..",self.DefaultSkin)
  116. else:
  117. ret = config.SKIN+"/sys.py/"+orig_file_or_dir
  118. if FileExists(ret) == False:
  119. ret = self.DefaultSkin+"/sys.py/"+orig_file_or_dir
  120. if FileExists( ret ):
  121. return ret
  122. else: ## if not existed both in default or custom skin ,return where it is
  123. return orig_file_or_dir
  124. def GiveWallpaper(self,png_name):
  125. #first SKIN/wallpapers/xxxx.png
  126. #second ../skin/DEOT/wallpapers/xxxx.png
  127. #finnal gameshell/wallpaper/xxxx.png
  128. #loading.png,seeyou.png,updating.png,gameover.png,desktopbg.png
  129. wlp = "/wallpaper/"
  130. if FileExists(config.SKIN+wlp+png_name):
  131. return config.SKIN+wlp+png_name
  132. elif FileExists(self.DefaultSkin+wlp+png_name):
  133. return self.DefaultSkin+wlp+png_name
  134. else:
  135. return "gameshell/wallpaper/"+png_name
  136. ##global MySkinManager Handler
  137. MySkinManager = None
  138. def InitMySkinManager():
  139. global MySkinManager
  140. if MySkinManager == None:
  141. MySkinManager = SkinManager()
  142. InitMySkinManager()