counter_screen.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import gobject
  4. import commands
  5. ## local package import
  6. from constants import Width,Height,RUNSYS
  7. from label import Label
  8. from fonts import fonts
  9. from full_screen import FullScreen
  10. import config
  11. class CounterScreen(FullScreen):
  12. _CounterFont = fonts["varela120"]
  13. _TextFont1 = fonts["varela15"]
  14. _TextFont2 = fonts["varela12"]
  15. _TopLabel = None
  16. _BottomLabel = None
  17. _NumberLabel = None
  18. _BGColor = pygame.Color(0,0,0)
  19. _FGColor = pygame.Color(255,255,255)
  20. _Counting = False
  21. _Number = 10
  22. _GobjectIntervalId = -1
  23. _inter_counter = 0
  24. def GObjectInterval(self):
  25. self._inter_counter+=1
  26. if self._Number == 0:
  27. self._Counting = False
  28. print("do the real shutdown")
  29. if config.CurKeySet != "PC":
  30. cmdpath = "feh --bg-center gameshell/wallpaper/seeyou.png;"
  31. cmdpath += "sleep 3;"
  32. cmdpath += "sudo halt -p"
  33. pygame.event.post( pygame.event.Event(RUNSYS, message=cmdpath))\
  34. return False
  35. if self._inter_counter >=2:
  36. self._Number -= 1
  37. if self._Number < 0:
  38. self._Number = 0
  39. print("sub Number %d " % self._Number)
  40. self._inter_counter = 0
  41. self.Draw()
  42. self.SwapAndShow()
  43. return self._Counting
  44. def StartCounter(self):
  45. if self._Counting == True:
  46. return
  47. self._Number = 10
  48. self._Counting = True
  49. self._GobjectIntervalId = gobject.timeout_add(500,self.GObjectInterval)
  50. def StopCounter(self):
  51. if self._Counting == False:
  52. return
  53. self._Counting = False
  54. self._Number = 10
  55. if self._GobjectIntervalId != -1:
  56. gobject.source_remove(self._GobjectIntervalId)
  57. self._GobjectIntervalId = -1
  58. return
  59. def Init(self):
  60. self._CanvasHWND = pygame.Surface((self._Width,self._Height))
  61. self._TopLabel = Label()
  62. self._TopLabel.SetCanvasHWND(self._CanvasHWND)
  63. self._TopLabel.Init("System shutdown in", self._TextFont1, self._FGColor)
  64. self._BottomLabel = Label()
  65. self._BottomLabel.SetCanvasHWND(self._CanvasHWND)
  66. self._BottomLabel.Init("Press any key to stop countdown", self._TextFont2, self._FGColor)
  67. self._NumberLabel = Label()
  68. self._NumberLabel.SetCanvasHWND(self._CanvasHWND)
  69. self._NumberLabel.Init(str(self._Number), self._CounterFont, self._FGColor)
  70. def Draw(self):
  71. self._CanvasHWND.fill( self._BGColor )
  72. self._TopLabel.NewCoord(Width/2, 15)
  73. self._TopLabel.DrawCenter()
  74. self._BottomLabel.NewCoord(Width/2, Height-15)
  75. self._BottomLabel.DrawCenter()
  76. self._NumberLabel.NewCoord(Width/2,Height/2)
  77. self._NumberLabel.SetText(str(self._Number))
  78. self._NumberLabel.DrawCenter()