Overflown/gamedata/objects/sliders/slider.py

80 lines
3.1 KiB
Python
Raw Normal View History

2021-06-25 14:42:35 +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
2021-06-25 16:13:52 +02:00
self.currentvalue = self.getvalue()
2021-06-25 14:42:35 +02:00
2021-06-25 16:13:52 +02:00
self.bgsprite = self.gensprite(w,h,folder)
2021-06-25 14:42:35 +02:00
self.sprite = game.pygame.transform.scale(game.sprite_lib["gui/slider/"+folder+"/circle.png"],(h,h))
self.maxwidth = w-h
2021-06-25 16:13:52 +02:00
self.rect = [self.getvalue()/self.max*(self.maxwidth)+self.baserect[0],self.baserect[1],h,h]
2021-06-25 14:42:35 +02:00
self.sfx = game.sound_lib["sfx/click.wav"]
self.dragged = False
2021-06-25 16:13:52 +02:00
def change(self):
2021-06-25 14:42:35 +02:00
print("Value : "+self.currentvalue)
2021-06-25 16:13:52 +02:00
def getvalue(self):
return self.game.globals["sfxvolume"]*100
2021-06-25 14:42:35 +02:00
def postovalue(self):
self.currentvalue = (self.rect[0]-self.baserect[0])/(self.maxwidth)*self.max
2021-06-25 16:13:52 +02:00
def gensprite(self,w,h,folder):
game = self.game
2021-06-25 14:42:35 +02:00
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
2021-06-25 16:13:52 +02:00
def step(self):
game = self.game
2021-06-25 14:42:35 +02:00
if self.rect[0]<=game.inputs["mouse"]["pos"][0]+game.globals["camerax"]<=self.rect[0]+self.rect[2]:
if self.rect[1]<=game.inputs["mouse"]["pos"][1]+game.globals["cameray"]<=self.rect[1]+self.rect[3]:
if game.inputs["mouse"]["click"]>1:
self.rect[0] = game.inputs["mouse"]["pos"][0]+game.globals["camerax"]-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()
if self.dragged and game.inputs["mouse"]["click"]==0:
# Le slider a été laché
2021-06-25 16:13:52 +02:00
self.change()
2021-06-25 14:42:35 +02:00
self.dragged = False
2021-06-25 16:13:52 +02:00
def draw(self):
game = self.game
2021-06-25 14:42:35 +02:00
game.window.blit(self.bgsprite,(self.baserect[0]-game.globals["camerax"],self.baserect[1]-game.globals["cameray"]))
game.window.blit(self.sprite,(self.rect[0]-game.globals["camerax"],self.rect[1]-game.globals["cameray"]))
txt = game.fontfile.render(str(round(self.currentvalue))+self.posttext,False,[255]*3)
px = self.baserect[0]+self.baserect[2]+50-game.globals["camerax"]
py = self.rect[1]+self.rect[3]/2-txt.get_height()/2-game.globals["camerax"]
game.window.blit(txt,[px,py])