list_page.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. ["","Sound","Sound Volume"],
  62. ["","Brightness","BackLight Brightness"],
  63. ["","Storage",""],
  64. ["","Time","Timezone"],
  65. ["","Update", ""],
  66. ["","About", "About"],
  67. ["","PowerOFF","Power off"],
  68. ["","ButtonsLayout","Buttons Layout"],]
  69. start_x = 0
  70. start_y = 0
  71. sys.path.append(myvars.basepath)# add self as import path
  72. for i,v in enumerate(alist):
  73. li = ListItem()
  74. li._Parent = self
  75. li._PosX = start_x
  76. li._PosY = start_y + i*ListItem._Height
  77. li._Width = Width
  78. li._Fonts["normal"] = self._ListFontObj
  79. if v[2] != "":
  80. li.Init(v[2])
  81. else:
  82. li.Init(v[1])
  83. #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":
  84. if FileExists(myvars.basepath+"/"+ v[1]):
  85. li._LinkObj = __import__(v[1])
  86. init_cb = getattr(li._LinkObj,"Init",None)
  87. if init_cb != None:
  88. if callable(init_cb):
  89. li._LinkObj.Init(self._Screen)
  90. self._MyList.append(li)
  91. self._Scroller = ListScroller()
  92. self._Scroller._Parent = self
  93. self._Scroller._PosX = self._Width - 10
  94. self._Scroller._PosY = 2
  95. self._Scroller.Init()
  96. def ScrollUp(self):
  97. if len(self._MyList) == 0:
  98. return
  99. self._PsIndex -= 1
  100. if self._PsIndex < 0:
  101. self._PsIndex = 0
  102. cur_li = self._MyList[self._PsIndex]
  103. if cur_li._PosY < 0:
  104. for i in range(0, len(self._MyList)):
  105. self._MyList[i]._PosY += self._MyList[i]._Height
  106. def ScrollDown(self):
  107. if len(self._MyList) == 0:
  108. return
  109. self._PsIndex +=1
  110. if self._PsIndex >= len(self._MyList):
  111. self._PsIndex = len(self._MyList) -1
  112. cur_li = self._MyList[self._PsIndex]
  113. if cur_li._PosY +cur_li._Height > self._Height:
  114. for i in range(0,len(self._MyList)):
  115. self._MyList[i]._PosY -= self._MyList[i]._Height
  116. def Click(self):
  117. cur_li = self._MyList[self._PsIndex]
  118. if cur_li._LinkObj != None:
  119. api_cb = getattr(cur_li._LinkObj,"API",None)
  120. if api_cb != None:
  121. if callable(api_cb):
  122. cur_li._LinkObj.API(self._Screen)
  123. def KeyDown(self,event):
  124. if event.key == CurKeys["A"] or event.key == CurKeys["Menu"]:
  125. self.ReturnToUpLevelPage()
  126. self._Screen.Draw()
  127. self._Screen.SwapAndShow()
  128. if event.key == CurKeys["Up"]:
  129. self.ScrollUp()
  130. self._Screen.Draw()
  131. self._Screen.SwapAndShow()
  132. if event.key == CurKeys["Down"]:
  133. self.ScrollDown()
  134. self._Screen.Draw()
  135. self._Screen.SwapAndShow()
  136. if event.key == CurKeys["Enter"]:
  137. self.Click()
  138. def Draw(self):
  139. self.ClearCanvas()
  140. if len(self._MyList) * ListItem._Height > self._Height:
  141. self._Ps._Width = self._Width - 11
  142. self._Ps.Draw()
  143. for i in self._MyList:
  144. i.Draw()
  145. self._Scroller.UpdateSize( len(self._MyList)*ListItem._Height, self._PsIndex*ListItem._Height)
  146. self._Scroller.Draw()
  147. else:
  148. self._Ps._Width = self._Width
  149. self._Ps.Draw()
  150. for i in self._MyList:
  151. i.Draw()