forked from ayte/PinmikPanik
90 lines
4.4 KiB
Python
90 lines
4.4 KiB
Python
|
import gamedata.objects.base as base
|
||
|
|
||
|
class Button(base.BaseObject):
|
||
|
def __init__(self,x,y,game,w=100,h=64,folder="base",folderact="baseAct"):
|
||
|
super().__init__(x,y,game,w,h) # initialise l'objet de base avec les bons arguments
|
||
|
self.text = "Hello"
|
||
|
|
||
|
self.spritenon = self.gensprite(w,h,folder)
|
||
|
self.spriteact = self.gensprite(w,h,folderact)
|
||
|
|
||
|
self.sfx = game.sound_lib["sfx/click.wav"]
|
||
|
|
||
|
self.trigger = False
|
||
|
self.collision = False
|
||
|
|
||
|
def click(self,game):
|
||
|
print("hello world ! ")
|
||
|
self.click = click
|
||
|
self.skip = False # Si je passe l'effet sonore de base
|
||
|
|
||
|
def gensprite(self,w,h,foldername):
|
||
|
game = self.game
|
||
|
# Initialisation des parties de sprites
|
||
|
sprites = {}
|
||
|
for i in ("middle","left","right","up","down","corner_ul","corner_ur","corner_dl","corner_dr"):
|
||
|
sprites[i] = game.sprite_lib["gui/button/"+foldername+"/"+i+".png"] # Je récupère tout les sprites correspondant aux boutons
|
||
|
basewidth = w-sprites["left"].get_width()-sprites["right"].get_width() # Largeur du sprite du milieu
|
||
|
baseheight = h-sprites["up"].get_height()-sprites["down"].get_height() # Hauteur du sprite du milieu
|
||
|
for i in ("left","right"):
|
||
|
sprites[i] = base.pygame.transform.scale(sprites[i],(sprites[i].get_width(),baseheight))
|
||
|
for i in ("up","down"):
|
||
|
sprites[i] = base.pygame.transform.scale(sprites[i],(basewidth,sprites[i].get_height()))
|
||
|
|
||
|
# Initialisation de son sprite
|
||
|
finalsprite = base.pygame.Surface((w,h))
|
||
|
# Bordures haut et bas
|
||
|
finalsprite.blit(sprites["up"],(sprites["left"].get_width(),0))
|
||
|
finalsprite.blit(sprites["down"],(sprites["left"].get_width(),sprites["up"].get_height()+baseheight))
|
||
|
# Bordures gauche et droite
|
||
|
finalsprite.blit(sprites["left"],(0,sprites["up"].get_height()))
|
||
|
finalsprite.blit(sprites["right"],(sprites["left"].get_width()+basewidth,sprites["up"].get_height()))
|
||
|
# Coins du bouton
|
||
|
# Coins du bas
|
||
|
sprites["corner_dr"] = base.pygame.transform.scale(sprites["corner_dr"],(sprites["right"].get_width(),sprites["down"].get_height()))
|
||
|
finalsprite.blit(sprites["corner_dr"],(sprites["left"].get_width()+basewidth,sprites["up"].get_height()+baseheight))
|
||
|
sprites["corner_dl"] = base.pygame.transform.scale(sprites["corner_dl"],(sprites["left"].get_width(),sprites["down"].get_height()))
|
||
|
finalsprite.blit(sprites["corner_dl"],(0,sprites["up"].get_height()+baseheight))
|
||
|
# Coins du haut
|
||
|
sprites["corner_ur"] = base.pygame.transform.scale(sprites["corner_ur"],(sprites["right"].get_width(),sprites["up"].get_height()))
|
||
|
finalsprite.blit(sprites["corner_ur"],(sprites["left"].get_width()+basewidth,0))
|
||
|
sprites["corner_ul"] = base.pygame.transform.scale(sprites["corner_ul"],(sprites["left"].get_width(),sprites["up"].get_height()))
|
||
|
finalsprite.blit(sprites["corner_ul"],(0,0))
|
||
|
# Sprite du milieu
|
||
|
finalsprite.blit(base.pygame.transform.scale(sprites["middle"],(basewidth,baseheight)),(sprites["left"].get_width(),sprites["up"].get_height()))
|
||
|
return finalsprite
|
||
|
def step(self):
|
||
|
game = self.game
|
||
|
# Je vérifie si mon curseur de souris est dans le rectangle de l'objet
|
||
|
self.collision = True
|
||
|
nb = len(self.rect)//2 # Le nombre de dimensions, dans ce cas 2
|
||
|
for i in range(nb):
|
||
|
self.collision = self.collision and self.rect[i]<=game.inputs["mouse"]["pos"][i]<=self.rect[i+nb]+self.rect[i]
|
||
|
|
||
|
if self.collision and game.inputs["mouse"]["click"]==1:
|
||
|
self.trigger = True
|
||
|
|
||
|
if self.trigger:
|
||
|
self.click(self,game)
|
||
|
if self.skip:
|
||
|
self.skip = False
|
||
|
else:
|
||
|
self.sfx.play()
|
||
|
self.trigger = False
|
||
|
|
||
|
def draw(self):
|
||
|
game = self.game
|
||
|
# J'utilise le sprite special selection quand j'ai la souris dessus
|
||
|
self.sprite = self.spritenon
|
||
|
if self.collision:
|
||
|
self.sprite = self.spriteact
|
||
|
super().draw()
|
||
|
# Affichage du texte centré
|
||
|
value = 150
|
||
|
if self.collision: # Eclaircissement du texte
|
||
|
value+=75
|
||
|
img = game.fontfile.render(self.text, False, [value]*4)
|
||
|
posx = self.rect[0]+self.rect[2]/2-img.get_width()/2
|
||
|
posy = self.rect[1]+self.rect[3]/2-img.get_height()/2
|
||
|
self.game.window.blit(img, (posx, posy))
|