1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-31 07:29:49 +00:00

Implement a basic extension system

Importing .pck or .zip Godot resource pack files into Pixelorama is now possible. This needs to be documented properly, but here's the basic idea, for now at least. This is super early work and I haven't tested it with a proper extension yet, so all of this could be a subject of change. I tested it with a custom theme extension though and it seems to be working perfectly.

Importing resource pack files, either by dragging and dropping them into the app window or by going to Edit>Preferences>Extensions>Add Extension, copies the files into user://extensions/. Extensions can be enabled/disabled and uninstalled. Uninstalling them deletes the resource pack files from user://extensions/.

The extension project source files need to be in a folder inside src/Extensions/ with the same name as the .pck or .zip file. **This is required for now, otherwise it will not work.** Inside that folder there also needs to be an extension.json file, with a structure similar to this:

{
	"name": "ExtensionName",
	"display_name": "Extension Name",
	"description": "A Pixelorama extension",
	"author": "Orama Interactive",
	"version": "0.1",
	"license": "MIT",
	"nodes": [
		"ExtensionExample.tscn"
	]
}

The `nodes` array leads to the packed scene files with the nodes that are to be instantiated. **The root nodes of these scenes need to have the same name as the .tscn files they belong to.** The scripts of these nodes should have _enter_tree() and _exit_tree() methods to handle the extension enabling/disabling (or even uninstalling) logic. Note that .json files need to be included in the export options while exporting the extension from Godot.

Enabling an extension means that the scenes found in the extension.json's "nodes" array get instantiated, and disabling gets rid of these nodes from Pixelorama's SceneTree.
This commit is contained in:
Manolis Papadeas 2022-02-19 03:21:08 +02:00
parent bafcb2620a
commit 375f3d4cb6
6 changed files with 370 additions and 77 deletions

View file

@ -453,6 +453,9 @@ msgstr ""
msgid "Performance"
msgstr ""
msgid "Extensions"
msgstr ""
msgid "Cursors"
msgstr ""
@ -1095,6 +1098,21 @@ msgstr ""
msgid "If this is toggled on, when the application's window loses focus, it gets paused. This helps lower CPU usage when idle. The application gets unpaused when the mouse enters the application's window."
msgstr ""
msgid "Add Extension"
msgstr ""
msgid "Enable"
msgstr ""
msgid "Disable"
msgstr ""
msgid "Uninstall"
msgstr ""
msgid "Open Folder"
msgstr ""
msgid "Brush:"
msgstr ""

View file

@ -37,6 +37,9 @@ func handle_loading_files(files: PoolStringArray) -> void:
elif file_ext == "gpl" or file_ext == "pal" or file_ext == "json":
Palettes.import_palette_from_path(file)
elif file_ext in ["pck", "zip"]: # Godot resource pack file
Global.preferences_dialog.extensions.install_extension(file)
else: # Image files
var image := Image.new()
var err := image.load(file)

View file

@ -0,0 +1,197 @@
extends Control
const EXTENSIONS_PATH := "user://extensions"
var extensions := {} # Extension name : Extension class
var extension_selected := -1
onready var extension_list: ItemList = $InstalledExtensions
onready var enable_button: Button = $HBoxContainer/EnableButton
onready var uninstall_button: Button = $HBoxContainer/UninstallButton
onready var extension_parent: Node = Global.control.get_node("Extensions")
class Extension:
var file_name := ""
var display_name := ""
var description := ""
var author := ""
var version := ""
var license := ""
var nodes := []
var enabled := false
func serialize(dict: Dictionary) -> void:
if dict.has("name"):
file_name = dict["name"]
if dict.has("display_name"):
display_name = dict["display_name"]
if dict.has("description"):
description = dict["description"]
if dict.has("author"):
author = dict["author"]
if dict.has("version"):
version = dict["version"]
if dict.has("license"):
license = dict["license"]
if dict.has("nodes"):
nodes = dict["nodes"]
func _ready() -> void:
if OS.get_name() == "HTML5":
$HBoxContainer/AddExtensionButton.disabled = true
$HBoxContainer/OpenFolderButton.visible = false
var dir := Directory.new()
var file_names := [] # Array of String(s)
dir.make_dir(EXTENSIONS_PATH)
if dir.open(EXTENSIONS_PATH) == OK:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
var ext: String = file_name.to_lower().get_extension()
if !dir.current_is_dir() and ext in ["pck", "zip"]:
file_names.append(file_name)
file_name = dir.get_next()
if file_names.empty():
return
for file_name in file_names:
_add_extension(file_name)
func install_extension(path: String) -> void:
var dir := Directory.new()
var file_name: String = path.get_file()
dir.copy(path, EXTENSIONS_PATH.plus_file(file_name))
_add_extension(file_name)
func _add_extension(file_name: String) -> void:
if extensions.has(file_name):
var extension: Extension = extensions[file_name]
if extension.enabled: # Reload the extension if it's enabled
extension.enabled = false
_enable_extension(extension, false)
extension.enabled = true
_enable_extension(extension, false)
return
var file_name_no_ext: String = file_name.get_basename()
var file_path: String = EXTENSIONS_PATH.plus_file(file_name)
var success := ProjectSettings.load_resource_pack(file_path)
if !success:
print("Failed loading resource pack.")
var dir := Directory.new()
dir.remove(file_path)
return
var extension_path: String = "res://src/Extensions/%s/" % file_name_no_ext
var extension_config_file_path: String = extension_path.plus_file("extension.json")
var extension_config_file := File.new()
var err := extension_config_file.open(extension_config_file_path, File.READ)
if err != OK:
print("Error loading config file: ", err)
extension_config_file.close()
return
var extension_json = parse_json(extension_config_file.get_as_text())
extension_config_file.close()
if !extension_json:
print("No JSON data found.")
return
var extension := Extension.new()
extension.serialize(extension_json)
extensions[file_name] = extension
extension_list.add_item(extension.display_name)
var item_count: int = extension_list.get_item_count() - 1
extension_list.set_item_tooltip(item_count, extension.description)
extension_list.set_item_metadata(item_count, file_name)
extension.enabled = Global.config_cache.get_value("extensions", extension.file_name, false)
if extension.enabled:
_enable_extension(extension)
func _enable_extension(extension: Extension, save_to_config := true) -> void:
var extension_path: String = "res://src/Extensions/%s/" % extension.file_name
if extension.enabled:
for node in extension.nodes:
var scene_path: String = extension_path.plus_file(node)
var extension_scene: PackedScene = load(scene_path)
if extension_scene:
var extension_node: Node = extension_scene.instance()
extension_parent.add_child(extension_node)
else:
for node in extension.nodes:
var ext_node = extension_parent.get_node(node.get_basename())
if ext_node:
extension_parent.remove_child(ext_node)
ext_node.queue_free()
if save_to_config:
Global.config_cache.set_value("extensions", extension.file_name, extension.enabled)
Global.config_cache.save("user://cache.ini")
func _on_InstalledExtensions_item_selected(index: int) -> void:
extension_selected = index
var file_name: String = extension_list.get_item_metadata(extension_selected)
var extension: Extension = extensions[file_name]
if extension.enabled:
enable_button.text = "Disable"
else:
enable_button.text = "Enable"
enable_button.disabled = false
uninstall_button.disabled = false
func _on_InstalledExtensions_nothing_selected() -> void:
enable_button.disabled = true
uninstall_button.disabled = true
func _on_AddExtensionButton_pressed() -> void:
Global.preferences_dialog.get_node("Popups/AddExtensionFileDialog").popup_centered()
func _on_EnableButton_pressed() -> void:
var file_name: String = extension_list.get_item_metadata(extension_selected)
var extension: Extension = extensions[file_name]
extension.enabled = !extension.enabled
_enable_extension(extension)
if extension.enabled:
enable_button.text = "Disable"
else:
enable_button.text = "Enable"
func _on_UninstallButton_pressed() -> void:
var dir := Directory.new()
var file_name: String = extension_list.get_item_metadata(extension_selected)
var err := dir.remove(EXTENSIONS_PATH.plus_file(file_name))
if err != OK:
print(err)
return
var extension: Extension = extensions[file_name]
extension.enabled = false
_enable_extension(extension, false)
extensions.erase(file_name)
extension_list.remove_item(extension_selected)
extension_selected = -1
enable_button.disabled = true
uninstall_button.disabled = true
func _on_OpenFolderButton_pressed() -> void:
OS.shell_open(ProjectSettings.globalize_path(EXTENSIONS_PATH))
func _on_AddExtensionFileDialog_files_selected(paths: PoolStringArray) -> void:
for path in paths:
install_extension(path)

View file

@ -57,6 +57,17 @@ func add_theme(theme: Theme) -> void:
colors_container.add_child(theme_color_preview)
func remove_theme(theme: Theme) -> void:
var index: int = themes.find(theme)
var theme_button = buttons_container.get_child(index)
var color_previews = colors_container.get_child(index)
buttons_container.remove_child(theme_button)
theme_button.queue_free()
colors_container.remove_child(color_previews)
color_previews.queue_free()
themes.erase(theme)
func change_theme(id: int) -> void:
theme_index = id
var theme: Theme = themes[id]
@ -96,7 +107,8 @@ func change_theme(id: int) -> void:
change_icon_colors()
Global.preferences_dialog.get_node("Popups/ShortcutSelector").theme = theme
for child in Global.preferences_dialog.get_node("Popups").get_children():
child.theme = theme
# Sets disabled theme color on palette swatches
Global.palette_panel.reset_empty_palette_swatches_color()

View file

@ -87,6 +87,7 @@ onready var autosave_container: Container = right_side.get_node("Backup/Autosave
onready var autosave_interval: SpinBox = autosave_container.get_node("AutosaveInterval")
onready var shrink_label: Label = right_side.get_node("Interface/ShrinkContainer/ShrinkLabel")
onready var themes: BoxContainer = right_side.get_node("Interface/Themes")
onready var extensions: BoxContainer = right_side.get_node("Extensions")
class Preference:

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]
[ext_resource path="res://src/Preferences/PreferencesDialog.gd" type="Script" id=1]
[ext_resource path="res://src/Preferences/HandleExtensions.gd" type="Script" id=2]
[ext_resource path="res://src/Preferences/HandleLanguages.gd" type="Script" id=4]
[ext_resource path="res://src/Preferences/HandleThemes.gd" type="Script" id=5]
[ext_resource path="res://src/Preferences/HandleShortcuts.gd" type="Script" id=6]
@ -818,81 +819,6 @@ margin_bottom = 48.0
rect_min_size = Vector2( 64, 20 )
mouse_default_cursor_shape = 2
[node name="Image" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"]
visible = false
margin_top = 240.0
margin_right = 506.0
margin_bottom = 316.0
[node name="ImageOptions" type="GridContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image"]
margin_right = 506.0
margin_bottom = 76.0
custom_constants/vseparation = 4
custom_constants/hseparation = 4
columns = 3
[node name="DefaultWidthLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_top = 5.0
margin_right = 110.0
margin_bottom = 19.0
rect_min_size = Vector2( 110, 0 )
hint_tooltip = "A default width of a new image"
mouse_filter = 0
text = "Default width:"
[node name="ImageDefaultWidth" type="SpinBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_left = 114.0
margin_right = 188.0
margin_bottom = 24.0
hint_tooltip = "A default width of a new image"
mouse_default_cursor_shape = 2
min_value = 1.0
max_value = 16384.0
value = 64.0
rounded = true
align = 2
suffix = "px"
[node name="DefaultHeightLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_top = 33.0
margin_right = 110.0
margin_bottom = 47.0
hint_tooltip = "A default height of a new image"
mouse_filter = 0
text = "Default height:"
[node name="ImageDefaultHeight" type="SpinBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_left = 114.0
margin_top = 28.0
margin_right = 188.0
margin_bottom = 52.0
hint_tooltip = "A default height of a new image"
mouse_default_cursor_shape = 2
min_value = 1.0
max_value = 16384.0
value = 64.0
rounded = true
align = 2
suffix = "px"
[node name="DefaultFillColorLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_top = 59.0
margin_right = 110.0
margin_bottom = 73.0
hint_tooltip = "A default background color of a new image"
mouse_filter = 0
text = "Default fill color:"
[node name="DefaultFillColor" type="ColorPickerButton" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_left = 114.0
margin_top = 56.0
margin_right = 188.0
margin_bottom = 76.0
rect_min_size = Vector2( 64, 20 )
hint_tooltip = "A default background color of a new image"
mouse_default_cursor_shape = 2
color = Color( 0, 0, 0, 0 )
[node name="Shortcuts" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"]
visible = false
margin_top = 28.0
@ -1443,6 +1369,48 @@ __meta__ = {
"_edit_use_anchors_": false
}
[node name="Extensions" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"]
visible = false
margin_top = 56.0
margin_right = 498.0
margin_bottom = 65.0
script = ExtResource( 2 )
[node name="InstalledExtensions" type="ItemList" parent="HSplitContainer/ScrollContainer/VBoxContainer/Extensions"]
margin_right = 498.0
margin_bottom = 9.0
auto_height = true
[node name="HBoxContainer" type="HBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer/Extensions"]
margin_right = 40.0
margin_bottom = 40.0
[node name="AddExtensionButton" type="Button" parent="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer"]
margin_right = 91.0
margin_bottom = 20.0
mouse_default_cursor_shape = 2
text = "Add Extension"
[node name="EnableButton" type="Button" parent="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer"]
margin_right = 91.0
margin_bottom = 20.0
mouse_default_cursor_shape = 2
disabled = true
text = "Enable"
[node name="UninstallButton" type="Button" parent="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer"]
margin_right = 91.0
margin_bottom = 20.0
mouse_default_cursor_shape = 2
disabled = true
text = "Uninstall"
[node name="OpenFolderButton" type="Button" parent="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer"]
margin_right = 12.0
margin_bottom = 20.0
mouse_default_cursor_shape = 2
text = "Open Folder"
[node name="Cursors" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"]
visible = false
margin_top = 56.0
@ -1553,6 +1521,81 @@ mouse_default_cursor_shape = 2
pressed = true
text = "On"
[node name="Image" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"]
visible = false
margin_top = 240.0
margin_right = 506.0
margin_bottom = 316.0
[node name="ImageOptions" type="GridContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image"]
margin_right = 506.0
margin_bottom = 76.0
custom_constants/vseparation = 4
custom_constants/hseparation = 4
columns = 3
[node name="DefaultWidthLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_top = 5.0
margin_right = 110.0
margin_bottom = 19.0
rect_min_size = Vector2( 110, 0 )
hint_tooltip = "A default width of a new image"
mouse_filter = 0
text = "Default width:"
[node name="ImageDefaultWidth" type="SpinBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_left = 114.0
margin_right = 188.0
margin_bottom = 24.0
hint_tooltip = "A default width of a new image"
mouse_default_cursor_shape = 2
min_value = 1.0
max_value = 16384.0
value = 64.0
rounded = true
align = 2
suffix = "px"
[node name="DefaultHeightLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_top = 33.0
margin_right = 110.0
margin_bottom = 47.0
hint_tooltip = "A default height of a new image"
mouse_filter = 0
text = "Default height:"
[node name="ImageDefaultHeight" type="SpinBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_left = 114.0
margin_top = 28.0
margin_right = 188.0
margin_bottom = 52.0
hint_tooltip = "A default height of a new image"
mouse_default_cursor_shape = 2
min_value = 1.0
max_value = 16384.0
value = 64.0
rounded = true
align = 2
suffix = "px"
[node name="DefaultFillColorLabel" type="Label" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_top = 59.0
margin_right = 110.0
margin_bottom = 73.0
hint_tooltip = "A default background color of a new image"
mouse_filter = 0
text = "Default fill color:"
[node name="DefaultFillColor" type="ColorPickerButton" parent="HSplitContainer/ScrollContainer/VBoxContainer/Image/ImageOptions"]
margin_left = 114.0
margin_top = 56.0
margin_right = 188.0
margin_bottom = 76.0
rect_min_size = Vector2( 64, 20 )
hint_tooltip = "A default background color of a new image"
mouse_default_cursor_shape = 2
color = Color( 0, 0, 0, 0 )
[node name="Popups" type="Node" parent="."]
[node name="ShortcutSelector" type="ConfirmationDialog" parent="Popups"]
@ -1577,11 +1620,30 @@ __meta__ = {
"_edit_use_anchors_": false
}
[node name="AddExtensionFileDialog" type="FileDialog" parent="Popups"]
margin_right = 429.0
margin_bottom = 356.0
window_title = "Open File(s)"
resizable = true
mode = 1
access = 2
filters = PoolStringArray( "*.pck ; Godot Resource Pack File", "*.zip ;" )
show_hidden_files = true
current_dir = "/home/overloaded/Documents/Orama/Pixelorama"
current_path = "/home/overloaded/Documents/Orama/Pixelorama/"
[connection signal="about_to_show" from="." to="." method="_on_PreferencesDialog_about_to_show"]
[connection signal="popup_hide" from="." to="." method="_on_PreferencesDialog_popup_hide"]
[connection signal="item_selected" from="HSplitContainer/List" to="." method="_on_List_item_selected"]
[connection signal="value_changed" from="HSplitContainer/ScrollContainer/VBoxContainer/Interface/ShrinkContainer/ShrinkHSlider" to="." method="_on_ShrinkHSlider_value_changed"]
[connection signal="pressed" from="HSplitContainer/ScrollContainer/VBoxContainer/Interface/ShrinkContainer/ShrinkApplyButton" to="." method="_on_ShrinkApplyButton_pressed"]
[connection signal="item_selected" from="HSplitContainer/ScrollContainer/VBoxContainer/Shortcuts/HBoxContainer/PresetOptionButton" to="HSplitContainer/ScrollContainer/VBoxContainer/Shortcuts" method="_on_PresetOptionButton_item_selected"]
[connection signal="item_selected" from="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/InstalledExtensions" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_InstalledExtensions_item_selected"]
[connection signal="nothing_selected" from="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/InstalledExtensions" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_InstalledExtensions_nothing_selected"]
[connection signal="pressed" from="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer/AddExtensionButton" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_AddExtensionButton_pressed"]
[connection signal="pressed" from="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer/EnableButton" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_EnableButton_pressed"]
[connection signal="pressed" from="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer/UninstallButton" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_UninstallButton_pressed"]
[connection signal="pressed" from="HSplitContainer/ScrollContainer/VBoxContainer/Extensions/HBoxContainer/OpenFolderButton" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_OpenFolderButton_pressed"]
[connection signal="confirmed" from="Popups/ShortcutSelector" to="HSplitContainer/ScrollContainer/VBoxContainer/Shortcuts" method="_on_ShortcutSelector_confirmed"]
[connection signal="popup_hide" from="Popups/ShortcutSelector" to="HSplitContainer/ScrollContainer/VBoxContainer/Shortcuts" method="_on_ShortcutSelector_popup_hide"]
[connection signal="files_selected" from="Popups/AddExtensionFileDialog" to="HSplitContainer/ScrollContainer/VBoxContainer/Extensions" method="_on_AddExtensionFileDialog_files_selected"]