ez_menu.py

Created by loic-azavant

Created on February 20, 2023

4.5 KB

Permet d’avoir un menu très facilement, il faut simplement mon autre script keyinput


from math import *
from kandinsky import *
from ion import *
from time import *
from keyinput import *

class Menu:
  
  def __init__(self, conf,bg=(60,)*3,col1=(255,)*3,col2=(250,200,0),varNumb=15,func=lambda x=0: x):
    self.conf = conf
    self.var = ["wy"]*varNumb
    self.cursor = 0
    self.oldCursor = 0
    self.menuNumb = 0
    self.oldMenuNumb = 0
    self.bgcolor = bg
    self.col1color = col1
    self.col2color = col2
    self.bg = lambda: self.bgcolor
    self.col1 = lambda: self.col1color
    self.col2 = lambda: self.col2color
    self.movHori = 0
    self.movVerti = 0
    self.func = func
    for i in range(len(self.conf)):
      for j in range(len(self.conf[i])):
        if self.conf[i][j][1] != "btn":
          if self.conf[i][j][1] == "nbr":
            setattr(self,self.conf[i][j][0][0:self.conf[i][j][0].index(":")],lambda x=self.conf[i][j][2][0]: self.var[x])
          elif self.conf[i][j][1] == "lst":
            setattr(self,self.conf[i][j][0][0:self.conf[i][j][0].index(":")],lambda x=self.conf[i][j][2][0],y=self.conf[i][j][3]: y[self.var[x]])
          self.var[self.conf[i][j][2][0]] = self.conf[i][j][2][1]
          self.conf[i][j][2] = self.conf[i][j][2][0]
    self.conf = eval(str(self.conf).replace("[","(").replace("]",")"))
    while self.var[-1]=="wy":
      del self.var[-1]
  
  def drwScrn(self,exeFunc=False):
    if exeFunc:
      fill_rect(0,0,320,222,self.bg())
      self.func()
    else:
      fill_rect(0,90,320,132,self.bg())
    for i in range(len(self.conf[self.menuNumb])):
      draw_string(self.conf[self.menuNumb][i][0],30,i*25+100,self.col1(),self.bg())
      if self.conf[self.menuNumb][i][1]=="nbr":
        draw_string(str(self.var[self.conf[self.menuNumb][i][2]]),30+len(self.conf[self.menuNumb][i][0])*10,i*25+100,self.col1(),self.bg())
      if self.conf[self.menuNumb][i][1]=="lst":
        draw_string(str(self.conf[self.menuNumb][i][3][self.var[self.conf[self.menuNumb][i][2]]]),30+len(self.conf[self.menuNumb][i][0])*10,i*25+100,self.col1(),self.bg())
    self.oldCursor = 0
    self.updtScrn()
    
  
  def updtScrn(self):
    draw_string("  "+self.conf[self.menuNumb][self.oldCursor][0],10,self.oldCursor*25+100,self.col1(),self.bg())
    draw_string("> "+self.conf[self.menuNumb][self.cursor][0],10,self.cursor*25+100,self.col2(),self.bg())

    if self.conf[self.menuNumb][self.oldCursor][1]=="nbr":
      draw_string(str(self.var[self.conf[self.menuNumb][self.oldCursor][2]])+"   ",30+len(self.conf[self.menuNumb][self.oldCursor][0])*10,self.oldCursor*25+100,self.col1(),self.bg())
    elif self.conf[self.menuNumb][self.oldCursor][1]=="lst":
      draw_string(str(self.conf[self.menuNumb][self.oldCursor][3][self.var[self.conf[self.menuNumb][self.oldCursor][2]]])+"   ",30+len(self.conf[self.menuNumb][self.oldCursor][0])*10,self.oldCursor*25+100,self.col1(),self.bg())

    if self.conf[self.menuNumb][self.cursor][1]=="nbr":
      draw_string(str(self.var[self.conf[self.menuNumb][self.cursor][2]])+"   ",30+len(self.conf[self.menuNumb][self.cursor][0])*10,self.cursor*25+100,self.col2(),self.bg())
    elif self.conf[self.menuNumb][self.cursor][1]=="lst":
      draw_string(str(self.conf[self.menuNumb][self.cursor][3][self.var[self.conf[self.menuNumb][self.cursor][2]]])+"   ",30+len(self.conf[self.menuNumb][self.cursor][0])*10,self.cursor*25+100,self.col2(),self.bg())
  
  def loop(self):
    while 1:
      self.oldCursor = self.cursor
      self.movHori = keyinput(3)-keyinput(0)
      self.movVerti = keyinput(2)-keyinput(1)
      self.okPressed = keyinput(4)
      if self.movVerti!=0:
        self.cursor=max(0,min(len(self.conf[self.menuNumb])-1,self.cursor+self.movVerti))
        self.drwScrn()
      if self.movHori!=0:
        if self.conf[self.menuNumb][self.cursor][1]=="nbr":
          self.var[self.conf[self.menuNumb][self.cursor][2]] = max(self.conf[self.menuNumb][self.cursor][3][0],min(self.conf[self.menuNumb][self.cursor][3][1],self.var[self.conf[self.menuNumb][self.cursor][2]]+self.movHori))
        elif self.conf[self.menuNumb][self.cursor][1]=="lst":
          self.var[self.conf[self.menuNumb][self.cursor][2]] = (self.var[self.conf[self.menuNumb][self.cursor][2]]+self.movHori)%len(self.conf[self.menuNumb][self.cursor][3])
        self.drwScrn()
      if self.okPressed and self.conf[self.menuNumb][self.cursor][1] == "btn":
        if self.conf[self.menuNumb][self.cursor][2] == -1:
          return self.conf[self.menuNumb][self.cursor][0]
        else:
          self.menuNumb = self.conf[self.menuNumb][self.cursor][2]
          self.cursor = 0
          self.drwScrn(True)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.