]>
Commit | Line | Data |
---|---|---|
4b2d79ca IB |
1 | from kivy.uix.widget import Widget |
2 | from kivy.properties import AliasProperty, BooleanProperty, ListProperty, StringProperty | |
3 | from kivy.clock import Clock | |
4 | from kivy.uix.behaviors import ButtonBehavior | |
5 | ||
be27763f IB |
6 | from .action import * |
7 | import time | |
4b2d79ca IB |
8 | |
9 | class Key(ButtonBehavior, Widget): | |
10 | key_sym = StringProperty(None) | |
11 | custom_color = ListProperty([0, 1, 0, 1]) | |
12 | custom_unready_color = ListProperty([0, 1, 0, 100/255]) | |
13 | description_title = StringProperty("") | |
14 | description = ListProperty([]) | |
15 | is_key_ready = BooleanProperty(True) | |
16 | ||
17 | def get_color(self): | |
18 | if not self.has_actions: | |
19 | return [1, 1, 1, 1] | |
20 | elif self.all_actions_ready: | |
21 | return self.custom_color | |
be27763f | 22 | else: |
4b2d79ca IB |
23 | return self.custom_unready_color |
24 | def set_color(self): | |
25 | pass | |
26 | ||
27 | color = AliasProperty(get_color, set_color, bind=['is_key_ready']) | |
be27763f | 28 | |
4b2d79ca IB |
29 | def __init__(self, **kwargs): |
30 | super(Key, self).__init__(**kwargs) | |
be27763f | 31 | self.actions = [] |
be27763f | 32 | |
4b2d79ca IB |
33 | def on_key_sym(self, key, key_sym): |
34 | if key_sym in self.parent.key_config: | |
35 | self.is_key_ready = False | |
36 | ||
37 | self.config = self.parent.key_config[key_sym] | |
be27763f | 38 | |
4b2d79ca IB |
39 | self.actions = [] |
40 | for key_action in self.config['actions']: | |
41 | self.add_action(key_action[0], **key_action[1]) | |
42 | ||
43 | if 'description' in self.config['properties']: | |
44 | key.set_description(self.config['properties']['description']) | |
45 | if 'color' in self.config['properties']: | |
46 | key.set_color(self.config['properties']['color']) | |
47 | ||
48 | Clock.schedule_interval(self.check_all_active, 1) | |
49 | ||
50 | def check_all_active(self, dt): | |
51 | if self.all_actions_ready: | |
52 | self.is_key_ready = True | |
53 | return False | |
be27763f | 54 | |
b86db9f1 | 55 | def set_description(self, description): |
4b2d79ca IB |
56 | if description[0] is not None: |
57 | self.description_title = str(description[0]) | |
58 | for desc in description[1:]: | |
d479af33 IB |
59 | if desc is None: |
60 | self.description.append("") | |
61 | else: | |
4b2d79ca | 62 | self.description.append(str(desc).replace(" ", " ")) |
b86db9f1 IB |
63 | |
64 | def set_color(self, color): | |
4b2d79ca IB |
65 | color = [x / 255 for x in color] |
66 | color.append(1) | |
67 | self.custom_color = color | |
68 | color[3] = 100 / 255 | |
b86db9f1 IB |
69 | self.custom_unready_color = tuple(color) |
70 | ||
4b2d79ca | 71 | @property |
be27763f IB |
72 | def has_actions(self): |
73 | return len(self.actions) > 0 | |
74 | ||
4b2d79ca | 75 | @property |
be27763f IB |
76 | def all_actions_ready(self): |
77 | return all(action.ready() for action in self.actions) | |
78 | ||
79 | def add_action(self, action_name, **arguments): | |
80 | self.actions.append(Action(action_name, self, **arguments)) | |
81 | ||
4b2d79ca | 82 | def do_actions(self): |
f029e879 IB |
83 | if not self.enabled: |
84 | return None | |
85 | ||
be27763f IB |
86 | print("running actions for {}".format(self.key_sym)) |
87 | start_time = time.time() | |
4b2d79ca | 88 | self.parent.start_running(self, start_time) |
cd255f24 | 89 | action_number = 0 |
be27763f | 90 | for action in self.actions: |
4b2d79ca IB |
91 | if self.parent.keep_running(self, start_time): |
92 | self.list_actions(action_number = action_number + 0.5) | |
be27763f | 93 | action.run() |
cd255f24 | 94 | action_number += 1 |
4b2d79ca | 95 | self.list_actions(action_number = action_number) |
ba9ea93a | 96 | |
4b2d79ca | 97 | self.parent.finished_running(self, start_time) |
be27763f | 98 | |
4b2d79ca IB |
99 | def list_actions(self, action_number = 0): |
100 | self.parent.parent.ids['ActionList'].update_list(self, action_number) | |
be27763f | 101 | |
4b2d79ca IB |
102 | def on_press(self): |
103 | self.list_actions() | |
104 | pass |