RaMenu.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. # -*- coding:UTF-8 -*-
  2. import json
  3. from PIL import Image,ImageDraw,ImageFont,ImageColor
  4. from picamera import PiCamera
  5. from time import sleep
  6. import math
  7. import time
  8. class RaMenu:
  9. __visible = False
  10. __visible_camera = False
  11. __showMenu = False
  12. __currentMenuItem = [0]
  13. __currentMenuType = ["submenu"]
  14. def __init__(self, Components):
  15. """
  16. Initialisiert das RaMenu
  17. :param Components: RaComponents Element
  18. :type Components: RaComponents
  19. """
  20. self.components = Components
  21. self.load_menu_from_json("menu.json")
  22. self.editOption = None
  23. self.editOptionValue = None
  24. self.progress = False
  25. self.progressValue = 81.2345
  26. self.components.Keypad.registerKeyDown(self.KeyDown)
  27. self.components.Keypad.registerKeyHold(self.KeyHold)
  28. self.components.Keypad.registerKeyPress(self.KeyPress)
  29. self.initOptions()
  30. def initOptions(self):
  31. """
  32. Initialisiert und läd die HUD Optionen
  33. """
  34. cfg = self.load_json("config.json")
  35. # Images
  36. self.__icon_bg = Image.open('img/'+cfg["hud"]["background"]).convert("RGBA")
  37. self.__icon_text = Image.open('img/menu_text.png').convert("RGBA")
  38. # Color
  39. self.__color = self.components.Util.getColor(cfg["hud"]["color_foreground"])
  40. self.__colorDisabled = self.components.Util.getColor(cfg["hud"]["color_disabled"])
  41. self.__colorBG = self.components.Util.getColor(cfg["hud"]["color_background"])
  42. self.__colorBorder = self.components.Util.getColor(cfg["hud"]["color_border"])
  43. self.__colorProgress = self.components.Util.getColor(cfg["hud"]["color_progress"])
  44. # Menu Settings
  45. self.__drawIconMenuText = cfg["hud"]["iconMenu_DrawText"]
  46. self.__iconMenu_Theme = cfg["hud"]["iconMenu_Theme"]
  47. # Fonts
  48. self.__font = ImageFont.truetype("font/"+cfg["hud"]["font"], 16)
  49. self.__listfont = ImageFont.truetype("font/"+cfg["hud"]["font"], 12)
  50. self.__fontProgress = ImageFont.truetype("font/"+cfg["hud"]["font"], 24)
  51. #finalize
  52. self.iconPath = 'img/'+self.__iconMenu_Theme+"/"
  53. def load_menu_from_json(self, filename):
  54. """
  55. Lädt die Menüstruktur aus einer JSON-Datei.
  56. :param filename: Der Name der JSON-Datei.
  57. """
  58. self.menu_structure = self.load_json(filename)
  59. def load_json(self, filename):
  60. """
  61. Lädt die Menüstruktur aus einer JSON-Datei.
  62. :param filename: Der Name der JSON-Datei.
  63. """
  64. res=dict()
  65. with open(filename, "r") as file:
  66. res = json.load(file) # Menüstruktur aus JSON laden
  67. return res
  68. def isVisible(self):
  69. return self.__visible
  70. def DrawHeadline(self, image,draw,Text):
  71. draw.rectangle([(0,0),127,20], fill =self.__colorBG, outline =self.__colorBorder)
  72. if isinstance(Text, str):
  73. _, _, w2, _ = draw.textbbox((0, 0), Text, font=self.__listfont)
  74. draw.text((126-w2, 4), text=Text, fill = self.__color, font=self.__font)
  75. elif isinstance(Text, int):
  76. _, _, w2, _ = draw.textbbox((0, 0), str(Text), font=self.__listfont)
  77. draw.text((126-w2, 4), text=str(Text), fill = self.__color, font=self.__font)
  78. elif isinstance(Text, float):
  79. _, _, w2, _ = draw.textbbox((0, 0), str(Text), font=self.__listfont)
  80. draw.text((126-w2, 4), text=str(Text), fill = self.__color, font=self.__font)
  81. elif "DisplayName" in Text:
  82. _, _, w2, _ = draw.textbbox((0, 0), Text["DisplayName"], font=self.__listfont)
  83. draw.text((126-w2, 4), text=Text["DisplayName"], fill = self.__color, font=self.__font)
  84. elif "displayName" in Text:
  85. _, _, w2, _ = draw.textbbox((0, 0), Text["displayName"], font=self.__listfont)
  86. draw.text((126-w2, 4), text=Text["displayName"], fill = self.__color, font=self.__font)
  87. elif "Displayname" in Text:
  88. _, _, w2, _ = draw.textbbox((0, 0), Text["Displayname"], font=self.__listfont)
  89. draw.text((126-w2, 4), text=Text["Displayname"], fill = self.__color, font=self.__font)
  90. elif "displayname" in Text:
  91. _, _, w2, _ = draw.textbbox((0, 0), Text["displayname"], font=self.__listfont)
  92. draw.text((126-w2, 4), text=Text["displayname"], fill = self.__color, font=self.__font)
  93. #def setDisplay(self, display):
  94. # self.__display = display
  95. #def setCamera(self, camera):
  96. # self.__camera = camera
  97. def getFrame_Submenu(self, image, draw):
  98. print("Draw Icon Menu")
  99. mnuItem = self.getCurrentMenuItem()
  100. if "Icon" in mnuItem:
  101. icon = Image.open(self.iconPath+mnuItem["Icon"]).convert("RGBA")
  102. image.paste(icon,(0,0,128,128),icon)
  103. elif "icon" in mnuItem:
  104. icon = Image.open(self.iconPath+mnuItem["icon"]).convert("RGBA")
  105. image.paste(icon,(0,0,128,128),icon)
  106. if self.__drawIconMenuText == True:
  107. if "DisplayName" in mnuItem:
  108. _, _, w, h = draw.textbbox((0, 0), mnuItem["DisplayName"], font=self.__font)
  109. image.paste(self.__icon_text,(0,0,128,128),self.__icon_text)
  110. draw.text(((128-w)/2, 101), mnuItem["DisplayName"], fill = self.__color, font=self.__font)
  111. elif "displayName" in mnuItem:
  112. _, _, w, h = draw.textbbox((0, 0), mnuItem["displayName"], font=self.__font)
  113. image.paste(self.__icon_text,(0,0,128,128),self.__icon_text)
  114. draw.text((int((128-w)/2), 101), mnuItem["displayName"], fill = self.__color, font=self.__font)
  115. def getFrame_ListMenu(self, image, draw):
  116. print("Draw List Menu")
  117. mnuItem = self.getCurrentMenuItem()
  118. mnu = self.getMenuItem(self.__currentMenuItem)
  119. index = self.__currentMenuItem[-1]
  120. _, _, w, h = draw.textbbox((0, 0), "abc", font=self.__listfont)
  121. ci = 0
  122. print("W/H: "+str(w)+"/"+str(h))
  123. icon_check = Image.open('img/check_small.png').convert("RGBA")
  124. icon_cross = Image.open('img/cross_small.png').convert("RGBA")
  125. icon_default = Image.open('img/default_small.png').convert("RGBA")
  126. for entry in mnu:
  127. y_pos = int(((128-h)/2) + ((ci-index)*(h+4)));
  128. if ci<index:
  129. y_pos-=10
  130. elif ci>index:
  131. y_pos+=10
  132. if y_pos>-12 and y_pos<128:
  133. displayText = ""
  134. if "DisplayName" in entry:
  135. displayText = entry["DisplayName"]
  136. elif "displayName" in entry:
  137. displayText = entry["displayName"]
  138. if ci==index:
  139. draw.text((2, y_pos), text=displayText, fill = self.__color, font=self.__font)
  140. else:
  141. draw.text((2, y_pos), text=displayText, fill = self.__colorDisabled, font=self.__listfont)
  142. if "Option" in entry:
  143. val = self.getConfigValue(entry["Option"]["_var"])
  144. valS = ""
  145. t = "int"
  146. if "_type" in entry["Option"]:
  147. t = entry["Option"]["_type"]
  148. if t=="list":
  149. if isinstance(val, str):
  150. valS = val
  151. elif isinstance(val, int):
  152. valS = entry["Option"]["Options"][val]
  153. elif isinstance(val, float):
  154. valS = str(val)
  155. elif isinstance(val, bool):
  156. if val==True:
  157. valS="On"
  158. else:
  159. valS="Off"
  160. if t=="string":
  161. if isinstance(val, str):
  162. valS = val
  163. elif isinstance(val, int):
  164. valS = str(val)
  165. elif isinstance(val, float):
  166. valS = str(val)
  167. elif isinstance(val, bool):
  168. if val==True:
  169. valS="On"
  170. else:
  171. valS="Off"
  172. if t=="numeric":
  173. if isinstance(val, str):
  174. valS = val
  175. elif isinstance(val, int):
  176. valS = str(val)
  177. elif isinstance(val, float):
  178. valS = str(val)
  179. elif isinstance(val, bool):
  180. if val==True:
  181. valS="1"
  182. else:
  183. valS="0"
  184. if t=="balance":
  185. if isinstance(val, str):
  186. valS = val
  187. elif isinstance(val, int):
  188. valS = str(val)
  189. elif isinstance(val, float):
  190. valS = str(val)
  191. elif isinstance(val, bool):
  192. if val==True:
  193. valS="1"
  194. else:
  195. valS="0"
  196. if t=="check":
  197. if isinstance(val, str):
  198. valS = val==""
  199. elif isinstance(val, int):
  200. valS = val>0
  201. elif isinstance(val, float):
  202. valS = val>0
  203. elif isinstance(val, bool):
  204. valS=val
  205. if val==None:
  206. image.paste(icon_default,(110,y_pos),icon_default)
  207. elif isinstance(valS, bool):
  208. if valS==True:
  209. image.paste(icon_check,(110,y_pos),icon_check)
  210. else:
  211. image.paste(icon_cross,(110,y_pos),icon_cross)
  212. else:
  213. _, _, w2, _ = draw.textbbox((0, 0), valS, font=self.__listfont)
  214. if ci==index:
  215. draw.text((126-w2, y_pos), text=valS, fill = self.__color, font=self.__font)
  216. else:
  217. draw.text((126-w2, y_pos), text=valS, fill = self.__colorDisabled, font=self.__listfont)
  218. ci+=1
  219. def getFrame_OptionList(self, image, draw):
  220. print("Draw Option List")
  221. mnuItem = self.getCurrentMenuItem()
  222. mnu = self.getMenuItem(self.__currentMenuItem)
  223. index = self.editOptionValue
  224. val = self.getConfigValue(self.editOption)
  225. _, _, w, h = draw.textbbox((0, 0), "abc", font=self.__listfont)
  226. ci = 0
  227. for optK,optV in mnuItem["Option"]["Options"].items():
  228. #y_pos = int(((128-h)/2) + ((ci-index)*(2+4)));
  229. y_pos = int(((128-h)/2) + ((ci-index)*(h+4)));
  230. if ci<index:
  231. y_pos-=10
  232. elif ci>index:
  233. y_pos+=10
  234. if y_pos>-12 and y_pos<128:
  235. if ci==index:
  236. draw.text((10, y_pos), text=optK, fill = self.__color, font=self.__font)
  237. else:
  238. draw.text((10, y_pos), text=optK, fill = self.__colorDisabled, font=self.__listfont)
  239. ci+=1
  240. self.DrawHeadline(image,draw,mnu)
  241. def getFrame_NumericOption(self,image,draw):
  242. print("Draw Numeric Option")
  243. mnu = self.getMenuItem(self.__currentMenuItem)
  244. mnuItem = self.getCurrentMenuItem()
  245. index = self.__currentMenuItem[-1]
  246. _min = mnuItem["Option"]["_min"]
  247. _max = mnuItem["Option"]["_max"]
  248. val = self.editOptionValue
  249. draw.rectangle([(108,22),(124,126)],outline =self.__color)
  250. barHeight = 100
  251. progress = barHeight - (((val - _min) / (_max - _min)) * barHeight)
  252. draw.rectangle([(110,24+progress),(122,124)],fill =self.__color)
  253. _, _, w, h = draw.textbbox((0, 0), str(val), font=self.__font)
  254. pos_x = (120-w)/2
  255. draw.text((pos_x, int((128-h)/2)), text=str(val), fill = self.__color, font=self.__font)
  256. #imgUp = Image.open('img/numUp.png').convert("RGBA")
  257. #imgDown = Image.open('img/numDown.png').convert("RGBA")
  258. #image.paste(imgUp,(30,int(((128-h)/2)-h)),imgUp)
  259. #image.paste(imgDown,(30,int(((128-h)/2)+h+h)),imgDown)
  260. trianglePosY = int(((128-h)/2)-h)
  261. draw.polygon([(30,trianglePosY+10), (pos_x, trianglePosY), (pos_x+(pos_x-30),trianglePosY+10)], fill = self.__color)
  262. trianglePosY = int(((128-h)/2)+h+h)
  263. draw.polygon([(30,trianglePosY), (pos_x, trianglePosY+10), (pos_x+(pos_x-30),trianglePosY)], fill = self.__color)
  264. self.DrawHeadline(image,draw,mnu)
  265. def getFrame_BalanceOption(self,image,draw):
  266. print("Draw Balance Option")
  267. mnu = self.getMenuItem(self.__currentMenuItem)
  268. mnuItem = self.getCurrentMenuItem()
  269. index = self.__currentMenuItem[-1]
  270. _min = mnuItem["Option"]["_min"]
  271. _max = mnuItem["Option"]["_max"]
  272. val = self.editOptionValue
  273. draw.rectangle([(108,22),(124,126)],outline =self.__color)
  274. barHeight = 96
  275. progress = barHeight - (((val - _min) / (_max - _min)) * barHeight)
  276. draw.rectangle([(110,22+progress),(122,26+progress)],fill =self.__color)
  277. _, _, w, h = draw.textbbox((0, 0), str(val), font=self.__font)
  278. pos_x = (120-w)/2
  279. draw.text((pos_x, int((128-h)/2)), text=str(val), fill = self.__color, font=self.__font)
  280. #imgUp = Image.open('img/numUp.png').convert("RGBA")
  281. #imgDown = Image.open('img/numDown.png').convert("RGBA")
  282. #image.paste(imgUp,(30,int(((128-h)/2)-h)),imgUp)
  283. #image.paste(imgDown,(30,int(((128-h)/2)+h+h)),imgDown)
  284. trianglePosY = int(((128-h)/2)-h)
  285. draw.polygon([(30,trianglePosY+10), (pos_x, trianglePosY), (pos_x+(pos_x-30),trianglePosY+10)], fill = self.__color)
  286. trianglePosY = int(((128-h)/2)+h+h)
  287. draw.polygon([(30,trianglePosY), (pos_x, trianglePosY+10), (pos_x+(pos_x-30),trianglePosY)], fill = self.__color)
  288. self.DrawHeadline(image,draw,mnu)
  289. def getFrame_Progress(self, image,draw):
  290. progressValue = self.progressValue
  291. if progressValue==-1:
  292. pos=(time.time() * 130) % 360
  293. draw.ellipse((31,31,97,97), fill=self.__colorDisabled)
  294. draw.pieslice((30, 30, 98, 98), start=pos, end=pos+80, fill=self.__colorProgress)
  295. draw.ellipse((35,35,93,93), fill=self.__colorBG)
  296. draw.ellipse((37,37,91,91), fill=self.__colorBG)
  297. else:
  298. draw.ellipse((31,31,97,97), fill=self.__colorDisabled)
  299. draw.pieslice((30, 30, 98, 98), start=-90, end=int(progressValue*3.6)-90, fill=self.__colorProgress)
  300. draw.ellipse((35,35,93,93), fill=self.__colorBG)
  301. draw.ellipse((37,37,91,91), fill=self.__colorBG)
  302. _, _, w, h = draw.textbbox((0, 0), str(math.floor(progressValue))+" %", font=self.__fontProgress)
  303. draw.text(((128-w) / 2, (128-h)/2), text=str(math.floor(progressValue))+" %", fill = self.__colorProgress, font=self.__fontProgress)
  304. def getFrame(self):
  305. if self.__visible == True:
  306. image = Image.new("RGBA", (128, 128), self.__colorBG)
  307. draw = ImageDraw.Draw(image)
  308. image.paste(self.__icon_bg,(0,0,128,128),self.__icon_bg)
  309. if self.__currentMenuType[-1]=="submenu":
  310. self.getFrame_Submenu(image, draw)
  311. elif self.__currentMenuType[-1]=="listmenu":
  312. self.getFrame_ListMenu(image,draw)
  313. elif self.__currentMenuType[-1]=="options":
  314. self.getFrame_OptionList(image,draw)
  315. elif self.__currentMenuType[-1]=="numeric":
  316. self.getFrame_NumericOption(image,draw)
  317. elif self.__currentMenuType[-1]=="balance":
  318. self.getFrame_BalanceOption(image,draw)
  319. if self.progress == True:
  320. time.sleep(0.1)
  321. self.getFrame_Progress(image,draw)
  322. else:
  323. time.sleep(0.3)
  324. return image
  325. return False
  326. def getConfigValue(self, path, default=None):
  327. if self.config == None:
  328. self.config = self.load_json("config.json")
  329. return self.getObjValue(self.config, path, 0, default)
  330. #pathes = path.split(".")
  331. #if len(pathes)==10:
  332. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]][pathes[8]][pathes[9]]
  333. #elif len(pathes)==9:
  334. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]][pathes[8]]
  335. #elif len(pathes)==8:
  336. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]]
  337. #elif len(pathes)==7:
  338. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]]
  339. #elif len(pathes)==6:
  340. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]]
  341. #elif len(pathes)==5:
  342. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]]
  343. #elif len(pathes)==4:
  344. # return self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]]
  345. #elif len(pathes)==3:
  346. # return self.config[pathes[0]][pathes[1]][pathes[2]]
  347. #elif len(pathes)==2:
  348. # return self.config[pathes[0]][pathes[1]]
  349. #elif len(pathes)==1:
  350. # return self.config[pathes[0]]
  351. def getObjValue(self,obj,path,index, default):
  352. pathes = path.split(".")
  353. if index==len(pathes):
  354. return obj
  355. else:
  356. if pathes[index] in obj:
  357. return self.getObjValue(obj[pathes[index]],path,index+1, default)
  358. else:
  359. return default
  360. def setConfigValue(self, path, value):
  361. pathes = path.split(".")
  362. if self.config == None:
  363. self.config = self.load_json("config.json")
  364. if value == None:
  365. if len(pathes)==10:
  366. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]][pathes[8]][pathes[9]]
  367. elif len(pathes)==9:
  368. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]][pathes[8]]
  369. elif len(pathes)==8:
  370. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]]
  371. elif len(pathes)==7:
  372. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]]
  373. elif len(pathes)==6:
  374. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]]
  375. elif len(pathes)==5:
  376. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]]
  377. elif len(pathes)==4:
  378. del self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]]
  379. elif len(pathes)==3:
  380. del self.config[pathes[0]][pathes[1]][pathes[2]]
  381. elif len(pathes)==2:
  382. del self.config[pathes[0]][pathes[1]]
  383. elif len(pathes)==1:
  384. del self.config[pathes[0]]
  385. else:
  386. if len(pathes)==10:
  387. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]][pathes[8]][pathes[9]] = value
  388. elif len(pathes)==9:
  389. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]][pathes[8]] = value
  390. elif len(pathes)==8:
  391. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]][pathes[7]] = value
  392. elif len(pathes)==7:
  393. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]][pathes[6]] = value
  394. elif len(pathes)==6:
  395. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]][pathes[5]] = value
  396. elif len(pathes)==5:
  397. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]][pathes[4]] = value
  398. elif len(pathes)==4:
  399. self.config[pathes[0]][pathes[1]][pathes[2]][pathes[3]] = value
  400. elif len(pathes)==3:
  401. self.config[pathes[0]][pathes[1]][pathes[2]] = value
  402. elif len(pathes)==2:
  403. self.config[pathes[0]][pathes[1]] = value
  404. elif len(pathes)==1:
  405. self.config[pathes[0]] = value
  406. with open("config.json", "w") as outfile:
  407. outfile.write(json.dumps(self.config))
  408. def getMenuItem(self, index):
  409. return self.__getMenuItemSub(self.menu_structure,index, 0)
  410. def getCurrentMenuItem(self):
  411. mnu = self.__getMenuItemSub(self.menu_structure, self.__currentMenuItem, 0)
  412. return mnu[self.__currentMenuItem[-1]]
  413. def __getMenuItemSub(self, mnu,index,i):
  414. length = len(index)
  415. if i<(length-1):
  416. if "sub" in mnu[index[i]]:
  417. return self.__getMenuItemSub(mnu[index[i]]["sub"],index,i+1)
  418. elif "Sub" in mnu[index[i]]:
  419. return self.__getMenuItemSub(mnu[index[i]]["Sub"],index,i+1)
  420. elif "SubMenu" in mnu[index[i]]:
  421. return self.__getMenuItemSub(mnu[index[i]]["SubMenu"],index,i+1)
  422. elif "subMenu" in mnu[index[i]]:
  423. return self.__getMenuItemSub(mnu[index[i]]["subMenu"],index,i+1)
  424. elif "Submenu" in mnu[index[i]]:
  425. return self.__getMenuItemSub(mnu[index[i]]["Submenu"],index,i+1)
  426. elif "list" in mnu[index[i]]:
  427. return self.__getMenuItemSub(mnu[index[i]]["list"],index,i+1)
  428. elif "List" in mnu[index[i]]:
  429. return self.__getMenuItemSub(mnu[index[i]]["List"],index,i+1)
  430. elif "ListMenu" in mnu[index[i]]:
  431. return self.__getMenuItemSub(mnu[index[i]]["ListMenu"],index,i+1)
  432. elif "listMenu" in mnu[index[i]]:
  433. return self.__getMenuItemSub(mnu[index[i]]["listMenu"],index,i+1)
  434. elif "Listmenu" in mnu[index[i]]:
  435. return self.__getMenuItemSub(mnu[index[i]]["Listmenu"],index,i+1)
  436. elif "Option" in mnu[index[i]]:
  437. return self.__getMenuItemSub(mnu[index[i]]["Option"],index,i+1)
  438. elif "option" in mnu[index[i]]:
  439. return self.__getMenuItemSub(mnu[index[i]]["option"],index,i+1)
  440. elif "Options" in mnu[index[i]]:
  441. return self.__getMenuItemSub(mnu[index[i]]["Options"],index,i+1)
  442. elif "options" in mnu[index[i]]:
  443. return self.__getMenuItemSub(mnu[index[i]]["options"],index,i+1)
  444. return mnu
  445. def __executeFunc(self,s : str):
  446. self.components.Util.Execute(s)
  447. def KeyDown(self, key):
  448. if self.__visible == True:
  449. if key=="KEY_LEFT":
  450. self.Back()
  451. if key=="KEY_RIGHT":
  452. self.Right()
  453. self.components.Display.RequestUpdate()
  454. if key=="KEY1":
  455. if self.components.Camera.isRecording():
  456. self.components.Camera.stopRecording()
  457. else:
  458. self.components.Camera.startRecording()
  459. if key=="KEY3":
  460. if self.__visible_camera==True:
  461. self.__visible_camera = False
  462. else:
  463. self.__visible_camera = True
  464. self.checkDisplayEnabled()
  465. def KeyHold(self, key, duration):
  466. if self.__visible == True:
  467. if key=="KEY_PRESS":
  468. if duration>=3:
  469. self.__visible = False
  470. self.checkDisplayEnabled()
  471. elif key=="KEY_UP":
  472. self.Up()
  473. elif key=="KEY_DOWN":
  474. self.Down()
  475. self.components.Display.RequestUpdate()
  476. def KeyPress(self, key, duration):
  477. if key=="KEY_PRESS":
  478. if duration<3:
  479. if self.__visible == True:
  480. self.Right()
  481. else:
  482. self.config = self.load_json("config.json")
  483. self.__visible = True
  484. self.checkDisplayEnabled()
  485. if self.components.Display is not None:
  486. self.components.Display.RequestUpdate()
  487. def checkDisplayEnabled(self):
  488. if self.__visible_camera == True or self.__visible == True:
  489. self.components.Display.On()
  490. else:
  491. self.components.Display.Off()
  492. def Back(self):
  493. if len(self.__currentMenuItem) <= 1:
  494. self.__visible = False
  495. self.__currentMenuItem = [0]
  496. self.__currentMenuType = ["submenu"]
  497. else:
  498. self.__currentMenuItem = self.__currentMenuItem[ : -1]
  499. self.__currentMenuType = self.__currentMenuType[ : -1]
  500. def Right(self):
  501. mnuItem = self.getCurrentMenuItem()
  502. #Save Option Values
  503. if self.editOption != None and self.__currentMenuType[-1]=="numeric":
  504. self.setConfigValue(self.editOption, self.editOptionValue)
  505. self.editOption = None
  506. self.editOptionValue = None
  507. self.__currentMenuType = self.__currentMenuType[:-1]
  508. return
  509. elif self.editOption != None and self.__currentMenuType[-1]=="balance":
  510. self.setConfigValue(self.editOption, self.editOptionValue)
  511. self.editOption = None
  512. self.editOptionValue = None
  513. self.__currentMenuType = self.__currentMenuType[:-1]
  514. return
  515. elif self.editOption != None and self.__currentMenuType[-1]=="options":
  516. index = self.editOptionValue
  517. for optK,optV in mnuItem["Option"]["Options"].items():
  518. if index==0:
  519. self.setConfigValue(self.editOption, optK)
  520. break;
  521. index-=1
  522. self.editOption = None
  523. self.editOptionValue = None
  524. self.__currentMenuType = self.__currentMenuType[:-1]
  525. return
  526. if "action" in mnuItem:
  527. self.__executeFunc(mnuItem["action"])
  528. elif "Action" in mnuItem:
  529. self.__executeFunc(mnuItem["Action"])
  530. if "sub" in mnuItem or "Sub" in mnuItem or "SubMenu" in mnuItem or "subMenu" in mnuItem or "Submenu" in mnuItem:
  531. self.__currentMenuItem.append(0)
  532. self.__currentMenuType.append("submenu")
  533. if "list" in mnuItem or "List" in mnuItem or "ListMenu" in mnuItem or "listMenu" in mnuItem or "Listmenu" in mnuItem:
  534. self.__currentMenuItem.append(0)
  535. self.__currentMenuType.append("listmenu")
  536. if "Option" in mnuItem:
  537. if "_type" in mnuItem["Option"]:
  538. if mnuItem["Option"]["_type"]=="list":
  539. print("Option Show List")
  540. val = self.getConfigValue(mnuItem["Option"]["_var"])
  541. self.editOptionValue=0
  542. for optK,optV in mnuItem["Option"]["Options"].items():
  543. if val==optV:
  544. break;
  545. self.editOptionValue+=1
  546. self.editOption=mnuItem["Option"]["_var"]
  547. self.__currentMenuType.append("options")
  548. elif mnuItem["Option"]["_type"]=="check":
  549. val = self.getConfigValue(mnuItem["Option"]["_var"])
  550. if isinstance(val, bool):
  551. if val==True:
  552. print("Option Change: "+mnuItem["Option"]["_var"]+" = False")
  553. self.setConfigValue(mnuItem["Option"]["_var"], False)
  554. else:
  555. print("Option Change: "+mnuItem["Option"]["_var"]+" = True")
  556. self.setConfigValue(mnuItem["Option"]["_var"], True)
  557. elif isinstance(val, int):
  558. if val>1:
  559. print("Option Change: "+mnuItem["Option"]["_var"]+" = 0")
  560. self.setConfigValue(mnuItem["Option"]["_var"],0)
  561. else:
  562. print("Option Change: "+mnuItem["Option"]["_var"]+" = 1")
  563. self.setConfigValue(mnuItem["Option"]["_var"],1)
  564. elif mnuItem["Option"]["_type"]=="numeric":
  565. print("Option Show Numeric")
  566. self.editOption=mnuItem["Option"]["_var"]
  567. val = self.getConfigValue(mnuItem["Option"]["_var"])
  568. self.editOptionValue=val
  569. self.__currentMenuType.append("numeric")
  570. elif mnuItem["Option"]["_type"]=="balance":
  571. print("Option Show Balance")
  572. self.editOption=mnuItem["Option"]["_var"]
  573. val = self.getConfigValue(mnuItem["Option"]["_var"])
  574. self.editOptionValue=val
  575. self.__currentMenuType.append("balance")
  576. def Up(self):
  577. if self.editOption != None and self.__currentMenuType[-1]=="numeric":
  578. self.editOptionValue+=1
  579. elif self.editOption != None and self.__currentMenuType[-1]=="balance":
  580. self.editOptionValue+=1
  581. elif self.editOption != None and self.__currentMenuType[-1]=="options":
  582. self.editOptionValue-=1
  583. elif self.__currentMenuItem[-1]>0:
  584. self.__currentMenuItem[-1] = self.__currentMenuItem[-1] - 1
  585. def Down(self):
  586. if self.editOption != None and self.__currentMenuType[-1]=="numeric":
  587. self.editOptionValue-=1
  588. if self.editOption != None and self.__currentMenuType[-1]=="balance":
  589. self.editOptionValue-=1
  590. elif self.editOption != None and self.__currentMenuType[-1]=="options":
  591. self.editOptionValue+=1
  592. else:
  593. mnu = self.getMenuItem(self.__currentMenuItem)
  594. maxMnu = len(mnu) -1
  595. if self.__currentMenuItem[-1]<maxMnu:
  596. self.__currentMenuItem[-1] = self.__currentMenuItem[-1] + 1
  597. def Progress(self, percent:float):
  598. self.progress = True
  599. self.progressValue = percent
  600. self.components.Display.RequestUpdate()
  601. self.components.Keypad.Enabled(False)
  602. def StopProgress(self):
  603. self.progress = False
  604. self.progressValue = 0.0
  605. self.components.Display.RequestUpdate()
  606. self.components.Keypad.Enabled(True)