-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.py
executable file
·283 lines (219 loc) · 9.21 KB
/
launch.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
#!/usr/bin/python3.8
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QFileDialog, QGraphicsScene
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtCore import Qt
from MultiHex2.guis.main_gui import main_gui
from MultiHex2.master_clicker import Clicker
from MultiHex2.clock import Time
from MultiHex2.generation.overland import fullsim
from MultiHex2.guis.savewarn import SaveWarnDialogGui
from MultiHex2.main_menu import MainMenuDialog
from MultiHex2.generation.map_gen_config import MapGenConfigDialog
from MultiHex2.tools import Basic_Tool
import os
import sys
import shutil
import json
from MultiHex2 import ALL_MODULES
if sys.platform=="linux":
SAVEDIR = os.path.join(os.environ["HOME"], ".local", "MultiHex")
elif sys.platform=="darwin":
raise NotImplementedError("You could probably swap error out for the linux SAVEDIR setting... I think the filesystems is similar?")
elif sys.platform=="win32":
SAVEDIR = os.path.join(os.environ["AppData"], "MultiHex")
else:
raise NotImplementedError("Unrecognized os {}".format(sys.platform))
if not os.path.exists(SAVEDIR):
os.mkdir(SAVEDIR)
class WarnWidget(QtWidgets.QDialog):
def __init__(self, parent) -> None:
super().__init__(parent)
self.ui = SaveWarnDialogGui()
self.ui.setupUi(self)
self.parent = parent
class main_window(QMainWindow):
def __init__(self,parent=None):
QWidget.__init__(self, parent)
self.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__),"MultiHex2", 'assets','multihex_small_logo.svg')))
# standard boiler-plate gui initialization
# we instantiate the default GUI before anything else
self.ui = main_gui()
self.ui.setupUi(self)
self.scene = Clicker( self.ui.graphicsView, self )
self.ui.events.configure(self.scene)
# Allow the graphics view to follow the mouse when it isn't being clicked, and associate the clicker control with the ui
self.ui.graphicsView.setScene( self.scene )
self.ui.graphicsView.setMouseTracking(True)
self.ui.actionOpen.triggered.connect(self.load)
self.ui.actionSave_As.triggered.connect(self.saveAs)
self.ui.actionSave.triggered.connect(self.save)
self.ui.actionQuit.triggered.connect(self.quit)
self.ui.actionNew.triggered.connect(self.new)
self.ui.export_image.triggered.connect(self.export_image)
config_name = ".config.json"
self.config_filepath = os.path.join(SAVEDIR, config_name)
if not os.path.exists(self.config_filepath):
shutil.copyfile(os.path.join(os.path.dirname(__file__),"MultiHex2", "resources", "template_config.json"), self.config_filepath)
self.module = None
self.reload_config()
self._loaded_module = False
self._will_generate = False
self.load_module(self.config["module"])
self.select_tool("basic")
self.open_menu()
self.tileset = ""
self.generator = None
if self._will_generate:
success = self.new()
while not success:
self.open_menu()
def update_clock_gui(self,time:Time):
self.ui.clock.set_time(time)
def jump_to_time(self, time:Time):
self.scene.skip_to_time(time)
def reload_config(self):
"""
Load in a config file.
If there's no module loaded yet, don't do anything yet! We're still in the setup phase
Otherwise check if the module type has changed, and if so swap out the modules
"""
f = open(self.config_filepath, 'rt')
self.config = json.load(f)
f.close()
self.scene.set_primary_mouse(self.config["primary_mouse"]=="left")
if self.module is not None:
if self.config["module"]!=self.module.name:
self.load_module(self.config["module"])
def clear_tools(self):
"""
clears otu the tools, used when we load new modules in
"""
self.scene._alltools = {}
self.add_tool("basic", Basic_Tool)
if self._loaded_module:
self.select_tool("basic")
def load_module(self, module_name:str):
"""
Clear all the buttons, tell the scene to delete all its tools
Then, add in all the relevant tools
"""
if self.module is not None:
if self.module.name==module_name: # the module is already loaded, don't waste time
return
self.ui.clear_buttons()
self.clear_tools()
self.module = ALL_MODULES[module_name]()
self.scene.module = module_name
self.scene.update_with_module()
_obj = open(self.module.tileset,'r')
tileset = json.load(_obj)
_obj.close()
self.scene.set_tileset(tileset)
all_tools = self.module.tools
for key in all_tools:
self.add_tool(key, all_tools[key])
self.generator = self.module.generator_function
print("Loaded Module '{}'".format(self.module.name))
self._loaded_module = True
def open_menu(self):
accepted = False
while not accepted:
dialog = MainMenuDialog(self , module_list = ALL_MODULES.keys())
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
accepted = dialog.Accepted
def export_image(self):
temp= QFileDialog.getSaveFileName(None, 'Exoport Image', SAVEDIR, 'PNGs (*.png)')[0]
if temp is None:
return
elif temp=='':
return
#self.main_map.dimensions
size = QtCore.QSize(self.scene.dimensions[0], self.scene.dimensions[1])
image = QtGui.QImage(size,QtGui.QImage.Format_ARGB32_Premultiplied)
painter= QtGui.QPainter(image)
self.scene.render(painter)
painter.end()
image.save(temp)
def new(self):
"""
Returns True if this successfully starts making a new map, otherwise returns False
"""
if not self._loaded_module:
self._will_generate = True
return True
else:
dialog = MapGenConfigDialog(self, self.module.generation_config)
#dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.exec_()
dialog.deleteLater()
if dialog.Accepted:
use_configs = dialog.get_config()
seed = dialog.get_seed()
self.generator(self.scene, config=use_configs, seed=seed)
self.ui.clock.set_time(self.scene.clock.time)
self.ui.events.update()
return True
else:
return False
def save(self):
self.scene.reset_save()
if self.scene.file_name=="":
self.saveAs()
else:
self.scene.save(self.scene.file_name)
def saveAs(self):
pathto = QFileDialog.getSaveFileName(None, 'Save As',SAVEDIR, 'Json (*.json)')[0]
if pathto is not None:
if pathto!="":
self.scene.save(pathto)
self.scene.reset_save()
def quit(self):
if self.scene.unsaved:
self.dialog = WarnWidget(parent=self)
self.dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.dialog.exec_()
sys.exit()
def load(self)->str:
pathto = QFileDialog.getOpenFileName(None, 'Save As',SAVEDIR, 'Json (*.json)')[0]
if pathto is not None:
if pathto!="":
self.scene.load( pathto)
self.scene.reset_save()
if self.module is not None:
if self.scene.module == self.module.name:
return pathto
self.load_module(self.scene.module)
return pathto
def keyPressEvent(self, a0: QtGui.QKeyEvent) -> None:
if a0.key()==Qt.Key_Escape:
self.select_tool("basic")
def add_tool(self,tool_name:str, tool:Basic_Tool):
self.ui.add_button(tool_name, tool.buttonIcon(), tool.altText(), tool.tool_layer() )
def tempfunc():
return self.select_tool(tool_name)
value = tool.tool_layer().value
if value==0 or value==1:
self.ui.buttons[tool_name].clicked.connect(tempfunc)
elif value==2:
self.ui.second_buttons[tool_name].clicked.connect(tempfunc)
elif value==4:
self.ui.map_use_buttons[tool_name].clicked.connect(tempfunc)
else:
raise NotImplementedError("Not layer {}".format(tool.tool_layer()))
self.scene.add_tool(tool_name, tool)
def select_tool(self, tool_name:str):
self.scene.select_tool(tool_name)
if self.ui.toolwidget is not None:
self.ui.toolPane.removeWidget(self.ui.toolwidget)
self.ui.toolwidget.deleteLater()
self.ui.toolwidget = None
self.ui.toolwidget = self.scene.tool.widget()(self.ui.centralwidget, self.scene.tool, self.module.tileset, self.module.text_source)
self.ui.toolPane.addWidget(self.ui.toolwidget)
self.ui.toolPane.update()
app = QApplication(sys.argv)
app_instance = main_window()
if __name__=="__main__":
# make sure the base saves folder exists
app_instance.show()
sys.exit(app.exec_())