52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
|
import gamedata.objects.base as base
|
||
|
import gamedata.objects.button as button
|
||
|
|
||
|
class Menu(base.BaseObject):
|
||
|
def __init__(self,x,y,game,w=100,h=100):
|
||
|
|
||
|
super().__init__(x,y,game,w,h) # initialise l'objet de base avec les bons arguments
|
||
|
self.init = False
|
||
|
|
||
|
# Initialisation de son sprite
|
||
|
self.sprite = None
|
||
|
|
||
|
# Variables de styles
|
||
|
self.hpadding = 2 # Les espaces entre les différents boutons
|
||
|
self.vpadding = 2
|
||
|
|
||
|
# Liste qui se remplira avec les objets bouttons
|
||
|
self.lbuttons = []
|
||
|
# Les dicos des boutons
|
||
|
def fnExemple(self,game):
|
||
|
print("Exemple")
|
||
|
btnExemple = {"name":"Bouton !!","function": fnExemple}
|
||
|
|
||
|
# Initialisation de la grille du menu
|
||
|
self.grid = [
|
||
|
[btnExemple],
|
||
|
[btnExemple,None]
|
||
|
]
|
||
|
|
||
|
def create(self,grid,rect):
|
||
|
game = self.game
|
||
|
elementheight = (rect[3]-(len(grid)-1)*self.vpadding)//len(grid)
|
||
|
for i in range(len(grid)):
|
||
|
elementwidth = (rect[2]-(len(grid[i])-1)*self.hpadding)//len(grid[i])
|
||
|
for j in range(len(grid[i])):
|
||
|
posx = rect[0]+j*(elementwidth+self.hpadding)
|
||
|
posy = rect[1]+i*(elementheight+self.vpadding)
|
||
|
if type(grid[i][j]).__name__=="dict":
|
||
|
# Si c'est un dico, l'emplacement est un bouton
|
||
|
folder = "base"
|
||
|
if "folder" in grid[i][j].keys():
|
||
|
folder = grid[i][j]["folder"]
|
||
|
btn = button.Button(posx,posy,game,elementwidth,elementheight,folder)
|
||
|
btn.text = grid[i][j]["name"]
|
||
|
btn.click = grid[i][j]["function"]
|
||
|
btn.menuowner = self # Le bouton a une référence à son menu
|
||
|
self.lbuttons.append(btn) # Je le garde en memoire
|
||
|
game.gameloop.summon(btn)
|
||
|
if type(grid[i][j]).__name__ in ("tuple","list"):
|
||
|
# Sinon, c'est une sous-grille
|
||
|
self.create(grid[i][j],[posx,posy,elementwidth,elementheight],game)
|