-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_planning_mode.py
275 lines (210 loc) · 8.87 KB
/
route_planning_mode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
##########################################################################
# Author: Griffin Della Grotte (gdellagr@andrew.cmu.edu)
#
# This UI module presents the interface for planning routable paths
##########################################################################
from pygame_structure import *
from map_pygame_structure import *
import routing_engine
import user_input
from mapcmu_data import *
import math
class RoutePlanningMode(MapPygameMode):
def __init__(self, call):
MapPygameMode.__init__(self, changeModeFn=call)
# Get a routing engine to add data to the node map
self.router = routing_engine.RoutingEngine()
self.textBox = user_input.TextInputBox()
self.initData()
self.initRouteNodes()
self.newFloor(self.selFloor)
#self.testRoute()
def initRouteNodes(self):
self.routeNodes = self.RouteNodeHandler(mToV=self.modelToView,
vToM=self.viewToModel)
self.refreshNodes()
self.mainSurf.objects.append(self.routeNodes)
def initData(self):
self.pointQueue = []
self.lastPoint = None
def newFloor(self, floor):
super().newFloor(floor)
self.initData()
self.refreshNodes()
def testRoute(self):
nodeA = self.router.getRoomNode("Parking - 1")
nodeB = self.router.getRoomNode("4303")
print("\nRoute: %r" % self.router.findRoute(nodeA,nodeB))
def removeLastAddedSegment(self):
if len(self.pointQueue) < 1: return False
else:
point = self.pointQueue.pop()
self.router.segTable.collection.remove({"_id" : point[0]})
self.routeNodes.removeNode(point[1])
findNode = [point[2][0],point[2][1],self.zPos,self.selBuilding]
q = { '$or' : [{"LOC_A" : findNode}, {"LOC_B" : findNode}]}
conn = self.router.segTable.collection.find(q)
if len(list(conn)) < 1:
self.routeNodes.removeNode(point[2])
self.lastPoint = None
else:
self.lastPoint = point[2]
def checkAndAddSegment(self, aLoc, bLoc):
#print("This point: %r Last point: %r" % (aLoc, bLoc))
if aLoc != None and bLoc != None:
aLocDB = [aLoc[0],aLoc[1],self.zPos,self.selBuilding]
bLocDB = [bLoc[0],bLoc[1],self.zPos,self.selBuilding]
q = { '$or' : [
{"LOC_A" : aLocDB, "LOC_B" : bLocDB},
{"LOC_B" : aLocDB, "LOC_A" : bLocDB}
]}
conn = self.router.segTable.collection.find(q)
if len(list(conn)) > 0: # This segment already exists
return
rId = self.addSegmentToMap(aLoc, bLoc)
self.pointQueue.append((rId,aLoc,bLoc))
if pygame.key.get_mods() & pygame.KMOD_CTRL:
self.textBox.doPopup("Enter room", self.textBoxCallback,
args=(rId,))
return rId
def addSegmentToMap(self, pos1, pos2):
return self.router.segTable.addSegment(
[pos1[0],pos1[1],self.zPos,self.selBuilding],
[pos2[0],pos2[1],self.zPos,self.selBuilding]
) # Returns entry id
def textBoxCallback(self, text, rId):
# When the user hits enter, the text box calls this function
print(text, rId)
self.router.segTable.collection.update({"_id" : rId},
{"$set" : {"NOTE" : text}})
##################################################
class RouteNode(PygameObject):
# Blue point that represents where segments meet or end
def initData(self):
self.radius = 5
self.color = (0,0,255)
def draw(self, surface, dims, offset=(0,0)):
super().draw(surface, dims, offset)
coords = self.mToV(self.pos, surface.surf)
pygame.draw.circle(surface.surf, self.color, coords, self.radius)
def getCoords(self):
return self.pos
def mousePressed(self, surface, x, y):
super().mousePressed(surface, x, y)
xr, yr = self.vToM((x,y), surface.surf)
distance = math.sqrt((xr - self.pos[0])**2 + \
(yr - self.pos[1])**2)
if distance < self.radius: # Clicked this point
return True
return False
class RouteNodeHandler(PygameObject):
# Container class for handling all the route nodes
def setTable(self, table):
self.table = table
def initData(self):
self.selectedPoint = None
self.NOT_SEL = (0,0,255)
self.SEL = (255,0,255)
def addNode(self, point):
self.objects.append(RoutePlanningMode.RouteNode(
point, self.mToV, self.vToM))
def removeNode(self, remNode):
for node in self.objects:
if node.pos == remNode:
self.objects.remove(node)
def purgeNodes(self, dbNodes):
# Reset all nodes to those passed in
self.objects = []
for node in dbNodes:
self.addNode((node[0],node[1]))
def getSelectedPosition(self):
if self.selectedPoint == None:
return None
else:
return self.selectedPoint.getCoords()
def clearSelection(self):
self.selectedPoint = None
for obj in self.objects:
obj.color = self.NOT_SEL
def mousePressed(self, surface, x, y):
found = None
for obj in self.objects:
result = obj.mousePressed(surface, x, y)
if result:
found = obj
obj.color = self.SEL
else:
obj.color = self.NOT_SEL
self.selectedPoint = found
def refreshNodes(self):
# Do a purge off of the database
self.routeNodes.purgeNodes(
self.router.getAllNodes(onFloor=self.selFloor))
#############################################
def drawRoutes(self):
segs = self.router.getAllSegments(onFloor=self.selFloor)
for seg in segs:
pos1, pos2 = seg["LOC_A"], seg["LOC_B"]
pos1, pos2 = pos1[:2], pos2[:2]
p1m = self.modelToView(pos1, self.mainSurf.surf)
p2m = self.modelToView(pos2, self.mainSurf.surf)
pygame.draw.line(self.mainSurf.surf, (0,0,255),
p1m, p2m)
def drawView(self,screen):
self.mainSurf.surf.fill((200,200,200))
self.drawMap()
self.drawRoutes()
self.mainSurf.drawObjects(offset=tuple(self.arrowOffset))
self.drawCornerMsg()
self.drawFooterHelp()
if self.showHelp:
self.drawHelpOverlay(Constants.helpRoutePlanning)
if self.textBox.enabled:
self.textBox.drawBox(self.mainSurf.surf)
super().drawView(screen)
#######################################
def mousePressed(self, event):
if self.textBox.enabled: return
# Do events for contained pygameobjects first
self.mainSurf.mouseObjects(event, self.arrowOffset)
xm, ym = self.viewToModel(event.pos, self.mainSurf.surf)
clickPos = self.routeNodes.getSelectedPosition()
if clickPos != None:
xm, ym = clickPos
else:
self.routeNodes.addNode((xm, ym))
segId = self.checkAndAddSegment((xm,ym), self.lastPoint)
self.lastPoint = (xm, ym)
def keyPressed(self, event):
super().keyPressed(event)
if self.textBox.enabled:
self.textBox.keyPressed(event)
return
mult = .1 if pygame.key.get_mods() & pygame.KMOD_SHIFT else 1
ctrl = pygame.key.get_mods() & pygame.KMOD_CTRL
if event.key == pygame.K_UP:
self.arrowOffset[1] -= int(100 * mult)
elif event.key == pygame.K_DOWN:
self.arrowOffset[1] += int(100 * mult)
elif event.key == pygame.K_LEFT:
self.arrowOffset[0] -= int(100 * mult)
elif event.key == pygame.K_RIGHT:
self.arrowOffset[0] += int(100 * mult)
elif event.key == pygame.K_PAGEUP:
self.upAFloor()
elif event.key == pygame.K_PAGEDOWN:
self.downAFloor()
elif event.key == pygame.K_u:
self.removeLastAddedSegment()
self.refreshNodes()
elif event.key == pygame.K_h:
self.showHelp = not self.showHelp
elif event.key == pygame.K_SPACE:
self.refreshNodes()
self.routeNodes.clearSelection()
self.lastPoint = None
elif event.key == pygame.K_ESCAPE:
self.changeModeFn()
#elif event.key == pygame.K_a:
# TESTING POPUP
#self.textBox.doPopup("Room", self.textBoxCallback)