skin_manager.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. DefaultSkin = "../skin/default"
  23. def __init__(self):
  24. self.Init()
  25. def configExists(self):
  26. if FileExists(config.SKIN+"/config.ini"):
  27. self._Config = CaseConfigParser()
  28. fname = config.SKIN+"/config.ini"
  29. try:
  30. return self._Config.read(fname)
  31. except Exception, e:
  32. print("skin config.ini read error %s" % str(e))
  33. return
  34. else:
  35. print("no skin config.ini file to read")
  36. return
  37. def ConvertToRGB(self,hexstr):
  38. h = hexstr.lstrip('#')
  39. return tuple(int(h[i:i+2], 16) for i in (0, 2 ,4))
  40. def Init(self):
  41. if not SkinManager._Colors:
  42. self.SetColors()
  43. if not SkinManager._Fonts:
  44. self.SetFonts()
  45. def SetFonts(self):
  46. if not pygame.font.get_init():
  47. pygame.font.init()
  48. skinpath = config.SKIN+"/truetype"
  49. fonts_path = {}
  50. fonts_path["varela"] = "%s/VarelaRound-Regular.ttf" % skinpath
  51. fonts_path["veramono"] = "%s/VeraMono.ttf" % skinpath
  52. fonts_path["noto"] = "%s/NotoSansMono-Regular.ttf" % skinpath
  53. fonts_path["notocjk"] = "%s/NotoSansCJK-Regular.ttf" % skinpath
  54. if self.configExists():
  55. if "Font_Paths" in self._Config.sections():
  56. font_opts = self._Config.options("Font_Paths")
  57. for i in fonts_path:
  58. if i in font_opts:
  59. try:
  60. fonts_path[i] = skinpath+"/"+self._Config.get("Font_Paths", i)+".ttf"
  61. except Exception, e:
  62. print("error in Font_Paths %s" % str(e))
  63. continue
  64. for i in range(10,29):
  65. self._Fonts["varela%d"%i] = pygame.font.Font(fonts_path["varela"],i)
  66. self._Fonts["varela34"] = pygame.font.Font(fonts_path["varela"],34)
  67. self._Fonts["varela40"] = pygame.font.Font(fonts_path["varela"],40)
  68. self._Fonts["varela120"] = pygame.font.Font(fonts_path["varela"],120)
  69. for i in range(10,26):
  70. self._Fonts["veramono%d"%i] = pygame.font.Font(fonts_path["veramono"],i)
  71. for i in range(10,28):
  72. self._Fonts["notosansmono%d"%i] = pygame.font.Font(fonts_path["noto"],i)
  73. for i in range(10,28):
  74. self._Fonts["notosanscjk%d"%i] = pygame.font.Font(fonts_path["notocjk"],i)
  75. self._Fonts["arial"] = pygame.font.SysFont("arial",16)
  76. def SetColors(self):
  77. Colors = {}
  78. Colors["High"] = pygame.Color(51, 166, 255)
  79. Colors["Text"] = pygame.Color(83, 83, 83)
  80. Colors["ReadOnlyText"] = pygame.Color(130,130,130)
  81. Colors["Front"] = pygame.Color(131, 199, 219)
  82. Colors["URL"] = pygame.Color(51, 166, 255)
  83. Colors["Line"] = pygame.Color(169, 169, 169)
  84. Colors["TitleBg"] = pygame.Color(228, 228, 228)
  85. Colors["Active"] = pygame.Color(175, 90, 0)
  86. Colors["Disabled"] = pygame.Color(204, 204, 204)
  87. Colors["White"] = pygame.Color(255, 255, 255)
  88. Colors["Black"] = pygame.Color(0, 0, 0)
  89. if self.configExists():
  90. if "Colors" in self._Config.sections():
  91. colour_opts = self._Config.options("Colors")
  92. for i in Colors:
  93. if i in colour_opts:
  94. try:
  95. Colors[i] = self.ConvertToRGB(
  96. self._Config.get("Colors", i))
  97. except Exception, e:
  98. print("error in ConvertToRGB %s" % str(e))
  99. continue
  100. SkinManager._Colors = Colors
  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/default/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()