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
Widget
:add
add_anchor
add_centered
anchor
attention_lost
augment_mouse_event
bg_color
bg_image
border_color
border_width
call_handler
call_parent_handler
defer_drawing
dismiss
draw
draw_over
fg_color
focus
focus_switch
font
get_cursor
get_focus
get_margin_rect
get_root
get_top_widget
get_visible
global_to_local
has_focus
inherited
invalidate
is_gl_container
key_down
key_up
local_to_global
margin
menu_bar
parent
parent_resized
present
rect
relative_mode
remove
remove_anchor
resized
scale_bg
sel_color
set_parent
set_size_for_text
tab_stop
visible