]>
Commit | Line | Data |
---|---|---|
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', | |
11 | 'unpause', | |
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): | |
32 | print(self.description()) | |
33 | getattr(self, self.action)(**self.arguments) | |
34 | ||
35 | def description(self): | |
36 | return getattr(self, self.action + "_print")(**self.arguments) | |
37 | ||
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: | |
45 | for music in self.key.parent.open_files.values(): | |
46 | if music.is_playing() and not music.is_paused(): | |
47 | music.pause() | |
48 | ||
49 | def unpause(self, music = None, **kwargs): | |
50 | if music is not None: | |
51 | music.unpause() | |
52 | else: | |
53 | for music in self.key.parent.open_files.values(): | |
54 | if music.is_playing() and music.is_paused(): | |
55 | music.unpause() | |
56 | ||
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: | |
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) | |
67 | else: | |
68 | pygame.mixer.unpause() | |
69 | ||
70 | def stop(self, music = None, fade_out = 0, **kwargs): | |
71 | if music is not None: | |
72 | music.stop(fade_out = fade_out) | |
73 | else: | |
74 | if fade_out > 0: | |
75 | pygame.mixer.fadeout(int(fade_out * 1000)) | |
76 | else: | |
77 | pygame.mixer.stop() | |
78 | ||
79 | def stop_all_actions(self, **kwargs): | |
80 | self.key.parent.stop_all_running() | |
81 | ||
82 | def volume(self, music = None, value = 100, **kwargs): | |
83 | if music is not None: | |
84 | music.set_volume(value) | |
85 | else: | |
86 | pass | |
87 | ||
88 | def wait(self, duration = 0, music = None, **kwargs): | |
89 | # FIXME: Make it stoppable | |
90 | # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait | |
91 | if music is None: | |
92 | time.sleep(duration) | |
93 | else: | |
94 | # TODO | |
95 | music.wait_end() | |
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: | |
102 | return "pausing « {} »".format(music.name) | |
103 | else: | |
104 | return "pausing all musics" | |
105 | ||
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 | ||
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: | |
116 | message += "« {} »".format(music.name) | |
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: | |
136 | return "stopping music « {} »".format(music.name) | |
137 | else: | |
138 | return "stopping music « {} » with {}s fadeout".format(music.name, fade_out) | |
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 | ||
145 | def stop_all_actions_print(self, **kwargs): | |
146 | return "stopping all actions" | |
147 | ||
148 | def volume_print(self, music = None, value = 100, **kwargs): | |
149 | if music is not None: | |
150 | return "setting volume of « {} » to {}%".format(music.name, value) | |
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 |