-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
576 lines (422 loc) · 20.4 KB
/
main.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import sys, getopt
import di, view, model.entity as entity, model.model as model, model.ui as modelui
import network
from data import Serializer
from data import Importer
import re
from options import getOptions
from callbacks import *
from pathfinder import highlightPath
from PyQt5 import QtCore, QtGui, QtWidgets
if __name__ == '__main__':
application = QtWidgets.QApplication(sys.argv)
application.setStyle('plastique')
QPixmap = QtGui.QPixmap("ui/icons/hychsohn_256x256x32_transparent.png")
QSplashScreen = QtWidgets.QSplashScreen(QPixmap)
QSplashScreen.show()
QProgressBar = QtWidgets.QProgressBar(QSplashScreen)
QProgressBar.setMinimum(0)
QProgressBar.setMaximum(100)
QProgressBar.setTextVisible(False)
QProgressBar.setAlignment(QtCore.Qt.AlignBottom | QtCore.Qt.AlignHCenter)
QProgressBar.setFixedWidth(250)
QProgressBar.move(0, 220)
QProgressBar.show()
QSplashScreen.raise_()
application.processEvents()
options = getOptions()
Serializer.mapFile = options.mapFile
registry = model.Registry()
navigator = model.Navigator()
factory = model.RoomFactory()
mapModel = model.Map()
clipboard = model.Clipboard()
di.container.register('Config', model.Config())
di.container.register('CoordinatesHelper', model.CoordinatesHelper())
di.container.register('RoomFactory', factory)
di.container.register('Registry', registry)
di.container.register('Navigator', navigator)
di.container.register('Map', mapModel)
di.container.register('Clipboard', clipboard)
registry.roomShadow = view.RoomShadow()
registry.roomShadow.hide()
registry.shadowLink = view.ShadowLink()
registry.shadowLink.hide()
window = view.uiMainWindow()
QProgressBar.setValue(20)
application.processEvents()
if options.noPanels: window.hidePanels()
registry.mainWindow = window
import os
baseDir = os.getenv("USERPROFILE") if sys.platform == 'win32' else os.getenv("HOME")
baseDir = baseDir+'/.tf-mapper/'
sys.path.insert(0, baseDir)
import shortcuts
import roomClasses
window.buildShortcuts(shortcuts)
window.buildClasses(roomClasses)
sys.path.pop(0)
#adding panel actions
toolsPanelAction = window.uiComponentToolsPanel.toggleViewAction()
toolsPanelAction.setShortcut('CTRL+T')
window.menuView.addAction(toolsPanelAction)
propsPanelAction = window.uiComponentPropertiesPanel.toggleViewAction()
propsPanelAction.setShortcut('CTRL+P')
window.menuView.addAction(propsPanelAction)
if options.width and options.height: window.resize(int(options.width), int(options.height))
window.compassU.setShortcut(QtWidgets.QApplication.translate("MainWindow", options.keyUp, None))
window.compassD.setShortcut(QtWidgets.QApplication.translate("MainWindow", options.keyDown, None))
roomProperties = modelui.RoomProperties(window)
#navigator.roomSelectedSignal.connect(roomProperties.updatePropertiesFromRoom)
di.container.register('Properties', roomProperties)
navigator = model.Navigator()
window.mapView().scale(0.5,0.5)
QProgressBar.setValue(35)
application.processEvents()
QProgressBar.setValue(70)
application.processEvents()
def zoomIn():
window.mapView().scale(1.2, 1.2)
def zoomOut():
window.mapView().scale(0.8, 0.8)
#print(window.mapView().transform())
registry.zoonInFunction = zoomIn
registry.zoonOutFunction = zoomOut
def deb(str):
print(str)
window.compassN.clicked.connect(navigator.goNorth)
window.compassNE.clicked.connect(navigator.goNorthEast)
window.compassE.clicked.connect(navigator.goEast)
window.compassSE.clicked.connect(navigator.goSouthEast)
window.compassS.clicked.connect(navigator.goSouth)
window.compassSW.clicked.connect(navigator.goSouthWest)
window.compassW.clicked.connect(navigator.goWest)
window.compassNW.clicked.connect(navigator.goNorthWest)
window.compassU.clicked.connect(navigator.goUp)
window.compassD.clicked.connect(navigator.goDown)
window.zoomIn.clicked.connect(zoomIn)
window.zoomOut.clicked.connect(zoomOut)
window.goLevelUp.clicked.connect(navigator.goLevelUp)
window.goLevelDown.clicked.connect(navigator.goLevelDown)
window.walkerModeSelector.currentIndexChanged.connect(navigator.enableCreation)
window.autoPlacement.toggled.connect(navigator.enableAutoPlacement)
window.sameUpDown.toggled.connect(navigator.enableSameUpDown)
def reportSceneRect():
print(window.mapView().sceneRect())
def reportActiveRoom():
print(registry.currentlyVisitedRoom.getId())
def reportServerStatus():
print(registry.broadcasterServer.tcpServer().isListening())
def dumpRoom():
room = registry.currentlyVisitedRoom
print(registry.currentlyVisitedRoom)
def fireCommand():
dispatchServerCommand(str(window.commandInput.text()))
window.commandTrigger.clicked.connect(fireCommand)
QProgressBar.setValue(80)
application.processEvents()
def commandDeleteActiveRoom():
if registry.currentlyVisitedRoom is not None:
commandDeleteRoomById(registry.currentlyVisitedRoom.getId())
def commandDeleteRoomById(roomId):
roomToDelete = mapModel.getRoomById(roomId)
if roomToDelete is not None:
roomToDelete.delete()
def dumpImage():
scene = window.mapView().scene()
scene.clearSelection()
scene.setSceneRect(scene.itemsBoundingRect())
img = QtGui.QImage(scene.sceneRect().size().toSize(), QtGui.QImage.Format_ARGB32)
img.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(img)
scene.render(painter)
painter.end()
print(img.save("/tmp/mapDump.png"))
def dispatchServerCommand(command):
if command == 'dumpImage': dumpImage()
if command == 'navigate:exit:n': navigator.goNorth()
if command == 'navigate:exit:polnoc': navigator.goNorth()
if command == 'navigate:exit:ne': navigator.goNorthEast()
if command == 'navigate:exit:polnocny-wschod': navigator.goNorthEast()
if command == 'navigate:exit:e': navigator.goEast()
if command == 'navigate:exit:wschod': navigator.goEast()
if command == 'navigate:exit:se': navigator.goSouthEast()
if command == 'navigate:exit:poludniowy-wschod': navigator.goSouthEast()
if command == 'navigate:exit:s': navigator.goSouth()
if command == 'navigate:exit:poludnie': navigator.goSouth()
if command == 'navigate:exit:sw': navigator.goSouthWest()
if command == 'navigate:exit:poludniowy-zachod': navigator.goSouthWest()
if command == 'navigate:exit:w': navigator.goWest()
if command == 'navigate:exit:zachod': navigator.goWest()
if command == 'navigate:exit:nw': navigator.goNorthWest()
if command == 'navigate:exit:polnocny-zachod': navigator.goNorthWest()
if command == 'navigate:exit:u': navigator.goUp()
if command == 'navigate:exit:gora': navigator.goUp()
if command == 'navigate:exit:d': navigator.goDown()
if command == 'navigate:exit:dol': navigator.goDown()
if command == 'revert': revertToLastRoom()
if command == 'map:undo': navigator.undoCreation()
if command == 'map:revert': revertToLastRoom()
if command == 'map:mode:walk': window.walkerModeSelector.setCurrentIndex(int(0))
if command == 'map:mode:create': window.walkerModeSelector.setCurrentIndex(int(1))
if command == 'map:mode:toggle': window.walkerModeSelector.setCurrentIndex(int(not window.walkerModeSelector.currentIndex()))
if command == 'map:zoom:in': zoomIn()
if command == 'map:zoom:out': zoomOut()
if command == 'map:room:delete': commandDeleteActiveRoom()
if command == 'path:clear': highlightPath(mapModel, registry.currentlyVisitedRoom, None)
match = re.match(r'map:room:delete:(.*)', command)
if match is not None:
commandDeleteRoomById(match.group(1))
match = re.match(r'path:highlight:(.*)', command)
if match is not None:
highlightPath(mapModel, registry.currentlyVisitedRoom, str(match.group(1)))
match = re.match(r'navigate:custom:(.*)', command)
if match is not None:
navigator.goCustom(match.group(1))
match = re.match(r'navigate:follow:(.*)', command)
if match is not None:
navigator.goFollow(match.group(1))
match = re.match(r'lookup:([a-z0-9\-]*)', command)
if match is not None:
lookupRoom(match.group(1))
def executeManualLink():
leftRoomId = str(window.manualLinkRoomLeft.text())
rightRoomId = str(window.manualLinkRoomRight.text())
leftExit = window.manualLinkLinkLeft.currentText()
rightExit = window.manualLinkLinkRight.currentText()
if not leftRoomId or not rightRoomId or not leftExit or not rightExit: return False
leftRoom = mapModel.rooms()[leftRoomId] if leftRoomId in mapModel.rooms() else None
rightRoom = mapModel.rooms()[rightRoomId] if rightRoomId in mapModel.rooms() else None
if not leftRoom or not rightRoom: return False
leftExit = model.Direction.mapFromLabel(leftExit)
rightExit = model.Direction.mapFromLabel(rightExit)
rightRebind = leftRebind = rightLinkMask = leftLinkMask = None
if model.Direction.OTHER in [leftExit, rightExit]:
leftRebind = window.manualLinkCustomLinkLeft.text() if len(window.manualLinkCustomLinkLeft.text()) else None
rightRebind = window.manualLinkCustomLinkRight.text() if len(window.manualLinkCustomLinkRight.text()) else None
factory.linkRooms(\
leftRoom, leftExit, rightRoom, rightExit, \
rightRoom.getLevel().getView() if leftExit not in [model.Direction.U, model.Direction.D] and rightExit not in [model.Direction.U, model.Direction.D] and rightRoom.getLevel().getId() == leftRoom.getLevel().getId() else None,\
None, \
None, \
leftRebind, \
rightRebind
)
leftRoom.addExit(leftExit)
rightRoom.addExit(rightExit)
leftRoom.getView().update()
rightRoom.getView().update()
def copyManualLinkRoomId(isRight=False):
if not isRight:
window.manualLinkRoomLeft.setText(window.roomIdDisplay.text())
else:
window.manualLinkRoomRight.setText(window.roomIdDisplay.text())
def manualMergeRooms():
leftRoomId = str(window.manualLinkRoomLeft.text())
rightRoomId = str(window.manualLinkRoomRight.text())
if not leftRoomId or not rightRoomId: return False
leftRoom = mapModel.rooms()[leftRoomId] if leftRoomId in mapModel.rooms() else None
rightRoom = mapModel.rooms()[rightRoomId] if rightRoomId in mapModel.rooms() else None
if not leftRoom or not rightRoom: return False
navigator.mergeRooms(rightRoom, leftRoom)
def manualLinkInsertFromSelection():
items = registry.currentLevel.getView().selectedItems()
try:
window.manualLinkRoomLeft.setText(items[0].getModel().getId())
window.manualLinkRoomRight.setText(items[1].getModel().getId())
except: return False
def manualLookupRoom():
if not window.roomIdDisplay.text(): return False
lookupRoom(str(window.roomIdDisplay.text()))
def lookupRoom(roomId):
roomId = str(roomId)
if roomId not in mapModel.rooms().keys(): return False
room = mapModel.rooms()[roomId]
navigator.markVisitedRoom(room)
def revertToLastRoom():
if registry.previouslyVisitedRoom is not None:
lookupRoom(registry.previouslyVisitedRoom.getId())
def toggleCustomLinkInput(isRight=False):
if isRight:
if window.manualLinkLinkRight.currentText() == 'Custom':
window.manualLinkCustomLinkRight.setEnabled(True)
else:
window.manualLinkCustomLinkRight.setEnabled(False)
else:
if window.manualLinkLinkLeft.currentText() == 'Custom':
window.manualLinkCustomLinkLeft.setEnabled(True)
else:
window.manualLinkCustomLinkLeft.setEnabled(False)
window.manualLinkRoomLeftInsert.clicked.connect(lambda: copyManualLinkRoomId())
window.manualLinkRoomRightInsert.clicked.connect(lambda: copyManualLinkRoomId(True))
window.manualLinkExecute.clicked.connect(executeManualLink)
window.manualMergeExecute.clicked.connect(manualMergeRooms)
window.manualLinkInsertFromSelection.clicked.connect(manualLinkInsertFromSelection)
window.manualLookupRoom.clicked.connect(manualLookupRoom)
window.manualLinkLinkLeft.currentIndexChanged.connect(lambda: toggleCustomLinkInput())
window.manualLinkLinkRight.currentIndexChanged.connect(lambda: toggleCustomLinkInput(True))
def switchClassesShowing(show):
registry.applyClasses = show
def switchColorsShowing(show):
registry.applyColors = show
registry.blockCreation = False
def switchCreationBlock(show):
registry.blockCreation = not show
def renameCurrentZone():
currentZone = mapModel.currentZone()
zoneName = QtWidgets.QInputDialog.getText(window, 'Rename zone', 'Name', QtWidgets.QLineEdit.Normal, currentZone.name())
zoneName, ok = zoneName
if not ok:
return
currentZone.setName(zoneName)
window.selectZone.setItemText(window.selectZone.currentIndex(), zoneName)
window.menuActionShowClasses.toggled.connect(switchClassesShowing)
window.menuActionShowColors.toggled.connect(switchColorsShowing)
#window.actionEnableCreation.toggled.connect(switchCreationBlock)
window.actionRenameZone.triggered.connect(renameCurrentZone)
def showCreationColorPicker():
if registry.defColor:
color = QtWidgets.QColorDialog.getColor(QtGui.QColor(registry.defColor))
else: color = QtWidgets.QColorDialog.getColor()
if not color.isValid(): return
registry.setDefaultColor(color)
window.uiCreationColor.blockSignals(True)
window.uiCreationColor.setText(registry.defColor)
window.uiCreationColor.blockSignals(False)
window.uiCreationColor.textChanged.emit(registry.defColor)
def updateSelectionColor(color):
for item in window.mapView().scene().selectedItems():
item.getModel().setProperty(model.Room.PROP_COLOR, str(color))
window.uiCreationColor.textChanged.connect(registry.setDefaultColor)
window.uiCreationColor.textChanged.connect(updateSelectionColor)
window.uiCreationColorPicker.clicked.connect(showCreationColorPicker)
def reapplyCreationClass():
window.uiCreationClass.currentIndexChanged.emit(window.uiCreationClass.currentIndex())
def applyCreationClass(roomClassIndex):
registry.setDefaultClass(window.uiCreationClass.currentText())
for item in window.mapView().scene().selectedItems():
item.getModel().setProperty(model.Room.PROP_CLASS, window.uiCreationClass.currentText())
pass
def processZoneSelect(zoneindex):
if zoneindex == -1: return
zoneId = window.selectZone.itemData(zoneindex)
navigator.changeZone(zoneId)
navigator.switchLevel(0)
window.selectZone.currentIndexChanged.connect(processZoneSelect)
window.uiCreationClass.currentIndexChanged.connect(applyCreationClass)
window.uiCreationClassApply.clicked.connect(reapplyCreationClass)
window.centerOnMove.toggled.connect(registry.setCenterAt)
QProgressBar.setValue(90)
application.processEvents()
if not options.noServer:
if not options.spawnRemoteConnection:
registry.connection = broadcasterServer = network.Broadcaster(23923)
broadcasterServer.dataReceived.connect(dispatchServerCommand)
else:
registry.connection = clientServer = network.Listener('localhost', 9999)
clientServer.dataReceived.connect(dispatchServerCommand)
from formlayout import fedit
settings = QtCore.QSettings('MudMapper', 'net.michaeldonat')
def showPreferences():
width = int(settings.value('width', 400))
height = int(settings.value('height', 200))
server = settings.value('server', False) == True
panels = settings.value('panels', False) == True
room = str(settings.value('room', ''))
map = str(settings.value('map', ''))
datalist = [('Width', width), ('Height', height), ('Disable socket server', server),('Show panels', panels),('Start room', room), ('Map file', map)]
result = fedit(datalist, title="Preferences")
if result is not None:
settings.setValue('width', result[0])
settings.setValue('height', result[1])
settings.setValue('server', result[2])
settings.setValue('panels', result[3])
settings.setValue('room', result[4])
settings.setValue('map', result[5])
window.actionPreferences.triggered.connect(showPreferences)
def openMap(fileName=None):
if not fileName or fileName is None:
fileName = QtWidgets.QFileDialog.getOpenFileName(None, 'Open map...', Serializer.getHomeDir(), 'Map (*.map *.db)')
if not fileName or fileName is None or str(fileName[0]) == "":
return
QProgressBar = QtWidgets.QProgressDialog(window)
QProgressBar.setMinimum(0)
QProgressBar.setMaximum(100)
QProgressBar.setLabelText(f'Loading {fileName}')
QProgressBar.setFixedWidth(250)
QProgressBar.show()
clearMap()
Serializer.mapFile = fileName
result = Serializer.loadMap(window, window.mapView(), QProgressBar, application)
updateTitle()
QProgressBar.setValue(100)
QProgressBar.hide()
QProgressBar.destroy()
QProgressBar = None
return result
def clearMap():
Serializer.mapFile = None
mapModel.clear()
registry.reinit()
updateTitle()
window.selectZone.clear()
def dumpMap():
if Serializer.mapFile is None:
fileName = QtWidgets.QFileDialog.getSaveFileName(None, 'Save map...', Serializer.getHomeDir(), 'Map (*.map *.db)')
if str(fileName[0]) == "":
return
Serializer.mapFile = str(fileName[0])
Serializer.saveMap('123', mapModel)
def dumpNewMap():
Serializer.mapFile = None
dumpMap()
def newMap():
clearMap()
createNewZone('Zone 1')
window.menuActionOpen.triggered.connect(openMap)
window.menuActionNew.triggered.connect(newMap)
window.menuActionSave.triggered.connect(dumpMap)
window.menuActionSaveAs.triggered.connect(dumpNewMap)
def updateTitle():
if Serializer.mapFile is not None:
window.setWindowTitle('MudMapper by thornag - %s' % Serializer.mapFile)
else:
window.setWindowTitle('MudMapper by thornag')
updateTitle()
#exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
#exitAction.setShortcut('Ctrl+Q')
#exitAction.setStatusTip('Exit application')
#exitAction.triggered.connect(QtGui.qApp.quit)
def createNewZone(name=None):
if name is None or name is False:
zoneName = QtWidgets.QInputDialog.getText(window, 'New zone', 'Name', QtWidgets.QLineEdit.Normal, '')
zoneName, ok = zoneName
if not ok:
return
else:
zoneName = name
zone = factory.spawnZone(zoneName)
window.selectZone.addItem(zone.name(), str(zone.id()))
window.selectZone.setCurrentIndex(window.selectZone.count()-1)
scene = factory.spawnLevel(0).getView()
window.mapView().setScene(scene)
window.addZoneButton.clicked.connect(createNewZone)
window.show()
window.raise_()
QProgressBar.setValue(100)
application.processEvents()
QSplashScreen.finish(window)
application.processEvents()
createLevel = True
if Serializer.mapFile is not None:
resop = openMap(Serializer.mapFile)
if resop is not False and resop is not None:
createLevel = False
if options.room: lookupRoom(options.room)
application.processEvents()
if createLevel:
zone = createNewZone('Zone 1')
navigator.enableCreation(False)
application.processEvents()
window.actionImportCMUD.triggered.connect(Importer.importCmud)
sys.exit(application.exec_())