aboutsummaryrefslogtreecommitdiff
path: root/logic/menu.gd
blob: 4fde89dd86049e9a8cec47852f42cdd04e45d674 (plain)
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
extends MenuButton

const LOGS_FILE_PATH: String = "user://logs/godot.log"
const menu_items: Array = [
	{ label = "Toggle Theme",		action = "toggle_theme_action" },
	{ label = "Import Option Sets",	action = "import_option_sets_action" },
	{ label = "Export Option Sets",	action = "export_option_sets_action" },
	{ label = "Clear Option Sets",	action = "clear_option_sets_action" },
	{ label = "Import Database",	action = "import_database_action" },
	{ label = "Export Database",	action = "export_database_action" },
	{ label = "Clear Database",		action = "clear_database_action" },
	{ label = "Export App Log",		action = "export_app_log_action" },
	{ label = "About",				action = "about_action" },
]

onready var main					:= get_node("/root/main") as Control
onready var menu					:= get_popup() as PopupMenu
onready var popup					:= get_node("/root/main/popup") as ModalPopup
onready var dialog					:= get_node("/root/main/dialog") as Dialog
onready var file_picker				:= get_node("/root/main/file_picker") as FileDialog
onready var database				:= get_node("/root/main/database") as Database
onready var stage					:= get_node("/root/main/stage") as Stage


func _ready():
	for idx in range(menu_items.size()):
		menu.add_item(menu_items[idx].label, idx)
	menu.connect("id_pressed", self, "id_pressed")


func id_pressed(id: int):
	self.call_deferred(menu_items[id].action)


func toggle_theme_action():
	main.toggle_theme()


func import_option_sets_action():
	dialog.setup("All option sets from the dropdown menus will be replaced.", "Continue", "No")
	dialog.connect("accepted", self, "import_option_sets_action_accepted")
	popup.open_popup("Replace option sets?", dialog)


func import_option_sets_action_accepted():
	file_picker.mode = FileDialog.MODE_OPEN_FILE
	file_picker.current_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
	file_picker.filters = ["*.json", "*.csv"]
	file_picker.current_file = ""
	file_picker.connect("file_selected", self, "import_option_sets")
	file_picker.show_modal(true)
	file_picker.invalidate()


func import_option_sets(file_path: String):
	stage.load_option_sets(file_path)
	stage.save_option_sets()


func export_option_sets_action():
	file_picker.mode = FileDialog.MODE_SAVE_FILE
	file_picker.current_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
	file_picker.filters = ["*.json"]
	file_picker.current_file = ""
	file_picker.connect("file_selected", stage, "save_option_sets")
	file_picker.show_modal(true)
	file_picker.invalidate()


func clear_option_sets_action():
	dialog.setup("All option sets from the dropdown menus will be deleted.", "Delete all", "No")
	dialog.connect("accepted", self, "clear_option_sets")
	popup.open_popup("Clear option sets?", dialog)


func clear_option_sets():
	stage.clear_option_sets()
	stage.save_option_sets()


func import_database_action():
	dialog.setup("All entries from the database will be replaced.", "Continue", "No")
	dialog.connect("accepted", self, "import_database_action_accepted")
	popup.open_popup("Replace database?", dialog)


func import_database_action_accepted():
	file_picker.mode = FileDialog.MODE_OPEN_FILE
	file_picker.current_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
	file_picker.filters = ["*.json", "*.csv"]
	file_picker.current_file = ""
	file_picker.connect("file_selected", self, "import_database")
	file_picker.show_modal(true)
	file_picker.invalidate()


func import_database(file_path: String):
	database.load_database(file_path)
	database.save_database()


func export_database_action():
	file_picker.mode = FileDialog.MODE_SAVE_FILE
	file_picker.current_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
	file_picker.filters = ["*.csv"]
	file_picker.current_file = ""
	file_picker.connect("file_selected", database, "save_database")
	file_picker.show_modal(true)
	file_picker.invalidate()


func clear_database_action():
	dialog.setup("All entries from the database will be deleted.", "Delete all", "No")
	dialog.connect("accepted", self, "clear_database")
	popup.open_popup("Clear database?", dialog)


func clear_database():
	database.clear_database()
	database.save_database()


func export_app_log_action():
	file_picker.mode = FileDialog.MODE_SAVE_FILE
	file_picker.current_dir = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)
	file_picker.filters = ["*.txt"]
	file_picker.current_file = ("log" )
	file_picker.connect("file_selected", self, "export_app_log")
	file_picker.show_modal(true)
	file_picker.invalidate()


func export_app_log(file_path: String):
	var error	: int
	var file	:= File.new()
	
	error = file.open(LOGS_FILE_PATH, File.READ)
	if error != OK:
		printerr("Failed to open log file '%s' (error %d)." % [LOGS_FILE_PATH, error])
		return
	var file_content = file.get_as_text()
	file.close()
	
	error = file.open(file_path, File.WRITE)
	if error != OK:
		printerr("Failed to open file '%s' to write log (error %d)." % [file_path, error])
		return
	file.store_string(file_content)
	file.close()


func about_action():
	dialog.setup("Surgery Log\nversion %s" % ProjectSettings.get_setting("global/version"), "", "")
	popup.open_popup("About", dialog)