]>
Commit | Line | Data |
---|---|---|
be27763f IB |
1 | import pygame |
2 | import time | |
3 | ||
4 | class Action: | |
5 | action_types = [ | |
6 | 'command', | |
7 | 'pause', | |
8 | 'play', | |
9 | 'stop', | |
10 | 'stop_all_actions', | |
9de92b6d | 11 | 'unpause', |
be27763f IB |
12 | 'volume', |
13 | 'wait', | |
14 | ] | |
15 | ||
16 | def __init__(self, action, key, **kwargs): | |
17 | if action in self.action_types: | |
18 | self.action = action | |
19 | else: | |
20 | raise Exception("Unknown action {}".format(action)) | |
21 | ||
22 | self.key = key | |
23 | self.arguments = kwargs | |
24 | ||
25 | def ready(self): | |
26 | if 'music' in self.arguments: | |
27 | return self.arguments['music'].loaded | |
28 | else: | |
29 | return True | |
30 | ||
31 | def run(self): | |
ba9ea93a | 32 | print(self.description()) |
9de92b6d | 33 | getattr(self, self.action)(**self.arguments) |
be27763f | 34 | |
ba9ea93a IB |
35 | def description(self): |
36 | return getattr(self, self.action + "_print")(**self.arguments) | |
37 | ||
be27763f IB |
38 | def command(self, command = "", **kwargs): |
39 | pass | |
40 | ||
41 | def pause(self, music = None, **kwargs): | |
42 | if music is not None: | |
43 | music.pause() | |
44 | else: | |
98ff4305 IB |
45 | for music in self.key.parent.open_files.values(): |
46 | if music.is_playing() and not music.is_paused(): | |
47 | music.pause() | |
be27763f | 48 | |
9de92b6d IB |
49 | def unpause(self, music = None, **kwargs): |
50 | if music is not None: | |
51 | music.unpause() | |
52 | else: | |
98ff4305 IB |
53 | for music in self.key.parent.open_files.values(): |
54 | if music.is_playing() and music.is_paused(): | |
55 | music.unpause() | |
9de92b6d | 56 | |
be27763f IB |
57 | def play(self, music = None, fade_in = 0, start_at = 0, |
58 | restart_if_running = False, volume = 100, **kwargs): | |
59 | if music is not None: | |
0e5d59f7 IB |
60 | if restart_if_running: |
61 | if music.is_playing(): | |
62 | music.stop() | |
63 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
64 | else: | |
65 | if not music.is_playing(): | |
66 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
be27763f IB |
67 | else: |
68 | pygame.mixer.unpause() | |
69 | ||
70 | def stop(self, music = None, fade_out = 0, **kwargs): | |
71 | if music is not None: | |
0e5d59f7 | 72 | music.stop(fade_out = fade_out) |
be27763f | 73 | else: |
0e5d59f7 | 74 | if fade_out > 0: |
9d505ec9 | 75 | pygame.mixer.fadeout(int(fade_out * 1000)) |
0e5d59f7 IB |
76 | else: |
77 | pygame.mixer.stop() | |
be27763f IB |
78 | |
79 | def stop_all_actions(self, **kwargs): | |
8612c5f8 | 80 | self.key.parent.stop_all_running() |
be27763f IB |
81 | |
82 | def volume(self, music = None, value = 100, **kwargs): | |
0e5d59f7 IB |
83 | if music is not None: |
84 | music.set_volume(value) | |
85 | else: | |
86 | pass | |
be27763f | 87 | |
b86db9f1 | 88 | def wait(self, duration = 0, music = None, **kwargs): |
0e5d59f7 IB |
89 | # FIXME: Make it stoppable |
90 | # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait | |
b86db9f1 IB |
91 | if music is None: |
92 | time.sleep(duration) | |
93 | else: | |
94 | # TODO | |
95 | music.wait_end() | |
be27763f IB |
96 | |
97 | def command_print(self, command = "", **kwargs): | |
98 | return "running command {}".format(command) | |
99 | ||
100 | def pause_print(self, music = None, **kwargs): | |
101 | if music is not None: | |
9de92b6d | 102 | return "pausing « {} »".format(music.name) |
be27763f IB |
103 | else: |
104 | return "pausing all musics" | |
105 | ||
9de92b6d IB |
106 | def unpause_print(self, music = None, **kwargs): |
107 | if music is not None: | |
108 | return "unpausing « {} »".format(music.name) | |
109 | else: | |
110 | return "unpausing all musics" | |
111 | ||
be27763f IB |
112 | def play_print(self, music = None, fade_in = 0, start_at = 0, |
113 | restart_if_running = False, volume = 100, **kwargs): | |
114 | message = "starting " | |
115 | if music is not None: | |
9de92b6d | 116 | message += "« {} »".format(music.name) |
be27763f IB |
117 | else: |
118 | message += "music" | |
119 | ||
120 | if start_at != 0: | |
121 | message += " at {}s".format(start_at) | |
122 | ||
123 | if fade_in != 0: | |
124 | message += " with {}s fade_in".format(fade_in) | |
125 | ||
126 | message += " at volume {}%".format(volume) | |
127 | ||
128 | if restart_if_running: | |
129 | message += " (restarting if already running)" | |
130 | ||
131 | return message | |
132 | ||
133 | def stop_print(self, music = None, fade_out = 0, **kwargs): | |
134 | if music is not None: | |
135 | if fade_out == 0: | |
9de92b6d | 136 | return "stopping music « {} »".format(music.name) |
be27763f | 137 | else: |
9de92b6d | 138 | return "stopping music « {} » with {}s fadeout".format(music.name, fade_out) |
be27763f IB |
139 | else: |
140 | if fade_out == 0: | |
141 | return "stopping all musics" | |
142 | else: | |
143 | return "stopping all musics with {}s fadeout".format(fade_out) | |
144 | ||
0e5d59f7 | 145 | def stop_all_actions_print(self, **kwargs): |
be27763f IB |
146 | return "stopping all actions" |
147 | ||
0e5d59f7 | 148 | def volume_print(self, music = None, value = 100, **kwargs): |
be27763f | 149 | if music is not None: |
9de92b6d | 150 | return "setting volume of « {} » to {}%".format(music.name, value) |
be27763f IB |
151 | else: |
152 | return "setting volume to {}%".format(value) | |
153 | ||
154 | def wait_print(self, duration, **kwargs): | |
155 | return "waiting {}s".format(duration) | |
156 | ||
157 |