]>
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 | 'volume', | |
12 | 'wait', | |
13 | ] | |
14 | ||
15 | def __init__(self, action, key, **kwargs): | |
16 | if action in self.action_types: | |
17 | self.action = action | |
18 | else: | |
19 | raise Exception("Unknown action {}".format(action)) | |
20 | ||
21 | self.key = key | |
22 | self.arguments = kwargs | |
23 | ||
24 | def ready(self): | |
25 | if 'music' in self.arguments: | |
26 | return self.arguments['music'].loaded | |
27 | else: | |
28 | return True | |
29 | ||
30 | def run(self): | |
31 | print(self.description()) | |
32 | return getattr(self, self.action)(**self.arguments) | |
33 | ||
34 | def description(self): | |
35 | return getattr(self, self.action + "_print")(**self.arguments) | |
36 | ||
37 | def command(self, command = "", **kwargs): | |
38 | pass | |
39 | ||
40 | def pause(self, music = None, **kwargs): | |
41 | if music is not None: | |
42 | music.pause() | |
43 | else: | |
44 | pygame.mixer.pause() | |
45 | ||
46 | def play(self, music = None, fade_in = 0, start_at = 0, | |
47 | restart_if_running = False, volume = 100, **kwargs): | |
48 | if music is not None: | |
49 | if restart_if_running: | |
50 | if music.is_playing(): | |
51 | music.stop() | |
52 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
53 | else: | |
54 | if not music.is_playing(): | |
55 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
56 | else: | |
57 | pygame.mixer.unpause() | |
58 | ||
59 | def stop(self, music = None, fade_out = 0, **kwargs): | |
60 | if music is not None: | |
61 | music.stop(fade_out = fade_out) | |
62 | else: | |
63 | if fade_out > 0: | |
64 | pygame.fadeout(fade_out * 1000) | |
65 | else: | |
66 | pygame.mixer.stop() | |
67 | ||
68 | def stop_all_actions(self, **kwargs): | |
69 | self.key.mapping.stop_all_running() | |
70 | ||
71 | def volume(self, music = None, value = 100, **kwargs): | |
72 | if music is not None: | |
73 | music.set_volume(value) | |
74 | else: | |
75 | pass | |
76 | ||
77 | def wait(self, duration = 0, **kwargs): | |
78 | # FIXME: Make it stoppable | |
79 | # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait | |
80 | time.sleep(duration) | |
81 | ||
82 | def command_print(self, command = "", **kwargs): | |
83 | return "running command {}".format(command) | |
84 | ||
85 | def pause_print(self, music = None, **kwargs): | |
86 | if music is not None: | |
87 | return "pausing {}".format(music.filename) | |
88 | else: | |
89 | return "pausing all musics" | |
90 | ||
91 | def play_print(self, music = None, fade_in = 0, start_at = 0, | |
92 | restart_if_running = False, volume = 100, **kwargs): | |
93 | message = "starting " | |
94 | if music is not None: | |
95 | message += music.filename | |
96 | else: | |
97 | message += "music" | |
98 | ||
99 | if start_at != 0: | |
100 | message += " at {}s".format(start_at) | |
101 | ||
102 | if fade_in != 0: | |
103 | message += " with {}s fade_in".format(fade_in) | |
104 | ||
105 | message += " at volume {}%".format(volume) | |
106 | ||
107 | if restart_if_running: | |
108 | message += " (restarting if already running)" | |
109 | ||
110 | return message | |
111 | ||
112 | def stop_print(self, music = None, fade_out = 0, **kwargs): | |
113 | if music is not None: | |
114 | if fade_out == 0: | |
115 | return "stopping music {}".format(music.filename) | |
116 | else: | |
117 | return "stopping music {} with {}s fadeout".format(music.filename, fade_out) | |
118 | else: | |
119 | if fade_out == 0: | |
120 | return "stopping all musics" | |
121 | else: | |
122 | return "stopping all musics with {}s fadeout".format(fade_out) | |
123 | ||
124 | def stop_all_actions_print(self, **kwargs): | |
125 | return "stopping all actions" | |
126 | ||
127 | def volume_print(self, music = None, value = 100, **kwargs): | |
128 | if music is not None: | |
129 | return "setting volume of {} to {}%".format(music.filename, value) | |
130 | else: | |
131 | return "setting volume to {}%".format(value) | |
132 | ||
133 | def wait_print(self, duration, **kwargs): | |
134 | return "waiting {}s".format(duration) | |
135 | ||
136 |