|
9 | 9 |
|
10 | 10 | import time
|
11 | 11 |
|
| 12 | +import json |
| 13 | + |
12 | 14 | import kivy
|
13 | 15 | kivy.require('1.0.7')
|
14 | 16 |
|
|
17 | 19 | from kivy.core.window import Window
|
18 | 20 | from kivy.config import Config
|
19 | 21 | from os.path import join, dirname
|
20 |
| -from kivy.properties import StringProperty |
| 22 | +from kivy.properties import StringProperty, NumericProperty, ListProperty |
21 | 23 | from glob import glob
|
22 | 24 | from kivy.logger import Logger
|
23 | 25 | from kivy.uix.widget import Widget
|
24 | 26 | from kivy.graphics import Color, Ellipse, Line
|
25 | 27 |
|
| 28 | +utteranceStarted = False |
| 29 | +ballCounter = 0 |
| 30 | +gestureBuffer = [] |
| 31 | + |
| 32 | +class Ball(Widget): |
| 33 | + id = NumericProperty(0) |
| 34 | + x = NumericProperty(25) |
| 35 | + y = NumericProperty(25) |
| 36 | + color = ListProperty([1, 1, 1]) |
| 37 | + width = NumericProperty(50) |
| 38 | + height = NumericProperty(50) |
| 39 | + |
| 40 | + def on_touch_down(self, touch): |
| 41 | + global gestureBuffer |
| 42 | + if self.collide_point(*touch.pos) and utteranceStarted: |
| 43 | + print "Ball: %d" % self.id |
| 44 | + gestureBuffer.append({"type" : "ball", "val" : self.id}) |
| 45 | + return True |
| 46 | + |
26 | 47 | class BallWorld(App):
|
27 | 48 |
|
28 |
| - def build(self): |
29 |
| - |
30 |
| - def utterance_begin(*l): |
31 |
| - print "start" |
32 |
| - r = requests.get('http://127.0.0.1:7500/utterance/begin') |
33 |
| - print r.content |
34 |
| - r = requests.get('http://127.0.0.1:7500/utterance/speech/begin') |
| 49 | + def utterance_get(self, *args): |
| 50 | + global ballCounter |
| 51 | + print "get" |
| 52 | + r = requests.get('http://127.0.0.1:7500/utterance/get') |
| 53 | + if(r): |
35 | 54 | print r.content
|
| 55 | + tree = json.loads(r.content) |
| 56 | + colorIndex = {"red" : [1, 0, 0], "green" : [0, 1, 0], "blue" : [0, 0, 1], "white" : [1, 1, 1], "yellow" : [1, 1, 0], "purple" : [1, 0, 1], "orange" : [1, 0.5, 0]} |
| 57 | + if tree.has_key("val"): |
| 58 | + if tree["val"] == "create": |
| 59 | + coords = tree["children"][1]["val"].split(',') |
| 60 | + print coords |
| 61 | + self.root.add_widget(Ball(center_x = int(coords[0]), center_y = int(coords[1]), color=colorIndex[tree["children"][0]["val"]], id=ballCounter)) |
| 62 | + ballCounter += 1 |
| 63 | + elif tree["val"] == "destroy": |
| 64 | + for attrs in tree["children"]: |
| 65 | + if attrs.has_key("val") and attrs.has_key("type"): |
| 66 | + if attrs["type"] == "ball": |
| 67 | + for child in self.root.children: |
| 68 | + if hasattr(child, "id"): |
| 69 | + if child.id == int(tree["children"][0]["val"]): |
| 70 | + print "found ball by id" |
| 71 | + print child.id |
| 72 | + self.root.remove_widget(child) |
| 73 | + elif attrs["type"] == "color": |
| 74 | + for child in self.root.children: |
| 75 | + if hasattr(child, "color"): |
| 76 | + if child.color == colorIndex[tree["children"][0]["val"]]: |
| 77 | + print "found ball by color" |
| 78 | + print child.id |
| 79 | + print child.color |
| 80 | + self.root.remove_widget(child) |
| 81 | + elif tree["val"] == "color": |
| 82 | + if tree["children"][0]["type"] == "ball": |
| 83 | + for child in self.root.children: |
| 84 | + if hasattr(child, "id"): |
| 85 | + if child.id == int(tree["children"][0]["val"]): |
| 86 | + print "found ball by id" |
| 87 | + print child.id |
| 88 | + print child.color |
| 89 | + child.color = colorIndex[tree["children"][1]["val"]] |
| 90 | + elif tree["children"][0]["type"] == "color": |
| 91 | + for child in self.root.children: |
| 92 | + if hasattr(child, "color"): |
| 93 | + if child.color == colorIndex[tree["children"][0]["val"]]: |
| 94 | + print "found ball by color" |
| 95 | + print child.id |
| 96 | + child.color = colorIndex[tree["children"][1]["val"]] |
| 97 | + elif tree["val"] == "move": |
| 98 | + s = tree["children"][1]["val"] |
| 99 | + destination = list() |
| 100 | + for item in s.split(','): |
| 101 | + destination.append(int(item)) |
| 102 | + if tree["children"][0]["type"] == "ball": |
| 103 | + for child in self.root.children: |
| 104 | + if hasattr(child, "id"): |
| 105 | + if child.id == int(tree["children"][0]["val"]): |
| 106 | + print "moving by id: %d" % child.id |
| 107 | + child.center = destination |
| 108 | + elif tree["children"][0]["type"] == "color": |
| 109 | + for child in self.root.children: |
| 110 | + if hasattr(child, "color"): |
| 111 | + if child.color == colorIndex[tree["children"][0]["val"]]: |
| 112 | + print "moving by color" |
| 113 | + child.center = destination |
| 114 | + |
36 | 115 |
|
37 |
| - def utterance_end(*l): |
38 |
| - time.sleep(0.25) |
39 |
| - print "end" |
40 |
| - r = requests.get('http://127.0.0.1:7500/utterance/speech/end') |
41 |
| - #print "gesture" |
42 |
| - #r = requests.get('http://127.0.0.1:7500/utterance/gesture/data') |
43 |
| - print r.content |
44 |
| - r = requests.get('http://127.0.0.1:7500/utterance/end') |
45 |
| - print r.content |
46 |
| - btn = Button(text='utterance') |
47 |
| - btn.bind(on_press=utterance_begin) |
48 |
| - btn.bind(on_release=utterance_end) |
49 |
| - return btn |
50 |
| - |
| 116 | + def utterance_begin(self, *args): |
| 117 | + print "start" |
| 118 | + r = requests.get('http://127.0.0.1:7500/utterance/begin') |
| 119 | + #print r.content |
| 120 | + r = requests.get('http://127.0.0.1:7500/utterance/speech/begin') |
| 121 | + #print r.content |
| 122 | + |
| 123 | + def utterance_end(self, *args): |
| 124 | + global gestureBuffer |
| 125 | + print "gesture" |
| 126 | + if gestureBuffer != []: |
| 127 | + jsonData = json.dumps(gestureBuffer) |
| 128 | + print jsonData |
| 129 | + r = requests.post('http://127.0.0.1:7500/utterance/gesture/data', jsonData) |
| 130 | + gestureBuffer = [] |
| 131 | + time.sleep(0.25) |
| 132 | + print "end" |
| 133 | + r = requests.get('http://127.0.0.1:7500/utterance/speech/end') |
| 134 | + #print r.content |
| 135 | + r = requests.get('http://127.0.0.1:7500/utterance/end') |
| 136 | + #print r.content |
| 137 | + time.sleep(0.5) |
| 138 | + self.utterance_get() |
| 139 | + |
| 140 | + |
| 141 | + def on_key_down(self, instance, scancode, *largs): |
| 142 | + global utteranceStarted |
| 143 | + if scancode == 32: # spacebar |
| 144 | + if utteranceStarted == False: |
| 145 | + utteranceStarted = True |
| 146 | + self.utterance_begin() |
| 147 | + elif scancode == 50: |
| 148 | + self.utterance_get() |
| 149 | + else: |
| 150 | + print scancode |
| 151 | + |
| 152 | + def on_key_up(self, scancode, *largs): |
| 153 | + global utteranceStarted |
| 154 | + if largs[0] == 32: # spacebar |
| 155 | + if utteranceStarted == True: |
| 156 | + utteranceStarted = False |
| 157 | + self.utterance_end() |
| 158 | + |
| 159 | + def on_touch_down(instance, self, touch): |
| 160 | + global gestureBuffer |
| 161 | + for child in self.children[:]: |
| 162 | + if child.dispatch('on_touch_down', touch): |
| 163 | + return True |
| 164 | + if utteranceStarted: |
| 165 | + print "Point: %d %d" % touch.pos |
| 166 | + gestureBuffer.append({"type" : "point", "val" : "%d,%d" % touch.pos}) |
| 167 | + |
| 168 | + def build(self): |
| 169 | + root = self.root |
| 170 | + requests.get('http://127.0.0.1:7500/pipeline/start') |
| 171 | + Window.bind(on_key_down=self.on_key_down) |
| 172 | + Window.bind(on_key_up=self.on_key_up) |
| 173 | + root.bind(on_touch_down=self.on_touch_down) |
| 174 | + |
| 175 | + |
51 | 176 | if __name__ == '__main__':
|
52 |
| - Config.set('input', 'default', 'mouse') |
53 |
| - Config.write() |
| 177 | + #Config.set('input', 'default', 'mouse') |
| 178 | + #Config.write() |
| 179 | + # do the build_config sometime |
54 | 180 | BallWorld().run()
|
| 181 | + requests.get('http://127.0.0.1:7500/pipeline/stop') |
55 | 182 |
|
0 commit comments