list_page.py 5.7 KB

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