util_funcs.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import os
  4. #from libs import easing
  5. #from datetime import datetime
  6. #import base64
  7. #from beeprint import pp
  8. import string
  9. from Xlib import X,display
  10. def X_center_mouse():
  11. d = display.Display()
  12. s = d.screen()
  13. root = s.root
  14. width = s.width_in_pixels
  15. height = s.height_in_pixels
  16. # print(width,height)
  17. root.warp_pointer(width/2,height/2)
  18. d.sync()
  19. def IsPythonPackage(self,dirname):
  20. files = os.listdir(dirname)
  21. for i in sorted(files):
  22. if i.endswith("__init__.py"):
  23. return True
  24. return False
  25. def MakeExecutable(path):
  26. mode = os.stat(path).st_mode
  27. mode |= (mode & 0o444) >> 2 # copy R bits to X
  28. os.chmod(path, mode)
  29. def GetExePath():# get self dir
  30. #dir_path = os.path.dirname(os.path.realpath(__file__))
  31. dir_path = os.getcwd()
  32. return dir_path
  33. def CmdClean(cmdpath):#escape spec chars
  34. spchars = "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
  35. for i in spchars:
  36. cmdpath = string.replace(cmdpath,i,"\\"+i)
  37. return cmdpath
  38. def ReplaceSuffix(orig_file_str,new_ext):
  39. filename,ext = os.path.splitext(orig_file_str)
  40. ext = ext.strip()
  41. if ext != "":
  42. return "%s.%s"%(filename,new_ext)
  43. def FileExists(name): # both file and dir checked
  44. return os.path.exists(name)
  45. def ReadTheFileContent(filename):
  46. data = ""
  47. with open(filename, 'r') as myfile:
  48. data = myfile.read()
  49. return data
  50. def midRect(x,y,width,height,canWidth,canHeight):
  51. return pygame.Rect(min(canWidth,x-width/2),min(canHeight,y-height/2),width,height)
  52. #surface color change
  53. def color_surface(surface, color):
  54. red = color.r
  55. green = color.g
  56. blue = color.b
  57. arr = pygame.surfarray.pixels3d(surface)
  58. arr[:,:,0] = red
  59. arr[:,:,1] = green
  60. arr[:,:,2] = blue
  61. def DrawText(canvas,text, x,y,width,height,canWidth,canHeight,fontObj):# text for content,fontObj for pygame.font.Font
  62. _w = 0
  63. _tp = len(text)
  64. for idx,t in enumerate(fontObj.metrics(text)):
  65. _w = _w + t[1] - t[0]
  66. if _w > icon_width:
  67. _tp = idx
  68. break
  69. width = _w #recalc the width of text
  70. if width > icon_width: ##Label width max is icon width
  71. width = icon_width
  72. if _tp < len(text):##cut the text to fit width
  73. text = text[0:_tp]
  74. canvas.blit(fontObj.render(text,True,(83,83,83)),midRect(x,y,width,height,canWidth,canHeight))
  75. def SwapAndShow():
  76. pygame.display.update()