Module albow.media.MusicVolumeControl

Source code
from pygame import Rect
from pygame import Surface

from pygame.event import Event

from albow.core.ui.Widget import Widget

try:
    from pygame.mixer import music
except ImportError:
    music = None
    print("Music not available")

if music:
    #
    #  Pygame 1.9 update
    #  music.set_endevent(MUSIC_END_EVENT)
    #  This needs updating
    music.set_endevent()


class MusicVolumeControl(Widget):
    """
    A control for adjusting the volume of music played by the music module.
    """
    def __init__(self, **kwds):
        #
        # Python 3 update
        #
        super().__init__(Rect((0, 0), (100, 20)), **kwds)

    def draw(self, surf: Surface):

        r = self.get_margin_rect()
        r.width = int(round(music.get_volume() * r.width))
        surf.fill(self.fg_color, r)

    def mouse_down(self, e: Event):
        self.mouse_drag(e)

    def mouse_drag(self, e: Event):

        m = self.margin
        w = self.width - 2 * m
        x = max(0.0, min(1.0, (e.local[0] - m) / w))
        music.set_volume(x)
        self.invalidate()

Classes

class MusicVolumeControl (**kwds)

A control for adjusting the volume of music played by the music module.

Creates a new widget, initially without any parent. If a rect is given, it specifies the new widget's initial s ize and position relative to its parent.

Args

rect
A PyGame rectangle defining the portion of the parent widget's coordinate system occupied by the

widget. Modifying this rectangle changes the widget's size and position.

**kwds
Additional attributes specified as key-value pairs
Source code
class MusicVolumeControl(Widget):
    """
    A control for adjusting the volume of music played by the music module.
    """
    def __init__(self, **kwds):
        #
        # Python 3 update
        #
        super().__init__(Rect((0, 0), (100, 20)), **kwds)

    def draw(self, surf: Surface):

        r = self.get_margin_rect()
        r.width = int(round(music.get_volume() * r.width))
        surf.fill(self.fg_color, r)

    def mouse_down(self, e: Event):
        self.mouse_drag(e)

    def mouse_drag(self, e: Event):

        m = self.margin
        w = self.width - 2 * m
        x = max(0.0, min(1.0, (e.local[0] - m) / w))
        music.set_volume(x)
        self.invalidate()

Ancestors

Methods

def mouse_down(self, e)
Source code
def mouse_down(self, e: Event):
    self.mouse_drag(e)
def mouse_drag(self, e)
Source code
def mouse_drag(self, e: Event):

    m = self.margin
    w = self.width - 2 * m
    x = max(0.0, min(1.0, (e.local[0] - m) / w))
    music.set_volume(x)
    self.invalidate()

Inherited members