list_page.py 5.5 KB

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