PinmikPanik/gamedata/objects/sliders/slider.py

79 lines
2.7 KiB
Python
Raw Normal View History

2021-09-10 19:22:30 +02:00
import gamedata.objects.base as base
class Slider(base.BaseObject):
def __init__(self,x,y,game,w=100,h=64,folder="base"):
super().__init__(x,y,game,w,h) # initialise l'objet de base avec les bons arguments
self.posttext = " %"
self.max = 100
self.currentvalue = self.getvalue()
self.bgsprite = self.gensprite(w,h,folder)
self.sprite = game.pygame.transform.scale(game.sprite_lib["gui/slider/"+folder+"/circle.png"],(h,h))
self.maxwidth = w-h
self.rect = [self.getvalue()/self.max*(self.maxwidth)+self.baserect[0],self.baserect[1],h,h]
self.sfx = game.sound_lib["sfx/click.wav"]
self.dragged = False
def change(self):
print("Value : "+self.currentvalue)
def getvalue(self):
return self.game.globals["sfxvolume"]*100
def postovalue(self):
self.currentvalue = (self.rect[0]-self.baserect[0])/(self.maxwidth)*self.max
def gensprite(self,w,h,folder):
game = self.game
left = game.sprite_lib["gui/slider/"+folder+"/left.png"]
left = game.pygame.transform.scale(left,(round(h/left.get_height()*left.get_width()),h))
right = game.sprite_lib["gui/slider/"+folder+"/right.png"]
right = game.pygame.transform.scale(right,(round(h/right.get_height()*right.get_width()),h))
middle = game.sprite_lib["gui/slider/"+folder+"/middle.png"]
middle = game.pygame.transform.scale(middle,(w-left.get_width()-right.get_width(),h))
surface = game.pygame.Surface((w,h),game.pygame.SRCALPHA)
surface.blit(left,(0,0))
surface.blit(middle,(left.get_width(),0))
surface.blit(right,(left.get_width()+middle.get_width(),0))
return surface
def step(self):
game = self.game
2021-09-17 07:43:53 +02:00
if self.baserect.collidepoint(game.inputs["mouse"]["pos"]):
if game.inputs["mouse"]["click"]>1:
self.rect[0] = game.inputs["mouse"]["pos"][0]-self.rect[3]/2
self.rect[0] = min(self.rect[0],self.baserect[0]+self.maxwidth)
self.rect[0] = max(self.rect[0],self.baserect[0])
self.dragged = True
self.postovalue()
2021-09-10 19:22:30 +02:00
if self.dragged and game.inputs["mouse"]["click"]==0:
# Le slider a été laché
self.change()
self.dragged = False
def draw(self):
game = self.game
2021-09-16 08:34:40 +02:00
game.window.blit(self.bgsprite,self.baserect[:2])
game.window.blit(self.sprite,self.rect[:2])
2021-09-10 19:22:30 +02:00
txt = game.fontfile.render(str(round(self.currentvalue))+self.posttext,False,[255]*3)
2021-09-16 08:34:40 +02:00
px = self.baserect[0]+self.baserect[2]+50
py = self.rect[1]+self.rect[3]/2-txt.get_height()/2
2021-09-10 19:22:30 +02:00
game.window.blit(txt,[px,py])