-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds_tab.py
110 lines (77 loc) · 3.25 KB
/
sounds_tab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from constants import FONT_MEDIUM
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
import os
from os.path import isfile, join
SOUNDS_DIR = 'sounds'
class SoundCategoryTab(TabbedPanelItem):
def __init__(self, directory, font_size=FONT_MEDIUM, *args, **kwargs):
super(SoundCategoryTab, self).__init__(
font_size=font_size, *args, **kwargs)
self.directory = directory
self.text = os.path.basename(directory)
self.choices = GridLayout(cols=10, spacing=10, size_hint_y=None)
self.choices.bind(minimum_height=self.choices.setter('height'))
choices_root = ScrollView()
choices_root.add_widget(self.choices)
self.content = BoxLayout(orientation='vertical')
self.content.add_widget(Button(
text='RELOAD',
on_press=lambda button: self.load_sounds(),
size_hint=(1, .1),
))
self.content.add_widget(choices_root)
self.load_sounds()
def load_sounds(self):
self.choices.clear_widgets()
sounds = list(sorted([
f for f in os.listdir(self.directory)
if isfile(join(self.directory, f))
]))
def play_sound(button):
if not button.sound:
print("Unable to play sound %s" % button.text)
if button.sound_state == 'stopped':
button.sound.play()
button.text += ' * '
elif button.sound_state == 'playing':
button.sound_state = 'looping'
button.sound.loop = True
button.text = button.text[:-3]
button.text += ' ! '
elif button.sound_state == 'looping':
button.sound.stop()
for sound in sounds:
btn = Button(
text='.'.join(sound.split('.')[:-1]),
size_hint_y=None,
height=100,
on_press=play_sound)
btn.sound_state = 'stopped'
def on_play(*args, button=btn):
button.sound_state = 'playing'
def on_stop(*args, button=btn):
button.sound_state = 'stopped'
button.text = button.text[:-3]
btn.sound = SoundLoader.load(join(self.directory, sound))
if btn.sound:
btn.sound.on_play = on_play
btn.sound.on_stop = on_stop
self.choices.add_widget(btn)
class SoundsTab(TabbedPanelItem):
def __init__(self, text, font_size=FONT_MEDIUM, *args, **kwargs):
super(SoundsTab, self).__init__(font_size=font_size, *args, **kwargs)
self.text = text
# Non player characters
content = BoxLayout(orientation='vertical')
self.add_widget(content)
category_tabs = TabbedPanel(do_default_tab=False, tab_width=200, tab_height=80)
categories = {x[0] for x in os.walk(SOUNDS_DIR)} - {SOUNDS_DIR}
print(categories)
for category in categories:
category_tabs.add_widget(SoundCategoryTab(category))
content.add_widget(category_tabs)