]>
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 IB |
33 | getattr(self, self.action)(**self.arguments) |
34 | pygame.event.post(pygame.event.Event(pygame.USEREVENT)) | |
be27763f | 35 | |
ba9ea93a IB |
36 | def description(self): |
37 | return getattr(self, self.action + "_print")(**self.arguments) | |
38 | ||
be27763f IB |
39 | def command(self, command = "", **kwargs): |
40 | pass | |
41 | ||
42 | def pause(self, music = None, **kwargs): | |
43 | if music is not None: | |
44 | music.pause() | |
45 | else: | |
46 | pygame.mixer.pause() | |
47 | ||
9de92b6d IB |
48 | def unpause(self, music = None, **kwargs): |
49 | if music is not None: | |
50 | music.unpause() | |
51 | else: | |
52 | pygame.mixer.unpause() | |
53 | ||
be27763f IB |
54 | def play(self, music = None, fade_in = 0, start_at = 0, |
55 | restart_if_running = False, volume = 100, **kwargs): | |
56 | if music is not None: | |
0e5d59f7 IB |
57 | if restart_if_running: |
58 | if music.is_playing(): | |
59 | music.stop() | |
60 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
61 | else: | |
62 | if not music.is_playing(): | |
63 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
be27763f IB |
64 | else: |
65 | pygame.mixer.unpause() | |
66 | ||
67 | def stop(self, music = None, fade_out = 0, **kwargs): | |
68 | if music is not None: | |
0e5d59f7 | 69 | music.stop(fade_out = fade_out) |
be27763f | 70 | else: |
0e5d59f7 IB |
71 | if fade_out > 0: |
72 | pygame.fadeout(fade_out * 1000) | |
73 | else: | |
74 | pygame.mixer.stop() | |
be27763f IB |
75 | |
76 | def stop_all_actions(self, **kwargs): | |
77 | self.key.mapping.stop_all_running() | |
78 | ||
79 | def volume(self, music = None, value = 100, **kwargs): | |
0e5d59f7 IB |
80 | if music is not None: |
81 | music.set_volume(value) | |
82 | else: | |
83 | pass | |
be27763f | 84 | |
b86db9f1 | 85 | def wait(self, duration = 0, music = None, **kwargs): |
0e5d59f7 IB |
86 | # FIXME: Make it stoppable |
87 | # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait | |
b86db9f1 IB |
88 | if music is None: |
89 | time.sleep(duration) | |
90 | else: | |
91 | # TODO | |
92 | music.wait_end() | |
be27763f IB |
93 | |
94 | def command_print(self, command = "", **kwargs): | |
95 | return "running command {}".format(command) | |
96 | ||
97 | def pause_print(self, music = None, **kwargs): | |
98 | if music is not None: | |
9de92b6d | 99 | return "pausing « {} »".format(music.name) |
be27763f IB |
100 | else: |
101 | return "pausing all musics" | |
102 | ||
9de92b6d IB |
103 | def unpause_print(self, music = None, **kwargs): |
104 | if music is not None: | |
105 | return "unpausing « {} »".format(music.name) | |
106 | else: | |
107 | return "unpausing all musics" | |
108 | ||
be27763f IB |
109 | def play_print(self, music = None, fade_in = 0, start_at = 0, |
110 | restart_if_running = False, volume = 100, **kwargs): | |
111 | message = "starting " | |
112 | if music is not None: | |
9de92b6d | 113 | message += "« {} »".format(music.name) |
be27763f IB |
114 | else: |
115 | message += "music" | |
116 | ||
117 | if start_at != 0: | |
118 | message += " at {}s".format(start_at) | |
119 | ||
120 | if fade_in != 0: | |
121 | message += " with {}s fade_in".format(fade_in) | |
122 | ||
123 | message += " at volume {}%".format(volume) | |
124 | ||
125 | if restart_if_running: | |
126 | message += " (restarting if already running)" | |
127 | ||
128 | return message | |
129 | ||
130 | def stop_print(self, music = None, fade_out = 0, **kwargs): | |
131 | if music is not None: | |
132 | if fade_out == 0: | |
9de92b6d | 133 | return "stopping music « {} »".format(music.name) |
be27763f | 134 | else: |
9de92b6d | 135 | return "stopping music « {} » with {}s fadeout".format(music.name, fade_out) |
be27763f IB |
136 | else: |
137 | if fade_out == 0: | |
138 | return "stopping all musics" | |
139 | else: | |
140 | return "stopping all musics with {}s fadeout".format(fade_out) | |
141 | ||
0e5d59f7 | 142 | def stop_all_actions_print(self, **kwargs): |
be27763f IB |
143 | return "stopping all actions" |
144 | ||
0e5d59f7 | 145 | def volume_print(self, music = None, value = 100, **kwargs): |
be27763f | 146 | if music is not None: |
9de92b6d | 147 | return "setting volume of « {} » to {}%".format(music.name, value) |
be27763f IB |
148 | else: |
149 | return "setting volume to {}%".format(value) | |
150 | ||
151 | def wait_print(self, duration, **kwargs): | |
152 | return "waiting {}s".format(duration) | |
153 | ||
154 |