Add files via upload

fixed csv and ini visual editors
This commit is contained in:
Nicolò Santilio 2020-06-19 19:35:00 +02:00 committed by GitHub
parent 031b8a1430
commit 276cc70dbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,258 +26,259 @@ var current_file_path : String = ""
signal update_file() signal update_file()
func _ready(): func _ready():
create_table_names() create_table_names()
connect_signals() connect_signals()
load_icons() load_icons()
clean_editor() clean_editor()
add_to_group("ini_editor") add_to_group("ini_editor")
# var metadata = [["name","Godot Engine"],["version","1.0.0"],["color","Light Blue"]] # var metadata = [["name","Godot Engine"],["version","1.0.0"],["color","Light Blue"]]
# load_section("Engine", metadata) # load_section("Engine", metadata)
func connect_signals(): func connect_signals():
Sections.connect("item_selected",self,"_on_section_selected") Sections.connect("item_selected",self,"_on_section_selected")
Sections.connect("nothing_selected",self,"_on_nosection_selected") Sections.connect("nothing_selected",self,"_on_nosection_selected")
BtnAddSection.connect("pressed",self,"_on_addsection_pressed") BtnAddSection.connect("pressed",self,"_on_addsection_pressed")
BtnRemoveSection.connect("pressed",self,"_on_removesection_pressed") BtnRemoveSection.connect("pressed",self,"_on_removesection_pressed")
Keys.connect("item_selected",self,"_on_key_selected") Keys.connect("item_selected",self,"_on_key_selected")
Keys.connect("nothing_selected",self,"_on_nokey_selected") Keys.connect("nothing_selected",self,"_on_nokey_selected")
BtnAddKey.connect("pressed",self,"_on_addkey_pressed") BtnAddKey.connect("pressed",self,"_on_addkey_pressed")
BtnRemoveKey.connect("pressed",self,"_on_removekey_pressed") BtnRemoveKey.connect("pressed",self,"_on_removekey_pressed")
BtnEditKey.connect("pressed",self,"_on_editkey_pressed") BtnEditKey.connect("pressed",self,"_on_editkey_pressed")
connect("visibility_changed",self,"_on_visibility_changed") connect("visibility_changed",self,"_on_visibility_changed")
func create_table_names(): func create_table_names():
create_root() create_root()
Keys.hide_root = true Keys.hide_root = true
Keys.set_column_titles_visible(true) Keys.set_column_titles_visible(true)
Keys.set_column_title(0,"Name") Keys.set_column_title(0,"Name")
Keys.set_column_title(1,"Value") Keys.set_column_title(1,"Value")
func load_icons(): func load_icons():
$VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/sections_icon.texture = IconLoader.load_icon_from_name("sections") $VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/sections_icon.texture = IconLoader.load_icon_from_name("sections")
$VBoxContainer/HSplitContainer/VBoxContainer2/HBoxContainer2/keys_icon.texture = IconLoader.load_icon_from_name("keys") $VBoxContainer/HSplitContainer/VBoxContainer2/HBoxContainer2/keys_icon.texture = IconLoader.load_icon_from_name("keys")
BtnAddSection.icon = IconLoader.load_icon_from_name("add") BtnAddSection.icon = IconLoader.load_icon_from_name("add")
BtnAddSection.hint_tooltip = "Add a new Section" BtnAddSection.hint_tooltip = "Add a new Section"
BtnRemoveSection.icon = IconLoader.load_icon_from_name("delete") BtnRemoveSection.icon = IconLoader.load_icon_from_name("delete")
BtnRemoveSection.hint_tooltip = "Remove selected Section" BtnRemoveSection.hint_tooltip = "Remove selected Section"
BtnAddKey.icon = IconLoader.load_icon_from_name("add") BtnAddKey.icon = IconLoader.load_icon_from_name("add")
BtnAddKey.hint_tooltip = "Add a new Key" BtnAddKey.hint_tooltip = "Add a new Key"
BtnRemoveKey.icon = IconLoader.load_icon_from_name("delete") BtnRemoveKey.icon = IconLoader.load_icon_from_name("delete")
BtnRemoveKey.hint_tooltip = "Remove selected Key" BtnRemoveKey.hint_tooltip = "Remove selected Key"
BtnEditKey.icon = IconLoader.load_icon_from_name("edit_") BtnEditKey.icon = IconLoader.load_icon_from_name("edit_")
BtnEditKey.hint_tooltip = "Edit selected Key" BtnEditKey.hint_tooltip = "Edit selected Key"
func _on_addsection_pressed(): func _on_addsection_pressed():
Section.get_node("Container/section/_name").show() Section.get_node("Container/section/_name").show()
Section.window_title = "Add a new Section" Section.window_title = "Add a new Section"
if not Section.is_connected("confirmed",self,"new_section"): if not Section.is_connected("confirmed",self,"new_section"):
Section.connect("confirmed",self,"new_section") Section.connect("confirmed",self,"new_section")
if Section.is_connected("confirmed",self,"remove_section"): if Section.is_connected("confirmed",self,"remove_section"):
Section.disconnect("confirmed",self,"remove_section") Section.disconnect("confirmed",self,"remove_section")
Section.popup() Section.popup()
func _on_removesection_pressed(): func _on_removesection_pressed():
Section.get_node("Container").hide() Section.get_node("Container").hide()
Section.window_title = "Remove selected Section" Section.window_title = "Remove selected Section"
Section.dialog_text = "Are you sure you want to remove this Section?" Section.dialog_text = "Are you sure you want to remove this Section?"
if not Section.is_connected("confirmed",self,"remove_section"): if not Section.is_connected("confirmed",self,"remove_section"):
Section.connect("confirmed",self,"remove_section") Section.connect("confirmed",self,"remove_section")
if Section.is_connected("confirmed",self,"new_section"): if Section.is_connected("confirmed",self,"new_section"):
Section.disconnect("confirmed",self,"new_section") Section.disconnect("confirmed",self,"new_section")
Section.popup() Section.popup()
func _on_addkey_pressed(): func _on_addkey_pressed():
Key.get_node("data").show() Key.get_node("data").show()
Key.get_node("data/HBoxContainer/name").editable = true Key.get_node("data/HBoxContainer/name").editable = true
Key.get_node("data/HBoxContainer/name").set_text("") Key.get_node("data/HBoxContainer/name").set_text("")
Key.window_title = "Add a new Key" Key.window_title = "Add a new Key"
Key.dialog_text = "" Key.dialog_text = ""
if not Key.is_connected("confirmed",self,"new_key"): if not Key.is_connected("confirmed",self,"new_key"):
Key.connect("confirmed",self,"new_key") Key.connect("confirmed",self,"new_key")
if Key.is_connected("confirmed",self,"edit_key"): if Key.is_connected("confirmed",self,"edit_key"):
Key.disconnect("confirmed",self,"edit_key") Key.disconnect("confirmed",self,"edit_key")
if Key.is_connected("confirmed",self,"remove_key"): if Key.is_connected("confirmed",self,"remove_key"):
Key.disconnect("confirmed",self,"remove_key") Key.disconnect("confirmed",self,"remove_key")
Key.popup() Key.popup()
func _on_removekey_pressed(): func _on_removekey_pressed():
Key.get_node("data").hide() Key.get_node("data").hide()
Key.window_title = "Delete selected Key" Key.window_title = "Delete selected Key"
Key.dialog_text = "Are you sure you want to remove the selected Key?" Key.dialog_text = "Are you sure you want to remove the selected Key?"
if not Key.is_connected("confirmed",self,"remove_key"): if not Key.is_connected("confirmed",self,"remove_key"):
Key.connect("confirmed",self,"remove_key") Key.connect("confirmed",self,"remove_key")
if Key.is_connected("confirmed",self,"edit_key"): if Key.is_connected("confirmed",self,"edit_key"):
Key.disconnect("confirmed",self,"edit_key") Key.disconnect("confirmed",self,"edit_key")
if Key.is_connected("confirmed",self,"new_key"): if Key.is_connected("confirmed",self,"new_key"):
Key.disconnect("confirmed",self,"new_key") Key.disconnect("confirmed",self,"new_key")
Key.popup() Key.popup()
func _on_editkey_pressed(): func _on_editkey_pressed():
Key.get_node("data").show() Key.get_node("data").show()
Key.get_node("data/HBoxContainer/name").editable = false Key.get_node("data/HBoxContainer/name").editable = false
Key.get_node("data/HBoxContainer/name").set_text(str(selected_key.get_text(0))) Key.get_node("data/HBoxContainer/name").set_text(str(selected_key.get_text(0)))
Key.window_title = "Edit selected Key" Key.get_node("data/HBoxContainer2/value").set_text(str(selected_key.get_text(1)))
Key.dialog_text = "" Key.window_title = "Edit selected Key"
if not Key.is_connected("confirmed",self,"edit_key"): Key.dialog_text = ""
Key.connect("confirmed",self,"edit_key") if not Key.is_connected("confirmed",self,"edit_key"):
if Key.is_connected("confirmed",self,"remove_key"): Key.connect("confirmed",self,"edit_key")
Key.disconnect("confirmed",self,"remove_key") if Key.is_connected("confirmed",self,"remove_key"):
if Key.is_connected("confirmed",self,"new_key"): Key.disconnect("confirmed",self,"remove_key")
Key.disconnect("confirmed",self,"new_key") if Key.is_connected("confirmed",self,"new_key"):
Key.popup() Key.disconnect("confirmed",self,"new_key")
Key.popup()
func clean_editor(): func clean_editor():
Keys.clear() Keys.clear()
Sections.clear() Sections.clear()
selected_section = -1 selected_section = -1
BtnAddKey.disabled = true BtnAddKey.disabled = true
if current_file_path == "": if current_file_path == "":
BtnAddSection.disabled = true BtnAddSection.disabled = true
else: else:
BtnAddSection.disabled = false BtnAddSection.disabled = false
BtnEditKey.disabled = true BtnEditKey.disabled = true
BtnRemoveKey.disabled = true BtnRemoveKey.disabled = true
BtnRemoveSection.disabled = true BtnRemoveSection.disabled = true
create_root() create_root()
func open_file(filemap : Array): func open_file(filemap : Array):
clean_editor() clean_editor()
for section in filemap: for section in filemap:
load_sections(section[0],section[1]) load_sections(section[0],section[1])
func new_section(): func new_section():
var file = ConfigFile.new() var file = ConfigFile.new()
file.load(current_file_path) file.load(current_file_path)
var section_name = str(Section.get_node("Container/section/_name").get_text()) var section_name = str(Section.get_node("Container/section/_name").get_text())
var key_name = str(Section.get_node("Container/key/_name").get_text()) var key_name = str(Section.get_node("Container/key/_name").get_text())
var key_value = Section.get_node("Container/value/_value").get_text() var key_value = Section.get_node("Container/value/_value").get_text()
if section_name and key_name and key_value: if section_name and key_name and key_value:
file.set_value(section_name,key_name,key_value) file.set_value(section_name,key_name,key_value)
file.save(current_file_path) file.save(current_file_path)
load_sections(section_name,[[key_name,key_value]]) load_sections(section_name,[[key_name,key_value]])
emit_signal("update_file") emit_signal("update_file")
else: else:
print("Section <",section_name,"> with Key name: <",key_name,"> and Key value: <",key_value,"> not valid.") print("Section <",section_name,"> with Key name: <",key_name,"> and Key value: <",key_value,"> not valid.")
func remove_section(): func remove_section():
var file = ConfigFile.new() var file = ConfigFile.new()
file.load(current_file_path) file.load(current_file_path)
var current_section = Sections.get_item_text(selected_section) var current_section = Sections.get_item_text(selected_section)
file.erase_section(current_section) file.erase_section(current_section)
Sections.remove_item(selected_section) Sections.remove_item(selected_section)
file.save(current_file_path) file.save(current_file_path)
emit_signal("update_file") emit_signal("update_file")
func new_key(): func new_key():
var key_name = str(Key.get_node("data/HBoxContainer/name").get_text()) var key_name = str(Key.get_node("data/HBoxContainer/name").get_text())
var key_value = Key.get_node("data/HBoxContainer2/value").get_text() var key_value = Key.get_node("data/HBoxContainer2/value").get_text()
if key_name and key_value: if key_name and key_value:
var file = ConfigFile.new() var file = ConfigFile.new()
file.load(current_file_path) file.load(current_file_path)
var current_section = Sections.get_item_text(selected_section) var current_section = Sections.get_item_text(selected_section)
file.set_value(current_section,key_name,key_value) file.set_value(current_section,key_name,key_value)
file.save(current_file_path) file.save(current_file_path)
load_keys_selected_section([[key_name,key_value]]) load_keys_selected_section([[key_name,key_value]])
file.save(current_file_path) file.save(current_file_path)
emit_signal("update_file") emit_signal("update_file")
else: else:
print("Key name: <",key_name,"> with Key value: <",key_value,"> not valid.") print("Key name: <",key_name,"> with Key value: <",key_value,"> not valid.")
func remove_key(): func remove_key():
var section = Sections.get_item_text(selected_section) var section = Sections.get_item_text(selected_section)
var sectionmetadata = Sections.get_item_metadata(selected_section) var sectionmetadata = Sections.get_item_metadata(selected_section)
for meta in sectionmetadata: for meta in sectionmetadata:
if meta.has(selected_key.get_text(0)): if meta.has(selected_key.get_text(0)):
sectionmetadata.erase(meta) sectionmetadata.erase(meta)
Sections.set_item_metadata(selected_section,sectionmetadata) Sections.set_item_metadata(selected_section,sectionmetadata)
if Sections.get_item_metadata(selected_section) == []: if Sections.get_item_metadata(selected_section) == []:
Sections.remove_item(selected_section) Sections.remove_item(selected_section)
var file = ConfigFile.new() var file = ConfigFile.new()
file.load(current_file_path) file.load(current_file_path)
file.set_value(section,selected_key.get_text(0),null) file.set_value(section,selected_key.get_text(0),null)
file.save(current_file_path) file.save(current_file_path)
Keys.clear() Keys.clear()
create_root() create_root()
load_keys_selected_section(sectionmetadata) load_keys_selected_section(sectionmetadata)
emit_signal("update_file") emit_signal("update_file")
func edit_key(): func edit_key():
remove_key() remove_key()
new_key() new_key()
# load a section with custom fields @section_name = name of section ; @section_metadata = keys of this section with keys' properties # load a section with custom fields @section_name = name of section ; @section_metadata = keys of this section with keys' properties
func load_sections(section_name : String, section_metadata : Array): func load_sections(section_name : String, section_metadata : Array):
Sections.add_item(section_name,IconLoader.load_icon_from_name("section"),true) Sections.add_item(section_name,IconLoader.load_icon_from_name("section"),true)
Sections.set_item_metadata(Sections.get_item_count()-1,section_metadata) Sections.set_item_metadata(Sections.get_item_count()-1,section_metadata)
# load a key of a selected section to fill the "keys" list # load a key of a selected section to fill the "keys" list
func load_keys_selected_section(metadata : Array): func load_keys_selected_section(metadata : Array):
for key in metadata: for key in metadata:
var key_item = Keys.create_item(root) var key_item = Keys.create_item(root)
key_item.set_text(0,key[0]) key_item.set_text(0,key[0])
key_item.set_text(1,key[1]) key_item.set_text(1,key[1])
func _on_section_selected(index : int): func _on_section_selected(index : int):
Keys.clear() Keys.clear()
create_root() create_root()
BtnRemoveSection.disabled = false BtnRemoveSection.disabled = false
BtnAddSection.disabled = false BtnAddSection.disabled = false
BtnAddKey.disabled = false BtnAddKey.disabled = false
BtnRemoveKey.disabled = true BtnRemoveKey.disabled = true
BtnEditKey.disabled = true BtnEditKey.disabled = true
selected_section = index selected_section = index
if Sections.get_item_metadata(index): if Sections.get_item_metadata(index):
load_keys_selected_section(Sections.get_item_metadata(index)) load_keys_selected_section(Sections.get_item_metadata(index))
func _on_key_selected(): func _on_key_selected():
selected_key = Keys.get_selected() selected_key = Keys.get_selected()
BtnRemoveKey.disabled = false BtnRemoveKey.disabled = false
BtnEditKey.disabled = false BtnEditKey.disabled = false
func _on_nosection_selected(): func _on_nosection_selected():
BtnRemoveKey.disabled = true BtnRemoveKey.disabled = true
BtnAddKey.disabled = true BtnAddKey.disabled = true
BtnEditKey.disabled = true BtnEditKey.disabled = true
BtnRemoveSection.disabled = true BtnRemoveSection.disabled = true
Keys.clear() Keys.clear()
selected_section = -1 selected_section = -1
func _on_nokey_selected(): func _on_nokey_selected():
BtnRemoveKey.disabled = true BtnRemoveKey.disabled = true
BtnEditKey.disabled = true BtnEditKey.disabled = true
func create_root(): func create_root():
root = Keys.create_item() root = Keys.create_item()
root.set_text(0,"KEY_NAME") root.set_text(0,"KEY_NAME")
root.set_text(1,"KEY_VALUE") root.set_text(1,"KEY_VALUE")
func _on_visibility_changed(): func _on_visibility_changed():
if visible: if visible:
pass pass