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