diff options
Diffstat (limited to 'helpers/action.py')
-rw-r--r-- | helpers/action.py | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/helpers/action.py b/helpers/action.py new file mode 100644 index 0000000..a68de90 --- /dev/null +++ b/helpers/action.py | |||
@@ -0,0 +1,119 @@ | |||
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(getattr(self, self.action + "_print")(**self.arguments)) | ||
32 | return getattr(self, self.action)(**self.arguments) | ||
33 | |||
34 | def command(self, command = "", **kwargs): | ||
35 | pass | ||
36 | |||
37 | def pause(self, music = None, **kwargs): | ||
38 | if music is not None: | ||
39 | music.pause() | ||
40 | else: | ||
41 | pygame.mixer.pause() | ||
42 | |||
43 | def play(self, music = None, fade_in = 0, start_at = 0, | ||
44 | restart_if_running = False, volume = 100, **kwargs): | ||
45 | if music is not None: | ||
46 | music.play() | ||
47 | else: | ||
48 | pygame.mixer.unpause() | ||
49 | |||
50 | def stop(self, music = None, fade_out = 0, **kwargs): | ||
51 | if music is not None: | ||
52 | music.stop() | ||
53 | else: | ||
54 | pygame.mixer.stop() | ||
55 | |||
56 | def stop_all_actions(self, **kwargs): | ||
57 | self.key.mapping.stop_all_running() | ||
58 | |||
59 | def volume(self, music = None, value = 100, **kwargs): | ||
60 | pass | ||
61 | |||
62 | def wait(self, duration = 0, **kwargs): | ||
63 | time.sleep(duration) | ||
64 | |||
65 | def command_print(self, command = "", **kwargs): | ||
66 | return "running command {}".format(command) | ||
67 | |||
68 | def pause_print(self, music = None, **kwargs): | ||
69 | if music is not None: | ||
70 | return "pausing {}".format(music.filename) | ||
71 | else: | ||
72 | return "pausing all musics" | ||
73 | |||
74 | def play_print(self, music = None, fade_in = 0, start_at = 0, | ||
75 | restart_if_running = False, volume = 100, **kwargs): | ||
76 | message = "starting " | ||
77 | if music is not None: | ||
78 | message += music.filename | ||
79 | else: | ||
80 | message += "music" | ||
81 | |||
82 | if start_at != 0: | ||
83 | message += " at {}s".format(start_at) | ||
84 | |||
85 | if fade_in != 0: | ||
86 | message += " with {}s fade_in".format(fade_in) | ||
87 | |||
88 | message += " at volume {}%".format(volume) | ||
89 | |||
90 | if restart_if_running: | ||
91 | message += " (restarting if already running)" | ||
92 | |||
93 | return message | ||
94 | |||
95 | def stop_print(self, music = None, fade_out = 0, **kwargs): | ||
96 | if music is not None: | ||
97 | if fade_out == 0: | ||
98 | return "stopping music {}".format(music.filename) | ||
99 | else: | ||
100 | return "stopping music {} with {}s fadeout".format(music.filename, fade_out) | ||
101 | else: | ||
102 | if fade_out == 0: | ||
103 | return "stopping all musics" | ||
104 | else: | ||
105 | return "stopping all musics with {}s fadeout".format(fade_out) | ||
106 | |||
107 | def stop_all_actions_print(self): | ||
108 | return "stopping all actions" | ||
109 | |||
110 | def volume_print(self, music = None, value = 100, *kwargs): | ||
111 | if music is not None: | ||
112 | return "setting volume of {} to {}%".format(music.filename, value) | ||
113 | else: | ||
114 | return "setting volume to {}%".format(value) | ||
115 | |||
116 | def wait_print(self, duration, **kwargs): | ||
117 | return "waiting {}s".format(duration) | ||
118 | |||
119 | |||