list_page.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. import sys
  4. from libs.roundrects import aa_round_rect
  5. ## local UI import
  6. from UI.constants import Width,Height
  7. from UI.page import Page,PageSelector
  8. from UI.label import Label
  9. from UI.fonts import fonts
  10. from UI.util_funcs import midRect,FileExists
  11. from UI.keys_def import CurKeys
  12. from UI.scroller import ListScroller
  13. from UI.skin_manager import SkinManager
  14. from list_item import ListItem
  15. import myvars
  16. class ListPageSelector(PageSelector):
  17. _BackgroundColor = SkinManager().GiveColor('Front')
  18. def __init__(self):
  19. self._PosX = 0
  20. self._PosY = 0
  21. self._Height = 0
  22. self._Width = Width
  23. def AnimateDraw(self,x2,y2):
  24. pass
  25. def Draw(self):
  26. idx = self._Parent._PsIndex
  27. if idx < len(self._Parent._MyList):
  28. x = 2
  29. y = self._Parent._MyList[idx]._PosY+1
  30. h = self._Parent._MyList[idx]._Height -3
  31. self._PosX = x
  32. self._PosY = y
  33. self._Height = h
  34. aa_round_rect(self._Parent._CanvasHWND,
  35. (x,y,self._Width-4,h),self._BackgroundColor,4,0,self._BackgroundColor)
  36. class ListPage(Page):
  37. _Icons = {}
  38. _Selector=None
  39. _FootMsg = ["Nav","","","Back","Enter"]
  40. _MyList = []
  41. _ListFontObj = fonts["varela15"]
  42. _Scroller = None
  43. def __init__(self):
  44. Page.__init__(self)
  45. self._Icons = {}
  46. self._CanvasHWND = None
  47. self._MyList = []
  48. def Init(self):
  49. self._PosX = self._Index * self._Screen._Width
  50. self._Width = self._Screen._Width
  51. self._Height = self._Screen._Height
  52. self._CanvasHWND = self._Screen._CanvasHWND
  53. ps = ListPageSelector()
  54. ps._Parent = self
  55. self._Ps = ps
  56. self._PsIndex = 0
  57. # "" pkgname, label
  58. alist = [["","Airplane","Airplane Mode"],
  59. ["","PowerOptions","Power Options"],
  60. ["","Wifi","Wi-Fi"],
  61. ["","Bluetooth","Bluetooth"],
  62. ["","Sound","Sound Volume"],
  63. ["","Brightness","BackLight Brightness"],
  64. ["","Storage",""],
  65. ["","Time","Timezone"],
  66. ["","Notification","Notification"],
  67. ["","Update", ""],
  68. ["","About", "About"],
  69. ["","PowerOFF","Power off"],
  70. ["","ButtonsLayout","Buttons Layout"],]
  71. start_x = 0
  72. start_y = 0
  73. sys.path.append(myvars.basepath)# add self as import path
  74. for i,v in enumerate(alist):
  75. li = ListItem()
  76. li._Parent = self
  77. li._PosX = start_x
  78. li._PosY = start_y + i*ListItem._Height
  79. li._Width = Width
  80. li._Fonts["normal"] = self._ListFontObj
  81. if v[2] != "":
  82. li.Init(v[2])
  83. else:
  84. li.Init(v[1])
  85. #if v[1] == "Wifi" or v[1] == "Sound" or v[1] == "Brightness" or v[1] == "Storage" or v[1] == "Update" or v[1] == "About" or v[1] == "PowerOFF" or v[1] == "HelloWorld":
  86. if FileExists(myvars.basepath+"/"+ v[1]):
  87. li._LinkObj = __import__(v[1])
  88. init_cb = getattr(li._LinkObj,"Init",None)
  89. if init_cb != None:
  90. if callable(init_cb):
  91. li._LinkObj.Init(self._Screen)
  92. self._MyList.append(li)
  93. self._Scroller = ListScroller()
  94. self._Scroller._Parent = self
  95. self._Scroller._PosX = self._Width - 10
  96. self._Scroller._PosY = 2
  97. self._Scroller.Init()
  98. def ScrollUp(self):
  99. if len(self._MyList) == 0:
  100. return
  101. self._PsIndex -= 1
  102. if self._PsIndex < 0:
  103. self._PsIndex = 0
  104. cur_li = self._MyList[self._PsIndex]
  105. if cur_li._PosY < 0:
  106. for i in range(0, len(self._MyList)):
  107. self._MyList[i]._PosY += self._MyList[i]._Height
  108. def ScrollDown(self):
  109. if len(self._MyList) == 0:
  110. return
  111. self._PsIndex +=1
  112. if self._PsIndex >= len(self._MyList):
  113. self._PsIndex = len(self._MyList) -1
  114. cur_li = self._MyList[self._PsIndex]
  115. if cur_li._PosY +cur_li._Height > self._Height:
  116. for i in range(0,len(self._MyList)):
  117. self._MyList[i]._PosY -= self._MyList[i]._Height
  118. def Click(self):
  119. cur_li = self._MyList[self._PsIndex]
  120. if cur_li._LinkObj != None:
  121. api_cb = getattr(cur_li._LinkObj,"API",None)
  122. if api_cb != None:
  123. if callable(api_cb):
  124. cur_li._LinkObj.API(self._Screen)
  125. def KeyDown(self,event):
  126. if event.key == CurKeys["A"] or event.key == CurKeys["Menu"]:
  127. self.ReturnToUpLevelPage()
  128. self._Screen.Draw()
  129. self._Screen.SwapAndShow()
  130. if event.key == CurKeys["Up"]:
  131. self.ScrollUp()
  132. self._Screen.Draw()
  133. self._Screen.SwapAndShow()
  134. if event.key == CurKeys["Down"]:
  135. self.ScrollDown()
  136. self._Screen.Draw()
  137. self._Screen.SwapAndShow()
  138. if event.key == CurKeys["Enter"]:
  139. self.Click()
  140. def Draw(self):
  141. self.ClearCanvas()
  142. if len(self._MyList) * ListItem._Height > self._Height:
  143. self._Ps._Width = self._Width - 11
  144. self._Ps.Draw()
  145. for i in self._MyList:
  146. i.Draw()
  147. self._Scroller.UpdateSize( len(self._MyList)*ListItem._Height, self._PsIndex*ListItem._Height)
  148. self._Scroller.Draw()
  149. else:
  150. self._Ps._Width = self._Width
  151. self._Ps.Draw()
  152. for i in self._MyList:
  153. i.Draw()